Skip to content

Commit

Permalink
feat: use local babel config if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Jan 25, 2019
1 parent c791667 commit 512f81d
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 15 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({
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,
});
},
};
}
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,11 @@ cross-spawn@^6.0.4, cross-spawn@^6.0.5:
shebang-command "^1.2.0"
which "^1.2.9"

crypto-random-string@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-1.0.0.tgz#a230f64f568310e1498009940790ec99545bca7e"
integrity sha1-ojD2T1aDEOFJgAmUB5DsmVRbyn4=

cssom@0.3.x, "cssom@>= 0.3.2 < 0.4.0":
version "0.3.4"
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.3.4.tgz#8cd52e8a3acfd68d3aed38ee0a640177d2f9d797"
Expand Down Expand Up @@ -4602,6 +4607,11 @@ tar@^4:
safe-buffer "^5.1.2"
yallist "^3.0.2"

temp-dir@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/temp-dir/-/temp-dir-1.0.0.tgz#0a7c0ea26d3a39afa7e0ebea9c1fc0bc4daa011d"
integrity sha1-CnwOom06Oa+n4OvqnB/AvE2qAR0=

temp@^0.8.1:
version "0.8.3"
resolved "https://registry.yarnpkg.com/temp/-/temp-0.8.3.tgz#e0c6bc4d26b903124410e4fed81103014dfc1f59"
Expand All @@ -4610,6 +4620,14 @@ temp@^0.8.1:
os-tmpdir "^1.0.0"
rimraf "~2.2.6"

tempy@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/tempy/-/tempy-0.2.1.tgz#9038e4dbd1c201b74472214179bc2c6f7776e54c"
integrity sha512-LB83o9bfZGrntdqPuRdanIVCPReam9SOZKW0fOy5I9X3A854GGWi0tjCqoXEk84XIEYBc/x9Hq3EFop/H5wJaw==
dependencies:
temp-dir "^1.0.0"
unique-string "^1.0.0"

test-exclude@^4.2.1:
version "4.2.3"
resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-4.2.3.tgz#a9a5e64474e4398339245a0a769ad7c2f4a97c20"
Expand Down Expand Up @@ -4768,6 +4786,13 @@ union-value@^1.0.0:
is-extendable "^0.1.1"
set-value "^0.4.3"

unique-string@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/unique-string/-/unique-string-1.0.0.tgz#9e1057cca851abb93398f8b33ae187b99caec11a"
integrity sha1-nhBXzKhRq7kzmPizOuGHuZyuwRo=
dependencies:
crypto-random-string "^1.0.0"

unset-value@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/unset-value/-/unset-value-1.0.0.tgz#8376873f7d2335179ffb1e6fc3a8ed0dfc8ab559"
Expand Down

0 comments on commit 512f81d

Please sign in to comment.