github facebook/jscodeshift v0.3.21

latest releases: 0.13.1, 0.13.0, 0.12.0...
7 years ago

New: Choose your parser

Problem: jscodeshift uses Babel v5 to parse source files. Since Babel v5 doesn't get updated anymore, jscodeshift is unable to parse files that contain more modern flow type annotation.

To solve this, jscodeshift now supports three parsers, babel v5, babylon and flow, and even allows you to pass your own. Babel v5 still stays the default parser, but we will likely use another parser (babylon or flow) as default in the future. Having the option to pass a custom parser allows us to experiment which works best with jscodeshift/recast.

How to choose a parser

CLI

--parser allows you specify one of the built-in parsers from the command line: --parser=babelv5, --parser=babylon, --parser=flow.

API

jscodeshift now accepts a second argument that is directly passed to recast's parse method. This allows you to load your own parser, as long as it is compatible with recast:

jscodeshift(source, {parser: require('myParser')})

Transformer

The value of the parser export of the transformer is used as parser. The value can either be the name of one of the built-in parsers (babel, babylon, flow) or an object that can be directly passed to recast.parse.

Examples:

export const parser = 'flow';
// or
export {default as parser} from 'myParser';

This allows transformers to specify their own parser (as long as it is compatible with ESTree / recast), making them a bit more independent from jscodeshift's internals.


jscodeshift --version now also lists the versions of the built-in parsers

$ jscodeshift --version
jscodeshift: 0.3.21
 - babel: 5.8.38
 - babylon: 6.8.1
 - flow: 0.26.0

Reinstalling jscodeshift is probably the simplest way for now to update the built-in parsers.

Don't miss a new jscodeshift release

NewReleases is sending notifications on new releases.