Skip to content

Commit 2c0f7d4

Browse files
guybedforddanielleadams
authored andcommitted
module: fix segment deprecation for imports field
PR-URL: #44883 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 57f507f commit 2c0f7d4

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

lib/internal/modules/esm/resolve.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,16 @@ function emitTrailingSlashPatternDeprecation(match, pjsonUrl, base) {
101101

102102
const doubleSlashRegEx = /[/\\][/\\]/;
103103

104-
function emitInvalidSegmentDeprecation(target, request, match, pjsonUrl, base) {
104+
function emitInvalidSegmentDeprecation(target, request, match, pjsonUrl, internal, base, isTarget) {
105105
if (!pendingDeprecation) { return; }
106106
const pjsonPath = fileURLToPath(pjsonUrl);
107-
const double = RegExpPrototypeExec(doubleSlashRegEx, target) !== null;
107+
const double = RegExpPrototypeExec(doubleSlashRegEx, isTarget ? target : request) !== null;
108108
process.emitWarning(
109109
`Use of deprecated ${double ? 'double slash' :
110110
'leading or trailing slash matching'} resolving "${target}" for module ` +
111111
`request "${request}" ${request !== match ? `matched to "${match}" ` : ''
112-
}in the "exports" field module resolution of the package at ${pjsonPath}${
113-
base ? ` imported from ${fileURLToPath(base)}` : ''}.`,
112+
}in the "${internal ? 'imports' : 'exports'}" field module resolution of the package at ${
113+
pjsonPath}${base ? ` imported from ${fileURLToPath(base)}` : ''}.`,
114114
'DeprecationWarning',
115115
'DEP0166'
116116
);
@@ -438,7 +438,7 @@ function resolvePackageTargetString(
438438
const resolvedTarget = pattern ?
439439
RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) :
440440
target;
441-
emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, base);
441+
emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, internal, base, true);
442442
}
443443
} else {
444444
throwInvalidPackageTarget(match, target, packageJSONUrl, internal, base);
@@ -461,7 +461,7 @@ function resolvePackageTargetString(
461461
const resolvedTarget = pattern ?
462462
RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) :
463463
target;
464-
emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, base);
464+
emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, internal, base, false);
465465
}
466466
} else {
467467
throwInvalidSubpath(request, match, packageJSONUrl, internal, base);

snapshot.blob

2.18 MB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Flags: --pending-deprecation
2+
import { mustCall } from '../common/index.mjs';
3+
import assert from 'assert';
4+
5+
let curWarning = 0;
6+
const expectedWarnings = [
7+
'Use of deprecated double slash',
8+
'Use of deprecated double slash',
9+
'./sub//null',
10+
'./sub/////null',
11+
'./sub//internal/test',
12+
'./sub//internal//test',
13+
'#subpath/////internal',
14+
'#subpath//asdf.asdf',
15+
'#subpath/as//df.asdf',
16+
'./sub//null',
17+
'./sub/////null',
18+
'./sub//internal/test',
19+
'./sub//internal//test',
20+
'#subpath/////internal',
21+
];
22+
23+
process.addListener('warning', mustCall((warning) => {
24+
assert(warning.stack.includes(expectedWarnings[curWarning++]), warning.stack);
25+
}, expectedWarnings.length));
26+
27+
await import('./test-esm-imports.mjs');

test/es-module/test-esm-imports.mjs

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const { requireImport, importImport } = importer;
2222
['#external/subpath/asdf.js', { default: 'asdf' }],
2323
// Trailing pattern imports
2424
['#subpath/asdf.asdf', { default: 'test' }],
25+
// Leading slash
26+
['#subpath//asdf.asdf', { default: 'test' }],
27+
// Double slash
28+
['#subpath/as//df.asdf', { default: 'test' }],
2529
]);
2630

2731
for (const [validSpecifier, expected] of internalImports) {

test/fixtures/es-modules/pkgimports/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"imports": {
3-
"#test": "./test.js",
43
"#branch": {
54
"import": "./importbranch.js",
65
"require": "./requirebranch.js"
@@ -26,6 +25,7 @@
2625
},
2726
"#subpath/nullshadow/*": [null],
2827
"#": "./test.js",
28+
"#*est": "./*est.js",
2929
"#/initialslash": "./test.js",
3030
"#notfound": "./notfound.js",
3131
"#encodedslash": "./..%2F/x.js",

0 commit comments

Comments
 (0)