Skip to content

Commit b2376cf

Browse files
committed
module: add SourceMap.lineLengths
Fix: #48460
1 parent 363eca1 commit b2376cf

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

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
@@ -49,6 +49,8 @@ const { readFileSync } = require('fs');
4949
assert.strictEqual(originalLine, 2);
5050
assert.strictEqual(originalColumn, 4);
5151
assert(originalSource.endsWith('disk.js'));
52+
assert(Array.isArray(sourceMap.lineLengths));
53+
assert(!sourceMap.lineLengths.some((len) => (typeof len !== 'number')));
5254
}
5355

5456
// findSourceMap() can be used in Error.prepareStackTrace() to lookup
@@ -96,7 +98,10 @@ const { readFileSync } = require('fs');
9698
const payload = JSON.parse(readFileSync(
9799
require.resolve('../fixtures/source-map/disk.map'), 'utf8'
98100
));
99-
const sourceMap = new SourceMap(payload);
101+
const lineLengths = readFileSync(
102+
require.resolve('../fixtures/source-map/disk.map'), 'utf8'
103+
).replace(/\n$/, '').split('\n').map((l) => l.length);
104+
const sourceMap = new SourceMap(payload, lineLengths);
100105
const {
101106
originalLine,
102107
originalColumn,
@@ -105,6 +110,11 @@ const { readFileSync } = require('fs');
105110
assert.strictEqual(originalLine, 2);
106111
assert.strictEqual(originalColumn, 4);
107112
assert(originalSource.endsWith('disk.js'));
113+
const sourceMapLineLengths = sourceMap.lineLengths;
114+
for (let i = 0; i < sourceMapLineLengths.length; i++) {
115+
assert.strictEqual(sourceMapLineLengths[i], lineLengths[i]);
116+
}
117+
assert.strictEqual(sourceMapLineLengths.length, lineLengths.length);
108118
// The stored payload should be a clone:
109119
assert.strictEqual(payload.mappings, sourceMap.payload.mappings);
110120
assert.notStrictEqual(payload, sourceMap.payload);

0 commit comments

Comments
 (0)