Skip to content

Instantly share code, notes, and snippets.

@alexeyraspopov
Created June 21, 2016 12:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexeyraspopov/781e046d98dd3148b333bbf7ea5c8631 to your computer and use it in GitHub Desktop.
Save alexeyraspopov/781e046d98dd3148b333bbf7ea5c8631 to your computer and use it in GitHub Desktop.
export default function transformer(file, api) {
const j = api.jscodeshift;
const {expression, statement, statements} = j.template;
return j(file.source)
.find(j.BinaryExpression, { operator: '==' })
.replaceWith(p => {
const {left, right} = p.value;
return expression`${left} === ${right}`;
})
.toSource();
};
export default function transformer(file, api) {
const j = api.jscodeshift;
const {expression, statement, statements} = j.template;
return j(file.source)
.find(j.BlockStatement)
.replaceWith(block => {
const identifiers = j(block).find(j.Identifier);
return j(block)
.find(j.VariableDeclaration)
.filter(v => {
const variable = v.value.declarations[0].id;
return identifiers.nodes()
.filter(i => i !== variable)
.every(i => i.name !== variable.name);
})
.remove()
.toSource();
})
.toSource();
};
export default function transformer(file, api) {
const j = api.jscodeshift;
const {expression, statement, statements} = j.template;
return j(file.source)
.find(j.VariableDeclaration)
.filter(p => p.value.declarations.length > 1)
.replaceWith(p => {
return j(p).replaceWith(pp => {
const kind = pp.value.kind;
return pp.value.declarations.map(decl => {
const value = decl.init ? j.identifier(j(decl.init).toSource()) : null;
return j.variableDeclaration(kind, [
j.variableDeclarator(j.identifier(decl.id.name), value)
]);
});
})
.toSource({quote: 'single'})
})
.toSource({quote: 'single'});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment