Skip to content

Commit feba0a3

Browse files
authored
Preserve original identifier names from input sourcemaps (#17992) (#17998)
* Add failing test case * Preserve original identifier names from input sourcemaps * fix stuff
1 parent 1ff1cdc commit feba0a3

8 files changed

Lines changed: 171 additions & 5 deletions

File tree

‎packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-external-preserve-original-name/input.js‎

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-external-preserve-original-name/input.js.map‎

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"inputSourceMap": true,
3+
"sourceType": "module",
4+
"sourceMaps": true,
5+
"plugins": ["./plugin.js"]
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import "./chunk-BUSYA2B4.js";
2+
3+
// ../../../../private/var/folders/ws/tlwxqvs5407f21nxy7w8bgr80000gp/T/vite-inline-return-0dbOSY/node_modules/dep/index.js
4+
var marker$$$ = "react/jsx-runtime";
5+
var root$$$ = {
6+
outer: 1
7+
};
8+
function test$$$() {
9+
let root2$$$ = {
10+
inner: 2
11+
};
12+
return root2$$$;
13+
}
14+
export { marker$$$ as marker, root$$$ as root, test$$$ as test };
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = function () {
2+
return {
3+
visitor: {
4+
Scope(path) {
5+
const bindingNames = Object.keys(path.scope.bindings);
6+
7+
for (const name of bindingNames) {
8+
path.scope.rename(name, `${name}$$$`);
9+
}
10+
},
11+
},
12+
};
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
(1:7-13) ort const mark <-- (4:0-4) var mark
2+
^^^^^^ ^^^^
3+
4+
(1:13-22) nst marker = "rea <-- (4:4-13) var marker$$$ = "
5+
^^^^^^^^^ ^^^^^^^^^
6+
7+
(1:13-22) nst marker = "rea <-- (4:13-16) r$$$ = "rea
8+
^^^^^^^^^ ^^^
9+
10+
(1:22-42) r = "react/jsx-runtime"; <-- (4:16-36) $ = "react/jsx-runtime";
11+
^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
12+
13+
(2:7-11) ort let root <-- (5:0-4) var root
14+
^^^^ ^^^^
15+
16+
(2:11-18) let root = { ou <-- (5:4-11) var root$$$ = {
17+
^^^^^^^ ^^^^^^^
18+
19+
(2:11-18) let root = { ou <-- (5:11-14) t$$$ = {
20+
^^^^^^^ ^^^
21+
22+
(2:18-20) t = { oute <-- (5:14-15) $ = {
23+
^^ ^
24+
25+
(2:20-27) = { outer: 1 }; <-- (6:2-7) outer: 1
26+
^^^^^^^ ^^^^^
27+
28+
(2:20-27) = { outer: 1 }; <-- (6:7-9) uter: 1
29+
^^^^^^^ ^^
30+
31+
(2:27-29) er: 1 }; <-- (6:9-10) er: 1
32+
^^ ^
33+
34+
(2:29-31) : 1 }; <-- (7:0-2) };
35+
^^ ^^
36+
37+
(4:7-16) ort function test <-- (8:0-9) function test
38+
^^^^^^^^^ ^^^^^^^^^
39+
40+
(4:16-23) ion test() { <-- (8:9-17) ion test$$$() {
41+
^^^^^^^ ^^^^^^^^
42+
43+
(4:16-23) ion test() { <-- (8:17-19) $$$() {
44+
^^^^^^^ ^^
45+
46+
(4:23-24) t() { <-- (8:19-20) $() {
47+
^ ^
48+
49+
(5:2-6) let root <-- (9:2-6) let root
50+
^^^^ ^^^^
51+
52+
(5:6-13) let root = { in <-- (9:6-14) let root2$$$ = {
53+
^^^^^^^ ^^^^^^^^
54+
55+
(5:6-13) let root = { in <-- (9:14-17) 2$$$ = {
56+
^^^^^^^ ^^^
57+
58+
(5:13-15) t = { inne <-- (9:17-18) $ = {
59+
^^ ^
60+
61+
(5:15-22) = { inner: 2 }; <-- (10:4-9) inner: 2
62+
^^^^^^^ ^^^^^
63+
64+
(5:15-22) = { inner: 2 }; <-- (10:9-11) nner: 2
65+
^^^^^^^ ^^
66+
67+
(5:22-24) er: 2 }; <-- (10:11-12) er: 2
68+
^^ ^
69+
70+
(5:24-26) : 2 }; <-- (11:2-4) };
71+
^^ ^^
72+
73+
(6:2-9) return root <-- (12:2-9) return root
74+
^^^^^^^ ^^^^^^^
75+
76+
(6:9-14) urn root; <-- (12:9-17) urn root2$$$;
77+
^^^^^ ^^^^^^^^
78+
79+
(6:9-14) urn root; <-- (12:17-18) 2$$$;
80+
^^^^^ ^
81+
82+
(7:0-1) } <-- (13:0-1) }
83+
^ ^
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"version": 3,
3+
"names": [
4+
"marker",
5+
"root",
6+
"outer",
7+
"test",
8+
"inner"
9+
],
10+
"sources": [
11+
"../../../node_modules/dep/index.js"
12+
],
13+
"sourcesContent": [
14+
"export const marker = \"react/jsx-runtime\";\nexport let root = { outer: 1 };\n\nexport function test() {\n let root = { inner: 2 };\n return root;\n}\n"
15+
],
16+
"mappings": ";;;AAAO,IAAMA,SAAA,GAAS;AACf,IAAIC,OAAA,GAAO;EAAEC,KAAA,EAAO;AAAE;AAEtB,SAASC,QAAA,EAAO;EACrB,IAAIF,QAAA,GAAO;IAAEG,KAAA,EAAO;EAAE;EACtB,OAAOH,QAAA;AACT",
17+
"ignoreList": []
18+
}

‎packages/babel-generator/src/source-map.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,19 @@ export default class SourceMap {
119119
column: column!,
120120
});
121121

122-
// If the we found a name, nothing else needs to be done
123-
// Maybe we're marking a `(` and the input map already had a name attached there,
124-
// or we're marking a `(` and the sourcemap spanned a `foo(`,
125-
// or we're marking an identifier, etc.
126-
if (!originalMapping.name && identifierNamePos) {
122+
// Prefer the original name from the input sourcemap when marking an
123+
// identifier token at the same column, or when marking a related
124+
// token such as `(` via identifierNamePos.
125+
if (
126+
originalMapping.name &&
127+
(identifierNamePos ||
128+
(identifierName != null && originalMapping.column === column))
129+
) {
130+
identifierName = originalMapping.name;
131+
} else if (identifierNamePos) {
132+
// Maybe we're marking a `(` and the input map already had a name attached there,
133+
// or we're marking a `(` and the sourcemap spanned a `foo(`,
134+
// or we're marking an identifier, etc.
127135
// We're trying to mark a `(` (as that's the only thing that provides
128136
// an identifierNamePos currently), and we the AST had an identifier attached.
129137
// Lookup it's original name.

0 commit comments

Comments
 (0)