Skip to content

Commit

Permalink
Fix error when parsing ignored files with @babel/eslint-parser (#13338
Browse files Browse the repository at this point in the history
)

* 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>
  • Loading branch information
2 people authored and nicolo-ribaudo committed May 19, 2021
1 parent 461ba25 commit 1219004
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
26 changes: 24 additions & 2 deletions eslint/babel-eslint-parser/src/worker/configuration.cjs
Expand Up @@ -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) {
const parseOptions = normalizeParserOptions(options);

if (process.env.BABEL_8_BREAKING) {
return babel
.loadPartialConfigAsync(parseOptions)
.then(config => validateResolvedConfig(config, options) || parseOptions);
.then(config => validateConfigWithFallback(config));
} else {
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);
}
}
};
18 changes: 15 additions & 3 deletions eslint/babel-eslint-parser/test/index.js
Expand Up @@ -5,9 +5,11 @@ import { fileURLToPath } from "url";
import { createRequire } from "module";
import { parseForESLint } from "../lib/index.cjs";

const dirname = path.dirname(fileURLToPath(import.meta.url));

const BABEL_OPTIONS = {
configFile: path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
dirname,
"../../babel-eslint-shared-fixtures/config/babel.config.js",
),
};
Expand Down Expand Up @@ -75,7 +77,7 @@ describe("Babel and Espree", () => {
expect(babelAST).toEqual(espreeAST);
}

beforeAll(async () => {
beforeAll(() => {
const require = createRequire(import.meta.url);

// Use the version of Espree that is a dependency of
Expand All @@ -88,7 +90,7 @@ describe("Babel and Espree", () => {
});

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`", {
eslintScopeManager: true,
eslintVisitorKeys: true,
Expand All @@ -98,6 +100,16 @@ describe("Babel and Espree", () => {
escope.analyze(ast);
}).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", () => {
Expand Down

0 comments on commit 1219004

Please sign in to comment.