Permalink
Browse files

Fix Negative Column in Binary Expression String Concatenation

Fix #1844
Closes gh-1846
  • Loading branch information...
Kingwl authored and ariya committed Jul 31, 2017
1 parent 99f8c0e commit 22926d5a1317a8e0e027bfa479e3dba00a9abfbd
View
@@ -380,11 +380,17 @@ export class Parser {
};
}
startNode(token): Marker {
startNode(token, lastLineStart = 0): Marker {
let column = token.start - token.lineStart;
let line = token.lineNumber;
if (column < 0) {
column += lastLineStart;
line--;
}
return {
index: token.start,
line: token.lineNumber,
column: token.start - token.lineStart
line: line,
column: column
};
}
@@ -1524,12 +1530,16 @@ export class Parser {
// Final reduce to clean-up the stack.
let i = stack.length - 1;
expr = stack[i];
markers.pop();
let lastMarker = markers.pop();
while (i > 1) {
const node = this.startNode(markers.pop());
const marker = markers.pop();
const lastLineStart = lastMarker && lastMarker.lineStart;
const node = this.startNode(marker, lastLineStart);
const operator = stack[i - 1];
expr = this.finalize(node, new Node.BinaryExpression(operator, stack[i - 2], expr));
i -= 2;
lastMarker = marker;
}
}
@@ -0,0 +1,2 @@
'\
' + bar
@@ -0,0 +1,148 @@
{
"type": "Program",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "BinaryExpression",
"operator": "+",
"left": {
"type": "Literal",
"value": "",
"raw": "'\\\n'",
"range": [
0,
4
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 1
}
}
},
"right": {
"type": "Identifier",
"name": "bar",
"range": [
7,
10
],
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 7
}
}
},
"range": [
0,
10
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 7
}
}
},
"range": [
0,
10
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 7
}
}
}
],
"sourceType": "script",
"tokens": [
{
"type": "String",
"value": "'\\\n'",
"range": [
0,
4
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 1
}
}
},
{
"type": "Punctuator",
"value": "+",
"range": [
5,
6
],
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 3
}
}
},
{
"type": "Identifier",
"value": "bar",
"range": [
7,
10
],
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 7
}
}
}
],
"range": [
0,
10
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 7
}
}
}

0 comments on commit 22926d5

Please sign in to comment.