Skip to content

Commit

Permalink
Revert "Use @babel/eslint-parser instead of babel-eslint (#2487)" (#2492
Browse files Browse the repository at this point in the history
)

This reverts commit ca56746
  • Loading branch information
saberduck authored Feb 12, 2021
1 parent 0ff86d0 commit 8f58cd7
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 191 deletions.
10 changes: 2 additions & 8 deletions eslint-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,10 @@
"ts-node": "9.0.0"
},
"dependencies": {
"@babel/core": "7.12.13",
"@babel/eslint-parser": "7.12.13",
"@babel/preset-flow": "^7.12.13",
"@babel/preset-react": "^7.12.13",
"@typescript-eslint/eslint-plugin": "4.15.0",
"@typescript-eslint/experimental-utils": "4.15.0",
"@typescript-eslint/parser": "4.15.0",
"babel-eslint": "10.1.0",
"body-parser": "1.18.3",
"builtin-modules": "3.1.0",
"bytes": "3.1.0",
Expand All @@ -62,10 +59,7 @@
"@typescript-eslint/eslint-plugin",
"@typescript-eslint/experimental-utils",
"@typescript-eslint/parser",
"@babel/core",
"@babel/eslint-parser",
"@babel/preset-flow",
"@babel/preset-react",
"babel-eslint",
"body-parser",
"builtin-modules",
"bytes",
Expand Down
33 changes: 11 additions & 22 deletions eslint-bridge/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* 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 babel from '@babel/eslint-parser';
import * as babel from 'babel-eslint';
import { Linter, SourceCode } from 'eslint';
import { ParsingError } from './analyzer';
import * as VueJS from 'vue-eslint-parser';
Expand Down Expand Up @@ -68,7 +68,7 @@ export function parseJavaScriptSourceFile(

let exceptionToReport: ParseException | null = null;
for (const config of [PARSER_CONFIG_MODULE, PARSER_CONFIG_SCRIPT]) {
const result = parse(babel.parseForESLint, babelConfig(config), fileContent);
const result = parse(babel.parseForESLint, config, fileContent);
if (result instanceof SourceCode) {
return result;
} else if (!exceptionToReport) {
Expand Down Expand Up @@ -119,19 +119,16 @@ export function parseVueSourceFile(
tsConfigs?: string[],
): SourceCode | ParsingError {
let exception: ParseException | null = null;
const parserOptions = {
filePath,
project: tsConfigs,
extraFileExtensions: ['.vue'],
...PARSER_CONFIG_MODULE,
};
const parsers = [
{ parser: '@typescript-eslint/parser', parserOptions },
{ parser: '@babel/eslint-parser', parserOptions: babelConfig(parserOptions) },
];
for (const { parser, parserOptions } of parsers) {
const parsers = ['@typescript-eslint/parser', 'babel-eslint'];
for (const parser of parsers) {
try {
const result = VueJS.parseForESLint(fileContent, { parser, ...parserOptions });
const result = VueJS.parseForESLint(fileContent, {
filePath,
parser,
project: tsConfigs,
extraFileExtensions: ['.vue'],
...PARSER_CONFIG_MODULE,
});
return new SourceCode(({
...result,
parserServices: result.services,
Expand Down Expand Up @@ -191,11 +188,3 @@ export function parseExceptionCodeOf(exceptionMsg: string): ParseExceptionCode {
return ParseExceptionCode.Parsing;
}
}

export function babelConfig(config: Linter.ParserOptions) {
const pluginPath = `${__dirname}/../node_modules`;
const babelOptions = {
presets: [`${pluginPath}/@babel/preset-react`, `${pluginPath}/@babel/preset-flow`],
};
return { ...config, requireConfigFile: false, babelOptions };
}
19 changes: 9 additions & 10 deletions eslint-bridge/tests/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ import {
parseVueSourceFile,
ParseExceptionCode,
parseExceptionCodeOf,
babelConfig,
} from 'parser';
import * as babel from '@babel/eslint-parser';
} from '../src/parser';
import * as babel from 'babel-eslint';
import { SourceCode } from 'eslint';
import { ParsingError } from 'analyzer';
import { ParsingError } from '../src/analyzer';
import visit from '../src/utils/visitor';
import * as path from 'path';
import * as fs from 'fs';
import { setContext } from 'context';
import { setContext } from '../src/context';

describe('parseJavaScriptSourceFile', () => {
beforeEach(() => {
Expand Down Expand Up @@ -281,7 +280,7 @@ describe('parseVueSourceFile', () => {
) as ParsingError;
expect(parsingError).toBeDefined();
expect(parsingError.line).toEqual(4);
expect(parsingError.message).toContain('Unexpected token (3:4)');
expect(parsingError.message).toEqual('Unexpected token (3:4)');
expect(parsingError.code).toEqual(ParseExceptionCode.Parsing);
});

Expand Down Expand Up @@ -335,13 +334,13 @@ function expectToNotParse(code: string, message: string) {
const parsingError = parseJavaScriptSourceFile(code, 'foo.js') as ParsingError;
expect(parsingError).toBeDefined();
expect(parsingError.line).toEqual(1);
expect(parsingError.message).toContain(message);
expect(parsingError.message).toEqual(message);
}

function expectToParseInNonStrictMode(code: string, msgInStrictMode: string) {
const result1 = parse(babel.parse, babelConfig(PARSER_CONFIG_MODULE), code);
expect((result1 as ParseException).message).toContain(msgInStrictMode);
const result1 = parse(babel.parse, PARSER_CONFIG_MODULE, code);
expect((result1 as ParseException).message).toEqual(msgInStrictMode);

const result2 = parse(babel.parse, babelConfig(PARSER_CONFIG_SCRIPT), code);
const result2 = parse(babel.parse, PARSER_CONFIG_SCRIPT, code);
expect((result2 as SourceCode).ast.body.length).toBeGreaterThan(0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { RuleTester } from 'eslint';
import { rule } from 'rules/declarations-in-global-scope';
import { babelRuleTester } from '../utils/babel-rule-tester';

const tsParserPath = require.resolve('@typescript-eslint/parser');
const ruleTester = new RuleTester({
Expand All @@ -38,7 +36,11 @@ const ruleTesterScript = new RuleTester({
parserOptions: { ecmaVersion: 2018, sourceType: 'script' },
});

const ruleTesterBabel = babelRuleTester();
const ruleTesterBabel = new RuleTester({
parser: require.resolve('babel-eslint'),
});

import { rule } from 'rules/declarations-in-global-scope';

ruleTester.run('Variables and functions should not be declared in the global scope', rule, {
valid: [
Expand Down
8 changes: 6 additions & 2 deletions eslint-bridge/tests/rules/no-dead-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { rule } from 'rules/no-dead-store';
import { RuleTester } from 'eslint';
import { RuleTesterTs } from '../RuleTesterTs';
import { babelRuleTester } from '../utils/babel-rule-tester';

const ruleTester = babelRuleTester();
const ruleTester = new RuleTester({
// we use babel to parse JSX syntax
parser: require.resolve('babel-eslint'),
parserOptions: { ecmaVersion: 2018, sourceType: 'module' },
});

const valid = [
{
Expand Down
5 changes: 3 additions & 2 deletions eslint-bridge/tests/rules/no-globals-shadowing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const ruleTester = new RuleTester({
});

import { rule } from 'rules/no-globals-shadowing';
import { babelRuleTester } from '../utils/babel-rule-tester';

ruleTester.run('Special identifiers should not be bound or assigned', rule, {
valid: [
Expand Down Expand Up @@ -197,7 +196,9 @@ ruleTester.run('Special identifiers should not be bound or assigned', rule, {
],
});

const ruleTesterBabel = babelRuleTester();
const ruleTesterBabel = new RuleTester({
parser: require.resolve('babel-eslint'),
});

ruleTesterBabel.run('Special identifiers should not be bound or assigned', rule, {
valid: [
Expand Down
9 changes: 3 additions & 6 deletions eslint-bridge/tests/rules/no-unused-expressions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,22 @@ ruleTester.run('Disallow unused expressions', rule, {
});

import { rules as chaiFriendlyRules } from 'eslint-plugin-chai-friendly';
import { babelConfig } from 'parser';

for (let { parser, languageSpecificRule, parserOptions } of [
for (let { parser, languageSpecificRule } of [
{
parser: '@typescript-eslint/parser',
languageSpecificRule: decorateTypescriptEslint(
require('@typescript-eslint/eslint-plugin').rules!['no-unused-expressions'],
),
parserOptions: { ecmaVersion: 2018 },
},
{
parser: '@babel/eslint-parser',
parser: 'babel-eslint',
languageSpecificRule: decorateJavascriptEslint(chaiFriendlyRules['no-unused-expressions']),
parserOptions: babelConfig({ ecmaVersion: 2018 }),
},
]) {
const tester = new RuleTester({
parser: require.resolve(parser),
parserOptions,
parserOptions: { ecmaVersion: 2018 },
});

tester.run(`Disallow unused expressions - negations & IIFE (${parser})`, languageSpecificRule, {
Expand Down
6 changes: 4 additions & 2 deletions eslint-bridge/tests/rules/unused-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
*/
import { RuleTester } from 'eslint';
import { rule } from 'rules/unused-import';
import { babelRuleTester } from '../utils/babel-rule-tester';

const ruleTesterJS = babelRuleTester();
const ruleTesterJS = new RuleTester({
parserOptions: { ecmaVersion: 2018, sourceType: 'module' },
parser: require.resolve('babel-eslint'),
});

ruleTesterJS.run('Unnecessary imports should be removed', rule, {
valid: [
Expand Down
29 changes: 0 additions & 29 deletions eslint-bridge/tests/utils/babel-rule-tester.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare module "@babel/eslint-parser" {
declare module "babel-eslint" {
import { AST, Linter, Scope, SourceCode } from "eslint";
function parse(input: string, config?: Linter.ParserOptions): AST.Program;
function parseForESLint(input: string, config?: Linter.ParserOptions): { ast: AST.Program, parserServices: SourceCode.ParserServices, scopeManager: Scope.ScopeManager, visitorKeys: SourceCode.VisitorKeys };
Expand Down
Loading

0 comments on commit 8f58cd7

Please sign in to comment.