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

@babel/eslint-parser: Refactor #10916

Merged
merged 1 commit into from Dec 23, 2019
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
44 changes: 22 additions & 22 deletions eslint/babel-eslint-parser/src/babylon-to-espree/convertAST.js
@@ -1,27 +1,6 @@
import { types as t } from "@babel/core";
import { types as t, traverse } from "@babel/core";
import convertProgramNode from "./convertProgramNode";

module.exports = function(ast, traverse, 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;
t.VISITOR_KEYS.MethodDefinition = [
"key",
"value",
"decorators",
"returnType",
"typeParameters",
];

traverse(ast, astTransformVisitor, null, state);

delete t.VISITOR_KEYS.Property;
delete t.VISITOR_KEYS.MethodDefinition;

convertProgramNode(ast);
};

const astTransformVisitor = {
noScope: true,
enter(path) {
Expand Down Expand Up @@ -94,3 +73,24 @@ const astTransformVisitor = {
}
},
};

export default function(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;
t.VISITOR_KEYS.MethodDefinition = [
"key",
"value",
"decorators",
"returnType",
"typeParameters",
];

traverse(ast, astTransformVisitor, null, state);

delete t.VISITOR_KEYS.Property;
delete t.VISITOR_KEYS.MethodDefinition;

convertProgramNode(ast);
}
@@ -1,4 +1,6 @@
export default function(tokens, tt) {
import { tokTypes as tt } from "@babel/core";

export default function(tokens) {
let curlyBrace = null;
let templateTokens = [];
const result = [];
Expand Down
@@ -1,4 +1,6 @@
export default function(token, tt, source) {
import { tokTypes as tt } from "@babel/core";

export default function(token, source) {
const type = token.type;
token.range = [token.start, token.end];

Expand Down
@@ -1,8 +1,8 @@
import convertTemplateType from "./convertTemplateType";
import convertToken from "./convertToken";

export default function(tokens, tt, code) {
return convertTemplateType(tokens, tt)
export default function(tokens, code) {
return convertTemplateType(tokens)
.filter(t => t.type !== "CommentLine" && t.type !== "CommentBlock")
.map(t => convertToken(t, tt, code));
.map(t => convertToken(t, code));
}
6 changes: 3 additions & 3 deletions eslint/babel-eslint-parser/src/babylon-to-espree/index.js
Expand Up @@ -2,8 +2,8 @@ import convertTokens from "./convertTokens";
import convertComments from "./convertComments";
import convertAST from "./convertAST";

export default function(ast, traverse, tt, code) {
ast.tokens = convertTokens(ast.tokens, tt, code);
export default function(ast, code) {
ast.tokens = convertTokens(ast.tokens, code);
convertComments(ast.comments);
convertAST(ast, traverse, code);
convertAST(ast, code);
}
2 changes: 1 addition & 1 deletion eslint/babel-eslint-parser/src/index.js
Expand Up @@ -12,7 +12,7 @@ const IS_RUNNING_SUPPORTED_VERSION = semver.satisfies(
);

export function parse(code, options) {
return exports.parseForESLint(code, options).ast;
return parseForESLint(code, options).ast;
}

export function parseForESLint(code, options = {}) {
Expand Down
4 changes: 2 additions & 2 deletions eslint/babel-eslint-parser/src/parse.js
@@ -1,4 +1,4 @@
import { parseSync as babelParse, tokTypes as tt, traverse } from "@babel/core";
import { parseSync as babelParse } from "@babel/core";
import babylonToEspree from "./babylon-to-espree";
import { normalizeBabelParseConfig } from "./configuration";

Expand All @@ -17,7 +17,7 @@ export default function parse(code, options) {
throw err;
}

babylonToEspree(ast, traverse, tt, code);
babylonToEspree(ast, code);

return ast;
}