Skip to content

Commit

Permalink
Fix minor line terminator/line number tracking issues
Browse files Browse the repository at this point in the history
FIX: Properly normalize line endings in raw strings of invalid template tokens.

FIX: Properly track line numbers for escaped newlines in strings.
  • Loading branch information
adams85 committed Feb 22, 2024
1 parent 2c186ad commit 80b3562
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion acorn/src/expression.js
Expand Up @@ -706,7 +706,7 @@ pp.parseTemplateElement = function({isTagged}) {
this.raiseRecoverable(this.start, "Bad escape sequence in untagged template literal")
}
elem.value = {
raw: this.value,
raw: this.value.replace(/\r\n?/g, "\n"),
cooked: null
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion acorn/src/statement.js
Expand Up @@ -42,7 +42,7 @@ pp.isLet = function(context) {
// Statement) is allowed here. If context is not empty then only a Statement
// is allowed. However, `let [` is an explicit negative lookahead for
// ExpressionStatement, so special-case it first.
if (nextCh === 91 || nextCh === 92) return true // '[', '/'
if (nextCh === 91 || nextCh === 92) return true // '[', '\'
if (context) return false

if (nextCh === 123 || nextCh > 0xd7ff && nextCh < 0xdc00) return true // '{', astral
Expand Down
3 changes: 2 additions & 1 deletion acorn/src/tokenize.js
Expand Up @@ -677,7 +677,7 @@ pp.readInvalidTemplateToken = function() {
return this.finishToken(tt.invalidTemplate, this.input.slice(this.start, this.pos))

case "\r":
if (this.input[this.pos] + 1 === "\n") ++this.pos
if (this.input[this.pos + 1] === "\n") ++this.pos
// fall through
case "\n": case "\u2028": case "\u2029":
++this.curLine
Expand Down Expand Up @@ -745,6 +745,7 @@ pp.readEscapedChar = function(inTemplate) {
if (isNewLine(ch)) {
// Unicode new line characters after \ get removed from output in both
// template literals and strings
if (this.options.locations) { this.lineStart = this.pos; ++this.curLine }
return ""
}
return String.fromCharCode(ch)
Expand Down
8 changes: 4 additions & 4 deletions test/tests-harmony.js
Expand Up @@ -816,23 +816,23 @@ test("`\\n\\r\\b\\v\\t\\f\\\n\\\r\n\\\u2028\\\u2029`", {
tail: true,
loc: {
start: {line: 1, column: 1},
end: {line: 3, column: 4}
end: {line: 5, column: 0}
}
}],
expressions: [],
loc: {
start: {line: 1, column: 0},
end: {line: 3, column: 5}
end: {line: 5, column: 1}
}
},
loc: {
start: {line: 1, column: 0},
end: {line: 3, column: 5}
end: {line: 5, column: 1}
}
}],
loc: {
start: {line: 1, column: 0},
end: {line: 3, column: 5}
end: {line: 5, column: 1}
}
}, {
ecmaVersion: 6,
Expand Down
24 changes: 12 additions & 12 deletions test/tests.js
Expand Up @@ -6928,8 +6928,8 @@ test("\"Hello\\\u2028world\"", {
column: 0
},
end: {
line: 1,
column: 14
line: 2,
column: 6
}
}
},
Expand All @@ -6939,8 +6939,8 @@ test("\"Hello\\\u2028world\"", {
column: 0
},
end: {
line: 1,
column: 14
line: 2,
column: 6
}
}
}
Expand All @@ -6951,8 +6951,8 @@ test("\"Hello\\\u2028world\"", {
column: 0
},
end: {
line: 1,
column: 14
line: 2,
column: 6
}
}
});
Expand All @@ -6972,8 +6972,8 @@ test("\"Hello\\\u2029world\"", {
column: 0
},
end: {
line: 1,
column: 14
line: 2,
column: 6
}
}
},
Expand All @@ -6983,8 +6983,8 @@ test("\"Hello\\\u2029world\"", {
column: 0
},
end: {
line: 1,
column: 14
line: 2,
column: 6
}
}
}
Expand All @@ -6995,8 +6995,8 @@ test("\"Hello\\\u2029world\"", {
column: 0
},
end: {
line: 1,
column: 14
line: 2,
column: 6
}
}
});
Expand Down

0 comments on commit 80b3562

Please sign in to comment.