Skip to content

Instantly share code, notes, and snippets.

@knoopx
Created September 1, 2016 22:49
Show Gist options
  • Save knoopx/19e99be2861fe402381e293c51aceeda to your computer and use it in GitHub Desktop.
Save knoopx/19e99be2861fe402381e293c51aceeda to your computer and use it in GitHub Desktop.
jscodeshift decorate autobind
export default (file, api) => {
const j = api.jscodeshift
const root = j(file.source)
if (root.find(j.ExportDefaultDeclaration).get().value.declaration.decorators.map(n => n.expression.name).indexOf("autobind") >= 0) { return null }
root.get().value.program.body.splice(1, 0, j.importDeclaration([j.importSpecifier(j.identifier('autobind'))], j.literal('core-decorators')))
root.find(j.ExportDefaultDeclaration).get().value.declaration.decorators.push(j.decorator(j.identifier('autobind')))
return root.toSource()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment