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

Use only babel-eslint to parse #1143

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion eslint-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"body-parser": "1.18.3",
"eslint": "5.6.0",
"eslint-plugin-sonarjs": "0.2.0",
"espree": "4.0.0",
"express": "4.16.3"
},
"prettier": {
Expand Down
9 changes: 2 additions & 7 deletions eslint-bridge/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import * as espree from "espree";
import * as babel from "babel-eslint";
import { SourceCode, Linter } from "eslint";

Expand All @@ -38,12 +37,8 @@ const PARSER_CONFIG: Linter.ParserOptions = {
const PARSER_CONFIG_NOT_STRICT: Linter.ParserOptions = { ...PARSER_CONFIG, sourceType: "script" };

export function parseSourceFile(fileContent: string, fileUri: string): SourceCode | undefined {
let parse = espree.parse;
let parser = "espree";
if (fileContent.includes("@flow")) {
parse = babel.parse;
parser = "babel-eslint";
}
const parse = babel.parse;
const parser = "babel-eslint";

try {
return parseSourceFileAsModule(parse, fileContent);
Expand Down
55 changes: 35 additions & 20 deletions eslint-bridge/tests/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseSourceFile, parseSourceFileAsModule, parseSourceFileAsScript } from "../src/parser";
import * as espree from "espree";
import * as babel from "babel-eslint";

describe("parseSourceFile", () => {
beforeEach(() => {
Expand All @@ -14,13 +14,11 @@ describe("parseSourceFile", () => {
it("should not parse when invalid code and log reason", () => {
expect(parseSourceFile("export { a, a }", "foo.js")).toBeUndefined();
expect(console.error).toBeCalledWith(
"Failed to parse file [foo.js] at line 1: Duplicate export 'a' (with espree parser in module mode)",
"Failed to parse file [foo.js] at line 1: `a` has already been exported. Exported identifiers must be unique. (1:12) (with babel-eslint parser in module mode)",
);
expect(console.log).toBeCalledWith(
"DEBUG Failed to parse file [foo.js] at line 1: 'import' and 'export' may appear only with 'sourceType: module' (with espree parser in script mode)",
"DEBUG Failed to parse file [foo.js] at line 1: `a` has already been exported. Exported identifiers must be unique. (1:12) (with babel-eslint parser in script mode)",
);

expect(parseSourceFile("export Foo from 'Foo'", "foo.js")).toBeUndefined();
});

it("should parse jsx", () => {
Expand All @@ -32,7 +30,6 @@ describe("parseSourceFile", () => {
it("should parse flow when with @flow", () => {
expect(parseSourceFile("/* @flow */ const foo: string = 'hello';", "foo.js")).toBeDefined();
expect(parseSourceFile("/* @flow */ var eval = 42", "foo.js")).toBeDefined();
expect(parseSourceFile("const foo: string = 'hello';", "foo.js")).toBeUndefined();
});

it("should parse scripts (with retry after module)", () => {
Expand All @@ -41,30 +38,48 @@ describe("parseSourceFile", () => {
});

it("should parse as script (non-strict mode)", () => {
expectToParseInNonStrictMode(`var eval = 42`, `"Binding eval in strict mode"`);
expectToParseInNonStrictMode(`eval = 42`, `"Assigning to eval in strict mode"`);
expectToParseInNonStrictMode(`var eval = 42`, `"eval is a reserved word in strict mode (1:4)"`);
expectToParseInNonStrictMode(`eval = 42`, `"eval is a reserved word in strict mode (1:0)"`);

// this is not an error in babel,
// see https://github.com/babel/babel/issues/6722
// expectToParseInNonStrictMode(
// `function foo() {}\n var foo = 42;`,
// `"Identifier 'foo' has already been declared"`,
// );

expectToParseInNonStrictMode(`x = 043;`, `"Invalid number (1:4)"`);
expectToParseInNonStrictMode(`'\\033'`, `"Octal literal in strict mode (1:2)"`);
expectToParseInNonStrictMode(`with (a) {}`, `"'with' in strict mode (1:0)"`);
expectToParseInNonStrictMode(`public = 42`, `"public is a reserved word in strict mode (1:0)"`);
expectToParseInNonStrictMode(
`function foo() {}\n var foo = 42;`,
`"Identifier 'foo' has already been declared"`,
`function foo(a, a) {}`,
`"Argument name clash in strict mode (1:16)"`,
);

expectToParseInNonStrictMode(`x = 043;`, `"Invalid number"`);
expectToParseInNonStrictMode(`'\\033'`, `"Octal literal in strict mode"`);
expectToParseInNonStrictMode(`with (a) {}`, `"'with' in strict mode"`);
expectToParseInNonStrictMode(`public = 42`, `"The keyword 'public' is reserved"`);
expectToParseInNonStrictMode(`function foo(a, a) {}`, `"Argument name clash"`);
expectToParseInNonStrictMode(`delete x`, `"Deleting local variable in strict mode"`);
expectToParseInNonStrictMode(`delete x`, `"Deleting local variable in strict mode (1:0)"`);

function expectToParseInNonStrictMode(sourceCode, msgInStrictMode) {
expect(() =>
parseSourceFileAsModule(espree.parse, sourceCode),
parseSourceFileAsModule(babel.parse, sourceCode),
).toThrowErrorMatchingInlineSnapshot(msgInStrictMode);
expect(parseSourceFileAsScript(espree.parse, sourceCode)).toBeDefined();
expect(parseSourceFileAsScript(babel.parse, sourceCode)).toBeDefined();
}
});

it("should parse recent javascript syntax", () => {
let sourceCode;
// stage-0
sourceCode = parseSourceFile(
`
class Foo {
static prop = 1;
bar = () => {}
}
`,
"foo.js",
);
expect(sourceCode.ast.body.length).toBeGreaterThan(0);

// ES2018
sourceCode = parseSourceFile(
`const obj = {foo: 1, bar: 2, baz: 3};
Expand Down Expand Up @@ -103,7 +118,7 @@ describe("parseSourceFile", () => {
expect(sourceCode).toBeUndefined();
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(
`Failed to parse file [foo.js] at line 1: Unexpected token ) (with espree parser in module mode)`,
`Failed to parse file [foo.js] at line 1: Unexpected token (1:3) (with babel-eslint parser in module mode)`,
);
});

Expand Down
4 changes: 0 additions & 4 deletions eslint-bridge/typings/espree.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion eslint-bridge/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1291,7 +1291,7 @@ [email protected]:
table "^4.0.3"
text-table "^0.2.0"

espree@4.0.0, espree@^4.0.0:
espree@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/espree/-/espree-4.0.0.tgz#253998f20a0f82db5d866385799d912a83a36634"
integrity sha512-kapdTCt1bjmspxStVKX6huolXVV5ZfyZguY1lcfhVVZstce3bqxH9mcLzNn3/mlgW6wQ732+0fuG9v7h0ZQoKg==
Expand Down