Skip to content

Commit 92f182b

Browse files
guybedfordBethGriggs
authored andcommittedSep 21, 2021
module: fix $ pattern replacements
PR-URL: #40044 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Zeyu Yang <himself65@outlook.com>
1 parent 4367a61 commit 92f182b

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed
 

‎lib/internal/modules/esm/resolve.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
ObjectGetOwnPropertyNames,
1111
ObjectPrototypeHasOwnProperty,
1212
RegExp,
13+
RegExpPrototypeSymbolReplace,
1314
RegExpPrototypeTest,
1415
SafeMap,
1516
SafeSet,
@@ -18,7 +19,6 @@ const {
1819
StringPrototypeIncludes,
1920
StringPrototypeIndexOf,
2021
StringPrototypeLastIndexOf,
21-
StringPrototypeReplace,
2222
StringPrototypeSlice,
2323
StringPrototypeSplit,
2424
StringPrototypeStartsWith,
@@ -470,7 +470,7 @@ function resolvePackageTargetString(
470470
} catch {}
471471
if (!isURL) {
472472
const exportTarget = pattern ?
473-
StringPrototypeReplace(target, patternRegEx, subpath) :
473+
RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) :
474474
target + subpath;
475475
return packageResolve(exportTarget, packageJSONUrl, conditions);
476476
}
@@ -494,8 +494,8 @@ function resolvePackageTargetString(
494494
throwInvalidSubpath(match + subpath, packageJSONUrl, internal, base);
495495

496496
if (pattern)
497-
return new URL(StringPrototypeReplace(resolved.href, patternRegEx,
498-
subpath));
497+
return new URL(RegExpPrototypeSymbolReplace(patternRegEx, resolved.href,
498+
() => subpath));
499499
return new URL(subpath, resolved);
500500
}
501501

@@ -939,7 +939,8 @@ function resolveAsCommonJS(specifier, parentURL) {
939939
// Normalize the path separator to give a valid suggestion
940940
// on Windows
941941
if (process.platform === 'win32') {
942-
found = StringPrototypeReplace(found, new RegExp(`\\${sep}`, 'g'), '/');
942+
found = RegExpPrototypeSymbolReplace(new RegExp(`\\${sep}`, 'g'),
943+
found, '/');
943944
}
944945
return found;
945946
} catch {

‎test/es-module/test-esm-exports.mjs

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
156156
['pkgexports/no-ext', `pkgexports${sep}asdf`],
157157
// Pattern specificity
158158
['pkgexports/dir2/trailer', `subpath${sep}dir2.js`],
159+
// Pattern double $$ escaping!
160+
['pkgexports/a/$$', `subpath${sep}$$.js`],
159161
]);
160162

161163
if (!isRequire) {

0 commit comments

Comments
 (0)
Please sign in to comment.