From 905124c291ec8b41b3511abad618e6989bf9daab Mon Sep 17 00:00:00 2001
From: Marijn Haverbeke
Date: Sun, 18 Nov 2012 21:17:49 +0100
Subject: [PATCH] Assign parenthesized expressions a range that includes the
parens
This way, composite expressions that they are part of won't get bogus
unsyntactic substrings as their range (i.e. in '(1)+1', the binop expression
would span '1)+1').
Closes #14
---
acorn.js | 9 +++++++
index.html | 9 +++++++
test/tests.js | 72 +++++++++++++++++++++++++--------------------------
3 files changed, 54 insertions(+), 36 deletions(-)
diff --git a/acorn.js b/acorn.js
index 81abc2da38..deac5bcf45 100644
--- a/acorn.js
+++ b/acorn.js
@@ -1433,8 +1433,17 @@
return finishNode(node, "Literal");
case _parenL:
+ var tokStartLoc1 = tokStartLoc, tokStart1 = tokStart;
next();
var val = parseExpression();
+ val.start = tokStart1;
+ val.end = tokEnd;
+ if (options.locations) {
+ val.loc.start = tokStartLoc1;
+ val.loc.end = tokEndLoc;
+ }
+ if (options.ranges)
+ val.range = [tokStart1, tokEnd];
expect(_parenR);
return val;
diff --git a/index.html b/index.html
index 033605112c..14bbb8685e 100644
--- a/index.html
+++ b/index.html
@@ -1045,8 +1045,17 @@ or {}.
return finishNode(node, "Literal");
case _parenL:
+ var tokStartLoc1 = tokStartLoc, tokStart1 = tokStart;
next();
var val = parseExpression();
+ val.start = tokStart1;
+ val.end = tokEnd;
+ if (options.locations) {
+ val.loc.start = tokStartLoc1;
+ val.loc.end = tokEndLoc;
+ }
+ if (options.ranges)
+ val.range = [tokStart1, tokEnd];
expect(_parenR);
return val;
diff --git a/test/tests.js b/test/tests.js
index 464a7f9e4a..f7b169bb42 100644
--- a/test/tests.js
+++ b/test/tests.js
@@ -175,11 +175,11 @@ test("(1 + 2 ) * 3", {
loc: {
start: {
line: 1,
- column: 1
+ column: 0
},
end: {
line: 1,
- column: 6
+ column: 8
}
}
},
@@ -201,7 +201,7 @@ test("(1 + 2 ) * 3", {
loc: {
start: {
line: 1,
- column: 1
+ column: 0
},
end: {
line: 1,
@@ -8175,11 +8175,11 @@ test("( new foo).bar()", {
loc: {
start: {
line: 1,
- column: 2
+ column: 0
},
end: {
line: 1,
- column: 9
+ column: 10
}
}
},
@@ -8201,7 +8201,7 @@ test("( new foo).bar()", {
loc: {
start: {
line: 1,
- column: 2
+ column: 0
},
end: {
line: 1,
@@ -8213,7 +8213,7 @@ test("( new foo).bar()", {
loc: {
start: {
line: 1,
- column: 2
+ column: 0
},
end: {
line: 1,
@@ -8344,11 +8344,11 @@ test("( foo )()", {
loc: {
start: {
line: 1,
- column: 5
+ column: 0
},
end: {
line: 1,
- column: 8
+ column: 11
}
}
},
@@ -8356,7 +8356,7 @@ test("( foo )()", {
loc: {
start: {
line: 1,
- column: 5
+ column: 0
},
end: {
line: 1,
@@ -17190,11 +17190,11 @@ test("if (morning) (function(){})", {
loc: {
start: {
line: 1,
- column: 14
+ column: 13
},
end: {
line: 1,
- column: 26
+ column: 27
}
}
},
@@ -20481,11 +20481,11 @@ test("(function(){ return })", {
loc: {
start: {
line: 1,
- column: 1
+ column: 0
},
end: {
line: 1,
- column: 21
+ column: 22
}
}
},
@@ -20554,11 +20554,11 @@ test("(function(){ return; })", {
loc: {
start: {
line: 1,
- column: 1
+ column: 0
},
end: {
line: 1,
- column: 22
+ column: 23
}
}
},
@@ -20640,11 +20640,11 @@ test("(function(){ return x; })", {
loc: {
start: {
line: 1,
- column: 1
+ column: 0
},
end: {
line: 1,
- column: 24
+ column: 25
}
}
},
@@ -20754,11 +20754,11 @@ test("(function(){ return x * y })", {
loc: {
start: {
line: 1,
- column: 1
+ column: 0
},
end: {
line: 1,
- column: 27
+ column: 28
}
}
},
@@ -23193,11 +23193,11 @@ test("(function test(t, t) { })", {
loc: {
start: {
line: 1,
- column: 1
+ column: 0
},
end: {
line: 1,
- column: 24
+ column: 25
}
}
},
@@ -24099,11 +24099,11 @@ test("(function(){})", {
loc: {
start: {
line: 1,
- column: 1
+ column: 0
},
end: {
line: 1,
- column: 13
+ column: 14
}
}
},
@@ -25209,11 +25209,11 @@ test("(function(){ return\nx; })", {
loc: {
start: {
line: 1,
- column: 1
+ column: 0
},
end: {
line: 2,
- column: 4
+ column: 5
}
}
},
@@ -25309,11 +25309,11 @@ test("(function(){ return // Comment\nx; })", {
loc: {
start: {
line: 1,
- column: 1
+ column: 0
},
end: {
line: 2,
- column: 4
+ column: 5
}
}
},
@@ -25409,11 +25409,11 @@ test("(function(){ return/* Multiline\nComment */x; })", {
loc: {
start: {
line: 1,
- column: 1
+ column: 0
},
end: {
line: 2,
- column: 14
+ column: 15
}
}
},
@@ -25854,15 +25854,15 @@ test("(function () { 'use strict'; '\0'; }())", {
},
expression: {
type: "CallExpression",
- start: 1,
+ start: 0,
loc: {
start: {
line: 1,
- column: 1
+ column: 0
},
end: {
line: 1,
- column: 37
+ column: 38
}
},
callee: {
@@ -25962,7 +25962,7 @@ test("(function () { 'use strict'; '\0'; }())", {
}
},
arguments: [],
- end: 37
+ end: 38
}
}
]
@@ -26120,7 +26120,7 @@ testFail("func() = 4",
"Assigning to rvalue (1:0)");
testFail("(1 + 1) = 10",
- "Assigning to rvalue (1:1)");
+ "Assigning to rvalue (1:0)");
testFail("1++",
"Assigning to rvalue (1:0)");
@@ -26135,7 +26135,7 @@ testFail("--1",
"Assigning to rvalue (1:2)");
testFail("for((1 + 1) in list) process(x);",
- "Assigning to rvalue (1:5)");
+ "Assigning to rvalue (1:4)");
testFail("[",
"Unexpected token (1:1)");
|