Adjust start positions of parenthesized expressions to not include parentheses

Issue #136
This commit is contained in:
Marijn Haverbeke 2014-10-08 11:52:40 +02:00
parent 1c9e0a9272
commit 3603f7d0b1
4 changed files with 174 additions and 300 deletions

View File

@ -1236,19 +1236,24 @@
return node; return node;
} }
// Start a node whose start offset information should be based on // Sometimes, a node is only started *after* the token stream passed
// the start of another node. For example, a binary operator node is // its start position. The functions below help storing a position
// only started after its left-hand side has already been parsed. // and creating a node from a previous position.
function startNodeFrom(other) { function storeCurrentPos() {
var node = new Node(); return options.locations ? [tokStart, tokStartLoc] : tokStart;
node.start = other.start; }
function startNodeAt(pos) {
var node = new Node(), start = pos;
if (options.locations) { if (options.locations) {
node.loc = new SourceLocation(); node.loc = new SourceLocation();
node.loc.start = other.loc.start; node.loc.start = start[1];
start = pos[0];
} }
node.start = start;
if (options.ranges) if (options.ranges)
node.range = [other.range[0], 0]; node.range = [start, 0];
return node; return node;
} }
@ -1827,9 +1832,10 @@
// or the `in` operator (in for loops initalization expressions). // or the `in` operator (in for loops initalization expressions).
function parseExpression(noComma, noIn) { function parseExpression(noComma, noIn) {
var start = storeCurrentPos();
var expr = parseMaybeAssign(noIn); var expr = parseMaybeAssign(noIn);
if (!noComma && tokType === _comma) { if (!noComma && tokType === _comma) {
var node = startNodeFrom(expr); var node = startNodeAt(start);
node.expressions = [expr]; node.expressions = [expr];
while (eat(_comma)) node.expressions.push(parseMaybeAssign(noIn)); while (eat(_comma)) node.expressions.push(parseMaybeAssign(noIn));
return finishNode(node, "SequenceExpression"); return finishNode(node, "SequenceExpression");
@ -1841,9 +1847,10 @@
// operators like `+=`. // operators like `+=`.
function parseMaybeAssign(noIn) { function parseMaybeAssign(noIn) {
var start = storeCurrentPos();
var left = parseMaybeConditional(noIn); var left = parseMaybeConditional(noIn);
if (tokType.isAssign) { if (tokType.isAssign) {
var node = startNodeFrom(left); var node = startNodeAt(start);
node.operator = tokVal; node.operator = tokVal;
node.left = tokType === _eq ? toAssignable(left) : left; node.left = tokType === _eq ? toAssignable(left) : left;
checkLVal(left); checkLVal(left);
@ -1857,9 +1864,10 @@
// Parse a ternary conditional (`?:`) operator. // Parse a ternary conditional (`?:`) operator.
function parseMaybeConditional(noIn) { function parseMaybeConditional(noIn) {
var start = storeCurrentPos();
var expr = parseExprOps(noIn); var expr = parseExprOps(noIn);
if (eat(_question)) { if (eat(_question)) {
var node = startNodeFrom(expr); var node = startNodeAt(start);
node.test = expr; node.test = expr;
node.consequent = parseExpression(true); node.consequent = parseExpression(true);
expect(_colon); expect(_colon);
@ -1872,7 +1880,8 @@
// Start the precedence parser. // Start the precedence parser.
function parseExprOps(noIn) { function parseExprOps(noIn) {
return parseExprOp(parseMaybeUnary(), -1, noIn); var start = storeCurrentPos();
return parseExprOp(parseMaybeUnary(), start, -1, noIn);
} }
// Parse binary operators with the operator precedence parsing // Parse binary operators with the operator precedence parsing
@ -1881,18 +1890,19 @@
// defer further parser to one of its callers when it encounters an // defer further parser to one of its callers when it encounters an
// operator that has a lower precedence than the set it is parsing. // operator that has a lower precedence than the set it is parsing.
function parseExprOp(left, minPrec, noIn) { function parseExprOp(left, leftStart, minPrec, noIn) {
var prec = tokType.binop; var prec = tokType.binop;
if (prec != null && (!noIn || tokType !== _in)) { if (prec != null && (!noIn || tokType !== _in)) {
if (prec > minPrec) { if (prec > minPrec) {
var node = startNodeFrom(left); var node = startNodeAt(leftStart);
node.left = left; node.left = left;
node.operator = tokVal; node.operator = tokVal;
var op = tokType; var op = tokType;
next(); next();
node.right = parseExprOp(parseMaybeUnary(), prec, noIn); var start = storeCurrentPos();
var exprNode = finishNode(node, (op === _logicalOR || op === _logicalAND) ? "LogicalExpression" : "BinaryExpression"); node.right = parseExprOp(parseMaybeUnary(), start, prec, noIn);
return parseExprOp(exprNode, minPrec, noIn); finishNode(node, (op === _logicalOR || op === _logicalAND) ? "LogicalExpression" : "BinaryExpression");
return parseExprOp(node, leftStart, minPrec, noIn);
} }
} }
return left; return left;
@ -1914,9 +1924,10 @@
raise(node.start, "Deleting local variable in strict mode"); raise(node.start, "Deleting local variable in strict mode");
return finishNode(node, update ? "UpdateExpression" : "UnaryExpression"); return finishNode(node, update ? "UpdateExpression" : "UnaryExpression");
} }
var start = storeCurrentPos();
var expr = parseExprSubscripts(); var expr = parseExprSubscripts();
while (tokType.postfix && !canInsertSemicolon()) { while (tokType.postfix && !canInsertSemicolon()) {
var node = startNodeFrom(expr); var node = startNodeAt(start);
node.operator = tokVal; node.operator = tokVal;
node.prefix = false; node.prefix = false;
node.argument = expr; node.argument = expr;
@ -1930,33 +1941,34 @@
// Parse call, dot, and `[]`-subscript expressions. // Parse call, dot, and `[]`-subscript expressions.
function parseExprSubscripts() { function parseExprSubscripts() {
return parseSubscripts(parseExprAtom()); var start = storeCurrentPos();
return parseSubscripts(parseExprAtom(), start);
} }
function parseSubscripts(base, noCalls) { function parseSubscripts(base, start, noCalls) {
if (eat(_dot)) { if (eat(_dot)) {
var node = startNodeFrom(base); var node = startNodeAt(start);
node.object = base; node.object = base;
node.property = parseIdent(true); node.property = parseIdent(true);
node.computed = false; node.computed = false;
return parseSubscripts(finishNode(node, "MemberExpression"), noCalls); return parseSubscripts(finishNode(node, "MemberExpression"), start, noCalls);
} else if (eat(_bracketL)) { } else if (eat(_bracketL)) {
var node = startNodeFrom(base); var node = startNodeAt(start);
node.object = base; node.object = base;
node.property = parseExpression(); node.property = parseExpression();
node.computed = true; node.computed = true;
expect(_bracketR); expect(_bracketR);
return parseSubscripts(finishNode(node, "MemberExpression"), noCalls); return parseSubscripts(finishNode(node, "MemberExpression"), start, noCalls);
} else if (!noCalls && eat(_parenL)) { } else if (!noCalls && eat(_parenL)) {
var node = startNodeFrom(base); var node = startNodeAt(start);
node.callee = base; node.callee = base;
node.arguments = parseExprList(_parenR, false); node.arguments = parseExprList(_parenR, false);
return parseSubscripts(finishNode(node, "CallExpression"), noCalls); return parseSubscripts(finishNode(node, "CallExpression"), start, noCalls);
} else if (tokType === _bquote) { } else if (tokType === _bquote) {
var node = startNodeFrom(base); var node = startNodeAt(start);
node.tag = base; node.tag = base;
node.quasi = parseTemplate(); node.quasi = parseTemplate();
return parseSubscripts(finishNode(node, "TaggedTemplateExpression"), noCalls); return parseSubscripts(finishNode(node, "TaggedTemplateExpression"), start, noCalls);
} return base; } return base;
} }
@ -1976,9 +1988,10 @@
if (inGenerator) return parseYield(); if (inGenerator) return parseYield();
case _name: case _name:
var start = storeCurrentPos();
var id = parseIdent(tokType !== _name); var id = parseIdent(tokType !== _name);
if (eat(_arrow)) { if (eat(_arrow)) {
return parseArrowExpression(startNodeFrom(id), [id]); return parseArrowExpression(startNodeAt(start), [id]);
} }
return id; return id;
@ -1997,11 +2010,12 @@
return finishNode(node, "Literal"); return finishNode(node, "Literal");
case _parenL: case _parenL:
var start = storeCurrentPos();
var tokStartLoc1 = tokStartLoc, tokStart1 = tokStart, val, exprList; var tokStartLoc1 = tokStartLoc, tokStart1 = tokStart, val, exprList;
next(); next();
// check whether this is generator comprehension or regular expression // check whether this is generator comprehension or regular expression
if (options.ecmaVersion >= 6 && tokType === _for) { if (options.ecmaVersion >= 6 && tokType === _for) {
val = parseComprehension(startNode(), true); val = parseComprehension(startNodeAt(start), true);
} else { } else {
var oldParenL = ++metParenL; var oldParenL = ++metParenL;
if (tokType !== _parenR) { if (tokType !== _parenR) {
@ -2013,7 +2027,7 @@
expect(_parenR); expect(_parenR);
// if '=>' follows '(...)', convert contents to arguments // if '=>' follows '(...)', convert contents to arguments
if (metParenL === oldParenL && eat(_arrow)) { if (metParenL === oldParenL && eat(_arrow)) {
val = parseArrowExpression(startNode(), exprList); val = parseArrowExpression(startNodeAt(start), exprList);
} else { } else {
// forbid '()' before everything but '=>' // forbid '()' before everything but '=>'
if (!val) unexpected(lastStart); if (!val) unexpected(lastStart);
@ -2025,15 +2039,6 @@
} }
} }
} }
val.start = tokStart1;
val.end = lastEnd;
if (options.locations) {
val.loc.start = tokStartLoc1;
val.loc.end = lastEndLoc;
}
if (options.ranges) {
val.range = [tokStart1, lastEnd];
}
return val; return val;
case _bracketL: case _bracketL:
@ -2078,7 +2083,8 @@
function parseNew() { function parseNew() {
var node = startNode(); var node = startNode();
next(); next();
node.callee = parseSubscripts(parseExprAtom(), true); var start = storeCurrentPos();
node.callee = parseSubscripts(parseExprAtom(), start, true);
if (eat(_parenL)) node.arguments = parseExprList(_parenR, false); if (eat(_parenL)) node.arguments = parseExprList(_parenR, false);
else node.arguments = empty; else node.arguments = empty;
return finishNode(node, "NewExpression"); return finishNode(node, "NewExpression");

View File

@ -43,10 +43,9 @@
else callback("ok", test.code); else callback("ok", test.code);
} else { } else {
var mis = misMatch(test.ast, ast); var mis = misMatch(test.ast, ast);
if (!mis && test.comments) mis = misMatch(test.comments, comments);
if (mis) callback("fail", test.code, mis); if (mis) callback("fail", test.code, mis);
if (test.comments) mis = misMatch(test.comments, comments); else callback("ok", test.code);
if (!mis) callback("ok", test.code);
else callback("fail", test.code, mis);
} }
} catch(e) { } catch(e) {
if (test.error && e instanceof SyntaxError) { if (test.error && e instanceof SyntaxError) {
@ -67,7 +66,7 @@
return str + " (" + pt + ")"; return str + " (" + pt + ")";
} }
function misMatch(exp, act) { var misMatch = exports.misMatch = function(exp, act) {
if (!exp || !act || (typeof exp != "object") || (typeof act != "object")) { if (!exp || !act || (typeof exp != "object") || (typeof act != "object")) {
if (exp !== act) return ppJSON(exp) + " !== " + ppJSON(act); if (exp !== act) return ppJSON(exp) + " !== " + ppJSON(act);
} else if (exp.splice) { } else if (exp.splice) {
@ -83,7 +82,7 @@
if (mis) return addPath(mis, prop); if (mis) return addPath(mis, prop);
} }
} }
} };
function mangle(ast) { function mangle(ast) {
if (typeof ast != "object" || !ast) return; if (typeof ast != "object" || !ast) return;

View File

@ -1511,10 +1511,10 @@ test("e => ({ property: 42 })", {
end: {line: 1, column: 20} end: {line: 1, column: 20}
} }
}], }],
range: [5, 23], range: [6, 22],
loc: { loc: {
start: {line: 1, column: 5}, start: {line: 1, column: 6},
end: {line: 1, column: 23} end: {line: 1, column: 22}
} }
}, },
rest: null, rest: null,
@ -2330,10 +2330,10 @@ test("(x => x)", {
rest: null, rest: null,
generator: false, generator: false,
expression: true, expression: true,
range: [0, 8], range: [1, 7],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 8} end: {line: 1, column: 7}
} }
}, },
range: [0, 8], range: [0, 8],
@ -2500,19 +2500,19 @@ test("(x) => ((y, z) => (x, y, z))", {
} }
} }
], ],
range: [18, 27], range: [19, 26],
loc: { loc: {
start: {line: 1, column: 18}, start: {line: 1, column: 19},
end: {line: 1, column: 27} end: {line: 1, column: 26}
} }
}, },
rest: null, rest: null,
generator: false, generator: false,
expression: true, expression: true,
range: [7, 28], range: [8, 27],
loc: { loc: {
start: {line: 1, column: 7}, start: {line: 1, column: 8},
end: {line: 1, column: 28} end: {line: 1, column: 27}
} }
}, },
rest: null, rest: null,
@ -4268,10 +4268,10 @@ test("({ responseText: text }) = res", {
end: {line: 1, column: 21} end: {line: 1, column: 21}
} }
}], }],
range: [0, 24], range: [1, 23],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 24} end: {line: 1, column: 23}
} }
}, },
right: { right: {
@ -6115,10 +6115,10 @@ test("(function* () { yield v })", {
rest: null, rest: null,
generator: true, generator: true,
expression: false, expression: false,
range: [0, 26], range: [1, 25],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 26} end: {line: 1, column: 25}
} }
}, },
range: [0, 26], range: [0, 26],
@ -6195,10 +6195,10 @@ test("(function* () { yield\nv })", {
rest: null, rest: null,
generator: true, generator: true,
expression: false, expression: false,
range: [0, 26], range: [1, 25],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 2, column: 4} end: {line: 2, column: 3}
} }
}, },
range: [0, 26], range: [0, 26],
@ -6264,10 +6264,10 @@ test("(function* () { yield *v })", {
rest: null, rest: null,
generator: true, generator: true,
expression: false, expression: false,
range: [0, 27], range: [1, 26],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 27} end: {line: 1, column: 26}
} }
}, },
range: [0, 27], range: [0, 27],
@ -6572,10 +6572,10 @@ test("(function* () { yield yield 10 })", {
rest: null, rest: null,
generator: true, generator: true,
expression: false, expression: false,
range: [0, 33], range: [1, 32],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 33} end: {line: 1, column: 32}
} }
}, },
range: [0, 33], range: [0, 33],
@ -8138,10 +8138,10 @@ test("\"use strict\"; (class A {constructor() { super() }})", {
end: {line: 1, column: 50} end: {line: 1, column: 50}
} }
}, },
range: [14, 51], range: [15, 50],
loc: { loc: {
start: {line: 1, column: 14}, start: {line: 1, column: 15},
end: {line: 1, column: 51} end: {line: 1, column: 50}
} }
}, },
range: [14, 51], range: [14, 51],
@ -8482,10 +8482,10 @@ test("\"use strict\"; (class A { static constructor() { super() }})", {
end: {line: 1, column: 58} end: {line: 1, column: 58}
} }
}, },
range: [14, 59], range: [15, 58],
loc: { loc: {
start: {line: 1, column: 14}, start: {line: 1, column: 15},
end: {line: 1, column: 59} end: {line: 1, column: 58}
} }
}, },
range: [14, 59], range: [14, 59],
@ -9437,10 +9437,10 @@ test("({[x]: 10})", {
end: {line: 1, column: 9} end: {line: 1, column: 9}
} }
}], }],
range: [0, 11], range: [1, 10],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 11} end: {line: 1, column: 10}
} }
}, },
range: [0, 11], range: [0, 11],
@ -9517,10 +9517,10 @@ test("({[\"x\" + \"y\"]: 10})", {
end: {line: 1, column: 17} end: {line: 1, column: 17}
} }
}], }],
range: [0, 19], range: [1, 18],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 19} end: {line: 1, column: 18}
} }
}, },
range: [0, 19], range: [0, 19],
@ -9590,10 +9590,10 @@ test("({[x]: function() {}})", {
end: {line: 1, column: 20} end: {line: 1, column: 20}
} }
}], }],
range: [0, 22], range: [1, 21],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 22} end: {line: 1, column: 21}
} }
}, },
range: [0, 22], range: [0, 22],
@ -9683,10 +9683,10 @@ test("({[x]: 10, y: 20})", {
} }
} }
], ],
range: [0, 18], range: [1, 17],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 18} end: {line: 1, column: 17}
} }
}, },
range: [0, 18], range: [0, 18],
@ -9810,10 +9810,10 @@ test("({get [x]() {}, set [x](v) {}})", {
} }
} }
], ],
range: [0, 31], range: [1, 30],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 31} end: {line: 1, column: 30}
} }
}, },
range: [0, 31], range: [0, 31],
@ -9883,10 +9883,10 @@ test("({[x]() {}})", {
end: {line: 1, column: 10} end: {line: 1, column: 10}
} }
}], }],
range: [0, 12], range: [1, 11],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 12} end: {line: 1, column: 11}
} }
}, },
range: [0, 12], range: [0, 12],
@ -10769,10 +10769,10 @@ test("({f: function({x} = {x: 10}) {}})", {
end: {line: 1, column: 31} end: {line: 1, column: 31}
} }
}], }],
range: [0, 33], range: [1, 32],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 33} end: {line: 1, column: 32}
} }
}, },
range: [0, 33], range: [0, 33],
@ -10917,10 +10917,10 @@ test("({f({x} = {x: 10}) {}})", {
end: {line: 1, column: 21} end: {line: 1, column: 21}
} }
}], }],
range: [0, 23], range: [1, 22],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 23} end: {line: 1, column: 22}
} }
}, },
range: [0, 23], range: [0, 23],
@ -11073,10 +11073,10 @@ test("(class {f({x} = {x: 10}) {}})", {
end: {line: 1, column: 28} end: {line: 1, column: 28}
} }
}, },
range: [0, 29], range: [1, 28],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 29} end: {line: 1, column: 28}
} }
}, },
range: [0, 29], range: [0, 29],
@ -11192,10 +11192,10 @@ test("(({x} = {x: 10}) => {})", {
rest: null, rest: null,
generator: false, generator: false,
expression: false, expression: false,
range: [0, 23], range: [1, 22],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 23} end: {line: 1, column: 22}
} }
}, },
range: [0, 23], range: [0, 23],
@ -12267,10 +12267,10 @@ test("(function x([ a, b ]){})", {
rest: null, rest: null,
generator: false, generator: false,
expression: false, expression: false,
range: [0, 24], range: [1, 23],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 24} end: {line: 1, column: 23}
} }
}, },
range: [0, 24], range: [0, 24],
@ -12388,10 +12388,10 @@ test("(function x({ a, b }){})", {
rest: null, rest: null,
generator: false, generator: false,
expression: false, expression: false,
range: [0, 24], range: [1, 23],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 24} end: {line: 1, column: 23}
} }
}, },
range: [0, 24], range: [0, 24],
@ -12467,10 +12467,10 @@ test("(function x(...[ a, b ]){})", {
}, },
generator: false, generator: false,
expression: false, expression: false,
range: [0, 27], range: [1, 26],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 27} end: {line: 1, column: 26}
} }
}, },
range: [0, 27], range: [0, 27],
@ -12704,10 +12704,10 @@ test("(function x({ a: { w, x }, b: [y, z] }, ...[a, b, c]){})", {
}, },
generator: false, generator: false,
expression: false, expression: false,
range: [0, 56], range: [1, 55],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 56} end: {line: 1, column: 55}
} }
}, },
range: [0, 56], range: [0, 56],
@ -12804,10 +12804,10 @@ test("({ x([ a, b ]){} })", {
end: {line: 1, column: 16} end: {line: 1, column: 16}
} }
}], }],
range: [0, 19], range: [1, 18],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 19} end: {line: 1, column: 18}
} }
}, },
range: [0, 19], range: [0, 19],
@ -12904,10 +12904,10 @@ test("({ x(...[ a, b ]){} })", {
end: {line: 1, column: 19} end: {line: 1, column: 19}
} }
}], }],
range: [0, 22], range: [1, 21],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 22} end: {line: 1, column: 21}
} }
}, },
range: [0, 22], range: [0, 22],
@ -13162,10 +13162,10 @@ test("({ x({ a: { w, x }, b: [y, z] }, ...[a, b, c]){} })", {
end: {line: 1, column: 48} end: {line: 1, column: 48}
} }
}], }],
range: [0, 51], range: [1, 50],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 51} end: {line: 1, column: 50}
} }
}, },
range: [0, 51], range: [0, 51],
@ -15086,10 +15086,10 @@ test("(function () { yield* 10 })", {
rest: null, rest: null,
generator: false, generator: false,
expression: false, expression: false,
range: [0, 27], range: [1, 26],
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 1},
end: {line: 1, column: 27} end: {line: 1, column: 26}
} }
}, },
range: [0, 27], range: [0, 27],
@ -15388,4 +15388,4 @@ test('function normal(x, y = 10) {}', {
}, },
expression: false expression: false
}] }]
}, {ecmaVersion: 6}); }, {ecmaVersion: 6});

View File

@ -2,9 +2,8 @@
// (http://esprima.org/test/) // (http://esprima.org/test/)
if (typeof exports != "undefined") { if (typeof exports != "undefined") {
var test = require("./driver.js").test; var driver = require("./driver.js");
var testFail = require("./driver.js").testFail; var test = driver.test, testFail = driver.testFail, testAssert = driver.testAssert, misMatch = driver.misMatch;
var testAssert = require("./driver.js").testAssert;
var acorn = require(".."); var acorn = require("..");
} }
@ -177,11 +176,11 @@ test("(1 + 2 ) * 3", {
loc: { loc: {
start: { start: {
line: 1, line: 1,
column: 0 column: 1
}, },
end: { end: {
line: 1, line: 1,
column: 8 column: 6
} }
} }
}, },
@ -8202,11 +8201,11 @@ test("( new foo).bar()", {
loc: { loc: {
start: { start: {
line: 1, line: 1,
column: 0 column: 2
}, },
end: { end: {
line: 1, line: 1,
column: 10 column: 9
} }
} }
}, },
@ -8371,11 +8370,11 @@ test("( foo )()", {
loc: { loc: {
start: { start: {
line: 1, line: 1,
column: 0 column: 5
}, },
end: { end: {
line: 1, line: 1,
column: 11 column: 8
} }
} }
}, },
@ -17217,11 +17216,11 @@ test("if (morning) (function(){})", {
loc: { loc: {
start: { start: {
line: 1, line: 1,
column: 13 column: 14
}, },
end: { end: {
line: 1, line: 1,
column: 27 column: 26
} }
} }
}, },
@ -20508,11 +20507,11 @@ test("(function(){ return })", {
loc: { loc: {
start: { start: {
line: 1, line: 1,
column: 0 column: 1
}, },
end: { end: {
line: 1, line: 1,
column: 22 column: 21
} }
} }
}, },
@ -20581,11 +20580,11 @@ test("(function(){ return; })", {
loc: { loc: {
start: { start: {
line: 1, line: 1,
column: 0 column: 1
}, },
end: { end: {
line: 1, line: 1,
column: 23 column: 22
} }
} }
}, },
@ -20667,11 +20666,11 @@ test("(function(){ return x; })", {
loc: { loc: {
start: { start: {
line: 1, line: 1,
column: 0 column: 1
}, },
end: { end: {
line: 1, line: 1,
column: 25 column: 24
} }
} }
}, },
@ -20781,11 +20780,11 @@ test("(function(){ return x * y })", {
loc: { loc: {
start: { start: {
line: 1, line: 1,
column: 0 column: 1
}, },
end: { end: {
line: 1, line: 1,
column: 28 column: 27
} }
} }
}, },
@ -23194,11 +23193,11 @@ test("(function test(t, t) { })", {
loc: { loc: {
start: { start: {
line: 1, line: 1,
column: 0 column: 1
}, },
end: { end: {
line: 1, line: 1,
column: 25 column: 24
} }
} }
}, },
@ -24412,11 +24411,11 @@ test("(function(){})", {
loc: { loc: {
start: { start: {
line: 1, line: 1,
column: 0 column: 1
}, },
end: { end: {
line: 1, line: 1,
column: 14 column: 13
} }
} }
}, },
@ -25522,11 +25521,11 @@ test("(function(){ return\nx; })", {
loc: { loc: {
start: { start: {
line: 1, line: 1,
column: 0 column: 1
}, },
end: { end: {
line: 2, line: 2,
column: 5 column: 4
} }
} }
}, },
@ -25622,11 +25621,11 @@ test("(function(){ return // Comment\nx; })", {
loc: { loc: {
start: { start: {
line: 1, line: 1,
column: 0 column: 1
}, },
end: { end: {
line: 2, line: 2,
column: 5 column: 4
} }
} }
}, },
@ -25722,11 +25721,11 @@ test("(function(){ return/* Multiline\nComment */x; })", {
loc: { loc: {
start: { start: {
line: 1, line: 1,
column: 0 column: 1
}, },
end: { end: {
line: 2, line: 2,
column: 15 column: 14
} }
} }
}, },
@ -26026,8 +26025,6 @@ test("", {
test("foo: if (true) break foo;", { test("foo: if (true) break foo;", {
type: "Program", type: "Program",
start: 0,
end: 25,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -26041,8 +26038,6 @@ test("foo: if (true) break foo;", {
body: [ body: [
{ {
type: "LabeledStatement", type: "LabeledStatement",
start: 0,
end: 25,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -26055,8 +26050,6 @@ test("foo: if (true) break foo;", {
}, },
body: { body: {
type: "IfStatement", type: "IfStatement",
start: 5,
end: 25,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -26069,8 +26062,6 @@ test("foo: if (true) break foo;", {
}, },
test: { test: {
type: "Literal", type: "Literal",
start: 9,
end: 13,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -26085,8 +26076,6 @@ test("foo: if (true) break foo;", {
}, },
consequent: { consequent: {
type: "BreakStatement", type: "BreakStatement",
start: 15,
end: 25,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -26099,8 +26088,6 @@ test("foo: if (true) break foo;", {
}, },
label: { label: {
type: "Identifier", type: "Identifier",
start: 21,
end: 24,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -26118,8 +26105,6 @@ test("foo: if (true) break foo;", {
}, },
label: { label: {
type: "Identifier", type: "Identifier",
start: 0,
end: 3,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -26138,8 +26123,6 @@ test("foo: if (true) break foo;", {
test("(function () {\n 'use strict';\n '\0';\n}())", { test("(function () {\n 'use strict';\n '\0';\n}())", {
type: "Program", type: "Program",
start: 0,
end: 40,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -26153,8 +26136,6 @@ test("(function () {\n 'use strict';\n '\0';\n}())", {
body: [ body: [
{ {
type: "ExpressionStatement", type: "ExpressionStatement",
start: 0,
end: 40,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -26167,21 +26148,18 @@ test("(function () {\n 'use strict';\n '\0';\n}())", {
}, },
expression: { expression: {
type: "CallExpression", type: "CallExpression",
start: 0,
loc: { loc: {
start: { start: {
line: 1, line: 1,
column: 0 column: 1
}, },
end: { end: {
line: 4, line: 4,
column: 4 column: 3
} }
}, },
callee: { callee: {
type: "FunctionExpression", type: "FunctionExpression",
start: 1,
end: 37,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -26196,8 +26174,6 @@ test("(function () {\n 'use strict';\n '\0';\n}())", {
params: [], params: [],
body: { body: {
type: "BlockStatement", type: "BlockStatement",
start: 13,
end: 37,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -26211,8 +26187,6 @@ test("(function () {\n 'use strict';\n '\0';\n}())", {
body: [ body: [
{ {
type: "ExpressionStatement", type: "ExpressionStatement",
start: 16,
end: 29,
loc: { loc: {
start: { start: {
line: 2, line: 2,
@ -26225,8 +26199,6 @@ test("(function () {\n 'use strict';\n '\0';\n}())", {
}, },
expression: { expression: {
type: "Literal", type: "Literal",
start: 16,
end: 28,
loc: { loc: {
start: { start: {
line: 2, line: 2,
@ -26242,8 +26214,6 @@ test("(function () {\n 'use strict';\n '\0';\n}())", {
}, },
{ {
type: "ExpressionStatement", type: "ExpressionStatement",
start: 31,
end: 35,
loc: { loc: {
start: { start: {
line: 3, line: 3,
@ -26256,8 +26226,6 @@ test("(function () {\n 'use strict';\n '\0';\n}())", {
}, },
expression: { expression: {
type: "Literal", type: "Literal",
start: 31,
end: 34,
loc: { loc: {
start: { start: {
line: 3, line: 3,
@ -26275,7 +26243,6 @@ test("(function () {\n 'use strict';\n '\0';\n}())", {
} }
}, },
arguments: [], arguments: [],
end: 40
} }
} }
] ]
@ -26283,43 +26250,29 @@ test("(function () {\n 'use strict';\n '\0';\n}())", {
test("123..toString(10)", { test("123..toString(10)", {
type: "Program", type: "Program",
start: 0,
end: 17,
body: [ body: [
{ {
type: "ExpressionStatement", type: "ExpressionStatement",
start: 0,
end: 17,
expression: { expression: {
type: "CallExpression", type: "CallExpression",
start: 0,
callee: { callee: {
type: "MemberExpression", type: "MemberExpression",
start: 0,
object: { object: {
type: "Literal", type: "Literal",
start: 0,
end: 4,
value: 123 value: 123
}, },
property: { property: {
type: "Identifier", type: "Identifier",
start: 5,
end: 13,
name: "toString" name: "toString"
}, },
computed: false, computed: false,
end: 13
}, },
arguments: [ arguments: [
{ {
type: "Literal", type: "Literal",
start: 14,
end: 16,
value: 10 value: 10
} }
], ],
end: 17
} }
} }
] ]
@ -26327,30 +26280,20 @@ test("123..toString(10)", {
test("123.+2", { test("123.+2", {
type: "Program", type: "Program",
start: 0,
end: 6,
body: [ body: [
{ {
type: "ExpressionStatement", type: "ExpressionStatement",
start: 0,
end: 6,
expression: { expression: {
type: "BinaryExpression", type: "BinaryExpression",
start: 0,
left: { left: {
type: "Literal", type: "Literal",
start: 0,
end: 4,
value: 123 value: 123
}, },
operator: "+", operator: "+",
right: { right: {
type: "Literal", type: "Literal",
start: 5,
end: 6,
value: 2 value: 2
}, },
end: 6
} }
} }
] ]
@ -26358,28 +26301,18 @@ test("123.+2", {
test("a\u2028b", { test("a\u2028b", {
type: "Program", type: "Program",
start: 0,
end: 3,
body: [ body: [
{ {
type: "ExpressionStatement", type: "ExpressionStatement",
start: 0,
end: 1,
expression: { expression: {
type: "Identifier", type: "Identifier",
start: 0,
end: 1,
name: "a" name: "a"
} }
}, },
{ {
type: "ExpressionStatement", type: "ExpressionStatement",
start: 2,
end: 3,
expression: { expression: {
type: "Identifier", type: "Identifier",
start: 2,
end: 3,
name: "b" name: "b"
} }
} }
@ -26437,28 +26370,18 @@ test("foo: 10; foo: 20;", {
test("if(1)/ foo/", { test("if(1)/ foo/", {
type: "Program", type: "Program",
start: 0,
end: 12,
body: [ body: [
{ {
type: "IfStatement", type: "IfStatement",
start: 0,
end: 12,
test: { test: {
type: "Literal", type: "Literal",
start: 3,
end: 4,
value: 1, value: 1,
raw: "1" raw: "1"
}, },
consequent: { consequent: {
type: "ExpressionStatement", type: "ExpressionStatement",
start: 5,
end: 12,
expression: { expression: {
type: "Literal", type: "Literal",
start: 5,
end: 12,
raw: "/ foo/" raw: "/ foo/"
} }
}, },
@ -26475,8 +26398,6 @@ test("price_9̶9̶_89", {
expression: { expression: {
type: "Identifier", type: "Identifier",
name: "price_9̶9̶_89", name: "price_9̶9̶_89",
start: 0,
end: 13
} }
} }
] ]
@ -26486,8 +26407,6 @@ test("price_9̶9̶_89", {
test("var a = 1;", { test("var a = 1;", {
type: "Program", type: "Program",
start: 0,
end: 10,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -26502,8 +26421,6 @@ test("var a = 1;", {
body: [ body: [
{ {
type: "VariableDeclaration", type: "VariableDeclaration",
start: 0,
end: 10,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -26518,8 +26435,6 @@ test("var a = 1;", {
declarations: [ declarations: [
{ {
type: "VariableDeclarator", type: "VariableDeclarator",
start: 4,
end: 9,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -26533,8 +26448,6 @@ test("var a = 1;", {
}, },
id: { id: {
type: "Identifier", type: "Identifier",
start: 4,
end: 5,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -26550,8 +26463,6 @@ test("var a = 1;", {
}, },
init: { init: {
type: "Literal", type: "Literal",
start: 8,
end: 9,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -26624,28 +26535,18 @@ test("{}/=/", {
test("foo <!--bar\n+baz", { test("foo <!--bar\n+baz", {
type: "Program", type: "Program",
start: 0,
end: 16,
body: [ body: [
{ {
type: "ExpressionStatement", type: "ExpressionStatement",
start: 0,
end: 16,
expression: { expression: {
type: "BinaryExpression", type: "BinaryExpression",
start: 0,
end: 16,
left: { left: {
type: "Identifier", type: "Identifier",
start: 0,
end: 3,
name: "foo" name: "foo"
}, },
operator: "+", operator: "+",
right: { right: {
type: "Identifier", type: "Identifier",
start: 13,
end: 16,
name: "baz" name: "baz"
} }
} }
@ -26795,7 +26696,7 @@ testFail("func() = 4",
"Assigning to rvalue (1:0)"); "Assigning to rvalue (1:0)");
testFail("(1 + 1) = 10", testFail("(1 + 1) = 10",
"Assigning to rvalue (1:0)"); "Assigning to rvalue (1:1)");
testFail("1++", testFail("1++",
"Assigning to rvalue (1:0)"); "Assigning to rvalue (1:0)");
@ -26810,7 +26711,7 @@ testFail("--1",
"Assigning to rvalue (1:2)"); "Assigning to rvalue (1:2)");
testFail("for((1 + 1) in list) process(x);", testFail("for((1 + 1) in list) process(x);",
"Assigning to rvalue (1:4)"); "Assigning to rvalue (1:5)");
testFail("[", testFail("[",
"Unexpected token (1:1)"); "Unexpected token (1:1)");
@ -27309,8 +27210,6 @@ testFail("for(let x = 0;;);", "Unexpected token (1:8)");
test("let++", { test("let++", {
type: "Program", type: "Program",
start: 0,
end: 5,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -27324,8 +27223,6 @@ test("let++", {
body: [ body: [
{ {
type: "ExpressionStatement", type: "ExpressionStatement",
start: 0,
end: 5,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -27338,8 +27235,6 @@ test("let++", {
}, },
expression: { expression: {
type: "UpdateExpression", type: "UpdateExpression",
start: 0,
end: 5,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -27354,8 +27249,6 @@ test("let++", {
prefix: false, prefix: false,
argument: { argument: {
type: "Identifier", type: "Identifier",
start: 0,
end: 3,
loc: { loc: {
start: { start: {
line: 1, line: 1,
@ -28704,8 +28597,6 @@ test("<!--\n;", {
{ {
type: tokTypes._var, type: tokTypes._var,
value: "var", value: "var",
start: 0,
end: 3,
loc: { loc: {
start: {line: 1, column: 0}, start: {line: 1, column: 0},
end: {line: 1, column: 3} end: {line: 1, column: 3}
@ -28714,8 +28605,6 @@ test("<!--\n;", {
{ {
type: tokTypes.name, type: tokTypes.name,
value: "x", value: "x",
start: 4,
end: 5,
loc: { loc: {
start: {line: 1, column: 4}, start: {line: 1, column: 4},
end: {line: 1, column: 5} end: {line: 1, column: 5}
@ -28724,8 +28613,6 @@ test("<!--\n;", {
{ {
type: tokTypes.eq, type: tokTypes.eq,
value: "=", value: "=",
start: 6,
end: 7,
loc: { loc: {
start: {line: 1, column: 6}, start: {line: 1, column: 6},
end: {line: 1, column: 7} end: {line: 1, column: 7}
@ -28734,8 +28621,6 @@ test("<!--\n;", {
{ {
type: tokTypes.parenL, type: tokTypes.parenL,
value: undefined, value: undefined,
start: 8,
end: 9,
loc: { loc: {
start: {line: 1, column: 8}, start: {line: 1, column: 8},
end: {line: 1, column: 9} end: {line: 1, column: 9}
@ -28744,8 +28629,6 @@ test("<!--\n;", {
{ {
type: tokTypes.num, type: tokTypes.num,
value: 1, value: 1,
start: 9,
end: 10,
loc: { loc: {
start: {line: 1, column: 9}, start: {line: 1, column: 9},
end: {line: 1, column: 10} end: {line: 1, column: 10}
@ -28754,8 +28637,6 @@ test("<!--\n;", {
{ {
type: {binop: 9, prefix: true, beforeExpr: true}, type: {binop: 9, prefix: true, beforeExpr: true},
value: "+", value: "+",
start: 11,
end: 12,
loc: { loc: {
start: {line: 1, column: 11}, start: {line: 1, column: 11},
end: {line: 1, column: 12} end: {line: 1, column: 12}
@ -28764,8 +28645,6 @@ test("<!--\n;", {
{ {
type: tokTypes.num, type: tokTypes.num,
value: 2, value: 2,
start: 13,
end: 14,
loc: { loc: {
start: {line: 1, column: 13}, start: {line: 1, column: 13},
end: {line: 1, column: 14} end: {line: 1, column: 14}
@ -28774,8 +28653,6 @@ test("<!--\n;", {
{ {
type: tokTypes.parenR, type: tokTypes.parenR,
value: undefined, value: undefined,
start: 14,
end: 15,
loc: { loc: {
start: {line: 1, column: 14}, start: {line: 1, column: 14},
end: {line: 1, column: 15} end: {line: 1, column: 15}
@ -28784,8 +28661,6 @@ test("<!--\n;", {
{ {
type: tokTypes.eof, type: tokTypes.eof,
value: undefined, value: undefined,
start: 15,
end: 15,
loc: { loc: {
start: {line: 1, column: 15}, start: {line: 1, column: 15},
end: {line: 1, column: 15} end: {line: 1, column: 15}
@ -28794,17 +28669,11 @@ test("<!--\n;", {
]; ];
testAssert('var x = (1 + 2)', function assert(ast) { testAssert('var x = (1 + 2)', function assert(ast) {
if (actualTokens.length !== expectedTokens.length) { if (actualTokens.length !== expectedTokens.length) {
return JSON.stringify(actualTokens) + " !== " + JSON.stringify(expectedTokens); return "Bad token stream length: expected " + expectedTokens.length + ", got " + actualTokens.length;
} else { } else {
for (var i=0, n=actualTokens.length; i < n; i++) { for (var i=0, n=actualTokens.length; i < n; i++) {
var actualToken = JSON.stringify( var mis = misMatch(expectedTokens[i], actualTokens[i]);
actualTokens[i], if (mis) return mis;
// just remove this when startLoc/endLoc support is dropped
function (key, value) { if (key !== 'startLoc' && key !== 'endLoc') return value; }
);
var expectedToken = JSON.stringify(expectedTokens[i]);
if (actualToken !== expectedToken)
return actualToken + ' !== ' + expectedToken;
} }
} }
}, { }, {