Last active
December 28, 2019 23:07
-
-
Save kcmr/1258be4499fbb391855a7c9761f67a5c to your computer and use it in GitHub Desktop.
Codemod to replace the first block scope in a file by a IIFE
This file contains hidden or 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(file, api) { | |
const j = api.jscodeshift; | |
return j(file.source) | |
.find(j.VariableDeclarator, { | |
id: { | |
type: 'Identifier', | |
name: 'Element', | |
}, | |
}) | |
.replaceWith(nodePath => { | |
const { node } = nodePath; | |
node.id.name = 'PolymerElement'; | |
return node; | |
}) | |
.toSource(); | |
} |
This file contains hidden or 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(file, api) { | |
const j = api.jscodeshift; | |
return j(file.source) | |
.find(j.BlockStatement) | |
.at(0) | |
.replaceWith(nodePath => { | |
const nodeValue = nodePath.value; | |
return j.expressionStatement( | |
j.callExpression( | |
j.functionExpression(null, [], j.blockStatement(nodeValue.body)), | |
[] | |
) | |
) | |
} | |
) | |
.toSource(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Transforms the following code:
To:
Only the first block statement is replaced.