Further refactoring of @babel/eslint-parser (#11071)
This commit is contained in:
parent
d57b9aabdd
commit
66721eaa0e
@ -318,7 +318,7 @@ class Referencer extends OriginalReferencer {
|
||||
}
|
||||
}
|
||||
|
||||
export default function(ast, parserOptions) {
|
||||
export default function analyzeScope(ast, parserOptions) {
|
||||
const options = {
|
||||
ignoreEval: true,
|
||||
optimistic: false,
|
||||
|
||||
@ -1,96 +0,0 @@
|
||||
import { types as t, traverse } from "@babel/core";
|
||||
import convertProgramNode from "./convertProgramNode";
|
||||
|
||||
const astTransformVisitor = {
|
||||
noScope: true,
|
||||
enter(path) {
|
||||
const node = path.node;
|
||||
|
||||
// private var to track original node type
|
||||
node._babelType = node.type;
|
||||
|
||||
if (node.innerComments) {
|
||||
delete node.innerComments;
|
||||
}
|
||||
|
||||
if (node.trailingComments) {
|
||||
delete node.trailingComments;
|
||||
}
|
||||
|
||||
if (node.leadingComments) {
|
||||
delete node.leadingComments;
|
||||
}
|
||||
},
|
||||
exit(path) {
|
||||
const node = path.node;
|
||||
|
||||
if (path.isTypeParameter()) {
|
||||
node.type = "Identifier";
|
||||
node.typeAnnotation = node.bound;
|
||||
delete node.bound;
|
||||
}
|
||||
|
||||
// flow: prevent "no-undef"
|
||||
// for "Component" in: "let x: React.Component"
|
||||
if (path.isQualifiedTypeIdentifier()) {
|
||||
delete node.id;
|
||||
}
|
||||
// for "b" in: "var a: { b: Foo }"
|
||||
if (path.isObjectTypeProperty()) {
|
||||
delete node.key;
|
||||
}
|
||||
// for "indexer" in: "var a: {[indexer: string]: number}"
|
||||
if (path.isObjectTypeIndexer()) {
|
||||
delete node.id;
|
||||
}
|
||||
// for "param" in: "var a: { func(param: Foo): Bar };"
|
||||
if (path.isFunctionTypeParam()) {
|
||||
delete node.name;
|
||||
}
|
||||
|
||||
// modules
|
||||
if (path.isImportDeclaration()) {
|
||||
delete node.isType;
|
||||
}
|
||||
|
||||
// template string range fixes
|
||||
if (path.isTemplateLiteral()) {
|
||||
for (let i = 0; i < node.quasis.length; i++) {
|
||||
const q = node.quasis[i];
|
||||
q.range[0] -= 1;
|
||||
if (q.tail) {
|
||||
q.range[1] += 1;
|
||||
} else {
|
||||
q.range[1] += 2;
|
||||
}
|
||||
q.loc.start.column -= 1;
|
||||
if (q.tail) {
|
||||
q.loc.end.column += 1;
|
||||
} else {
|
||||
q.loc.end.column += 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
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,38 +0,0 @@
|
||||
export default function(ast) {
|
||||
ast.type = "Program";
|
||||
ast.sourceType = ast.program.sourceType;
|
||||
ast.directives = ast.program.directives;
|
||||
ast.body = ast.program.body;
|
||||
delete ast.program;
|
||||
|
||||
if (ast.comments.length) {
|
||||
const lastComment = ast.comments[ast.comments.length - 1];
|
||||
|
||||
if (!ast.tokens.length) {
|
||||
// if no tokens, the program starts at the end of the last comment
|
||||
ast.start = lastComment.end;
|
||||
ast.loc.start.line = lastComment.loc.end.line;
|
||||
ast.loc.start.column = lastComment.loc.end.column;
|
||||
} else {
|
||||
const lastToken = ast.tokens[ast.tokens.length - 1];
|
||||
|
||||
if (lastComment.end > lastToken.end) {
|
||||
// If there is a comment after the last token, the program ends at the
|
||||
// last token and not the comment
|
||||
ast.range[1] = lastToken.end;
|
||||
ast.loc.end.line = lastToken.loc.end.line;
|
||||
ast.loc.end.column = lastToken.loc.end.column;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!ast.tokens.length) {
|
||||
ast.loc.start.line = 1;
|
||||
ast.loc.end.line = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (ast.body && ast.body.length > 0) {
|
||||
ast.loc.start.line = ast.body[0].loc.start.line;
|
||||
ast.range[0] = ast.body[0].start;
|
||||
}
|
||||
}
|
||||
@ -1,92 +0,0 @@
|
||||
import { tokTypes as tt } from "@babel/core";
|
||||
|
||||
export default function(tokens) {
|
||||
let curlyBrace = null;
|
||||
let templateTokens = [];
|
||||
const result = [];
|
||||
|
||||
function addTemplateType() {
|
||||
const start = templateTokens[0];
|
||||
const end = templateTokens[templateTokens.length - 1];
|
||||
|
||||
const value = templateTokens.reduce((result, token) => {
|
||||
if (token.value) {
|
||||
result += token.value;
|
||||
} else if (token.type !== tt.template) {
|
||||
result += token.type.label;
|
||||
}
|
||||
|
||||
return result;
|
||||
}, "");
|
||||
|
||||
result.push({
|
||||
type: "Template",
|
||||
value: value,
|
||||
start: start.start,
|
||||
end: end.end,
|
||||
loc: {
|
||||
start: start.loc.start,
|
||||
end: end.loc.end,
|
||||
},
|
||||
});
|
||||
|
||||
templateTokens = [];
|
||||
}
|
||||
|
||||
tokens.forEach(token => {
|
||||
switch (token.type) {
|
||||
case tt.backQuote:
|
||||
if (curlyBrace) {
|
||||
result.push(curlyBrace);
|
||||
curlyBrace = null;
|
||||
}
|
||||
|
||||
templateTokens.push(token);
|
||||
|
||||
if (templateTokens.length > 1) {
|
||||
addTemplateType();
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case tt.dollarBraceL:
|
||||
templateTokens.push(token);
|
||||
addTemplateType();
|
||||
break;
|
||||
|
||||
case tt.braceR:
|
||||
if (curlyBrace) {
|
||||
result.push(curlyBrace);
|
||||
}
|
||||
|
||||
curlyBrace = token;
|
||||
break;
|
||||
|
||||
case tt.template:
|
||||
if (curlyBrace) {
|
||||
templateTokens.push(curlyBrace);
|
||||
curlyBrace = null;
|
||||
}
|
||||
|
||||
templateTokens.push(token);
|
||||
break;
|
||||
|
||||
case tt.eof:
|
||||
if (curlyBrace) {
|
||||
result.push(curlyBrace);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
if (curlyBrace) {
|
||||
result.push(curlyBrace);
|
||||
curlyBrace = null;
|
||||
}
|
||||
|
||||
result.push(token);
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
@ -1,8 +0,0 @@
|
||||
import convertTemplateType from "./convertTemplateType";
|
||||
import convertToken from "./convertToken";
|
||||
|
||||
export default function(tokens, code) {
|
||||
return convertTemplateType(tokens)
|
||||
.filter(t => t.type !== "CommentLine" && t.type !== "CommentBlock")
|
||||
.map(t => convertToken(t, code));
|
||||
}
|
||||
137
eslint/babel-eslint-parser/src/convert/convertAST.js
Normal file
137
eslint/babel-eslint-parser/src/convert/convertAST.js
Normal file
@ -0,0 +1,137 @@
|
||||
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;
|
||||
|
||||
if (node.innerComments) {
|
||||
delete node.innerComments;
|
||||
}
|
||||
|
||||
if (node.trailingComments) {
|
||||
delete node.trailingComments;
|
||||
}
|
||||
|
||||
if (node.leadingComments) {
|
||||
delete node.leadingComments;
|
||||
}
|
||||
},
|
||||
exit(path) {
|
||||
const node = path.node;
|
||||
|
||||
if (path.isTypeParameter()) {
|
||||
node.type = "Identifier";
|
||||
node.typeAnnotation = node.bound;
|
||||
delete node.bound;
|
||||
}
|
||||
|
||||
// flow: prevent "no-undef"
|
||||
// for "Component" in: "let x: React.Component"
|
||||
if (path.isQualifiedTypeIdentifier()) {
|
||||
delete node.id;
|
||||
}
|
||||
// for "b" in: "var a: { b: Foo }"
|
||||
if (path.isObjectTypeProperty()) {
|
||||
delete node.key;
|
||||
}
|
||||
// for "indexer" in: "var a: {[indexer: string]: number}"
|
||||
if (path.isObjectTypeIndexer()) {
|
||||
delete node.id;
|
||||
}
|
||||
// for "param" in: "var a: { func(param: Foo): Bar };"
|
||||
if (path.isFunctionTypeParam()) {
|
||||
delete node.name;
|
||||
}
|
||||
|
||||
// modules
|
||||
if (path.isImportDeclaration()) {
|
||||
delete node.isType;
|
||||
}
|
||||
|
||||
// template string range fixes
|
||||
if (path.isTemplateLiteral()) {
|
||||
for (let i = 0; i < node.quasis.length; i++) {
|
||||
const q = node.quasis[i];
|
||||
q.range[0] -= 1;
|
||||
if (q.tail) {
|
||||
q.range[1] += 1;
|
||||
} else {
|
||||
q.range[1] += 2;
|
||||
}
|
||||
q.loc.start.column -= 1;
|
||||
if (q.tail) {
|
||||
q.loc.end.column += 1;
|
||||
} else {
|
||||
q.loc.end.column += 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
// 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);
|
||||
|
||||
// These can be safely deleted because they are not defined in the original visitor keys.
|
||||
delete t.VISITOR_KEYS.Property;
|
||||
delete t.VISITOR_KEYS.MethodDefinition;
|
||||
}
|
||||
|
||||
function convertProgramNode(ast) {
|
||||
ast.type = "Program";
|
||||
ast.sourceType = ast.program.sourceType;
|
||||
ast.directives = ast.program.directives;
|
||||
ast.body = ast.program.body;
|
||||
delete ast.program;
|
||||
|
||||
if (ast.comments.length) {
|
||||
const lastComment = ast.comments[ast.comments.length - 1];
|
||||
|
||||
if (!ast.tokens.length) {
|
||||
// if no tokens, the program starts at the end of the last comment
|
||||
ast.start = lastComment.end;
|
||||
ast.loc.start.line = lastComment.loc.end.line;
|
||||
ast.loc.start.column = lastComment.loc.end.column;
|
||||
} else {
|
||||
const lastToken = ast.tokens[ast.tokens.length - 1];
|
||||
|
||||
if (lastComment.end > lastToken.end) {
|
||||
// If there is a comment after the last token, the program ends at the
|
||||
// last token and not the comment
|
||||
ast.range[1] = lastToken.end;
|
||||
ast.loc.end.line = lastToken.loc.end.line;
|
||||
ast.loc.end.column = lastToken.loc.end.column;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (!ast.tokens.length) {
|
||||
ast.loc.start.line = 1;
|
||||
ast.loc.end.line = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (ast.body && ast.body.length > 0) {
|
||||
ast.loc.start.line = ast.body[0].loc.start.line;
|
||||
ast.range[0] = ast.body[0].start;
|
||||
}
|
||||
}
|
||||
|
||||
export default function convertAST(ast, code) {
|
||||
convertNodes(ast, code);
|
||||
convertProgramNode(ast);
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
export default function(comments) {
|
||||
export default function convertComments(comments) {
|
||||
for (let i = 0; i < comments.length; i++) {
|
||||
const comment = comments[i];
|
||||
if (comment.type === "CommentBlock") {
|
||||
@ -1,6 +1,97 @@
|
||||
import { tokTypes as tt } from "@babel/core";
|
||||
|
||||
export default function(token, source) {
|
||||
function convertTemplateType(tokens) {
|
||||
let curlyBrace = null;
|
||||
let templateTokens = [];
|
||||
const result = [];
|
||||
|
||||
function addTemplateType() {
|
||||
const start = templateTokens[0];
|
||||
const end = templateTokens[templateTokens.length - 1];
|
||||
|
||||
const value = templateTokens.reduce((result, token) => {
|
||||
if (token.value) {
|
||||
result += token.value;
|
||||
} else if (token.type !== tt.template) {
|
||||
result += token.type.label;
|
||||
}
|
||||
|
||||
return result;
|
||||
}, "");
|
||||
|
||||
result.push({
|
||||
type: "Template",
|
||||
value: value,
|
||||
start: start.start,
|
||||
end: end.end,
|
||||
loc: {
|
||||
start: start.loc.start,
|
||||
end: end.loc.end,
|
||||
},
|
||||
});
|
||||
|
||||
templateTokens = [];
|
||||
}
|
||||
|
||||
tokens.forEach(token => {
|
||||
switch (token.type) {
|
||||
case tt.backQuote:
|
||||
if (curlyBrace) {
|
||||
result.push(curlyBrace);
|
||||
curlyBrace = null;
|
||||
}
|
||||
|
||||
templateTokens.push(token);
|
||||
|
||||
if (templateTokens.length > 1) {
|
||||
addTemplateType();
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case tt.dollarBraceL:
|
||||
templateTokens.push(token);
|
||||
addTemplateType();
|
||||
break;
|
||||
|
||||
case tt.braceR:
|
||||
if (curlyBrace) {
|
||||
result.push(curlyBrace);
|
||||
}
|
||||
|
||||
curlyBrace = token;
|
||||
break;
|
||||
|
||||
case tt.template:
|
||||
if (curlyBrace) {
|
||||
templateTokens.push(curlyBrace);
|
||||
curlyBrace = null;
|
||||
}
|
||||
|
||||
templateTokens.push(token);
|
||||
break;
|
||||
|
||||
case tt.eof:
|
||||
if (curlyBrace) {
|
||||
result.push(curlyBrace);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
if (curlyBrace) {
|
||||
result.push(curlyBrace);
|
||||
curlyBrace = null;
|
||||
}
|
||||
|
||||
result.push(token);
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function convertToken(token, source) {
|
||||
const type = token.type;
|
||||
token.range = [token.start, token.end];
|
||||
|
||||
@ -86,3 +177,9 @@ export default function(token, source) {
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
export default function convertTokens(tokens, code) {
|
||||
return convertTemplateType(tokens)
|
||||
.filter(t => t.type !== "CommentLine" && t.type !== "CommentBlock")
|
||||
.map(t => convertToken(t, code));
|
||||
}
|
||||
@ -2,7 +2,7 @@ import visitorKeys from "./visitor-keys";
|
||||
import analyzeScope from "./analyze-scope";
|
||||
import parse from "./parse";
|
||||
|
||||
export default function(code, options) {
|
||||
export default function parseWithScope(code, options) {
|
||||
const ast = parse(code, options);
|
||||
const scopeManager = analyzeScope(ast, options);
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import { parseSync as babelParse } from "@babel/core";
|
||||
import babylonToEspree from "./babylon-to-espree";
|
||||
import convert from "./convert";
|
||||
import { normalizeBabelParseConfig } from "./configuration";
|
||||
|
||||
export default function parse(code, options) {
|
||||
@ -17,7 +17,7 @@ export default function parse(code, options) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
babylonToEspree(ast, code);
|
||||
convert(ast, code);
|
||||
|
||||
return ast;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user