Skip to content

Commit

Permalink
Use maybeAddMapping to add sourcemap markings (#14510)
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Apr 30, 2022
1 parent c90add7 commit 4ce5740
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 125 deletions.
@@ -1,13 +1,15 @@
{
"mappings": "AAAC;ACAD,K",
"version": 3,
"mappings": "AAAC,KCAD;ACAA,K",
"names": [],
"sources": [
"bar.js",
"input.tsx",
"baz.js"
],
"sourcesContent": [
"<bar />",
"foo(1);\nfunction foo(bar: number): never {\n throw new Error('Intentional.');\n}",
"baz();"
],
"version": 3
}
]
}
@@ -1,6 +1,6 @@
{
"version": 3,
"mappings": "AAAC;;ACCD,SAASA,GAAT,CAAaC,GAAb,EAAwB;EACpB,MAAM,IAAIC,KAAJ,CAAU,cAAV,CAAN;AACH",
"mappings": "AAAC,KCAD;;AACA,SAASA,GAAT,CAAaC,GAAb,EAAwB;EACpB,MAAM,IAAIC,KAAJ,CAAU,cAAV,CAAN;AACH",
"names": [
"foo",
"bar",
Expand Down
@@ -1,7 +1,13 @@
{
"mappings": "AAAC",
"version": 3,
"mappings": "AAAC,KCAD",
"names": [],
"sources": ["test.js"],
"sourcesContent": ["<bar />"],
"version": 3
}
"sources": [
"test.js",
"input.tsx"
],
"sourcesContent": [
"<bar />",
"foo(1);\nfunction foo(bar: number): never {\n throw new Error('Intentional.');\n}"
]
}
2 changes: 1 addition & 1 deletion packages/babel-generator/package.json
Expand Up @@ -20,7 +20,7 @@
],
"dependencies": {
"@babel/types": "workspace:^",
"@jridgewell/gen-mapping": "^0.1.0",
"@jridgewell/gen-mapping": "^0.3.0",
"jsesc": "condition: BABEL_8_BREAKING ? ^3.0.2 : ^2.5.1"
},
"devDependencies": {
Expand Down
97 changes: 23 additions & 74 deletions packages/babel-generator/src/buffer.ts
Expand Up @@ -17,7 +17,6 @@ type SourcePos = {
line: number | undefined;
column: number | undefined;
filename: string | undefined;
force: boolean;
};

function SourcePos(): SourcePos {
Expand All @@ -26,7 +25,6 @@ function SourcePos(): SourcePos {
line: undefined,
column: undefined,
filename: undefined,
force: false,
};
}

Expand Down Expand Up @@ -88,9 +86,8 @@ export default class Buffer {

append(str: string): void {
this._flush();
const { line, column, filename, identifierName, force } =
this._sourcePosition;
this._append(str, line, column, identifierName, filename, force);
const { line, column, filename, identifierName } = this._sourcePosition;
this._append(str, line, column, identifierName, filename);
}

/**
Expand All @@ -104,23 +101,15 @@ export default class Buffer {
}
}

const { line, column, filename, identifierName, force } =
this._sourcePosition;
this._queue.unshift([str, line, column, identifierName, filename, force]);
const { line, column, filename, identifierName } = this._sourcePosition;
this._queue.unshift([str, line, column, identifierName, filename]);
}

/**
* Same as queue, but this indentation will never have a sourcmap marker.
*/
queueIndentation(str: string): void {
this._queue.unshift([
str,
undefined,
undefined,
undefined,
undefined,
false,
]);
this._queue.unshift([str, undefined, undefined, undefined, undefined]);
}

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

_append(
str: string,
line: number,
column: number,
line: number | undefined,
column: number | undefined,
identifierName: string | undefined,
filename: string | undefined,
force: boolean,
): void {
this._buf += str;
this._last = str.charCodeAt(str.length - 1);
Expand All @@ -151,7 +139,7 @@ export default class Buffer {
// If the string starts with a newline char, then adding a mark is redundant.
// This catches both "no newlines" and "newline after several chars".
if (i !== 0) {
this._mark(line, column, identifierName, filename, force);
this._mark(line, column, identifierName, filename);
}

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

_mark(
line: number,
column: number,
identifierName?: string | null,
filename?: string | null,
force?: boolean,
line: number | undefined,
column: number | undefined,
identifierName: string | undefined,
filename: string | undefined,
): void {
this._map?.mark(
this._position,
line,
column,
identifierName,
filename,
force,
);
this._map?.mark(this._position, line, column, identifierName, filename);
}

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

cb();

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

source(prop: string, loc: t.SourceLocation, force?: boolean): void {
source(prop: string, loc: t.SourceLocation): void {
if (prop && !loc) return;

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

/**
Expand All @@ -314,23 +290,16 @@ export default class Buffer {
cb();

if (
// If the current active position is forced, we only want to reactivate
// the old position if it is different from the newest position.
(!this._sourcePosition.force ||
this._sourcePosition.line !== originalLine ||
this._sourcePosition.column !== originalColumn ||
this._sourcePosition.filename !== originalFilename) &&
// Verify if reactivating this specific position has been disallowed.
(!this._disallowedPop ||
this._disallowedPop.line !== originalLine ||
this._disallowedPop.column !== originalColumn ||
this._disallowedPop.filename !== originalFilename)
!this._disallowedPop ||
this._disallowedPop.line !== originalLine ||
this._disallowedPop.column !== originalColumn ||
this._disallowedPop.filename !== originalFilename
) {
this._sourcePosition.line = originalLine;
this._sourcePosition.column = originalColumn;
this._sourcePosition.filename = originalFilename;
this._sourcePosition.identifierName = originalIdentifierName;
this._sourcePosition.force = false;
this._disallowedPop = null;
}
}
Expand All @@ -343,42 +312,22 @@ export default class Buffer {
_disallowPop(prop: string, loc: t.SourceLocation) {
if (prop && !loc) return;

this._disallowedPop = this._normalizePosition(
prop,
loc,
SourcePos(),
false,
);
this._disallowedPop = this._normalizePosition(prop, loc, SourcePos());
}

_normalizePosition(
prop: string,
loc: Loc | undefined | null,
targetObj: SourcePos,
force: boolean,
) {
const pos = loc ? loc[prop] : null;

const origLine = targetObj.line;
const origColumn = targetObj.column;
const origFilename = targetObj.filename;

targetObj.identifierName =
(prop === "start" && loc?.identifierName) || null;
(prop === "start" && loc?.identifierName) || undefined;
targetObj.line = pos?.line;
targetObj.column = pos?.column;
targetObj.filename = loc?.filename;

// We want to skip reassigning `force` if we're re-setting the same position.
if (
force ||
targetObj.line !== origLine ||
targetObj.column !== origColumn ||
targetObj.filename !== origFilename
) {
targetObj.force = force;
}

return targetObj;
}

Expand Down
32 changes: 6 additions & 26 deletions packages/babel-generator/src/source-map.ts
@@ -1,10 +1,10 @@
import {
GenMapping,
addMapping,
maybeAddMapping,
setSourceContent,
allMappings,
encodedMap,
decodedMap,
toEncodedMap,
toDecodedMap,
} from "@jridgewell/gen-mapping";

import type {
Expand Down Expand Up @@ -55,11 +55,11 @@ export default class SourceMap {
* Get the sourcemap.
*/
get(): EncodedSourceMap {
return encodedMap(this._map);
return toEncodedMap(this._map);
}

getDecoded(): DecodedSourceMap {
return decodedMap(this._map);
return toDecodedMap(this._map);
}

getRawMappings(): Mapping[] {
Expand All @@ -77,30 +77,10 @@ export default class SourceMap {
column: number,
identifierName?: string | null,
filename?: string | null,
force?: boolean,
) {
const generatedLine = generated.line;

// Adding an empty mapping at the start of a generated line just clutters the map.
if (this._lastGenLine !== generatedLine && line == null) return;

// If this mapping points to the same source location as the last one, we can ignore it since
// the previous one covers it.
if (
!force &&
this._lastGenLine === generatedLine &&
this._lastSourceLine === line &&
this._lastSourceColumn === column
) {
return;
}

this._rawMappings = undefined;
this._lastGenLine = generatedLine;
this._lastSourceLine = line;
this._lastSourceColumn = column;

addMapping(this._map, {
maybeAddMapping(this._map, {
name: identifierName,
generated,
source:
Expand Down
29 changes: 15 additions & 14 deletions yarn.lock
Expand Up @@ -456,7 +456,7 @@ __metadata:
"@babel/helper-fixtures": "workspace:^"
"@babel/parser": "workspace:^"
"@babel/types": "workspace:^"
"@jridgewell/gen-mapping": ^0.1.0
"@jridgewell/gen-mapping": ^0.3.0
"@jridgewell/trace-mapping": ^0.3.8
"@types/jsesc": ^2.5.0
charcodes: ^0.2.0
Expand Down Expand Up @@ -4070,13 +4070,14 @@ __metadata:
languageName: node
linkType: hard

"@jridgewell/gen-mapping@npm:^0.1.0":
version: 0.1.0
resolution: "@jridgewell/gen-mapping@npm:0.1.0"
"@jridgewell/gen-mapping@npm:^0.3.0":
version: 0.3.0
resolution: "@jridgewell/gen-mapping@npm:0.3.0"
dependencies:
"@jridgewell/set-array": 1.0.0
"@jridgewell/set-array": ^1.0.0
"@jridgewell/sourcemap-codec": ^1.4.10
checksum: 8611de5acbea55fa84bcde0d6f803e006560f7a8ce618ba427e20a76913d18308fc14d5cfb324e30d0051556a96527fbc4e64cf65d68f121170776243387dcce
"@jridgewell/trace-mapping": ^0.3.9
checksum: ccc9b288d2bbf09d81a85cd07b0e0dd461f2a8d2fd0d46eefc1c04e994e9f95a51bb9e1c8718f4182e87a21c11d2f5ed687a789024cbd160c8e5421de8b011f6
languageName: node
linkType: hard

Expand All @@ -4087,10 +4088,10 @@ __metadata:
languageName: node
linkType: hard

"@jridgewell/set-array@npm:1.0.0":
version: 1.0.0
resolution: "@jridgewell/set-array@npm:1.0.0"
checksum: 35dd48a205099839fa8b9e9b7138d382d1f33457404c4e96c95ad1cd9aea99904737b351d4dea8b21fa17b50fcd0dc2fdbf4c04fc4bcd3c1a42d079217261444
"@jridgewell/set-array@npm:^1.0.0":
version: 1.1.0
resolution: "@jridgewell/set-array@npm:1.1.0"
checksum: 86ddd72ce7d2f7756dfb69804b35d0e760a85dcef30ed72e8610bf2c5e843f8878d977a0c77c4fdfa6a0e3d5b18e5bde4a1f1dd73fd2db06b200c998e9b5a6c5
languageName: node
linkType: hard

Expand All @@ -4101,13 +4102,13 @@ __metadata:
languageName: node
linkType: hard

"@jridgewell/trace-mapping@npm:^0.3.0, @jridgewell/trace-mapping@npm:^0.3.8":
version: 0.3.8
resolution: "@jridgewell/trace-mapping@npm:0.3.8"
"@jridgewell/trace-mapping@npm:^0.3.0, @jridgewell/trace-mapping@npm:^0.3.8, @jridgewell/trace-mapping@npm:^0.3.9":
version: 0.3.9
resolution: "@jridgewell/trace-mapping@npm:0.3.9"
dependencies:
"@jridgewell/resolve-uri": ^3.0.3
"@jridgewell/sourcemap-codec": ^1.4.10
checksum: 4b46f28c785133773de521677983186dfe7ed80872bee57b73624527e345954139ea45dfb7551a5f04de1773dc9501b896554a8f1c9e1c44ab2146a3c66b6fda
checksum: d89597752fd88d3f3480845691a05a44bd21faac18e2185b6f436c3b0fd0c5a859fbbd9aaa92050c4052caf325ad3e10e2e1d1b64327517471b7d51babc0ddef
languageName: node
linkType: hard

Expand Down

0 comments on commit 4ce5740

Please sign in to comment.