Skip to content

Commit 60196fc

Browse files
jquensedanez
authored andcommitted
feat: use local babel config if it exists (#320)
* feat: use local babel config if it exists * fix import deuplicates * fix lock file
1 parent a05d74f commit 60196fc

File tree

6 files changed

+60
-17
lines changed

6 files changed

+60
-17
lines changed

bin/react-docgen.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ if (argv.resolver) {
112112
}
113113
}
114114

115-
function parse(source) {
115+
function parse(source, filename) {
116116
return parser.parse(source, resolver, null, {
117+
filename,
117118
legacyDecorators: argv.legacyDecorators,
118119
decoratorsBeforeExport: argv.decoratorsBeforeExport,
119120
});
@@ -153,7 +154,7 @@ function traverseDir(filePath, result, done) {
153154
throw error;
154155
}
155156
try {
156-
result[filename] = parse(content);
157+
result[filename] = parse(content, path.join(filePath, filename));
157158
} catch (parseError) {
158159
writeError(parseError, filename);
159160
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"author": "Felix Kling",
3737
"license": "BSD-3-Clause",
3838
"dependencies": {
39-
"@babel/parser": "^7.1.3",
39+
"@babel/core": "^7.0.0",
4040
"@babel/runtime": "^7.0.0",
4141
"async": "^2.1.4",
4242
"commander": "^2.19.0",

src/__tests__/parse-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

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

13+
const fs = require('fs');
14+
const temp = require('temp');
15+
1316
jest.disableAutomock();
1417

1518
describe('parse', () => {
@@ -48,4 +51,25 @@ describe('parse', () => {
4851
);
4952
expect(resolver).toBeCalled();
5053
});
54+
55+
it.only('uses local babelrc', () => {
56+
const dir = temp.mkdirSync();
57+
58+
try {
59+
// Write and empty babelrc to override the parser defaults
60+
fs.writeFileSync(`${dir}/.babelrc`, '{}');
61+
62+
expect(() =>
63+
parse('const chained = () => foo?.bar?.join?.()', () => {}, null, {
64+
cwd: dir,
65+
filename: `${dir}/component.js`,
66+
}),
67+
).toThrowError(
68+
/.*Support for the experimental syntax 'optionalChaining' isn't currently enabled.*/,
69+
);
70+
} finally {
71+
fs.unlinkSync(`${dir}/.babelrc`);
72+
fs.rmdirSync(dir);
73+
}
74+
});
5175
});

src/babelParser.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*/
1212

13-
const parser = require('@babel/parser');
13+
const babel = require('@babel/core');
1414

1515
const babelParserOptions = {
1616
sourceType: 'module',
@@ -43,33 +43,52 @@ const babelParserOptions = {
4343
};
4444

4545
export type Options = {
46+
cwd?: string,
47+
filename?: string,
4648
legacyDecorators?: boolean,
4749
decoratorsBeforeExport?: boolean,
4850
};
4951

50-
function buildOptions(options?: Options = {}) {
52+
function buildOptions(options: Options) {
5153
const parserOptions = {
52-
...babelParserOptions,
53-
plugins: [...babelParserOptions.plugins],
54+
strictMode: false,
55+
tokens: true,
56+
plugins: [],
5457
};
58+
5559
if (options.legacyDecorators) {
5660
parserOptions.plugins.push('decorators-legacy');
57-
} else {
58-
parserOptions.plugins.push([
59-
'decorators',
60-
{ decoratorsBeforeExport: options.decoratorsBeforeExport || false },
61-
]);
61+
}
62+
63+
const partialConfig = babel.loadPartialConfig({
64+
cwd: options.cwd,
65+
filename: options.filename,
66+
});
67+
68+
if (!partialConfig.hasFilesystemConfig()) {
69+
parserOptions.plugins = [...babelParserOptions.plugins];
70+
71+
if (!options.legacyDecorators) {
72+
parserOptions.plugins.push([
73+
'decorators',
74+
{ decoratorsBeforeExport: options.decoratorsBeforeExport || false },
75+
]);
76+
}
6277
}
6378

6479
return parserOptions;
6580
}
6681

67-
export default function buildParse(options: Options) {
68-
const parserOptions = buildOptions(options);
82+
export default function buildParse(options?: Options = {}) {
83+
const parserOpts = buildOptions(options);
6984

7085
return {
7186
parse(src: string) {
72-
return parser.parse(src, parserOptions);
87+
return babel.parseSync(src, {
88+
parserOpts,
89+
cwd: options.cwd,
90+
filename: options.filename,
91+
});
7392
},
7493
};
7594
}

src/resolver/__tests__/findAllExportedComponentDefinitions-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ describe('findAllExportedComponentDefinitions', () => {
761761

762762
parsed = parse(`
763763
import React from "React"
764-
var React = require("React");
764+
765765
var Component = React.createClass({});
766766
export {Component, foo}
767767
`);

src/resolver/__tests__/findExportedComponentDefinition-test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,6 @@ describe('findExportedComponentDefinition', () => {
619619

620620
source = `
621621
import React from "React"
622-
var React = require("React");
623622
var Component = React.createClass({});
624623
export {Component, foo}
625624
`;

0 commit comments

Comments
 (0)