From 6c104c4d085ef34462f870ba7704709e02d1354f Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Wed, 7 Feb 2024 16:28:27 +0100 Subject: [PATCH] Properly tack line numbers in readInvalidTemplateToken FIX: Fix a bug that broke line number accounting after a template literal with invalid escape sequences. Closes https://github.com/acornjs/acorn/issues/1275 --- acorn/src/tokenize.js | 5 +++ test/tests-harmony.js | 94 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/acorn/src/tokenize.js b/acorn/src/tokenize.js index d575c67fd..81cf5b9ea 100644 --- a/acorn/src/tokenize.js +++ b/acorn/src/tokenize.js @@ -679,6 +679,11 @@ pp.readInvalidTemplateToken = function() { case "`": return this.finishToken(tt.invalidTemplate, this.input.slice(this.start, this.pos)) + case "\r": case "\n": case "\u2028": case "\u2029": + ++this.curLine + this.lineStart = this.pos + 1 + break + // no default } } diff --git a/test/tests-harmony.js b/test/tests-harmony.js index bdcefe8a8..c54f02aa5 100644 --- a/test/tests-harmony.js +++ b/test/tests-harmony.js @@ -1093,6 +1093,100 @@ test("`outer${{x: {y: 10}}}bar${`nested${function(){return 1;}}endnest`}end`",{ ecmaVersion: 6 }); +test("foo`\n\\x\n$\n`;", { + type: "Program", + loc: { + start: { + line: 1, + column: 0 + }, + end: { + line: 4, + column: 2 + } + }, + body: [ + { + type: "ExpressionStatement", + loc: { + start: { + line: 1, + column: 0 + }, + end: { + line: 4, + column: 2 + } + }, + expression: { + type: "TaggedTemplateExpression", + loc: { + start: { + line: 1, + column: 0 + }, + end: { + line: 4, + column: 1 + } + }, + tag: { + type: "Identifier", + loc: { + start: { + line: 1, + column: 0 + }, + end: { + line: 1, + column: 3 + } + }, + name: "foo" + }, + quasi: { + type: "TemplateLiteral", + loc: { + start: { + line: 1, + column: 3 + }, + end: { + line: 4, + column: 1 + } + }, + expressions: [], + quasis: [ + { + type: "TemplateElement", + loc: { + start: { + line: 1, + column: 4 + }, + end: { + line: 4, + column: 0 + } + }, + value: { + raw: "\n\\x\n$\n", + cooked: null + }, + tail: true + } + ] + } + } + } + ], + sourceType: "script" +}, { + ecmaVersion: 10, + locations: true +}); + // ES6: Switch Case Declaration