-
-
Save stephenwil/2d74c0d2e6c9c78d3dcdd215ae615958 to your computer and use it in GitHub Desktop.
jscodeshift for removing empty constructors
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
/* | |
Removes empty constructors (ignoring calls to super) | |
Run with codeshift: | |
jscodeshift -t ./remove-empty-constructor.js --extensions js,jsx --ignore-pattern 'node_modules' src/ | |
*/ | |
export default function transformer(file, api) { | |
const j = api.jscodeshift | |
const root = j(file.source) | |
root | |
.find(j.ClassDeclaration) | |
.find(j.MethodDefinition, { kind: 'constructor' }) | |
.forEach(constructorDefinition => { | |
const nonSuperCalls = j(constructorDefinition) | |
.find(j.BlockStatement) | |
.get('body') | |
.filter(x => { | |
const superCalls = j(x).find(j.CallExpression, { | |
callee: { type: 'Super' } | |
}) | |
return superCalls.size() === 0 | |
}) | |
if (nonSuperCalls.length === 0) { | |
j(constructorDefinition).remove() | |
} | |
}) | |
return root.toSource() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment