Skip to content

Commit 6406f50

Browse files
isaacsjuanarbol
authored andcommitted
module: add SourceMap.lineLengths
Fix: #48460 PR-URL: #48461 Fixes: #48460 Reviewed-By: Chengzhong Wu <[email protected]>
1 parent fa94deb commit 6406f50

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

doc/api/module.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@ added:
176176
- v12.17.0
177177
-->
178178
179-
#### `new SourceMap(payload)`
179+
#### `new SourceMap(payload[, { lineLengths }])`
180180
181181
* `payload` {Object}
182+
* `lineLengths` {number\[]}
182183
183184
Creates a new `sourceMap` instance.
184185
@@ -192,6 +193,9 @@ Creates a new `sourceMap` instance.
192193
* `mappings`: {string}
193194
* `sourceRoot`: {string}
194195
196+
`lineLengths` is an optional array of the length of each line in the
197+
generated code.
198+
195199
#### `sourceMap.payload`
196200
197201
* 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 } = { __proto__: null }) {
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, { lineLengths: 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)