Skip to content

Avoid match looping in sourceMappingURL tag detection by using negative lookahead #58704

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 13 additions & 20 deletions lib/internal/source_map/source_map_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
JSONParse,
ObjectFreeze,
RegExpPrototypeExec,
RegExpPrototypeSymbolMatch,
SafeMap,
StringPrototypeCodePointAt,
StringPrototypeIndexOf,
StringPrototypeSplit,
StringPrototypeSubstring,
} = primordials;

// See https://tc39.es/ecma426/ for SourceMap V3 specification.
Expand Down Expand Up @@ -37,8 +40,8 @@
// source url magic comments.
const generatedSourceMapCache = new SafeMap();
const kLeadingProtocol = /^\w+:\/\//;
const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=(?<sourceMappingURL>[^\s]+)/g;
const kSourceURLMagicComment = /\/[*/]#\s+sourceURL=(?<sourceURL>[^\s]+)/g;
const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=[^\s]+/g;
const kSourceURLMagicComment = /\/[*/]#\s+sourceURL=[^\s]+/g;

const { isAbsolute } = require('path');
const { fileURLToPath, pathToFileURL, URL, URLParse } = require('internal/url');
Expand Down Expand Up @@ -99,17 +102,12 @@
* @returns {string | null} source url or null if not present
*/
function extractSourceURLMagicComment(content) {
let match;
let matchSourceURL;
// A while loop is used here to get the last occurrence of sourceURL.
// This is needed so that we don't match sourceURL in string literals.
while ((match = RegExpPrototypeExec(kSourceURLMagicComment, content))) {
matchSourceURL = match;
}
if (matchSourceURL == null) {
const matches = RegExpPrototypeSymbolMatch(kSourceURLMagicComment, content);

Check failure on line 105 in lib/internal/source_map/source_map_cache.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

RegExpPrototypeSymbolMatch looks up the "exec" property of `this` value
if (matches == null || matches.length === 0) {
return null;
}
let sourceURL = matchSourceURL.groups.sourceURL;
const match = matches[matches.length - 1];
let sourceURL = StringPrototypeSubstring(match, StringPrototypeIndexOf(match, '=') + 1);
if (sourceURL != null && RegExpPrototypeExec(kLeadingProtocol, sourceURL) === null) {
sourceURL = pathToFileURL(sourceURL).href;
}
Expand All @@ -125,17 +123,12 @@
* @returns {string | null} source map url or null if not present
*/
function extractSourceMapURLMagicComment(content) {
let match;
let lastMatch;
// A while loop is used here to get the last occurrence of sourceMappingURL.
// This is needed so that we don't match sourceMappingURL in string literals.
while ((match = RegExpPrototypeExec(kSourceMappingURLMagicComment, content))) {
lastMatch = match;
}
if (lastMatch == null) {
const matches = RegExpPrototypeSymbolMatch(kSourceMappingURLMagicComment, content);

Check failure on line 126 in lib/internal/source_map/source_map_cache.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

RegExpPrototypeSymbolMatch looks up the "exec" property of `this` value
if (matches == null || matches.length === 0) {
return null;
}
return lastMatch.groups.sourceMappingURL;
const match = matches[matches.length - 1];
return StringPrototypeSubstring(match, StringPrototypeIndexOf(match, '=') + 1);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/source-map/output/source_map_eval.snapshot
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
*/synthesized/workspace/tabs-source-url.coffee:26
*/synthesized/workspace/tabs-source-url.coffee:32
alert "I knew it!"
^


ReferenceError: alert is not defined
at Object.eval (*/synthesized/workspace/tabs-source-url.coffee:26:2)
at Object.eval (*/synthesized/workspace/tabs-source-url.coffee:32:2)
at eval (*/synthesized/workspace/tabs-source-url.coffee:1:14)
at Object.<anonymous> (*/test/fixtures/source-map/output/source_map_eval.js:11:1)

Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/source-map/tabs-source-url.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ math =
race = (winner, runners...) ->
print winner, runners

# Multiple sourceMappingURL comments
## sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFicy1zb3VyY2UtdXJsLmpzIiwic

# sourceMappingURL inside strings
sourceMappingURL = "//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFicy1zb3VyY2UtdXJsLmpzIiwic"

# Existence:
if true
alert "I knew it!"
Expand Down
10 changes: 8 additions & 2 deletions test/fixtures/source-map/tabs-source-url.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading