Skip to content

Instantly share code, notes, and snippets.

@stephenwil
Forked from tkers/remove-empty-constructor.js
Last active July 1, 2020 11:26
Show Gist options
  • Save stephenwil/2d74c0d2e6c9c78d3dcdd215ae615958 to your computer and use it in GitHub Desktop.
Save stephenwil/2d74c0d2e6c9c78d3dcdd215ae615958 to your computer and use it in GitHub Desktop.
jscodeshift for removing empty constructors
/*
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