Skip to content

Commit 3ea89d2

Browse files
committed
chore: address code review
1 parent bc7c0c0 commit 3ea89d2

File tree

4 files changed

+39
-16
lines changed

4 files changed

+39
-16
lines changed

doc/api/modules.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1034,8 +1034,9 @@ import('fs').then((esmFS) => {
10341034
```
10351035
10361036
## Source Map V3 Support
1037-
1038-
<!--introduced_in=REPLACEME-->
1037+
<!-- YAML
1038+
added: REPLACEME
1039+
-->
10391040
10401041
> Stability: 1 - Experimental
10411042

lib/internal/source_map/source_map.js

+30-12
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@
6666

6767
'use strict';
6868

69+
const {
70+
Array
71+
} = primordials;
72+
73+
const {
74+
ERR_INVALID_ARG_TYPE
75+
} = require('internal/errors').codes;
76+
6977
let base64Map;
7078

7179
const VLQ_BASE_SHIFT = 5;
@@ -112,7 +120,7 @@ class StringCharIterator {
112120
* @param {SourceMapV3} payload
113121
*/
114122
class SourceMap {
115-
#payload = null;
123+
#payload;
116124
#reverseMappingsBySourceURL = [];
117125
#mappings = [];
118126
#sources = {};
@@ -130,15 +138,15 @@ class SourceMap {
130138
for (let i = 0; i < base64Digits.length; ++i)
131139
base64Map[base64Digits[i]] = i;
132140
}
133-
this.#payload = payload;
141+
this.#payload = cloneSourceMapV3(payload);
134142
this.#parseMappingPayload();
135143
}
136144

137145
/**
138146
* @return {Object} raw source map v3 payload.
139147
*/
140148
get payload() {
141-
return this.#payload;
149+
return cloneSourceMapV3(this.#payload);
142150
}
143151

144152
/**
@@ -169,13 +177,6 @@ class SourceMap {
169177
findEntry(lineNumber, columnNumber) {
170178
let first = 0;
171179
let count = this.#mappings.length;
172-
const nullEntry = {
173-
generatedLine: null,
174-
generatedColumn: null,
175-
originalSource: null,
176-
originalLine: null,
177-
originalColumn: null
178-
};
179180
while (count > 1) {
180181
const step = count >> 1;
181182
const middle = first + step;
@@ -191,9 +192,9 @@ class SourceMap {
191192
const entry = this.#mappings[first];
192193
if (!first && entry && (lineNumber < entry[0] ||
193194
(lineNumber === entry[0] && columnNumber < entry[1]))) {
194-
return nullEntry;
195+
return {};
195196
} else if (!entry) {
196-
return nullEntry;
197+
return {};
197198
} else {
198199
return {
199200
generatedLine: entry[0],
@@ -306,6 +307,23 @@ function decodeVLQ(stringCharIterator) {
306307
return negative ? -result : result;
307308
}
308309

310+
/**
311+
* @param {SourceMapV3} payload
312+
* @return {SourceMapV3}
313+
*/
314+
function cloneSourceMapV3(payload) {
315+
if (typeof payload !== 'object') {
316+
throw new ERR_INVALID_ARG_TYPE('payload', ['Object'], payload);
317+
}
318+
payload = { ...payload };
319+
for (const key in payload) {
320+
if (payload.hasOwnProperty(key) && Array.isArray(payload[key])) {
321+
payload[key] = payload[key].slice(0);
322+
}
323+
}
324+
return payload;
325+
}
326+
309327
module.exports = {
310328
SourceMap
311329
};

lib/internal/source_map/source_map_cache.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ function findSourceMap(uri, error) {
244244
if (sourceMap && sourceMap.data) {
245245
return new SourceMap(sourceMap.data);
246246
} else {
247-
return null;
247+
return undefined;
248248
}
249249
}
250250

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,9 @@ const { readFileSync } = require('fs');
7676
assert.strictEqual(originalLine, 2);
7777
assert.strictEqual(originalColumn, 4);
7878
assert(originalSource.endsWith('disk.js'));
79-
assert.strictEqual(payload, sourceMap.payload);
79+
// The stored payload should be a clone:
80+
assert.strictEqual(payload.mappings, sourceMap.payload.mappings);
81+
assert.notStrictEqual(payload, sourceMap.payload);
82+
assert.strictEqual(payload.sources[0], sourceMap.payload.sources[0]);
83+
assert.notStrictEqual(payload.sources, sourceMap.payload.sources);
8084
}

0 commit comments

Comments
 (0)