From 2541fc93b9bacf179661d8e27e040afe011e2926 Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Sat, 25 Mar 2017 00:18:57 -0400 Subject: [PATCH 1/7] toAst pass "source" in state instead of keeping in scope --- babylon-to-espree/toAST.js | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/babylon-to-espree/toAST.js b/babylon-to-espree/toAST.js index c26fa5ba..ba1e8d74 100644 --- a/babylon-to-espree/toAST.js +++ b/babylon-to-espree/toAST.js @@ -1,18 +1,16 @@ -var source; - module.exports = function (ast, traverse, code) { - source = code; + var state = { source: code }; ast.range = [ast.start, ast.end]; - traverse(ast, astTransformVisitor); + traverse(ast, astTransformVisitor, null, state); }; -function changeToLiteral(node) { +function changeToLiteral(node, state) { node.type = "Literal"; if (!node.raw) { if (node.extra && node.extra.raw) { node.raw = node.extra.raw; } else { - node.raw = source.slice(node.start, node.end); + node.raw = state.source.slice(node.start, node.end); } } } @@ -55,14 +53,10 @@ var astTransformVisitor = { // make '_paths' non-enumerable (babel-eslint #200) Object.defineProperty(node, "_paths", { value: node._paths, writable: true }); }, - exit (path) { + exit (path, state) { var node = path.node; - [ - fixDirectives, - ].forEach((fixer) => { - fixer(path); - }); + fixDirectives(path, state); if (path.isJSXText()) { node.type = "Literal"; @@ -71,7 +65,7 @@ var astTransformVisitor = { if (path.isNumericLiteral() || path.isStringLiteral()) { - changeToLiteral(node); + changeToLiteral(node, state); } if (path.isBooleanLiteral()) { @@ -104,7 +98,7 @@ var astTransformVisitor = { } 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("("); node.value = { @@ -230,7 +224,7 @@ var astTransformVisitor = { }; -function fixDirectives (path) { +function fixDirectives (path, state) { if (!(path.isProgram() || path.isFunction())) return; var node = path.node; @@ -249,7 +243,7 @@ function fixDirectives (path) { directive.expression = directive.value; delete directive.value; directive.expression.type = "Literal"; - changeToLiteral(directive.expression); + changeToLiteral(directive.expression, state); body.unshift(directive); }); delete directivesContainer.directives; From 5d32ad0d56c8769aeea680cc7a4aafa1df4c2a04 Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Sat, 25 Mar 2017 17:07:41 -0400 Subject: [PATCH 2/7] Consolidate versions of "convertComments" --- babylon-to-espree/convertComments.js | 15 +++++++++++++++ babylon-to-espree/index.js | 16 +--------------- babylon-to-espree/toAST.js | 18 ++++-------------- 3 files changed, 20 insertions(+), 29 deletions(-) create mode 100644 babylon-to-espree/convertComments.js diff --git a/babylon-to-espree/convertComments.js b/babylon-to-espree/convertComments.js new file mode 100644 index 00000000..11c2a335 --- /dev/null +++ b/babylon-to-espree/convertComments.js @@ -0,0 +1,15 @@ +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]; + } + } +}; diff --git a/babylon-to-espree/index.js b/babylon-to-espree/index.js index 401570b6..4dd83f08 100644 --- a/babylon-to-espree/index.js +++ b/babylon-to-espree/index.js @@ -3,18 +3,4 @@ exports.attachComments = require("./attachComments"); exports.toTokens = require("./toTokens"); exports.toAST = require("./toAST"); -exports.convertComments = 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]; - } - } -}; +exports.convertComments = require("./convertComments"); diff --git a/babylon-to-espree/toAST.js b/babylon-to-espree/toAST.js index ba1e8d74..ecdb9366 100644 --- a/babylon-to-espree/toAST.js +++ b/babylon-to-espree/toAST.js @@ -1,3 +1,5 @@ +var convertComments = require("./convertComments"); + module.exports = function (ast, traverse, code) { var state = { source: code }; ast.range = [ast.start, ast.end]; @@ -15,18 +17,6 @@ function changeToLiteral(node, state) { } } -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 = { noScope: true, enter (path) { @@ -43,11 +33,11 @@ var astTransformVisitor = { } if (node.trailingComments) { - changeComments(node.trailingComments); + convertComments(node.trailingComments); } if (node.leadingComments) { - changeComments(node.leadingComments); + convertComments(node.leadingComments); } // make '_paths' non-enumerable (babel-eslint #200) From 06c3a31b96cb890fe3544931f60f021e5186f62a Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Sat, 25 Mar 2017 17:28:01 -0400 Subject: [PATCH 3/7] Inline fixDirectives and use for-loop --- babylon-to-espree/toAST.js | 49 ++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/babylon-to-espree/toAST.js b/babylon-to-espree/toAST.js index ecdb9366..c21c7a11 100644 --- a/babylon-to-espree/toAST.js +++ b/babylon-to-espree/toAST.js @@ -46,7 +46,27 @@ var astTransformVisitor = { exit (path, state) { var node = path.node; - fixDirectives(path, state); + // 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()) { node.type = "Literal"; @@ -212,30 +232,3 @@ var astTransformVisitor = { } } }; - - -function fixDirectives (path, state) { - 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, state); - body.unshift(directive); - }); - delete directivesContainer.directives; -} -// fixDirectives From 539af05bae280d15059d783b59d77b8814a48084 Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Sat, 25 Mar 2017 17:36:20 -0400 Subject: [PATCH 4/7] Only iterate over tokens once --- babylon-to-espree/toTokens.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/babylon-to-espree/toTokens.js b/babylon-to-espree/toTokens.js index 1f06d3e5..f3c0e848 100644 --- a/babylon-to-espree/toTokens.js +++ b/babylon-to-espree/toTokens.js @@ -4,12 +4,13 @@ var toToken = require("./toToken"); module.exports = function (tokens, tt, code) { // transform tokens to type "Template" 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++) { - transformedTokens[i] = toToken(transformedTokens[i], tt, code); + var transformedTokens = []; + 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; From d2ce7894bc864ed46c0c91eeef60af0a91886d8a Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Sat, 25 Mar 2017 17:42:51 -0400 Subject: [PATCH 5/7] Use for-loop for template literal conversion --- babylon-to-espree/toAST.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/babylon-to-espree/toAST.js b/babylon-to-espree/toAST.js index c21c7a11..0892563a 100644 --- a/babylon-to-espree/toAST.js +++ b/babylon-to-espree/toAST.js @@ -215,7 +215,8 @@ var astTransformVisitor = { // template string range fixes 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; if (q.tail) { q.range[1] += 1; @@ -228,7 +229,7 @@ var astTransformVisitor = { } else { q.loc.end.column += 2; } - }); + } } } }; From 6c5beec589cbf343c528f1a78efe42874d7c83d4 Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Sat, 25 Mar 2017 18:01:13 -0400 Subject: [PATCH 6/7] Move ast convert steps to babylon-to-espree --- babylon-to-espree/index.js | 36 ++++++++++++++++++++++++++++++++---- index.js | 28 +--------------------------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/babylon-to-espree/index.js b/babylon-to-espree/index.js index 4dd83f08..157a3ace 100644 --- a/babylon-to-espree/index.js +++ b/babylon-to-espree/index.js @@ -1,6 +1,34 @@ -exports.attachComments = require("./attachComments"); +var attachComments = require("./attachComments"); +var convertComments = require("./convertComments"); +var toTokens = require("./toTokens"); +var toAST = require("./toAST"); -exports.toTokens = require("./toTokens"); -exports.toAST = require("./toAST"); +module.exports = function (ast, traverse, tt, code) { + // remove EOF token, eslint doesn't use this for anything and it interferes + // with some rules see https://github.com/babel/babel-eslint/issues/2 + // todo: find a more elegant way to do this + ast.tokens.pop(); -exports.convertComments = require("./convertComments"); + // convert tokens + ast.tokens = toTokens(ast.tokens, tt, code); + + // add comments + 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; + delete ast._paths; + + attachComments(ast, ast.comments, ast.tokens); +}; diff --git a/index.js b/index.js index 3b243cc8..b56f74e5 100644 --- a/index.js +++ b/index.js @@ -428,33 +428,7 @@ exports.parseNoPatch = function (code, options) { throw err; } - // remove EOF token, eslint doesn't use this for anything and it interferes with some rules - // 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); + babylonToEspree(ast, traverse, tt, code); return ast; }; From ec14787172795c81fa2c544d100727d897521ebe Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Sat, 25 Mar 2017 18:03:17 -0400 Subject: [PATCH 7/7] Enable strict mode in all of babylon-to-espree --- babylon-to-espree/attachComments.js | 2 ++ babylon-to-espree/convertComments.js | 2 ++ babylon-to-espree/convertTemplateType.js | 2 ++ babylon-to-espree/index.js | 2 ++ babylon-to-espree/toAST.js | 2 ++ babylon-to-espree/toToken.js | 2 ++ babylon-to-espree/toTokens.js | 2 ++ 7 files changed, 14 insertions(+) diff --git a/babylon-to-espree/attachComments.js b/babylon-to-espree/attachComments.js index 4040ce7e..9fc9f339 100644 --- a/babylon-to-espree/attachComments.js +++ b/babylon-to-espree/attachComments.js @@ -1,3 +1,5 @@ +"use strict"; + // comment fixes module.exports = function (ast, comments, tokens) { if (comments.length) { diff --git a/babylon-to-espree/convertComments.js b/babylon-to-espree/convertComments.js index 11c2a335..19c6ce8c 100644 --- a/babylon-to-espree/convertComments.js +++ b/babylon-to-espree/convertComments.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = function (comments) { for (var i = 0; i < comments.length; i++) { var comment = comments[i]; diff --git a/babylon-to-espree/convertTemplateType.js b/babylon-to-espree/convertTemplateType.js index c9537ddd..8b647c39 100644 --- a/babylon-to-espree/convertTemplateType.js +++ b/babylon-to-espree/convertTemplateType.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = function (tokens, tt) { var startingToken = 0; var currentToken = 0; diff --git a/babylon-to-espree/index.js b/babylon-to-espree/index.js index 157a3ace..94e6832f 100644 --- a/babylon-to-espree/index.js +++ b/babylon-to-espree/index.js @@ -1,3 +1,5 @@ +"use strict"; + var attachComments = require("./attachComments"); var convertComments = require("./convertComments"); var toTokens = require("./toTokens"); diff --git a/babylon-to-espree/toAST.js b/babylon-to-espree/toAST.js index 0892563a..84d77a53 100644 --- a/babylon-to-espree/toAST.js +++ b/babylon-to-espree/toAST.js @@ -1,3 +1,5 @@ +"use strict"; + var convertComments = require("./convertComments"); module.exports = function (ast, traverse, code) { diff --git a/babylon-to-espree/toToken.js b/babylon-to-espree/toToken.js index dcfd48f8..6173f760 100644 --- a/babylon-to-espree/toToken.js +++ b/babylon-to-espree/toToken.js @@ -1,3 +1,5 @@ +"use strict"; + module.exports = function (token, tt, source) { var type = token.type; token.range = [token.start, token.end]; diff --git a/babylon-to-espree/toTokens.js b/babylon-to-espree/toTokens.js index f3c0e848..81ec9850 100644 --- a/babylon-to-espree/toTokens.js +++ b/babylon-to-espree/toTokens.js @@ -1,3 +1,5 @@ +"use strict"; + var convertTemplateType = require("./convertTemplateType"); var toToken = require("./toToken");