Skip to content

Commit da4b63a

Browse files
committed
module: add SourceMap.lineLengths
Fix: #48460
1 parent 7eafd2f commit da4b63a

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

doc/api/module.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ added:
274274
#### `new SourceMap(payload)`
275275
276276
* `payload` {Object}
277+
* `lineLengths` {number\[]}
277278
278279
Creates a new `sourceMap` instance.
279280
@@ -287,6 +288,9 @@ Creates a new `sourceMap` instance.
287288
* `mappings`: {string}
288289
* `sourceRoot`: {string}
289290
291+
`lineLengths` is an array of the length of each line in the
292+
generated code.
293+
290294
#### `sourceMap.payload`
291295
292296
* Returns: {Object}

lib/internal/source_map/source_map.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const {
7171
ArrayPrototypePush,
7272
ArrayPrototypeSlice,
7373
ArrayPrototypeSort,
74+
ArrayLength,
7475
ObjectPrototypeHasOwnProperty,
7576
StringPrototypeCharAt,
7677
} = primordials;
@@ -125,12 +126,13 @@ class SourceMap {
125126
#mappings = [];
126127
#sources = {};
127128
#sourceContentByURL = {};
129+
#lineLengths = undefined;
128130

129131
/**
130132
* @constructor
131133
* @param {SourceMapV3} payload
132134
*/
133-
constructor(payload) {
135+
constructor(payload, lineLengths) {
134136
if (!base64Map) {
135137
const base64Digits =
136138
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
@@ -140,6 +142,9 @@ class SourceMap {
140142
}
141143
this.#payload = cloneSourceMapV3(payload);
142144
this.#parseMappingPayload();
145+
if (ArrayIsArray(lineLengths) && ArrayLength(lineLengths)) {
146+
this.#lineLengths = lineLengths;
147+
}
143148
}
144149

145150
/**
@@ -149,6 +154,16 @@ class SourceMap {
149154
return cloneSourceMapV3(this.#payload);
150155
}
151156

157+
/**
158+
* @return {number[] | undefined} line lengths of generated source code
159+
*/
160+
get lineLengths() {
161+
if (this.#lineLengths) {
162+
return ArrayPrototypeSlice(this.#lineLengths);
163+
}
164+
return undefined;
165+
}
166+
152167
#parseMappingPayload = () => {
153168
if (this.#payload.sections) {
154169
this.#parseSections(this.#payload.sections);

lib/internal/source_map/source_map_cache.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ function findSourceMap(sourceURL) {
317317
}
318318
let sourceMap = entry.sourceMap;
319319
if (sourceMap === undefined) {
320-
sourceMap = new SourceMap(entry.data);
320+
sourceMap = new SourceMap(entry.data, entry.lineLengths);
321321
entry.sourceMap = sourceMap;
322322
}
323323
return sourceMap;

test/parallel/test-source-map-api.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ const { readFileSync } = require('fs');
5757
assert.strictEqual(fileName, originalSource);
5858
assert.strictEqual(lineNumber, 3);
5959
assert.strictEqual(columnNumber, 6);
60+
assert(Array.isArray(sourceMap.lineLengths));
61+
assert(!sourceMap.lineLengths.some((len) => (typeof len !== 'number')));
6062
}
6163

6264
// findSourceMap() can be used in Error.prepareStackTrace() to lookup
@@ -116,7 +118,10 @@ const { readFileSync } = require('fs');
116118
const payload = JSON.parse(readFileSync(
117119
require.resolve('../fixtures/source-map/disk.map'), 'utf8'
118120
));
119-
const sourceMap = new SourceMap(payload);
121+
const lineLengths = readFileSync(
122+
require.resolve('../fixtures/source-map/disk.map'), 'utf8'
123+
).replace(/\n$/, '').split('\n').map((l) => l.length);
124+
const sourceMap = new SourceMap(payload, lineLengths);
120125
const {
121126
originalLine,
122127
originalColumn,
@@ -125,6 +130,11 @@ const { readFileSync } = require('fs');
125130
assert.strictEqual(originalLine, 2);
126131
assert.strictEqual(originalColumn, 4);
127132
assert(originalSource.endsWith('disk.js'));
133+
const sourceMapLineLengths = sourceMap.lineLengths;
134+
for (let i = 0; i < sourceMapLineLengths.length; i++) {
135+
assert.strictEqual(sourceMapLineLengths[i], lineLengths[i]);
136+
}
137+
assert.strictEqual(sourceMapLineLengths.length, lineLengths.length);
128138
// The stored payload should be a clone:
129139
assert.strictEqual(payload.mappings, sourceMap.payload.mappings);
130140
assert.notStrictEqual(payload, sourceMap.payload);

0 commit comments

Comments
 (0)