Skip to content

Migrate babylon to @babel/parser #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

`react-docgen` is a CLI and toolbox to help extracting information from [React][] components, and generate documentation from it.

It uses [recast][] and [babylon][] to parse the source into an AST and provides methods to process this AST to extract the desired information. The output / return value is a JSON blob / JavaScript object.
It uses [recast][] and [@babel/parser][] to parse the source into an AST and provides methods to process this AST to extract the desired information. The output / return value is a JSON blob / JavaScript object.

It provides a default implementation for React components defined via
`React.createClass`, [ES2015 class definitions][classes] or functions
Expand Down Expand Up @@ -381,5 +381,5 @@ The structure of the JSON blob / JavaScript object is as follows:
[react]: http://facebook.github.io/react/
[flow]: http://flowtype.org/
[recast]: https://github.com/benjamn/recast
[babylon]: https://github.com/babel/babylon
[@babel/parser]: https://github.com/babel/babel/tree/master/packages/babel-parser
[classes]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
5 changes: 4 additions & 1 deletion bin/react-docgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ argv
'Filename or regex to exclude. Default: ' + JSON.stringify(defaultExclude),
collect,
[])
.option(
'--legacy-decorators',
'Enable parsing of legacy decorators proposal. By default only the new decorators syntax will be parsable.')
.option(
'-i, --ignore <path>',
'Folders to ignore. Default: ' + JSON.stringify(defaultIgnore),
Expand Down Expand Up @@ -100,7 +103,7 @@ if (argv.resolver) {
}

function parse(source) {
return parser.parse(source, resolver);
return parser.parse(source, resolver, null, { legacyDecorators: argv.legacyDecorators });
}

function writeError(msg, filePath) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
"author": "Felix Kling",
"license": "BSD-3-Clause",
"dependencies": {
"@babel/parser": "7.0.0-beta.48",
"async": "^2.1.4",
"babel-runtime": "^6.9.2",
"babylon": "7.0.0-beta.44",
"commander": "^2.9.0",
"doctrine": "^2.0.0",
"node-dir": "^0.1.10",
Expand Down
69 changes: 69 additions & 0 deletions src/babelParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*
*/

var parser = require('@babel/parser');

var babelParserOptions = {
sourceType: 'module',
strictMode: false,
plugins: [
'jsx',
'flow',
'estree',
'doExpressions',
'objectRestSpread',
'classProperties',
'classPrivateProperties',
'classPrivateMethods',
'exportDefaultFrom',
'exportNamespaceFrom',
'asyncGenerators',
'functionBind',
'functionSent',
'dynamicImport',
'numericSeparator',
'optionalChaining',
'importMeta',
'bigInt',
'optionalCatchBinding',
'throwExpressions',
'pipelineOperator',
'nullishCoalescingOperator',
],
};

export type Options = {
legacyDecorators ?: boolean,
};

function buildOptions(options?: Options = {}) {
const parserOptions = { ...babelParserOptions, plugins: [...babelParserOptions.plugins] };
if (options.legacyDecorators) {
parserOptions.plugins.push('decorators-legacy');
} else {
parserOptions.plugins.push('decorators');
}

return parserOptions;
}

export default function buildParse(options: Options) {
const parserOptions = buildOptions(options);

return {
parse(src: string) {
var file = parser.parse(src, parserOptions);
file.program.comments = file.comments;
return file.program;
},
};
}
48 changes: 0 additions & 48 deletions src/babylon.js

This file was deleted.

28 changes: 17 additions & 11 deletions src/handlers/__tests__/componentDocblockHandler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,18 @@ describe('componentDocblockHandler', () => {
* Decorates can only be assigned to class and therefore only make sense for
* class declarations and export declarations.
*/
function testDecorators(definitionSrc, parse) { // eslint-disable-line no-shadow
function testDecorators(classSrc, parse, exportSrc = '') { // eslint-disable-line no-shadow
describe('decorators', () => {
it('uses the docblock above the decorator if it\'s the only one', () => {
var definition = parse(`
import something from 'somewhere';
/**
* Component description
*/
${exportSrc}
@Decorator1
@Decorator2
${definitionSrc}
${classSrc}
`);

componentDocblockHandler(documentation, definition);
Expand All @@ -109,15 +110,17 @@ describe('componentDocblockHandler', () => {
it('uses the component docblock if present', () => {
var definition = parse(`
import something from 'somewhere';

${exportSrc}
/**
* Decorator description
*/
* Decorator description
*/
@Decorator1
@Decorator2
/**
* Component description
*/
${definitionSrc}
${classSrc}
`);

componentDocblockHandler(documentation, definition);
Expand Down Expand Up @@ -181,8 +184,9 @@ describe('componentDocblockHandler', () => {
src => lastStatement(src).get('declaration')
);
testDecorators(
'export default class Component {}',
src => lastStatement(src).get('declaration')
'class Component {}',
src => lastStatement(src).get('declaration'),
'export default'
);
});

Expand All @@ -192,8 +196,9 @@ describe('componentDocblockHandler', () => {
src => lastStatement(src).get('declaration')
);
testDecorators(
'export default class {}',
src => lastStatement(src).get('declaration')
'class {}',
src => lastStatement(src).get('declaration'),
'export default'
);
});

Expand Down Expand Up @@ -241,8 +246,9 @@ describe('componentDocblockHandler', () => {
src => lastStatement(src).get('declaration')
);
testDecorators(
'export class Component {}',
src => lastStatement(src).get('declaration')
'class Component {}',
src => lastStatement(src).get('declaration'),
'export'
);
});

Expand Down
6 changes: 4 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as handlers from './handlers';
import parse from './parse';
import * as resolver from './resolver';
import * as utils from './utils';
import type { Options } from './babelParser';

var defaultResolver = resolver.findExportedComponentDefinition;
var defaultHandlers = [
Expand Down Expand Up @@ -45,7 +46,8 @@ var defaultHandlers = [
function defaultParse( // eslint-disable-line no-unused-vars
src: string,
resolver?: ?Resolver, // eslint-disable-line no-shadow
handlers?: ?Array<Handler> // eslint-disable-line no-shadow
handlers?: ?Array<Handler>, // eslint-disable-line no-shadow
options ?: Options = {}
): Array<Object>|Object {
if (!resolver) {
resolver = defaultResolver;
Expand All @@ -54,7 +56,7 @@ function defaultParse( // eslint-disable-line no-unused-vars
handlers = defaultHandlers;
}

return parse(src, resolver, handlers);
return parse(src, resolver, handlers, options);
}

export {
Expand Down
7 changes: 4 additions & 3 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import Documentation from './Documentation';
import postProcessDocumentation from './utils/postProcessDocumentation';

import babylon from './babylon';
import buildParser, { type Options } from './babelParser';
import recast from 'recast';

var ERROR_MISSING_DEFINITION = 'No suitable component definition found.';
Expand Down Expand Up @@ -49,9 +49,10 @@ function executeHandlers(handlers, componentDefinitions) {
export default function parse(
src: string,
resolver: Resolver,
handlers: Array<Handler>
handlers: Array<Handler>,
options: Options
): Array<Object>|Object {
var ast = recast.parse(src, {esprima: babylon});
var ast = recast.parse(src, { parser: buildParser(options) });
var componentDefinitions = resolver(ast.program, recast);

if (Array.isArray(componentDefinitions)) {
Expand Down
6 changes: 3 additions & 3 deletions tests/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import _recast from 'recast';
import babylon from '../src/babylon';
import buildParser from '../src/babelParser';

function stringify(value) {
if (Array.isArray(value)) {
Expand All @@ -15,9 +15,9 @@ function stringify(value) {
/**
* Returns a NodePath to the program node of the passed node
*/
export function parse(src, recast=_recast) {
export function parse(src, recast = _recast, options = {}) {
return new recast.types.NodePath(
recast.parse(stringify(src), {esprima: babylon}).program
recast.parse(stringify(src), { parser: buildParser(options) }).program
);
}

Expand Down
15 changes: 14 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
esutils "^2.0.2"
js-tokens "^3.0.0"

"@babel/parser@7.0.0-beta.48":
version "7.0.0-beta.48"
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.0.0-beta.48.tgz#f93895cbacee703c0ec98e5af3901c77edd9f1d7"

"@babel/template@7.0.0-beta.44":
version "7.0.0-beta.44"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.0.0-beta.44.tgz#f8832f4fdcee5d59bf515e595fc5106c529b394f"
Expand Down Expand Up @@ -124,7 +128,16 @@ ajv-keywords@^2.1.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-2.1.1.tgz#617997fc5f60576894c435f940d819e135b80762"

ajv@^5.1.0, ajv@^5.2.3, ajv@^5.3.0:
ajv@^5.1.0:
version "5.3.0"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-5.3.0.tgz#4414ff74a50879c208ee5fdc826e32c303549eda"
dependencies:
co "^4.6.0"
fast-deep-equal "^1.0.0"
fast-json-stable-stringify "^2.0.0"
json-schema-traverse "^0.3.0"

ajv@^5.2.3, ajv@^5.3.0:
version "5.5.2"
resolved "https://registry.yarnpkg.com/ajv/-/ajv-5.5.2.tgz#73b5eeca3fab653e3d3f9422b341ad42205dc965"
dependencies:
Expand Down