update template literal parsing to properly handle newlines

This commit is contained in:
Sebastian McKenzie 2015-06-05 09:36:37 +01:00
parent f3acedbf08
commit 02a6feed73
3 changed files with 17 additions and 15 deletions

View File

@ -513,7 +513,7 @@ pp.parseNew = function() {
pp.parseTemplateElement = function() {
let elem = this.startNode()
elem.value = {
raw: this.input.slice(this.start, this.end),
raw: this.input.slice(this.start, this.end).replace(/\r\n?/g, '\n'),
cooked: this.value
}
this.next()

View File

@ -577,11 +577,15 @@ pp.readTmplToken = function() {
} else if (isNewLine(ch)) {
out += this.input.slice(chunkStart, this.pos)
++this.pos
if (ch === 13 && this.input.charCodeAt(this.pos) === 10) {
++this.pos
out += "\n"
} else {
out += String.fromCharCode(ch)
switch (ch) {
case 13:
if (this.input.charCodeAt(this.pos) === 10) ++this.pos;
case 10:
out += "\n";
break;
default:
out += String.fromCharCode(ch);
break;
}
if (this.options.locations) {
++this.curLine

View File

@ -813,7 +813,7 @@ test("`\\n\\r\\b\\v\\t\\f\\\n\\\r\n`", {
type: "TemplateLiteral",
quasis: [{
type: "TemplateElement",
value: {raw: "\\n\\r\\b\\v\\t\\f\\\n\\\r\n", cooked: "\n\r\b\u000b\t\f"},
value: {raw: "\\n\\r\\b\\v\\t\\f\\\n\\\n", cooked: "\n\r\b\u000b\t\f"},
tail: true,
loc: {
start: {line: 1, column: 1},
@ -841,7 +841,7 @@ test("`\\n\\r\\b\\v\\t\\f\\\n\\\r\n`", {
locations: true
});
test("`\n\r\n`", {
test("`\n\r\n\r`", {
type: "Program",
body: [{
type: "ExpressionStatement",
@ -849,27 +849,27 @@ test("`\n\r\n`", {
type: "TemplateLiteral",
quasis: [{
type: "TemplateElement",
value: {raw: "\n\r\n", cooked: "\n\n"},
value: {raw: "\n\n\n", cooked: "\n\n\n"},
tail: true,
loc: {
start: {line: 1, column: 1},
end: {line: 3, column: 0}
end: {line: 4, column: 0}
}
}],
expressions: [],
loc: {
start: {line: 1, column: 0},
end: {line: 3, column: 1}
end: {line: 4, column: 1}
}
},
loc: {
start: {line: 1, column: 0},
end: {line: 3, column: 1}
end: {line: 4, column: 1}
}
}],
loc: {
start: {line: 1, column: 0},
end: {line: 3, column: 1}
end: {line: 4, column: 1}
}
}, {
ecmaVersion: 6,
@ -14134,8 +14134,6 @@ test('function normal(x, y = 10) {}', {
}]
}, {ecmaVersion: 6});
test("'use strict'; function f([x,,z]) {}", {}, {ecmaVersion: 6});
// test preserveParens option with arrow functions
test("() => 42", {
type: "Program",