From b8126ce4d0191a16778c5a66017fba3c91af2695 Mon Sep 17 00:00:00 2001 From: Kai Cataldo Date: Fri, 11 Jan 2019 12:23:34 -0500 Subject: [PATCH] Remove comment attachment (#736) * Remove comment attachment * Simplify error messaging in tests --- lib/babylon-to-espree/attachComments.js | 59 ------------------- .../{toAST.js => convertAST.js} | 9 +-- lib/babylon-to-espree/convertProgramNode.js | 40 +++++++++++++ .../{toToken.js => convertToken.js} | 0 .../{toTokens.js => convertTokens.js} | 4 +- lib/babylon-to-espree/index.js | 27 ++------- test/specs/babel-eslint.js | 35 +---------- 7 files changed, 52 insertions(+), 122 deletions(-) delete mode 100644 lib/babylon-to-espree/attachComments.js rename lib/babylon-to-espree/{toAST.js => convertAST.js} (92%) create mode 100644 lib/babylon-to-espree/convertProgramNode.js rename lib/babylon-to-espree/{toToken.js => convertToken.js} (100%) rename lib/babylon-to-espree/{toTokens.js => convertTokens.js} (72%) diff --git a/lib/babylon-to-espree/attachComments.js b/lib/babylon-to-espree/attachComments.js deleted file mode 100644 index 6e198753..00000000 --- a/lib/babylon-to-espree/attachComments.js +++ /dev/null @@ -1,59 +0,0 @@ -"use strict"; - -// comment fixes -module.exports = function(ast, comments, tokens) { - if (comments.length) { - const firstComment = comments[0]; - const lastComment = comments[comments.length - 1]; - // fixup program start - if (!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; - - if (ast.leadingComments === null && ast.innerComments.length) { - ast.leadingComments = ast.innerComments; - } - } else if (firstComment.start < tokens[0].start) { - // if there are comments before the first token, the program starts at the first token - const token = tokens[0]; - // ast.start = token.start; - // ast.loc.start.line = token.loc.start.line; - // ast.loc.start.column = token.loc.start.column; - - // estraverse do not put leading comments on first node when the comment - // appear before the first token - if (ast.body.length) { - const node = ast.body[0]; - node.leadingComments = []; - const firstTokenStart = token.start; - const len = comments.length; - for (let i = 0; i < len && comments[i].start < firstTokenStart; i++) { - node.leadingComments.push(comments[i]); - } - } - } - // fixup program end - if (tokens.length) { - const lastToken = tokens[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.end = lastToken.end; - ast.range[1] = lastToken.end; - ast.loc.end.line = lastToken.loc.end.line; - ast.loc.end.column = lastToken.loc.end.column; - } - } - } else { - if (!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; - } -}; diff --git a/lib/babylon-to-espree/toAST.js b/lib/babylon-to-espree/convertAST.js similarity index 92% rename from lib/babylon-to-espree/toAST.js rename to lib/babylon-to-espree/convertAST.js index 2ab165df..0ef37750 100644 --- a/lib/babylon-to-espree/toAST.js +++ b/lib/babylon-to-espree/convertAST.js @@ -1,7 +1,7 @@ "use strict"; const t = require("@babel/core").types; -const convertComments = require("./convertComments"); +const convertProgramNode = require("./convertProgramNode"); module.exports = function(ast, traverse, code) { const state = { source: code }; @@ -20,6 +20,8 @@ module.exports = function(ast, traverse, code) { delete t.VISITOR_KEYS.Property; delete t.VISITOR_KEYS.MethodDefinition; + + convertProgramNode(ast); }; const astTransformVisitor = { @@ -31,16 +33,15 @@ const astTransformVisitor = { node._babelType = node.type; if (node.innerComments) { - node.trailingComments = node.innerComments; delete node.innerComments; } if (node.trailingComments) { - convertComments(node.trailingComments); + delete node.trailingComments; } if (node.leadingComments) { - convertComments(node.leadingComments); + delete node.leadingComments; } }, exit(path) { diff --git a/lib/babylon-to-espree/convertProgramNode.js b/lib/babylon-to-espree/convertProgramNode.js new file mode 100644 index 00000000..810b1988 --- /dev/null +++ b/lib/babylon-to-espree/convertProgramNode.js @@ -0,0 +1,40 @@ +"use strict"; + +module.exports = 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; + } +}; diff --git a/lib/babylon-to-espree/toToken.js b/lib/babylon-to-espree/convertToken.js similarity index 100% rename from lib/babylon-to-espree/toToken.js rename to lib/babylon-to-espree/convertToken.js diff --git a/lib/babylon-to-espree/toTokens.js b/lib/babylon-to-espree/convertTokens.js similarity index 72% rename from lib/babylon-to-espree/toTokens.js rename to lib/babylon-to-espree/convertTokens.js index 2927c4b0..0f49d937 100644 --- a/lib/babylon-to-espree/toTokens.js +++ b/lib/babylon-to-espree/convertTokens.js @@ -1,10 +1,10 @@ "use strict"; const convertTemplateType = require("./convertTemplateType"); -const toToken = require("./toToken"); +const convertToken = require("./convertToken"); module.exports = function(tokens, tt, code) { return convertTemplateType(tokens, tt) .filter(t => t.type !== "CommentLine" && t.type !== "CommentBlock") - .map(t => toToken(t, tt, code)); + .map(t => convertToken(t, tt, code)); }; diff --git a/lib/babylon-to-espree/index.js b/lib/babylon-to-espree/index.js index c0fdf323..cc4dda97 100644 --- a/lib/babylon-to-espree/index.js +++ b/lib/babylon-to-espree/index.js @@ -1,30 +1,11 @@ "use strict"; -const attachComments = require("./attachComments"); +const convertTokens = require("./convertTokens"); const convertComments = require("./convertComments"); -const toTokens = require("./toTokens"); -const toAST = require("./toAST"); +const convertAST = require("./convertAST"); module.exports = function(ast, traverse, tt, code) { - // convert tokens - ast.tokens = toTokens(ast.tokens, tt, code); - - // add comments + ast.tokens = convertTokens(ast.tokens, tt, code); convertComments(ast.comments); - - // 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; - - attachComments(ast, ast.comments, ast.tokens); + convertAST(ast, traverse, code); }; diff --git a/test/specs/babel-eslint.js b/test/specs/babel-eslint.js index becf4e31..849e37ab 100644 --- a/test/specs/babel-eslint.js +++ b/test/specs/babel-eslint.js @@ -4,23 +4,9 @@ const assert = require("assert"); const babelEslint = require("../.."); const espree = require("espree"); const escope = require("eslint-scope"); -const util = require("util"); const unpad = require("dedent"); const assertImplementsAST = require("../helpers/assert-implements-ast"); -function lookup(obj, keypath, backwardsDepth) { - if (!keypath) { - return obj; - } - - return keypath - .split(".") - .slice(0, -1 * backwardsDepth) - .reduce((base, segment) => { - return base && base[segment], obj; - }); -} - function parseAndAssertSame(code) { code = unpad(code); const esAST = espree.parse(code, { @@ -38,7 +24,6 @@ function parseAndAssertSame(code) { loc: true, range: true, comment: true, - attachComment: true, ecmaVersion: 2018, sourceType: "module", }); @@ -46,25 +31,7 @@ function parseAndAssertSame(code) { eslintVisitorKeys: true, eslintScopeManager: true, }).ast; - try { - assertImplementsAST(esAST, babylonAST); - } catch (err) { - const traversal = err.message.slice(3, err.message.indexOf(":")); - err.message += unpad(` - espree: - ${util.inspect(lookup(esAST, traversal, 2), { - depth: err.depth, - colors: true, - })} - babel-eslint: - ${util.inspect(lookup(babylonAST, traversal, 2), { - depth: err.depth, - colors: true, - })} - `); - throw err; - } - //assert.equal(esAST, babylonAST); + assertImplementsAST(esAST, babylonAST); } describe("babylon-to-espree", () => {