Skip to content

Commit 92f182b

Browse files
guybedfordBethGriggs
authored andcommitted
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

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

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

Lines changed: 6 additions & 5 deletions
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
constexportTarget=pattern ?
473-
StringPrototypeReplace(target,patternRegEx,subpath) :
473+
RegExpPrototypeSymbolReplace(patternRegEx,target,()=>subpath) :
474474
target+subpath;
475475
returnpackageResolve(exportTarget,packageJSONUrl,conditions);
476476
}
@@ -494,8 +494,8 @@ function resolvePackageTargetString(
494494
throwInvalidSubpath(match+subpath,packageJSONUrl,internal,base);
495495

496496
if(pattern)
497-
returnnewURL(StringPrototypeReplace(resolved.href,patternRegEx,
498-
subpath));
497+
returnnewURL(RegExpPrototypeSymbolReplace(patternRegEx,resolved.href,
498+
()=>subpath));
499499
returnnewURL(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,newRegExp(`\\${sep}`,'g'),'/');
942+
found=RegExpPrototypeSymbolReplace(newRegExp(`\\${sep}`,'g'),
943+
found,'/');
943944
}
944945
returnfound;
945946
}catch{

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

Lines changed: 2 additions & 0 deletions
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)