Merge pull request babel/babel-eslint#455 from babel/babylon-to-espree-tidy

Tidy up babylon-to-espree
This commit is contained in:
Henry Zhu 2017-03-25 19:15:23 -04:00
parent 2ca65d5451
commit 711c1e2f4b
8 changed files with 101 additions and 105 deletions

View File

@ -1,3 +1,5 @@
"use strict";
// comment fixes // comment fixes
module.exports = function (ast, comments, tokens) { module.exports = function (ast, comments, tokens) {
if (comments.length) { if (comments.length) {

View File

@ -0,0 +1,17 @@
"use strict";
module.exports = function (comments) {
for (var i = 0; i < comments.length; i++) {
var comment = comments[i];
if (comment.type === "CommentBlock") {
comment.type = "Block";
} else if (comment.type === "CommentLine") {
comment.type = "Line";
}
// sometimes comments don't get ranges computed,
// even with options.ranges === true
if (!comment.range) {
comment.range = [comment.start, comment.end];
}
}
};

View File

@ -1,3 +1,5 @@
"use strict";
module.exports = function (tokens, tt) { module.exports = function (tokens, tt) {
var startingToken = 0; var startingToken = 0;
var currentToken = 0; var currentToken = 0;

View File

@ -1,20 +1,36 @@
exports.attachComments = require("./attachComments"); "use strict";
exports.toTokens = require("./toTokens"); var attachComments = require("./attachComments");
exports.toAST = require("./toAST"); var convertComments = require("./convertComments");
var toTokens = require("./toTokens");
var toAST = require("./toAST");
exports.convertComments = function (comments) { module.exports = function (ast, traverse, tt, code) {
for (var i = 0; i < comments.length; i++) { // remove EOF token, eslint doesn't use this for anything and it interferes
var comment = comments[i]; // with some rules see https://github.com/babel/babel-eslint/issues/2
if (comment.type === "CommentBlock") { // todo: find a more elegant way to do this
comment.type = "Block"; ast.tokens.pop();
} else if (comment.type === "CommentLine") {
comment.type = "Line"; // convert tokens
} ast.tokens = toTokens(ast.tokens, tt, code);
// sometimes comments don't get ranges computed,
// even with options.ranges === true // add comments
if (!comment.range) { convertComments(ast.comments);
comment.range = [comment.start, comment.end];
} // transform esprima and acorn divergent nodes
} toAST(ast, traverse, code);
// ast.program.tokens = ast.tokens;
// ast.program.comments = ast.comments;
// ast = ast.program;
// remove File
ast.type = "Program";
ast.sourceType = ast.program.sourceType;
ast.directives = ast.program.directives;
ast.body = ast.program.body;
delete ast.program;
delete ast._paths;
attachComments(ast, ast.comments, ast.tokens);
}; };

View File

@ -1,34 +1,24 @@
var source; "use strict";
var convertComments = require("./convertComments");
module.exports = function (ast, traverse, code) { module.exports = function (ast, traverse, code) {
source = code; var state = { source: code };
ast.range = [ast.start, ast.end]; ast.range = [ast.start, ast.end];
traverse(ast, astTransformVisitor); traverse(ast, astTransformVisitor, null, state);
}; };
function changeToLiteral(node) { function changeToLiteral(node, state) {
node.type = "Literal"; node.type = "Literal";
if (!node.raw) { if (!node.raw) {
if (node.extra && node.extra.raw) { if (node.extra && node.extra.raw) {
node.raw = node.extra.raw; node.raw = node.extra.raw;
} else { } else {
node.raw = source.slice(node.start, node.end); node.raw = state.source.slice(node.start, node.end);
} }
} }
} }
function changeComments(nodeComments) {
for (var i = 0; i < nodeComments.length; i++) {
var comment = nodeComments[i];
if (comment.type === "CommentLine") {
comment.type = "Line";
} else if (comment.type === "CommentBlock") {
comment.type = "Block";
}
comment.range = [comment.start, comment.end];
}
}
var astTransformVisitor = { var astTransformVisitor = {
noScope: true, noScope: true,
enter (path) { enter (path) {
@ -45,24 +35,40 @@ var astTransformVisitor = {
} }
if (node.trailingComments) { if (node.trailingComments) {
changeComments(node.trailingComments); convertComments(node.trailingComments);
} }
if (node.leadingComments) { if (node.leadingComments) {
changeComments(node.leadingComments); convertComments(node.leadingComments);
} }
// make '_paths' non-enumerable (babel-eslint #200) // make '_paths' non-enumerable (babel-eslint #200)
Object.defineProperty(node, "_paths", { value: node._paths, writable: true }); Object.defineProperty(node, "_paths", { value: node._paths, writable: true });
}, },
exit (path) { exit (path, state) {
var node = path.node; var node = path.node;
[ // fixDirectives
fixDirectives, if (path.isFunction() || path.isProgram()) {
].forEach((fixer) => { var directivesContainer = node;
fixer(path); 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";
@ -71,7 +77,7 @@ var astTransformVisitor = {
if (path.isNumericLiteral() || if (path.isNumericLiteral() ||
path.isStringLiteral()) { path.isStringLiteral()) {
changeToLiteral(node); changeToLiteral(node, state);
} }
if (path.isBooleanLiteral()) { if (path.isBooleanLiteral()) {
@ -104,7 +110,7 @@ var astTransformVisitor = {
} }
if (path.isClassMethod() || path.isObjectMethod()) { if (path.isClassMethod() || path.isObjectMethod()) {
var code = source.slice(node.key.end, node.body.start); var code = state.source.slice(node.key.end, node.body.start);
var offset = code.indexOf("("); var offset = code.indexOf("(");
node.value = { node.value = {
@ -211,7 +217,8 @@ var astTransformVisitor = {
// template string range fixes // template string range fixes
if (path.isTemplateLiteral()) { if (path.isTemplateLiteral()) {
node.quasis.forEach((q) => { for (var j = 0; j < node.quasis.length; j++) {
var q = node.quasis[j];
q.range[0] -= 1; q.range[0] -= 1;
if (q.tail) { if (q.tail) {
q.range[1] += 1; q.range[1] += 1;
@ -224,34 +231,7 @@ var astTransformVisitor = {
} else { } else {
q.loc.end.column += 2; q.loc.end.column += 2;
} }
}); }
} }
} }
}; };
function fixDirectives (path) {
if (!(path.isProgram() || path.isFunction())) return;
var node = path.node;
var directivesContainer = node;
var body = node.body;
if (node.type !== "Program") {
directivesContainer = body;
body = body.body;
}
if (!directivesContainer.directives) return;
directivesContainer.directives.reverse().forEach((directive) => {
directive.type = "ExpressionStatement";
directive.expression = directive.value;
delete directive.value;
directive.expression.type = "Literal";
changeToLiteral(directive.expression);
body.unshift(directive);
});
delete directivesContainer.directives;
}
// fixDirectives

View File

@ -1,3 +1,5 @@
"use strict";
module.exports = function (token, tt, source) { module.exports = function (token, tt, source) {
var type = token.type; var type = token.type;
token.range = [token.start, token.end]; token.range = [token.start, token.end];

View File

@ -1,15 +1,18 @@
"use strict";
var convertTemplateType = require("./convertTemplateType"); var convertTemplateType = require("./convertTemplateType");
var toToken = require("./toToken"); var toToken = require("./toToken");
module.exports = function (tokens, tt, code) { module.exports = function (tokens, tt, code) {
// transform tokens to type "Template" // transform tokens to type "Template"
convertTemplateType(tokens, tt); convertTemplateType(tokens, tt);
var transformedTokens = tokens.filter((token) => {
return token.type !== "CommentLine" && token.type !== "CommentBlock";
});
for (var i = 0, l = transformedTokens.length; i < l; i++) { var transformedTokens = [];
transformedTokens[i] = toToken(transformedTokens[i], tt, code); for (var i = 0; i < tokens.length; i++) {
var token = tokens[i];
if (token.type !== "CommentLine" && token.type !== "CommentBlock") {
transformedTokens.push(toToken(token, tt, code));
}
} }
return transformedTokens; return transformedTokens;

View File

@ -428,33 +428,7 @@ exports.parseNoPatch = function (code, options) {
throw err; throw err;
} }
// remove EOF token, eslint doesn't use this for anything and it interferes with some rules babylonToEspree(ast, traverse, tt, code);
// see https://github.com/babel/babel-eslint/issues/2 for more info
// todo: find a more elegant way to do this
ast.tokens.pop();
// convert tokens
ast.tokens = babylonToEspree.toTokens(ast.tokens, tt, code);
// add comments
babylonToEspree.convertComments(ast.comments);
// transform esprima and acorn divergent nodes
babylonToEspree.toAST(ast, traverse, code);
// ast.program.tokens = ast.tokens;
// ast.program.comments = ast.comments;
// ast = ast.program;
// remove File
ast.type = "Program";
ast.sourceType = ast.program.sourceType;
ast.directives = ast.program.directives;
ast.body = ast.program.body;
delete ast.program;
delete ast._paths;
babylonToEspree.attachComments(ast, ast.comments, ast.tokens);
return ast; return ast;
}; };