Skip to content

Commit ad74ac8

Browse files
committed
feat: use local babel config if it exists
1 parent c791667 commit ad74ac8

File tree

5 files changed

+86
-16
lines changed

5 files changed

+86
-16
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: 3 additions & 2 deletions
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",
@@ -64,7 +64,8 @@
6464
"jest-matcher-utils": "^23.6.0",
6565
"prettier": "^1.14.3",
6666
"rimraf": "^2.3.2",
67-
"temp": "^0.8.1"
67+
"temp": "^0.8.1",
68+
"tempy": "^0.2.1"
6869
},
6970
"jest": {
7071
"setupTestFrameworkScriptFile": "<rootDir>/tests/setupTestFramework.js",

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 tempy = require('tempy');
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 = tempy.directory();
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
}

yarn.lock

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,11 @@ cross-spawn@^6.0.4, cross-spawn@^6.0.5:
14061406
shebang-command "^1.2.0"
14071407
which "^1.2.9"
14081408

1409+
crypto-random-string@^1.0.0:
1410+
version "1.0.0"
1411+
resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-1.0.0.tgz#a230f64f568310e1498009940790ec99545bca7e"
1412+
integrity sha1-ojD2T1aDEOFJgAmUB5DsmVRbyn4=
1413+
14091414
[email protected], "cssom@>= 0.3.2 < 0.4.0":
14101415
version "0.3.4"
14111416
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.3.4.tgz#8cd52e8a3acfd68d3aed38ee0a640177d2f9d797"
@@ -4602,6 +4607,11 @@ tar@^4:
46024607
safe-buffer "^5.1.2"
46034608
yallist "^3.0.2"
46044609

4610+
temp-dir@^1.0.0:
4611+
version "1.0.0"
4612+
resolved "https://registry.yarnpkg.com/temp-dir/-/temp-dir-1.0.0.tgz#0a7c0ea26d3a39afa7e0ebea9c1fc0bc4daa011d"
4613+
integrity sha1-CnwOom06Oa+n4OvqnB/AvE2qAR0=
4614+
46054615
temp@^0.8.1:
46064616
version "0.8.3"
46074617
resolved "https://registry.yarnpkg.com/temp/-/temp-0.8.3.tgz#e0c6bc4d26b903124410e4fed81103014dfc1f59"
@@ -4610,6 +4620,14 @@ temp@^0.8.1:
46104620
os-tmpdir "^1.0.0"
46114621
rimraf "~2.2.6"
46124622

4623+
tempy@^0.2.1:
4624+
version "0.2.1"
4625+
resolved "https://registry.yarnpkg.com/tempy/-/tempy-0.2.1.tgz#9038e4dbd1c201b74472214179bc2c6f7776e54c"
4626+
integrity sha512-LB83o9bfZGrntdqPuRdanIVCPReam9SOZKW0fOy5I9X3A854GGWi0tjCqoXEk84XIEYBc/x9Hq3EFop/H5wJaw==
4627+
dependencies:
4628+
temp-dir "^1.0.0"
4629+
unique-string "^1.0.0"
4630+
46134631
test-exclude@^4.2.1:
46144632
version "4.2.3"
46154633
resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-4.2.3.tgz#a9a5e64474e4398339245a0a769ad7c2f4a97c20"
@@ -4768,6 +4786,13 @@ union-value@^1.0.0:
47684786
is-extendable "^0.1.1"
47694787
set-value "^0.4.3"
47704788

4789+
unique-string@^1.0.0:
4790+
version "1.0.0"
4791+
resolved "https://registry.yarnpkg.com/unique-string/-/unique-string-1.0.0.tgz#9e1057cca851abb93398f8b33ae187b99caec11a"
4792+
integrity sha1-nhBXzKhRq7kzmPizOuGHuZyuwRo=
4793+
dependencies:
4794+
crypto-random-string "^1.0.0"
4795+
47714796
unset-value@^1.0.0:
47724797
version "1.0.0"
47734798
resolved "https://registry.yarnpkg.com/unset-value/-/unset-value-1.0.0.tgz#8376873f7d2335179ffb1e6fc3a8ed0dfc8ab559"

0 commit comments

Comments
 (0)