Skip to content

Instantly share code, notes, and snippets.

@tkers
Created March 13, 2018 10:00
Show Gist options
  • Save tkers/551eb02abc3173152d4ebbc506d0c14b to your computer and use it in GitHub Desktop.
Save tkers/551eb02abc3173152d4ebbc506d0c14b to your computer and use it in GitHub Desktop.
/*
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