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

Make sure babel parser throws exactly same recoverable errors when estree plugin is enabled #12375

Merged
merged 13 commits into from
Dec 3, 2020
Merged
Prev Previous commit
Next Next commit
feat: check recoverable errors on estree-throw
  • Loading branch information
JLHwung committed Nov 19, 2020
commit 054e7db91a4bc8fdff6e2b4d125ea83757d8e472
29 changes: 26 additions & 3 deletions packages/babel-parser/test/helpers/runFixtureTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ export function runThrowTestsWithEstree(fixturesPath, parseFunction) {
Object.keys(fixtures).forEach(function (name) {
fixtures[name].forEach(function (testSuite) {
testSuite.tests.forEach(function (task) {
if (!task.options.throws) return;
if (!task.options.throws) {
const hasErrors =
!task.disabled && "errors" in JSON.parse(task.expect.code);
if (!hasErrors) {
return;
}
}

task.options.plugins = task.options.plugins || [];
task.options.plugins.push("estree");
Expand All @@ -92,7 +98,7 @@ export function runThrowTestsWithEstree(fixturesPath, parseFunction) {

testFn(name + "/" + testSuite.title + "/" + task.title, function () {
try {
runTest(task, parseFunction);
runTest(task, parseFunction, true);
} catch (err) {
const fixturePath = `${path.relative(
rootPath,
Expand Down Expand Up @@ -126,7 +132,19 @@ function save(test, ast) {
);
}

function runTest(test, parseFunction) {
/**
* run parser on given tests
*
* @param {Test} A {@link packages/babel-helper-fixtures/src/index.js Test} instance
generated from `getFixtures`
* @param {*} parseFunction A parser with the same interface of `@babel/parser#parse`
* @param {boolean} [compareErrorsOnly=false] Whether we should only compare the "errors"
* of generated ast against the expected AST. Used for `runThrowTestsWithEstree` where an
* ESTree AST is generated but we want to make sure `@babel/parser` still throws expected
* recoverable errors on given code locations.
* @returns {void}
*/
function runTest(test, parseFunction, compareErrorsOnly = false) {
const opts = test.options;

if (opts.throws && test.expect.code) {
Expand Down Expand Up @@ -189,6 +207,11 @@ function runTest(test, parseFunction) {
throw new Error(
"Expected error message: " + opts.throws + ". But parsing succeeded.",
);
} else if (compareErrorsOnly) {
const mis = misMatch(JSON.parse(test.expect.code).errors, ast.errors);
if (mis) {
throw new Error(mis);
}
} else {
const mis = misMatch(JSON.parse(test.expect.code), ast);

Expand Down