Skip to content

Commit

Permalink
Fix source maps for template literals (#16515)
Browse files Browse the repository at this point in the history
* Add test

* Fix source maps for template literals
  • Loading branch information
nicolo-ribaudo committed May 19, 2024
1 parent d6c2343 commit fbfe16c
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let a = `a${b}c`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sourceMaps": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let a = `a${b}c`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(1:0-4) let a = <-- (1:0-4) let a =
^^^^ ^^^^

(1:4-5) let a = ` <-- (1:4-5) let a = `
^ ^

(1:5-8) et a = `a${ <-- (1:5-8) et a = `a${
^^^ ^^^

(1:8-12) a = `a${b}c` <-- (1:8-12) a = `a${b}c`
^^^^ ^^^^

(1:12-13) `a${b}c`; <-- (1:12-13) `a${b}c`;
^ ^

(1:13-16) a${b}c`; <-- (1:13-16) a${b}c`;
^^^ ^^^

(1:16-17) b}c`; <-- (1:16-17) b}c`;
^ ^
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": 3,
"names": [
"a",
"b"
],
"sources": [
"source-maps/template-literal/input.js"
],
"sourcesContent": [
"let a = `a${b}c`;"
],
"mappings": "AAAA,IAAIA,CAAC,GAAG,IAAIC,CAAC,GAAG",
"ignoreList": []
}
21 changes: 9 additions & 12 deletions packages/babel-generator/src/generators/template-literals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,24 @@ export function TaggedTemplateExpression(
this.print(node.quasi, node);
}

export function TemplateElement(
this: Printer,
node: t.TemplateElement,
parent: t.TemplateLiteral,
) {
const isFirst = parent.quasis[0] === node;
const isLast = parent.quasis[parent.quasis.length - 1] === node;

const value = (isFirst ? "`" : "}") + node.value.raw + (isLast ? "`" : "${");

this.token(value, true);
export function TemplateElement(this: Printer) {
throw new Error("TemplateElement printing is handled in TemplateLiteral");
}

export function TemplateLiteral(this: Printer, node: t.TemplateLiteral) {
const quasis = node.quasis;

let partRaw = "`";

for (let i = 0; i < quasis.length; i++) {
this.print(quasis[i], node);
partRaw += quasis[i].value.raw;

if (i + 1 < quasis.length) {
this.token(partRaw + "${", true);
this.print(node.expressions[i], node);
partRaw = "}";
}
}

this.token(partRaw + "`", true);
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"sourcesContent": [
"// Newline\n`before\nafter`;\n\n// Newline newline\n`before\n\nafter`;\n\n// Newline LineContinuation\n`before\n\\\nafter`;\n\n// LineContinuation\n`before\\\nafter`;\n\n// LineContinuation newline\n`before\\\n\nafter`;\n\n// LineContinuation LineContinuation\n`before\\\n\\\nafter`;"
],
"mappings": "AAAA;AACC;AACD,MAAM;;AAEN;AACC;AACD;AACA,MAAM;;AAEN;AACC;AACD;AACA,MAAM;;AAEN;AACC;AACD,MAAM;;AAEN;AACC;AACD;AACA,MAAM;;AAEN;AACC;AACD;AACA,MAAM",
"mappings": "AAAA;AACA;AACA,MAAM;;AAEN;AACA;AACA;AACA,MAAM;;AAEN;AACA;AACA;AACA,MAAM;;AAEN;AACA;AACA,MAAM;;AAEN;AACA;AACA;AACA,MAAM;;AAEN;AACA;AACA;AACA,MAAM",
"ignoreList": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,8 @@
(10:0-4) foo3``; <-- (9:4-13) (0, _foo.foo3)``;
^^^^ ^^^^^^^^^

(10:4-5) foo3``; <-- (9:13-14) foo3)``;
^ ^

(10:5-6) oo3``; <-- (9:14-16) oo3)``;
^ ^^
(10:4-6) foo3``; <-- (9:13-16) foo3)``;
^^ ^^^

(10:6-7) o3``; <-- (9:16-17) 3)``;
^ ^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"sourcesContent": [
"import foo from \"foo\";\nimport { default as foo2 } from \"foo\";\nimport { foo3 } from \"foo\";\nimport * as foo4 from \"foo\";\n\nfoo;\nfoo2;\nfoo3;\nfoo3();\nfoo3``;\nfoo3?.();"
],
"mappings": ";;AAAA,IAAAA,IAAA,GAAAC,YAAA,CAAAC,sBAAA,CAAAC,OAAA;AAAsB,IAAAC,IAAA,GAAAJ,IAAA;AAKtBK,YAAG;AACHC,YAAI;AACJC,SAAI;AACJ,IAAAA,SAAI,EAAC,CAAC;AACN,IAAAA,SAAI,CAAC,EAAC;AACN,IAAAA,SAAI,IAAG,CAAC",
"mappings": ";;AAAA,IAAAA,IAAA,GAAAC,YAAA,CAAAC,sBAAA,CAAAC,OAAA;AAAsB,IAAAC,IAAA,GAAAJ,IAAA;AAKtBK,YAAG;AACHC,YAAI;AACJC,SAAI;AACJ,IAAAA,SAAI,EAAC,CAAC;AACN,IAAAA,SAAI,GAAE;AACN,IAAAA,SAAI,IAAG,CAAC",
"ignoreList": []
}

0 comments on commit fbfe16c

Please sign in to comment.