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() { pp.parseTemplateElement = function() {
let elem = this.startNode() let elem = this.startNode()
elem.value = { 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 cooked: this.value
} }
this.next() this.next()

View File

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

View File

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