Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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