Skip to content

Commit 4ce5740

Browse files
authored
Use maybeAddMapping to add sourcemap markings (#14510)
1 parent c90add7 commit 4ce5740

7 files changed

Lines changed: 63 additions & 125 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
{
2-
"mappings": "AAAC;ACAD,K",
2+
"version": 3,
3+
"mappings": "AAAC,KCAD;ACAA,K",
34
"names": [],
45
"sources": [
56
"bar.js",
7+
"input.tsx",
68
"baz.js"
79
],
810
"sourcesContent": [
911
"<bar />",
12+
"foo(1);\nfunction foo(bar: number): never {\n throw new Error('Intentional.');\n}",
1013
"baz();"
11-
],
12-
"version": 3
13-
}
14+
]
15+
}

packages/babel-core/test/fixtures/transformation/source-maps/input-source-map-multiple-output-sources/source-map.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"version": 3,
3-
"mappings": "AAAC;;ACCD,SAASA,GAAT,CAAaC,GAAb,EAAwB;EACpB,MAAM,IAAIC,KAAJ,CAAU,cAAV,CAAN;AACH",
3+
"mappings": "AAAC,KCAD;;AACA,SAASA,GAAT,CAAaC,GAAb,EAAwB;EACpB,MAAM,IAAIC,KAAJ,CAAU,cAAV,CAAN;AACH",
44
"names": [
55
"foo",
66
"bar",
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
{
2-
"mappings": "AAAC",
2+
"version": 3,
3+
"mappings": "AAAC,KCAD",
34
"names": [],
4-
"sources": ["test.js"],
5-
"sourcesContent": ["<bar />"],
6-
"version": 3
7-
}
5+
"sources": [
6+
"test.js",
7+
"input.tsx"
8+
],
9+
"sourcesContent": [
10+
"<bar />",
11+
"foo(1);\nfunction foo(bar: number): never {\n throw new Error('Intentional.');\n}"
12+
]
13+
}

packages/babel-generator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
],
2121
"dependencies": {
2222
"@babel/types": "workspace:^",
23-
"@jridgewell/gen-mapping": "^0.1.0",
23+
"@jridgewell/gen-mapping": "^0.3.0",
2424
"jsesc": "condition: BABEL_8_BREAKING ? ^3.0.2 : ^2.5.1"
2525
},
2626
"devDependencies": {

packages/babel-generator/src/buffer.ts

Lines changed: 23 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ type SourcePos = {
1717
line: number | undefined;
1818
column: number | undefined;
1919
filename: string | undefined;
20-
force: boolean;
2120
};
2221

2322
function SourcePos(): SourcePos {
@@ -26,7 +25,6 @@ function SourcePos(): SourcePos {
2625
line: undefined,
2726
column: undefined,
2827
filename: undefined,
29-
force: false,
3028
};
3129
}
3230

@@ -88,9 +86,8 @@ export default class Buffer {
8886

8987
append(str: string): void {
9088
this._flush();
91-
const { line, column, filename, identifierName, force } =
92-
this._sourcePosition;
93-
this._append(str, line, column, identifierName, filename, force);
89+
const { line, column, filename, identifierName } = this._sourcePosition;
90+
this._append(str, line, column, identifierName, filename);
9491
}
9592

9693
/**
@@ -104,23 +101,15 @@ export default class Buffer {
104101
}
105102
}
106103

107-
const { line, column, filename, identifierName, force } =
108-
this._sourcePosition;
109-
this._queue.unshift([str, line, column, identifierName, filename, force]);
104+
const { line, column, filename, identifierName } = this._sourcePosition;
105+
this._queue.unshift([str, line, column, identifierName, filename]);
110106
}
111107

112108
/**
113109
* Same as queue, but this indentation will never have a sourcmap marker.
114110
*/
115111
queueIndentation(str: string): void {
116-
this._queue.unshift([
117-
str,
118-
undefined,
119-
undefined,
120-
undefined,
121-
undefined,
122-
false,
123-
]);
112+
this._queue.unshift([str, undefined, undefined, undefined, undefined]);
124113
}
125114

126115
_flush(): void {
@@ -132,11 +121,10 @@ export default class Buffer {
132121

133122
_append(
134123
str: string,
135-
line: number,
136-
column: number,
124+
line: number | undefined,
125+
column: number | undefined,
137126
identifierName: string | undefined,
138127
filename: string | undefined,
139-
force: boolean,
140128
): void {
141129
this._buf += str;
142130
this._last = str.charCodeAt(str.length - 1);
@@ -151,7 +139,7 @@ export default class Buffer {
151139
// If the string starts with a newline char, then adding a mark is redundant.
152140
// This catches both "no newlines" and "newline after several chars".
153141
if (i !== 0) {
154-
this._mark(line, column, identifierName, filename, force);
142+
this._mark(line, column, identifierName, filename);
155143
}
156144

157145
// Now, find each reamining newline char in the string.
@@ -163,28 +151,20 @@ export default class Buffer {
163151
// We mark the start of each line, which happens directly after this newline char
164152
// unless this is the last char.
165153
if (last < str.length) {
166-
this._mark(++line, 0, identifierName, filename, force);
154+
this._mark(++line, 0, identifierName, filename);
167155
}
168156
i = str.indexOf("\n", last);
169157
}
170158
this._position.column += str.length - last;
171159
}
172160

173161
_mark(
174-
line: number,
175-
column: number,
176-
identifierName?: string | null,
177-
filename?: string | null,
178-
force?: boolean,
162+
line: number | undefined,
163+
column: number | undefined,
164+
identifierName: string | undefined,
165+
filename: string | undefined,
179166
): void {
180-
this._map?.mark(
181-
this._position,
182-
line,
183-
column,
184-
identifierName,
185-
filename,
186-
force,
187-
);
167+
this._map?.mark(this._position, line, column, identifierName, filename);
188168
}
189169

190170
removeTrailingNewline(): void {
@@ -262,11 +242,7 @@ export default class Buffer {
262242
* over "();", where previously it would have been a single mapping.
263243
*/
264244
exactSource(loc: any, cb: () => void) {
265-
// In cases where parent expressions start at the same locations as the
266-
// identifier itself, the current active location could already be the
267-
// start of this range. We use 'force' here to explicitly start a new
268-
// mapping range for this new token.
269-
this.source("start", loc, true /* force */);
245+
this.source("start", loc);
270246

271247
cb();
272248

@@ -286,12 +262,12 @@ export default class Buffer {
286262
* will be given this position in the sourcemap.
287263
*/
288264

289-
source(prop: string, loc: t.SourceLocation, force?: boolean): void {
265+
source(prop: string, loc: t.SourceLocation): void {
290266
if (prop && !loc) return;
291267

292268
// Since this is called extremely often, we re-use the same _sourcePosition
293269
// object for the whole lifetime of the buffer.
294-
this._normalizePosition(prop, loc, this._sourcePosition, force);
270+
this._normalizePosition(prop, loc, this._sourcePosition);
295271
}
296272

297273
/**
@@ -314,23 +290,16 @@ export default class Buffer {
314290
cb();
315291

316292
if (
317-
// If the current active position is forced, we only want to reactivate
318-
// the old position if it is different from the newest position.
319-
(!this._sourcePosition.force ||
320-
this._sourcePosition.line !== originalLine ||
321-
this._sourcePosition.column !== originalColumn ||
322-
this._sourcePosition.filename !== originalFilename) &&
323293
// Verify if reactivating this specific position has been disallowed.
324-
(!this._disallowedPop ||
325-
this._disallowedPop.line !== originalLine ||
326-
this._disallowedPop.column !== originalColumn ||
327-
this._disallowedPop.filename !== originalFilename)
294+
!this._disallowedPop ||
295+
this._disallowedPop.line !== originalLine ||
296+
this._disallowedPop.column !== originalColumn ||
297+
this._disallowedPop.filename !== originalFilename
328298
) {
329299
this._sourcePosition.line = originalLine;
330300
this._sourcePosition.column = originalColumn;
331301
this._sourcePosition.filename = originalFilename;
332302
this._sourcePosition.identifierName = originalIdentifierName;
333-
this._sourcePosition.force = false;
334303
this._disallowedPop = null;
335304
}
336305
}
@@ -343,42 +312,22 @@ export default class Buffer {
343312
_disallowPop(prop: string, loc: t.SourceLocation) {
344313
if (prop && !loc) return;
345314

346-
this._disallowedPop = this._normalizePosition(
347-
prop,
348-
loc,
349-
SourcePos(),
350-
false,
351-
);
315+
this._disallowedPop = this._normalizePosition(prop, loc, SourcePos());
352316
}
353317

354318
_normalizePosition(
355319
prop: string,
356320
loc: Loc | undefined | null,
357321
targetObj: SourcePos,
358-
force: boolean,
359322
) {
360323
const pos = loc ? loc[prop] : null;
361324

362-
const origLine = targetObj.line;
363-
const origColumn = targetObj.column;
364-
const origFilename = targetObj.filename;
365-
366325
targetObj.identifierName =
367-
(prop === "start" && loc?.identifierName) || null;
326+
(prop === "start" && loc?.identifierName) || undefined;
368327
targetObj.line = pos?.line;
369328
targetObj.column = pos?.column;
370329
targetObj.filename = loc?.filename;
371330

372-
// We want to skip reassigning `force` if we're re-setting the same position.
373-
if (
374-
force ||
375-
targetObj.line !== origLine ||
376-
targetObj.column !== origColumn ||
377-
targetObj.filename !== origFilename
378-
) {
379-
targetObj.force = force;
380-
}
381-
382331
return targetObj;
383332
}
384333

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

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {
22
GenMapping,
3-
addMapping,
3+
maybeAddMapping,
44
setSourceContent,
55
allMappings,
6-
encodedMap,
7-
decodedMap,
6+
toEncodedMap,
7+
toDecodedMap,
88
} from "@jridgewell/gen-mapping";
99

1010
import type {
@@ -55,11 +55,11 @@ export default class SourceMap {
5555
* Get the sourcemap.
5656
*/
5757
get(): EncodedSourceMap {
58-
return encodedMap(this._map);
58+
return toEncodedMap(this._map);
5959
}
6060

6161
getDecoded(): DecodedSourceMap {
62-
return decodedMap(this._map);
62+
return toDecodedMap(this._map);
6363
}
6464

6565
getRawMappings(): Mapping[] {
@@ -77,30 +77,10 @@ export default class SourceMap {
7777
column: number,
7878
identifierName?: string | null,
7979
filename?: string | null,
80-
force?: boolean,
8180
) {
82-
const generatedLine = generated.line;
83-
84-
// Adding an empty mapping at the start of a generated line just clutters the map.
85-
if (this._lastGenLine !== generatedLine && line == null) return;
86-
87-
// If this mapping points to the same source location as the last one, we can ignore it since
88-
// the previous one covers it.
89-
if (
90-
!force &&
91-
this._lastGenLine === generatedLine &&
92-
this._lastSourceLine === line &&
93-
this._lastSourceColumn === column
94-
) {
95-
return;
96-
}
97-
9881
this._rawMappings = undefined;
99-
this._lastGenLine = generatedLine;
100-
this._lastSourceLine = line;
101-
this._lastSourceColumn = column;
10282

103-
addMapping(this._map, {
83+
maybeAddMapping(this._map, {
10484
name: identifierName,
10585
generated,
10686
source:

yarn.lock

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ __metadata:
456456
"@babel/helper-fixtures": "workspace:^"
457457
"@babel/parser": "workspace:^"
458458
"@babel/types": "workspace:^"
459-
"@jridgewell/gen-mapping": ^0.1.0
459+
"@jridgewell/gen-mapping": ^0.3.0
460460
"@jridgewell/trace-mapping": ^0.3.8
461461
"@types/jsesc": ^2.5.0
462462
charcodes: ^0.2.0
@@ -4070,13 +4070,14 @@ __metadata:
40704070
languageName: node
40714071
linkType: hard
40724072

4073-
"@jridgewell/gen-mapping@npm:^0.1.0":
4074-
version: 0.1.0
4075-
resolution: "@jridgewell/gen-mapping@npm:0.1.0"
4073+
"@jridgewell/gen-mapping@npm:^0.3.0":
4074+
version: 0.3.0
4075+
resolution: "@jridgewell/gen-mapping@npm:0.3.0"
40764076
dependencies:
4077-
"@jridgewell/set-array": 1.0.0
4077+
"@jridgewell/set-array": ^1.0.0
40784078
"@jridgewell/sourcemap-codec": ^1.4.10
4079-
checksum: 8611de5acbea55fa84bcde0d6f803e006560f7a8ce618ba427e20a76913d18308fc14d5cfb324e30d0051556a96527fbc4e64cf65d68f121170776243387dcce
4079+
"@jridgewell/trace-mapping": ^0.3.9
4080+
checksum: ccc9b288d2bbf09d81a85cd07b0e0dd461f2a8d2fd0d46eefc1c04e994e9f95a51bb9e1c8718f4182e87a21c11d2f5ed687a789024cbd160c8e5421de8b011f6
40804081
languageName: node
40814082
linkType: hard
40824083

@@ -4087,10 +4088,10 @@ __metadata:
40874088
languageName: node
40884089
linkType: hard
40894090

4090-
"@jridgewell/set-array@npm:1.0.0":
4091-
version: 1.0.0
4092-
resolution: "@jridgewell/set-array@npm:1.0.0"
4093-
checksum: 35dd48a205099839fa8b9e9b7138d382d1f33457404c4e96c95ad1cd9aea99904737b351d4dea8b21fa17b50fcd0dc2fdbf4c04fc4bcd3c1a42d079217261444
4091+
"@jridgewell/set-array@npm:^1.0.0":
4092+
version: 1.1.0
4093+
resolution: "@jridgewell/set-array@npm:1.1.0"
4094+
checksum: 86ddd72ce7d2f7756dfb69804b35d0e760a85dcef30ed72e8610bf2c5e843f8878d977a0c77c4fdfa6a0e3d5b18e5bde4a1f1dd73fd2db06b200c998e9b5a6c5
40944095
languageName: node
40954096
linkType: hard
40964097

@@ -4101,13 +4102,13 @@ __metadata:
41014102
languageName: node
41024103
linkType: hard
41034104

4104-
"@jridgewell/trace-mapping@npm:^0.3.0, @jridgewell/trace-mapping@npm:^0.3.8":
4105-
version: 0.3.8
4106-
resolution: "@jridgewell/trace-mapping@npm:0.3.8"
4105+
"@jridgewell/trace-mapping@npm:^0.3.0, @jridgewell/trace-mapping@npm:^0.3.8, @jridgewell/trace-mapping@npm:^0.3.9":
4106+
version: 0.3.9
4107+
resolution: "@jridgewell/trace-mapping@npm:0.3.9"
41074108
dependencies:
41084109
"@jridgewell/resolve-uri": ^3.0.3
41094110
"@jridgewell/sourcemap-codec": ^1.4.10
4110-
checksum: 4b46f28c785133773de521677983186dfe7ed80872bee57b73624527e345954139ea45dfb7551a5f04de1773dc9501b896554a8f1c9e1c44ab2146a3c66b6fda
4111+
checksum: d89597752fd88d3f3480845691a05a44bd21faac18e2185b6f436c3b0fd0c5a859fbbd9aaa92050c4052caf325ad3e10e2e1d1b64327517471b7d51babc0ddef
41114112
languageName: node
41124113
linkType: hard
41134114

0 commit comments

Comments
 (0)