Skip to content

Commit 80b3562

Browse files
authored
Fix minor line terminator/line number tracking issues
FIX: Properly normalize line endings in raw strings of invalid template tokens. FIX: Properly track line numbers for escaped newlines in strings.
1 parent 2c186ad commit 80b3562

File tree

5 files changed

+20
-19
lines changed

5 files changed

+20
-19
lines changed

acorn/src/expression.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ pp.parseTemplateElement = function({isTagged}) {
706706
this.raiseRecoverable(this.start, "Bad escape sequence in untagged template literal")
707707
}
708708
elem.value = {
709-
raw: this.value,
709+
raw: this.value.replace(/\r\n?/g, "\n"),
710710
cooked: null
711711
}
712712
} else {

acorn/src/statement.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pp.isLet = function(context) {
4242
// Statement) is allowed here. If context is not empty then only a Statement
4343
// is allowed. However, `let [` is an explicit negative lookahead for
4444
// ExpressionStatement, so special-case it first.
45-
if (nextCh === 91 || nextCh === 92) return true // '[', '/'
45+
if (nextCh === 91 || nextCh === 92) return true // '[', '\'
4646
if (context) return false
4747

4848
if (nextCh === 123 || nextCh > 0xd7ff && nextCh < 0xdc00) return true // '{', astral

acorn/src/tokenize.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ pp.readInvalidTemplateToken = function() {
677677
return this.finishToken(tt.invalidTemplate, this.input.slice(this.start, this.pos))
678678

679679
case "\r":
680-
if (this.input[this.pos] + 1 === "\n") ++this.pos
680+
if (this.input[this.pos + 1] === "\n") ++this.pos
681681
// fall through
682682
case "\n": case "\u2028": case "\u2029":
683683
++this.curLine
@@ -745,6 +745,7 @@ pp.readEscapedChar = function(inTemplate) {
745745
if (isNewLine(ch)) {
746746
// Unicode new line characters after \ get removed from output in both
747747
// template literals and strings
748+
if (this.options.locations) { this.lineStart = this.pos; ++this.curLine }
748749
return ""
749750
}
750751
return String.fromCharCode(ch)

test/tests-harmony.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -816,23 +816,23 @@ test("`\\n\\r\\b\\v\\t\\f\\\n\\\r\n\\\u2028\\\u2029`", {
816816
tail: true,
817817
loc: {
818818
start: {line: 1, column: 1},
819-
end: {line: 3, column: 4}
819+
end: {line: 5, column: 0}
820820
}
821821
}],
822822
expressions: [],
823823
loc: {
824824
start: {line: 1, column: 0},
825-
end: {line: 3, column: 5}
825+
end: {line: 5, column: 1}
826826
}
827827
},
828828
loc: {
829829
start: {line: 1, column: 0},
830-
end: {line: 3, column: 5}
830+
end: {line: 5, column: 1}
831831
}
832832
}],
833833
loc: {
834834
start: {line: 1, column: 0},
835-
end: {line: 3, column: 5}
835+
end: {line: 5, column: 1}
836836
}
837837
}, {
838838
ecmaVersion: 6,

test/tests.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6928,8 +6928,8 @@ test("\"Hello\\\u2028world\"", {
69286928
column: 0
69296929
},
69306930
end: {
6931-
line: 1,
6932-
column: 14
6931+
line: 2,
6932+
column: 6
69336933
}
69346934
}
69356935
},
@@ -6939,8 +6939,8 @@ test("\"Hello\\\u2028world\"", {
69396939
column: 0
69406940
},
69416941
end: {
6942-
line: 1,
6943-
column: 14
6942+
line: 2,
6943+
column: 6
69446944
}
69456945
}
69466946
}
@@ -6951,8 +6951,8 @@ test("\"Hello\\\u2028world\"", {
69516951
column: 0
69526952
},
69536953
end: {
6954-
line: 1,
6955-
column: 14
6954+
line: 2,
6955+
column: 6
69566956
}
69576957
}
69586958
});
@@ -6972,8 +6972,8 @@ test("\"Hello\\\u2029world\"", {
69726972
column: 0
69736973
},
69746974
end: {
6975-
line: 1,
6976-
column: 14
6975+
line: 2,
6976+
column: 6
69776977
}
69786978
}
69796979
},
@@ -6983,8 +6983,8 @@ test("\"Hello\\\u2029world\"", {
69836983
column: 0
69846984
},
69856985
end: {
6986-
line: 1,
6987-
column: 14
6986+
line: 2,
6987+
column: 6
69886988
}
69896989
}
69906990
}
@@ -6995,8 +6995,8 @@ test("\"Hello\\\u2029world\"", {
69956995
column: 0
69966996
},
69976997
end: {
6998-
line: 1,
6999-
column: 14
6998+
line: 2,
6999+
column: 6
70007000
}
70017001
}
70027002
});

0 commit comments

Comments
 (0)