Coder Social home page Coder Social logo

calypso-codemods's Introduction

This repository has moved!

It is now part of the Calypso repository.

The published npm package will continue to be available as before, no changes necessary!


Calypso Codemods

Build Status

What are codemods?

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).

Getting started

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-exports-from-default client/blocks/ client/components/

List of available transformations

5to6-codemod scripts (docs)

  • commonjs-exports

    • This codemod converts module.exports to export and export default.
  • commonjs-imports

    • This transformation converts occurrences of require( '...' ) to import ... from '...' occurring at the top level scope. It will ignore CommonJS imports inside block statements, like conditionals or function definitions.
  • commonjs-imports-hoist

    • This transformation hoists all occurrences of require( '...' ) inside if, loop, and function blocks. This can cause breakage! Use with caution.
  • 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').

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.

Local scripts

  • 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 to this.props.translate - this.moment to this.props.moment - this.numberFormat to this.props.numberFormat
      • If any of the above conversions is performed, this transformation will wrap the React.createClass instance with a localize() higher-order component.
  • 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 single Layout component tree that includes both, and render it to #layout.
  • 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.

Contributing codemods

Write the transform

Write your transform using the standard jscodeshift api and place it in the transforms directory. You can look at the current directory for inspiration.

Add some tests!

calypso-codemods uses jest snapshots to maintain its tests. in order to easily add tests for a new transform, follow these steps:

  1. add a directory to tests with the exact same name as the added transform.
  2. add a file named codemod.spec.js with this as its contents contents:
test_folder(__dirname);
  1. add any input files to the folder that you wish to be tested

  2. run npm test or yarn 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.

  3. make sure to commit any modified snapshots and include it in your pull request

calypso-codemods's People

Contributors

coreylight avatar jsnajdr avatar ockham avatar samouri avatar sgomes avatar sirreal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

calypso-codemods's Issues

npx compatibility

Hey there!

For some reason calypso-codemods seems to do nothing when run with npx (which, I know, isn't documented and thus I'd imagine not yet supported).

$ npx calypso-codemods commonjs-imports legacy-js/
Running commonjs-imports on legacy-js/
$

Compared to yarn add --dev calypso-codemods and then:

$ ./node_modules/.bin/calypso-codemods commonjs-imports legacy-js/
Running commonjs-imports on legacy-js/
Processing 13 files...
Spawning 7 workers...
All done.
$

It would be handy if this worked, since e.g. react-codemod endorses using npx for a fire-and-forget run.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.