Skip to content

Commit

Permalink
feat: use local babel config if it exists (#320)
Browse files Browse the repository at this point in the history
* feat: use local babel config if it exists

* fix import deuplicates

* fix lock file
  • Loading branch information
jquense authored and danez committed Jan 28, 2019
1 parent a05d74f commit 60196fc
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 17 deletions.
5 changes: 3 additions & 2 deletions bin/react-docgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ if (argv.resolver) {
}
}

function parse(source) {
function parse(source, filename) {
return parser.parse(source, resolver, null, {
filename,
legacyDecorators: argv.legacyDecorators,
decoratorsBeforeExport: argv.decoratorsBeforeExport,
});
Expand Down Expand Up @@ -153,7 +154,7 @@ function traverseDir(filePath, result, done) {
throw error;
}
try {
result[filename] = parse(content);
result[filename] = parse(content, path.join(filePath, filename));
} catch (parseError) {
writeError(parseError, filename);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"author": "Felix Kling",
"license": "BSD-3-Clause",
"dependencies": {
"@babel/parser": "^7.1.3",
"@babel/core": "^7.0.0",
"@babel/runtime": "^7.0.0",
"async": "^2.1.4",
"commander": "^2.19.0",
Expand Down
24 changes: 24 additions & 0 deletions src/__tests__/parse-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

/*global jest, describe, beforeEach, it, expect*/

const fs = require('fs');
const temp = require('temp');

jest.disableAutomock();

describe('parse', () => {
Expand Down Expand Up @@ -48,4 +51,25 @@ describe('parse', () => {
);
expect(resolver).toBeCalled();
});

it.only('uses local babelrc', () => {
const dir = temp.mkdirSync();

try {
// Write and empty babelrc to override the parser defaults
fs.writeFileSync(`${dir}/.babelrc`, '{}');

expect(() =>
parse('const chained = () => foo?.bar?.join?.()', () => {}, null, {
cwd: dir,
filename: `${dir}/component.js`,
}),
).toThrowError(
/.*Support for the experimental syntax 'optionalChaining' isn't currently enabled.*/,
);
} finally {
fs.unlinkSync(`${dir}/.babelrc`);
fs.rmdirSync(dir);
}
});
});
43 changes: 31 additions & 12 deletions src/babelParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
*/

const parser = require('@babel/parser');
const babel = require('@babel/core');

const babelParserOptions = {
sourceType: 'module',
Expand Down Expand Up @@ -43,33 +43,52 @@ const babelParserOptions = {
};

export type Options = {
cwd?: string,
filename?: string,
legacyDecorators?: boolean,
decoratorsBeforeExport?: boolean,
};

function buildOptions(options?: Options = {}) {
function buildOptions(options: Options) {
const parserOptions = {
...babelParserOptions,
plugins: [...babelParserOptions.plugins],
strictMode: false,
tokens: true,
plugins: [],
};

if (options.legacyDecorators) {
parserOptions.plugins.push('decorators-legacy');
} else {
parserOptions.plugins.push([
'decorators',
{ decoratorsBeforeExport: options.decoratorsBeforeExport || false },
]);
}

const partialConfig = babel.loadPartialConfig({

This comment has been minimized.

Copy link
@fkling

fkling Feb 28, 2019

Member

I may not understand how loadPartialConfig works, but shouldn't the discovered options be used somehow in this method? How do the discovered options make it to parseSync?

This comment has been minimized.

Copy link
@danez

danez Feb 28, 2019

Collaborator

The actual config loading is happening inside parseSync. We just use loadPartialConfig here to see if there will be a config file or not.

https://github.com/babel/babel/blob/master/packages/babel-core/src/parse.js#L61..L72

This comment has been minimized.

Copy link
@fkling

fkling Feb 28, 2019

Member

Oh I see. Currently trying to troubleshoot a problem where we get the error

react-docgen failure: Cannot find module '@babel/plugin-proposal-class-properties' from '/path/to/some/dir'

in our CI runs, but not when I run react-docgen on my devserver. I figured it must have something to do with the babel config chances since that's the really the only thing that changed in v4.

Hard to find out what the reall problem is though 😞

This comment has been minimized.

Copy link
@danez

danez Mar 1, 2019

Collaborator

How are you runnning docgen? Is /path/to/some/dir in the project and would resolving node_modules from there work?

My guess is that the cwd is somehow wrong when running docgen, but hard to say. If you are running it with the node API you can also specify the cwd in the options. And soon some more options: #334

cwd: options.cwd,
filename: options.filename,
});

if (!partialConfig.hasFilesystemConfig()) {
parserOptions.plugins = [...babelParserOptions.plugins];

if (!options.legacyDecorators) {
parserOptions.plugins.push([
'decorators',
{ decoratorsBeforeExport: options.decoratorsBeforeExport || false },
]);
}
}

return parserOptions;
}

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

return {
parse(src: string) {
return parser.parse(src, parserOptions);
return babel.parseSync(src, {
parserOpts,
cwd: options.cwd,
filename: options.filename,
});
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ describe('findAllExportedComponentDefinitions', () => {

parsed = parse(`
import React from "React"
var React = require("React");
var Component = React.createClass({});
export {Component, foo}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,6 @@ describe('findExportedComponentDefinition', () => {

source = `
import React from "React"
var React = require("React");
var Component = React.createClass({});
export {Component, foo}
`;
Expand Down

0 comments on commit 60196fc

Please sign in to comment.