Use babylon estree and ranges (babel/babel-eslint#489)
This commit is contained in:
parent
36a630e023
commit
bffbaff7e2
@ -30,7 +30,6 @@ module.exports = function (ast, traverse, tt, code) {
|
|||||||
ast.directives = ast.program.directives;
|
ast.directives = ast.program.directives;
|
||||||
ast.body = ast.program.body;
|
ast.body = ast.program.body;
|
||||||
delete ast.program;
|
delete ast.program;
|
||||||
delete ast._paths;
|
|
||||||
|
|
||||||
attachComments(ast, ast.comments, ast.tokens);
|
attachComments(ast, ast.comments, ast.tokens);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,31 +1,26 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
var t = require("babel-types");
|
||||||
var convertComments = require("./convertComments");
|
var convertComments = require("./convertComments");
|
||||||
|
|
||||||
module.exports = function (ast, traverse, code) {
|
module.exports = function (ast, traverse, code) {
|
||||||
var state = { source: code };
|
var state = { source: code };
|
||||||
ast.range = [ast.start, ast.end];
|
|
||||||
traverse(ast, astTransformVisitor, null, state);
|
|
||||||
};
|
|
||||||
|
|
||||||
function changeToLiteral(node, state) {
|
// Monkey patch visitor keys in order to be able to traverse the estree nodes
|
||||||
node.type = "Literal";
|
t.VISITOR_KEYS.Property = t.VISITOR_KEYS.ObjectProperty;
|
||||||
if (!node.raw) {
|
t.VISITOR_KEYS.MethodDefinition = ["key", "value", "decorators", "returnType", "typeParameters"];
|
||||||
if (node.extra && node.extra.raw) {
|
|
||||||
node.raw = node.extra.raw;
|
traverse(ast, astTransformVisitor, null, state);
|
||||||
} else {
|
|
||||||
node.raw = state.source.slice(node.start, node.end);
|
delete t.VISITOR_KEYS.Property;
|
||||||
}
|
delete t.VISITOR_KEYS.MethodDefinition;
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
var astTransformVisitor = {
|
var astTransformVisitor = {
|
||||||
noScope: true,
|
noScope: true,
|
||||||
enter (path) {
|
enter (path) {
|
||||||
var node = path.node;
|
var node = path.node;
|
||||||
|
|
||||||
node.range = [node.start, node.end];
|
|
||||||
|
|
||||||
// private var to track original node type
|
// private var to track original node type
|
||||||
node._babelType = node.type;
|
node._babelType = node.type;
|
||||||
|
|
||||||
@ -41,133 +36,17 @@ var astTransformVisitor = {
|
|||||||
if (node.leadingComments) {
|
if (node.leadingComments) {
|
||||||
convertComments(node.leadingComments);
|
convertComments(node.leadingComments);
|
||||||
}
|
}
|
||||||
|
|
||||||
// make '_paths' non-enumerable (babel-eslint #200)
|
|
||||||
Object.defineProperty(node, "_paths", { value: node._paths, writable: true });
|
|
||||||
},
|
},
|
||||||
exit (path, state) {
|
exit (path) {
|
||||||
var node = path.node;
|
var node = path.node;
|
||||||
|
|
||||||
// fixDirectives
|
|
||||||
if (path.isFunction() || path.isProgram()) {
|
|
||||||
var directivesContainer = node;
|
|
||||||
var body = node.body;
|
|
||||||
if (node.type !== "Program") {
|
|
||||||
directivesContainer = body;
|
|
||||||
body = body.body;
|
|
||||||
}
|
|
||||||
if (directivesContainer.directives) {
|
|
||||||
for (var i = directivesContainer.directives.length - 1; i >= 0; i--) {
|
|
||||||
var directive = directivesContainer.directives[i];
|
|
||||||
directive.type = "ExpressionStatement";
|
|
||||||
directive.expression = directive.value;
|
|
||||||
delete directive.value;
|
|
||||||
directive.expression.type = "Literal";
|
|
||||||
changeToLiteral(directive.expression, state);
|
|
||||||
body.unshift(directive);
|
|
||||||
}
|
|
||||||
delete directivesContainer.directives;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (path.isJSXText()) {
|
if (path.isJSXText()) {
|
||||||
node.type = "Literal";
|
node.type = "Literal";
|
||||||
node.raw = node.value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.isNumericLiteral() ||
|
// TODO estree plugin bug
|
||||||
path.isStringLiteral()) {
|
if (node.type === "Property") {
|
||||||
changeToLiteral(node, state);
|
if (!node.shorthand) node.shorthand = false;
|
||||||
}
|
|
||||||
|
|
||||||
if (path.isBooleanLiteral()) {
|
|
||||||
node.type = "Literal";
|
|
||||||
node.raw = String(node.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (path.isNullLiteral()) {
|
|
||||||
node.type = "Literal";
|
|
||||||
node.raw = "null";
|
|
||||||
node.value = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (path.isRegExpLiteral()) {
|
|
||||||
node.type = "Literal";
|
|
||||||
node.raw = node.extra.raw;
|
|
||||||
try {
|
|
||||||
node.value = new RegExp(node.pattern, node.flags);
|
|
||||||
} catch (err) {
|
|
||||||
node.value = null;
|
|
||||||
}
|
|
||||||
node.regex = {
|
|
||||||
pattern: node.pattern,
|
|
||||||
flags: node.flags
|
|
||||||
};
|
|
||||||
delete node.extra;
|
|
||||||
delete node.pattern;
|
|
||||||
delete node.flags;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (path.isObjectProperty()) {
|
|
||||||
node.type = "Property";
|
|
||||||
node.kind = "init";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (path.isClassMethod() || path.isObjectMethod()) {
|
|
||||||
var code = state.source.slice(node.key.end, node.body.start);
|
|
||||||
var offset = code.indexOf("(");
|
|
||||||
|
|
||||||
node.value = {
|
|
||||||
type: "FunctionExpression",
|
|
||||||
id: node.id,
|
|
||||||
params: node.params,
|
|
||||||
body: node.body,
|
|
||||||
async: node.async,
|
|
||||||
generator: node.generator,
|
|
||||||
expression: node.expression,
|
|
||||||
defaults: [], // basic support - TODO: remove (old esprima)
|
|
||||||
loc: {
|
|
||||||
start: {
|
|
||||||
line: node.key.loc.start.line,
|
|
||||||
column: node.key.loc.end.column + offset // a[() {]
|
|
||||||
},
|
|
||||||
end: node.body.loc.end
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// [asdf]() {
|
|
||||||
node.value.range = [node.key.end + offset, node.body.end];
|
|
||||||
|
|
||||||
node.value.start = node.value.range && node.value.range[0] || node.value.loc.start.column;
|
|
||||||
node.value.end = node.value.range && node.value.range[1] || node.value.loc.end.column;
|
|
||||||
|
|
||||||
if (node.returnType) {
|
|
||||||
node.value.returnType = node.returnType;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (node.typeParameters) {
|
|
||||||
node.value.typeParameters = node.typeParameters;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (path.isClassMethod()) {
|
|
||||||
node.type = "MethodDefinition";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (path.isObjectMethod()) {
|
|
||||||
node.type = "Property";
|
|
||||||
if (node.kind === "method") {
|
|
||||||
node.kind = "init";
|
|
||||||
}
|
|
||||||
node.shorthand = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
delete node.body;
|
|
||||||
delete node.id;
|
|
||||||
delete node.async;
|
|
||||||
delete node.generator;
|
|
||||||
delete node.expression;
|
|
||||||
delete node.params;
|
|
||||||
delete node.returnType;
|
|
||||||
delete node.typeParameters;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.isRestElement() && path.parent && path.parent.type === "ObjectPattern") {
|
if (path.isRestElement() && path.parent && path.parent.type === "ObjectPattern") {
|
||||||
@ -178,7 +57,7 @@ var astTransformVisitor = {
|
|||||||
node.type = "ExperimentalSpreadProperty";
|
node.type = "ExperimentalSpreadProperty";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.isTypeParameter && path.isTypeParameter()) {
|
if (path.isTypeParameter()) {
|
||||||
node.type = "Identifier";
|
node.type = "Identifier";
|
||||||
node.typeAnnotation = node.bound;
|
node.typeAnnotation = node.bound;
|
||||||
delete node.bound;
|
delete node.bound;
|
||||||
@ -208,22 +87,6 @@ var astTransformVisitor = {
|
|||||||
delete node.isType;
|
delete node.isType;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (path.isExportDeclaration()) {
|
|
||||||
var declar = path.get("declaration");
|
|
||||||
if (declar.isClassExpression()) {
|
|
||||||
node.declaration.type = "ClassDeclaration";
|
|
||||||
} else if (declar.isFunctionExpression()) {
|
|
||||||
node.declaration.type = "FunctionDeclaration";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: remove (old esprima)
|
|
||||||
if (path.isFunction()) {
|
|
||||||
if (!node.defaults) {
|
|
||||||
node.defaults = [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// template string range fixes
|
// template string range fixes
|
||||||
if (path.isTemplateLiteral()) {
|
if (path.isTemplateLiteral()) {
|
||||||
for (var j = 0; j < node.quasis.length; j++) {
|
for (var j = 0; j < node.quasis.length; j++) {
|
||||||
|
|||||||
@ -388,9 +388,11 @@ exports.parseNoPatch = function (code, options) {
|
|||||||
allowImportExportEverywhere: options.allowImportExportEverywhere, // consistent with espree
|
allowImportExportEverywhere: options.allowImportExportEverywhere, // consistent with espree
|
||||||
allowReturnOutsideFunction: true,
|
allowReturnOutsideFunction: true,
|
||||||
allowSuperOutsideMethod: true,
|
allowSuperOutsideMethod: true,
|
||||||
|
ranges: true,
|
||||||
plugins: [
|
plugins: [
|
||||||
"flow",
|
"flow",
|
||||||
"jsx",
|
"jsx",
|
||||||
|
"estree",
|
||||||
"asyncFunctions",
|
"asyncFunctions",
|
||||||
"asyncGenerators",
|
"asyncGenerators",
|
||||||
"classConstructorCall",
|
"classConstructorCall",
|
||||||
|
|||||||
@ -371,7 +371,7 @@ describe("babylon-to-esprima", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("babel 6 tests", () => {
|
describe("babel tests", () => {
|
||||||
it("MethodDefinition", () => {
|
it("MethodDefinition", () => {
|
||||||
parseAndAssertSame(
|
parseAndAssertSame(
|
||||||
unpad(`
|
unpad(`
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user