Skip to content

Commit 71ac3c4

Browse files
committed
lib: simplify match last sourceMappingURL code
1 parent 5fe7800 commit 71ac3c4

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

lib/internal/source_map/source_map_cache.js

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ const {
77
RegExpPrototypeExec,
88
SafeMap,
99
StringPrototypeCodePointAt,
10+
StringPrototypeIndexOf,
11+
StringPrototypeMatch,
1012
StringPrototypeSplit,
13+
StringPrototypeSubstring,
1114
} = primordials;
1215

1316
// See https://tc39.es/ecma426/ for SourceMap V3 specification.
@@ -37,8 +40,8 @@ const getModuleSourceMapCache = getLazy(() => {
3740
// source url magic comments.
3841
const generatedSourceMapCache = new SafeMap();
3942
const kLeadingProtocol = /^\w+:\/\//;
40-
const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=(?<sourceMappingURL>[^\s]+)/g;
41-
const kSourceURLMagicComment = /\/[*/]#\s+sourceURL=(?<sourceURL>[^\s]+)/g;
43+
const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=[^\s]+/g;
44+
const kSourceURLMagicComment = /\/[*/]#\s+sourceURL=[^\s]+/g;
4245

4346
const { isAbsolute } = require('path');
4447
const { fileURLToPath, pathToFileURL, URL, URLParse } = require('internal/url');
@@ -99,17 +102,11 @@ function setSourceMapsSupport(enabled, options = kEmptyObject) {
99102
* @returns {string | null} source url or null if not present
100103
*/
101104
function extractSourceURLMagicComment(content) {
102-
let match;
103-
let matchSourceURL;
104-
// A while loop is used here to get the last occurrence of sourceURL.
105-
// This is needed so that we don't match sourceURL in string literals.
106-
while ((match = RegExpPrototypeExec(kSourceURLMagicComment, content))) {
107-
matchSourceURL = match;
108-
}
109-
if (matchSourceURL == null) {
110-
return null;
105+
const match = StringPrototypeMatch(content, kSourceURLMagicComment)?.at(-1);
106+
if (match == null) {
107+
return null;
111108
}
112-
let sourceURL = matchSourceURL.groups.sourceURL;
109+
let sourceURL = StringPrototypeSubstring(match, StringPrototypeIndexOf(match, '=') + 1);
113110
if (sourceURL != null && RegExpPrototypeExec(kLeadingProtocol, sourceURL) === null) {
114111
sourceURL = pathToFileURL(sourceURL).href;
115112
}
@@ -125,17 +122,11 @@ function extractSourceURLMagicComment(content) {
125122
* @returns {string | null} source map url or null if not present
126123
*/
127124
function extractSourceMapURLMagicComment(content) {
128-
let match;
129-
let lastMatch;
130-
// A while loop is used here to get the last occurrence of sourceMappingURL.
131-
// This is needed so that we don't match sourceMappingURL in string literals.
132-
while ((match = RegExpPrototypeExec(kSourceMappingURLMagicComment, content))) {
133-
lastMatch = match;
134-
}
135-
if (lastMatch == null) {
136-
return null;
125+
const match = StringPrototypeMatch(content, kSourceMappingURLMagicComment)?.at(-1);
126+
if (match == null) {
127+
return null;
137128
}
138-
return lastMatch.groups.sourceMappingURL;
129+
return StringPrototypeSubstring(match, StringPrototypeIndexOf(match, '=') + 1);
139130
}
140131

141132
/**

test/fixtures/source-map/output/source_map_eval.snapshot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
*/synthesized/workspace/tabs-source-url.coffee:26
1+
*/synthesized/workspace/tabs-source-url.coffee:32
22
alert "I knew it!"
33
^
44

55

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

test/fixtures/source-map/tabs-source-url.coffee

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ math =
2121
race = (winner, runners...) ->
2222
print winner, runners
2323

24+
# Multiple sourceMappingURL comments
25+
## sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFicy1zb3VyY2UtdXJsLmpzIiwic
26+
27+
# sourceMappingURL inside strings
28+
sourceMappingURL = "//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGFicy1zb3VyY2UtdXJsLmpzIiwic"
29+
2430
# Existence:
2531
if true
2632
alert "I knew it!"

test/fixtures/source-map/tabs-source-url.js

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)