Created
June 21, 2016 12:52
-
-
Save alexeyraspopov/781e046d98dd3148b333bbf7ea5c8631 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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