Skip to content

Commit

Permalink
Chore: align babel-eslint-parser & espree
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Feb 13, 2020
1 parent 3fc904e commit 2d77d45
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 69 deletions.
19 changes: 12 additions & 7 deletions eslint/babel-eslint-parser/src/convert/convertAST.js
@@ -1,14 +1,10 @@
import { types as t, traverse } from "@babel/core";

function convertNodes(ast, code) {
const state = { source: code };
const astTransformVisitor = {
noScope: true,
enter(path) {
const node = path.node;

// private var to track original node type
node._babelType = node.type;
const { node } = path;

if (node.innerComments) {
delete node.innerComments;
Expand All @@ -23,7 +19,15 @@ function convertNodes(ast, code) {
}
},
exit(path) {
const node = path.node;
const { node } = path;

if (Object.hasOwnProperty.call(node, "extra")) {
delete node.extra;
}

if (node.loc && Object.hasOwnProperty.call(node.loc, "identifierName")) {
delete node.loc.identifierName;
}

if (path.isTypeParameter()) {
node.type = "Identifier";
Expand Down Expand Up @@ -74,6 +78,7 @@ function convertNodes(ast, code) {
}
},
};
const state = { source: code };

// Monkey patch visitor keys in order to be able to traverse the estree nodes
t.VISITOR_KEYS.Property = t.VISITOR_KEYS.ObjectProperty;
Expand All @@ -95,9 +100,9 @@ function convertNodes(ast, code) {
function convertProgramNode(ast) {
ast.type = "Program";
ast.sourceType = ast.program.sourceType;
ast.directives = ast.program.directives;
ast.body = ast.program.body;
delete ast.program;
delete ast.errors;

if (ast.comments.length) {
const lastComment = ast.comments[ast.comments.length - 1];
Expand Down
40 changes: 0 additions & 40 deletions eslint/babel-eslint-parser/test/helpers/assert-implements-ast.js

This file was deleted.

73 changes: 51 additions & 22 deletions eslint/babel-eslint-parser/test/index.js
@@ -1,19 +1,43 @@
import assert from "assert";
import espree from "espree";
import escope from "eslint-scope";
import unpad from "dedent";
import { parseForESLint } from "../src";
import assertImplementsAST from "./helpers/assert-implements-ast";

const babelOptions = {
const BABEL_OPTIONS = {
configFile: require.resolve(
"@babel/eslint-shared-fixtures/config/babel.config.js",
),
};
const ALLOWED_PROPERTIES = [
"importKind",
"exportKind",
"variance",
"typeArguments",
];

function deeplyRemoveProperties(obj, props) {
for (const [k, v] of Object.entries(obj)) {
if (typeof v === "object") {
if (Array.isArray(v)) {
for (const el of v) {
if (el != null) {
deeplyRemoveProperties(el, props);
}
}
}

if (props.includes(k)) delete obj[k];
else v != null && deeplyRemoveProperties(v, props);
continue;
}

if (props.includes(k)) delete obj[k];
}
}

function parseAndAssertSame(code) {
code = unpad(code);
const esAST = espree.parse(code, {
const espreeAST = espree.parse(code, {
ecmaFeatures: {
// enable JSX parsing
jsx: true,
Expand All @@ -31,21 +55,22 @@ function parseAndAssertSame(code) {
ecmaVersion: 2020,
sourceType: "module",
});
const babylonAST = parseForESLint(code, {
const babelAST = parseForESLint(code, {
eslintVisitorKeys: true,
eslintScopeManager: true,
babelOptions,
babelOptions: BABEL_OPTIONS,
}).ast;
assertImplementsAST(esAST, babylonAST);
deeplyRemoveProperties(babelAST, ALLOWED_PROPERTIES);
expect(babelAST).toEqual(espreeAST);
}

describe("babylon-to-espree", () => {
describe("Babel and Espree", () => {
describe("compatibility", () => {
it("should allow ast.analyze to be called without options", function() {
const esAST = parseForESLint("`test`", {
eslintScopeManager: true,
eslintVisitorKeys: true,
babelOptions,
babelOptions: BABEL_OPTIONS,
}).ast;
expect(() => {
escope.analyze(esAST);
Expand Down Expand Up @@ -244,9 +269,9 @@ describe("babylon-to-espree", () => {
const babylonAST = parseForESLint(code, {
eslintVisitorKeys: true,
eslintScopeManager: true,
babelOptions,
babelOptions: BABEL_OPTIONS,
}).ast;
assert.strictEqual(babylonAST.tokens[1].type, "Punctuator");
expect(babylonAST.tokens[1].type).toEqual("Punctuator");
});

// Espree doesn't support the nullish coalescing operator yet
Expand All @@ -255,9 +280,9 @@ describe("babylon-to-espree", () => {
const babylonAST = parseForESLint(code, {
eslintVisitorKeys: true,
eslintScopeManager: true,
babelOptions,
babelOptions: BABEL_OPTIONS,
}).ast;
assert.strictEqual(babylonAST.tokens[1].type, "Punctuator");
expect(babylonAST.tokens[1].type).toEqual("Punctuator");
});

// Espree doesn't support the pipeline operator yet
Expand All @@ -266,9 +291,9 @@ describe("babylon-to-espree", () => {
const babylonAST = parseForESLint(code, {
eslintVisitorKeys: true,
eslintScopeManager: true,
babelOptions,
babelOptions: BABEL_OPTIONS,
}).ast;
assert.strictEqual(babylonAST.tokens[1].type, "Punctuator");
expect(babylonAST.tokens[1].type).toEqual("Punctuator");
});

// Espree doesn't support private fields yet
Expand All @@ -277,10 +302,10 @@ describe("babylon-to-espree", () => {
const babylonAST = parseForESLint(code, {
eslintVisitorKeys: true,
eslintScopeManager: true,
babelOptions,
babelOptions: BABEL_OPTIONS,
}).ast;
assert.strictEqual(babylonAST.tokens[3].type, "Punctuator");
assert.strictEqual(babylonAST.tokens[3].value, "#");
expect(babylonAST.tokens[3].type).toEqual("Punctuator");
expect(babylonAST.tokens[3].value).toEqual("#");
});

// eslint-disable-next-line jest/no-disabled-tests
Expand Down Expand Up @@ -448,19 +473,23 @@ describe("babylon-to-espree", () => {
});

it("do not allow import export everywhere", () => {
assert.throws(() => {
expect(() => {
parseAndAssertSame('function F() { import a from "a"; }');
}, /SyntaxError: 'import' and 'export' may only appear at the top level/);
}).toThrow(
new SyntaxError(
"'import' and 'export' may only appear at the top level",
),
);
});

it("return outside function", () => {
parseAndAssertSame("return;");
});

it("super outside method", () => {
assert.throws(() => {
expect(() => {
parseAndAssertSame("function F() { super(); }");
}, /SyntaxError: 'super' keyword outside a method/);
}).toThrow(new SyntaxError("'super' keyword outside a method"));
});

it("StringLiteral", () => {
Expand Down

0 comments on commit 2d77d45

Please sign in to comment.