Fix error when parsing ignored files with @babel/eslint-parser (#13338)

* fix(babel-eslint-parser): prevent typeerror in maybeParse
* fix(babel-eslint-parser): prevent other typeerrors in convert modules
* test(babel-eslint-parser): test maybeParse when file is ignored
* refactor(babel-eslint-parser): use fallback options instead of typeerror protections

Co-authored-by: devfservant <43757707+devfservant@users.noreply.github.com>
Co-authored-by: François Servant <francois.servant.e@thalesdigital.io>
This commit is contained in:
François Servant 2021-05-19 12:30:04 +02:00 committed by Nicolò Ribaudo
parent 461ba2531a
commit 12190042e6
2 changed files with 39 additions and 5 deletions

View File

@ -58,15 +58,37 @@ function validateResolvedConfig(config, options) {
} }
} }
function getDefaultParserOptions(options) {
return {
plugins: [],
...options,
babelrc: false,
configFile: false,
browserslistConfigFile: false,
ignore: null,
only: null,
};
}
module.exports = function normalizeBabelParseConfig(options) { module.exports = function normalizeBabelParseConfig(options) {
const parseOptions = normalizeParserOptions(options); const parseOptions = normalizeParserOptions(options);
if (process.env.BABEL_8_BREAKING) { if (process.env.BABEL_8_BREAKING) {
return babel return babel
.loadPartialConfigAsync(parseOptions) .loadPartialConfigAsync(parseOptions)
.then(config => validateResolvedConfig(config, options) || parseOptions); .then(config => validateConfigWithFallback(config));
} else { } else {
const config = babel.loadPartialConfigSync(parseOptions); const config = babel.loadPartialConfigSync(parseOptions);
return validateResolvedConfig(config, options) || parseOptions; return validateConfigWithFallback(config);
}
function validateConfigWithFallback(inputConfig) {
const result = validateResolvedConfig(inputConfig, options);
if (result) {
return result;
} else {
// Fallback when `loadPartialConfig` returns `null` (e.g.: when the file is ignored)
return getDefaultParserOptions(parseOptions);
}
} }
}; };

View File

@ -5,9 +5,11 @@ import { fileURLToPath } from "url";
import { createRequire } from "module"; import { createRequire } from "module";
import { parseForESLint } from "../lib/index.cjs"; import { parseForESLint } from "../lib/index.cjs";
const dirname = path.dirname(fileURLToPath(import.meta.url));
const BABEL_OPTIONS = { const BABEL_OPTIONS = {
configFile: path.resolve( configFile: path.resolve(
path.dirname(fileURLToPath(import.meta.url)), dirname,
"../../babel-eslint-shared-fixtures/config/babel.config.js", "../../babel-eslint-shared-fixtures/config/babel.config.js",
), ),
}; };
@ -75,7 +77,7 @@ describe("Babel and Espree", () => {
expect(babelAST).toEqual(espreeAST); expect(babelAST).toEqual(espreeAST);
} }
beforeAll(async () => { beforeAll(() => {
const require = createRequire(import.meta.url); const require = createRequire(import.meta.url);
// Use the version of Espree that is a dependency of // Use the version of Espree that is a dependency of
@ -88,7 +90,7 @@ describe("Babel and Espree", () => {
}); });
describe("compatibility", () => { describe("compatibility", () => {
it("should allow ast.analyze to be called without options", function () { it("should allow ast.analyze to be called without options", () => {
const ast = parseForESLint("`test`", { const ast = parseForESLint("`test`", {
eslintScopeManager: true, eslintScopeManager: true,
eslintVisitorKeys: true, eslintVisitorKeys: true,
@ -98,6 +100,16 @@ describe("Babel and Espree", () => {
escope.analyze(ast); escope.analyze(ast);
}).not.toThrow(new TypeError("Should allow no options argument.")); }).not.toThrow(new TypeError("Should allow no options argument."));
}); });
it("should not crash when `loadPartialConfigSync` returns `null`", () => {
const thunk = () =>
parseForESLint("`test`", {
eslintScopeManager: true,
eslintVisitorKeys: true,
babelOptions: { filename: "test.js", ignore: [/./] },
});
expect(thunk).not.toThrow();
});
}); });
describe("templates", () => { describe("templates", () => {