Created
March 13, 2018 10:00
-
-
Save tkers/551eb02abc3173152d4ebbc506d0c14b 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
/* | |
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