Skip to content

Commit 1485dc8

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

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

doc/api/module.md

+4
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

+15-1
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,13 @@ class SourceMap {
125125
#mappings = [];
126126
#sources = {};
127127
#sourceContentByURL = {};
128+
#lineLengths = undefined;
128129

129130
/**
130131
* @constructor
131132
* @param {SourceMapV3} payload
132133
*/
133-
constructor(payload) {
134+
constructor(payload, lineLengths) {
134135
if (!base64Map) {
135136
const base64Digits =
136137
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
@@ -140,6 +141,9 @@ class SourceMap {
140141
}
141142
this.#payload = cloneSourceMapV3(payload);
142143
this.#parseMappingPayload();
144+
if (ArrayIsArray(lineLengths) && lineLengths.length) {
145+
this.#lineLengths = lineLengths;
146+
}
143147
}
144148

145149
/**
@@ -149,6 +153,16 @@ class SourceMap {
149153
return cloneSourceMapV3(this.#payload);
150154
}
151155

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

lib/internal/source_map/source_map_cache.js

+1-1
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

+11-1
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)