Skip to content

Instantly share code, notes, and snippets.

@sebcode
Created April 6, 2017 18:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebcode/4a43760ab4abaf078f987f1dd94d4178 to your computer and use it in GitHub Desktop.
Save sebcode/4a43760ab4abaf078f987f1dd94d4178 to your computer and use it in GitHub Desktop.
Replace @autoBind decorator with class instance properties
// https://github.com/andreypopp/autobind-decorator
// https://github.com/facebook/codemod
// Example replacement:
// - @autobind onClick(e: SyntheticEvent) {
// + onClick = (e: SyntheticEvent) => {
codemod -m -d . --extensions js \
'@autobind\ ([a-zA-Z0-9]+)\(([^)]*)\)' \
'\1 = (\2) =>'
@vicapow
Copy link

vicapow commented Jul 25, 2018

I had to modify the regex a bit to account for our code:

codemod -m -d . --extensions js   '@autobind\s*([a-zA-Z0-9_]+)\(([^)]*)\)'   '\1 = (\2) =>'

and also to remove imports

# to remove imports
codemod -m -d . --extensions js "import autobind from 'autobind-decorator';\n"  ''

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment