From 22c3161b6ebf4c87d56a1babce68d8252d9b3bd4 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Wed, 2 Oct 2013 20:39:51 +0200 Subject: [PATCH] Fix wrong end position for VariableDeclaration nodes It should include the semicolon. Closes #67 --- acorn.js | 7 ++++--- index.html | 11 ++++++----- test/tests.js | 18 +++++++++--------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/acorn.js b/acorn.js index 30f89182b9..826378a672 100644 --- a/acorn.js +++ b/acorn.js @@ -1180,6 +1180,7 @@ var init = startNode(); next(); parseVar(init, true); + finishNode(init, "VariableDeclaration"); if (init.declarations.length === 1 && eat(_in)) return parseForIn(node, init); return parseFor(node, init); @@ -1277,9 +1278,9 @@ case _var: next(); - node = parseVar(node); + parseVar(node); semicolon(); - return node; + return finishNode(node, "VariableDeclaration"); case _while: next(); @@ -1400,7 +1401,7 @@ node.declarations.push(finishNode(decl, "VariableDeclarator")); if (!eat(_comma)) break; } - return finishNode(node, "VariableDeclaration"); + return node; } // ### Expression parsing diff --git a/index.html b/index.html index a32801bb07..67f043a607 100644 --- a/index.html +++ b/index.html @@ -842,6 +842,7 @@ a regular for loop.

var init = startNode(); next(); parseVar(init, true); + finishNode(init, "VariableDeclaration"); if (init.declarations.length === 1 && eat(_in)) return parseForIn(node, init); return parseFor(node, init); @@ -931,9 +932,9 @@ adding statements to.

case _var: next(); - node = parseVar(node); + parseVar(node); semicolon(); - return node; + return finishNode(node, "VariableDeclaration"); case _while: next(); @@ -1030,7 +1031,7 @@ expression.

node.declarations.push(finishNode(decl, "VariableDeclarator")); if (!eat(_comma)) break; } - return finishNode(node, "VariableDeclaration"); + return node; }

Expression parsing

These nest, from the most general expression type at the top to 'atomic', nondivisible expression types at the bottom. Most of the functions will simply let the function(s) below them parse, @@ -1085,8 +1086,8 @@ operator that has a lower precedence than the set it is parsing.

node.operator = tokVal; next(); node.right = parseExprOp(parseMaybeUnary(), prec, noIn); - var node = finishNode(node, /&&|\|\|/.test(node.operator) ? "LogicalExpression" : "BinaryExpression"); - return parseExprOp(node, minPrec, noIn); + var exprNode = finishNode(node, /&&|\|\|/.test(node.operator) ? "LogicalExpression" : "BinaryExpression"); + return parseExprOp(exprNode, minPrec, noIn); } } return left; diff --git a/test/tests.js b/test/tests.js index 25c06a47d0..c9a080eef0 100644 --- a/test/tests.js +++ b/test/tests.js @@ -16215,7 +16215,7 @@ test("var x, y;", { }, end: { line: 1, - column: 8 + column: 9 } } } @@ -17307,7 +17307,7 @@ test("if (morning) var x = 0;", { }, end: { line: 1, - column: 22 + column: 23 } } }, @@ -23685,7 +23685,7 @@ test("var hi = function() { sayHi() };", { }, end: { line: 1, - column: 31 + column: 32 } } } @@ -23786,7 +23786,7 @@ test("var hi = function eval() { };", { }, end: { line: 1, - column: 28 + column: 29 } } } @@ -23887,7 +23887,7 @@ test("var hi = function arguments() { };", { }, end: { line: 1, - column: 33 + column: 34 } } } @@ -24030,7 +24030,7 @@ test("var hello = function hi() { sayHi() };", { }, end: { line: 1, - column: 37 + column: 38 } } } @@ -24348,7 +24348,7 @@ test("var x /* comment */;", { }, end: { line: 1, - column: 5 + column: 20 } } } @@ -26164,7 +26164,7 @@ test("var a = 1;", { { type: "VariableDeclaration", start: 0, - end: 9, + end: 10, loc: { start: { line: 1, @@ -26172,7 +26172,7 @@ test("var a = 1;", { }, end: { line: 1, - column: 9 + column: 10 }, source: "test.js" },