Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: add SourceMap.lineLengths #48461

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/api/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ added:
#### `new SourceMap(payload)`
Copy link
Member

@legendecas legendecas Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function signature should be updated to include the optional parameter too.

Suggested change
#### `new SourceMap(payload)`
#### `new SourceMap(payload[, lineLengths])`

As the generated code line lengths property is not part of the source maps spec, would it be more prudent to set the property with an option bag instead of a positional parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be more prudent to set the property with an option bag instead of a positional parameter?

No strong opinions here, but it's a pita to change this kind of thing later, so I'm feeling like it's probably a good idea, too. Anyone else wanna weight on it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bcoe would you mind taking a look at this PR? Thank you!


* `payload` {Object}
* `lineLengths` {number\[]}

Creates a new `sourceMap` instance.

Expand All @@ -287,6 +288,9 @@ Creates a new `sourceMap` instance.
* `mappings`: {string}
* `sourceRoot`: {string}

`lineLengths` is an array of the length of each line in the
generated code.

#### `sourceMap.payload`

* Returns: {Object}
Expand Down
16 changes: 15 additions & 1 deletion lib/internal/source_map/source_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,13 @@ class SourceMap {
#mappings = [];
#sources = {};
#sourceContentByURL = {};
#lineLengths = undefined;

/**
* @constructor
* @param {SourceMapV3} payload
*/
constructor(payload) {
constructor(payload, lineLengths) {
if (!base64Map) {
const base64Digits =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
Expand All @@ -140,6 +141,9 @@ class SourceMap {
}
this.#payload = cloneSourceMapV3(payload);
this.#parseMappingPayload();
if (ArrayIsArray(lineLengths) && lineLengths.length) {
this.#lineLengths = lineLengths;
}
}

/**
Expand All @@ -149,6 +153,16 @@ class SourceMap {
return cloneSourceMapV3(this.#payload);
}

/**
* @return {number[] | undefined} line lengths of generated source code
*/
get lineLengths() {
if (this.#lineLengths) {
return ArrayPrototypeSlice(this.#lineLengths);
}
return undefined;
}

#parseMappingPayload = () => {
if (this.#payload.sections) {
this.#parseSections(this.#payload.sections);
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/source_map/source_map_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ function findSourceMap(sourceURL) {
}
let sourceMap = entry.sourceMap;
if (sourceMap === undefined) {
sourceMap = new SourceMap(entry.data);
sourceMap = new SourceMap(entry.data, entry.lineLengths);
entry.sourceMap = sourceMap;
}
return sourceMap;
Expand Down
12 changes: 11 additions & 1 deletion test/parallel/test-source-map-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const { readFileSync } = require('fs');
assert.strictEqual(fileName, originalSource);
assert.strictEqual(lineNumber, 3);
assert.strictEqual(columnNumber, 6);
assert(Array.isArray(sourceMap.lineLengths));
assert(!sourceMap.lineLengths.some((len) => (typeof len !== 'number')));
}

// findSourceMap() can be used in Error.prepareStackTrace() to lookup
Expand Down Expand Up @@ -116,7 +118,10 @@ const { readFileSync } = require('fs');
const payload = JSON.parse(readFileSync(
require.resolve('../fixtures/source-map/disk.map'), 'utf8'
));
const sourceMap = new SourceMap(payload);
const lineLengths = readFileSync(
require.resolve('../fixtures/source-map/disk.map'), 'utf8'
).replace(/\n$/, '').split('\n').map((l) => l.length);
const sourceMap = new SourceMap(payload, lineLengths);
const {
originalLine,
originalColumn,
Expand All @@ -125,6 +130,11 @@ const { readFileSync } = require('fs');
assert.strictEqual(originalLine, 2);
assert.strictEqual(originalColumn, 4);
assert(originalSource.endsWith('disk.js'));
const sourceMapLineLengths = sourceMap.lineLengths;
for (let i = 0; i < sourceMapLineLengths.length; i++) {
assert.strictEqual(sourceMapLineLengths[i], lineLengths[i]);
}
assert.strictEqual(sourceMapLineLengths.length, lineLengths.length);
// The stored payload should be a clone:
assert.strictEqual(payload.mappings, sourceMap.payload.mappings);
assert.notStrictEqual(payload, sourceMap.payload);
Expand Down