Skip to content

Commit 78040e3

Browse files
committed
lib: simplify match last sourceMappingURL code
1 parent 5fe7800 commit 78040e3

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

lib/internal/source_map/source_map_cache.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ const {
55
JSONParse,
66
ObjectFreeze,
77
RegExpPrototypeExec,
8+
RegExpPrototypeSymbolMatch,
89
SafeMap,
910
StringPrototypeCodePointAt,
11+
StringPrototypeIndexOf,
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,12 @@ 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) {
105+
const matches = RegExpPrototypeSymbolMatch(kSourceURLMagicComment, content);
106+
if (matches == null || matches.length === 0) {
110107
return null;
111108
}
112-
let sourceURL = matchSourceURL.groups.sourceURL;
109+
const match = matches[matches.length - 1];
110+
let sourceURL = StringPrototypeSubstring(match, StringPrototypeIndexOf(match, '=') + 1);
113111
if (sourceURL != null && RegExpPrototypeExec(kLeadingProtocol, sourceURL) === null) {
114112
sourceURL = pathToFileURL(sourceURL).href;
115113
}
@@ -125,17 +123,12 @@ function extractSourceURLMagicComment(content) {
125123
* @returns {string | null} source map url or null if not present
126124
*/
127125
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) {
126+
const matches = RegExpPrototypeSymbolMatch(kSourceMappingURLMagicComment, content);
127+
if (matches == null || matches.length === 0) {
136128
return null;
137129
}
138-
return lastMatch.groups.sourceMappingURL;
130+
const match = matches[matches.length - 1];
131+
return StringPrototypeSubstring(match, StringPrototypeIndexOf(match, '=') + 1);
139132
}
140133

141134
/**

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)