Code modification scripts, also known as codemods, are transformation scripts that can simultaneously modify multiple files with precision and reliability. Codemods were popularized by Facebook's engineering team and depends greatly on Facebook's jscodeshift library, which wraps over a library named recast (author of which is associated with the Meteor project).
Install calypso-codemods using npm
or yarn
:
npm install -g calypso-codemods
Now you can run codemods using the following cli:
calypso-codemods transformation-name[,second-name,third-name] target [additional targets]
For example, if I wanted to run the commonjs-exports
transformation on client/devdocs/
, I can do the following:
calypso-codemods commonjs-exports client/devdocs/
Do you want to target files individually? We can do that, too!
calypso-codemods commonjs-exports client/devdocs/a.js client/devdocs/b.js client/devdocs/c.js
How about chaining codemods on multiple directories?
calypso-codemods commonjs-imports,commonjs-exports,named-export-from-default client/blocks/ client/components/
5to6-codemod scripts (docs)
-
commonjs-exports
- This codemod converts
module.exports
toexport
andexport default
.
- This codemod converts
-
commonjs-imports
- This transformation converts occurrences of
require( '...' )
toimport ... from '...'
occurring at the top level scope. It will ignore CommonJS imports inside block statements, like conditionals or function definitions.
- This transformation converts occurrences of
-
commonjs-imports-hoist
- This transformation hoists all occurrences of
require( '...' )
inside if, loop, and function blocks. This can cause breakage! Use with caution.
- This transformation hoists all occurrences of
-
named-exports-from-default
- This transformation generates named exports given a
default export { ... }
. This can be useful in transitioning away from namespace imports (import * as blah from 'blah'
) to named imports (import named from 'blah'
).
- This transformation generates named exports given a
React scripts (docs)
-
react-create-class
- This transformation converts instances of React.createClass to use React.Component instead.
-
react-proptypes
- This transformation converts instances of React.PropTypes to use prop-types instead.
-
combine-reducer-with-persistence
- This transformation converts combineReducers imports to use combineReducersWithPersistence.
-
combine-state-utils-imports
- This transformation combines state/utils imports.
-
i18n-mixin-to-localize
- This transformation converts the following:
-
this.translate
tothis.props.translate
-this.moment
tothis.props.moment
-this.numberFormat
tothis.props.numberFormat
- If any of the above conversions is performed, this transformation will wrap the React.createClass instance with a
localize()
higher-order component.
- If any of the above conversions is performed, this transformation will wrap the React.createClass instance with a
- This transformation converts the following:
-
-
merge-lodash-imports
- This transformation merges multiple named lodash imports into one
-
modular-lodash-no-more
- This transformation converts modular lodash imports to ES2015-style imports
-
modular-lodash-requires-no-more
- This transformation converts modular lodash requires to ES2015-style imports
-
rename-combine-reducers
- This transformation converts combineReducersWithPersistence imports to use combineReducers from 'state/utils'
-
single-tree-rendering
- Instead of rendering two distinct React element trees to the
#primary
and#secondary
s, use a singleLayout
component tree that includes both, and render it to#layout
.
- Instead of rendering two distinct React element trees to the
-
sort-imports
- This transformation adds import comment blocks and sorts them as necessary.
- Note: It only needs to be run twice because of a bug where in certain cases an extra newline is added on the first run. The second run removes the extra newline.
Write your transform using the standard jscodeshift api and place it in the transforms directory. You can look at the current directory for inspiration.
calypso-codemods uses jest snapshots to maintain its tests. in order to easily add tests for a new transform, follow these steps:
- add a directory to
tests
with the exact same name as the added transform. - add a file named
codemod.spec.js
with this as its contents contents:
test_folder(__dirname);
-
add any input files to the folder that you wish to be tested
-
run
npm test
oryarn test
. if the tests fail, its usually because a snapshot would be modified and behavior has changed. If you've verified that the updated snapshots look correct, then you can update the snapshots with:yarn test -- -u
. -
make sure to commit any modified snapshots and include it in your pull request