Skip to content

Commit 15f1051

Browse files
committed
module: fix segment deprecation for imports field
PR-URL: #44883 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 03fb789 commit 15f1051

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

lib/internal/modules/esm/resolve.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ function emitTrailingSlashPatternDeprecation(match, pjsonUrl, base) {
9898

9999
const doubleSlashRegEx = /[/\\][/\\]/;
100100

101-
function emitInvalidSegmentDeprecation(target, request, match, pjsonUrl, base) {
101+
function emitInvalidSegmentDeprecation(target, request, match, pjsonUrl, internal, base, isTarget) {
102102
const pjsonPath = fileURLToPath(pjsonUrl);
103-
const double = RegExpPrototypeExec(doubleSlashRegEx, target) !== null;
103+
const double = RegExpPrototypeExec(doubleSlashRegEx, isTarget ? target : request) !== null;
104104
process.emitWarning(
105105
`Use of deprecated ${double ? 'double slash' :
106106
'leading or trailing slash matching'} resolving "${target}" for module ` +
107107
`request "${request}" ${request !== match ? `matched to "${match}" ` : ''
108-
}in the "exports" field module resolution of the package at ${pjsonPath}${
109-
base ? ` imported from ${fileURLToPath(base)}` : ''}.`,
108+
}in the "${internal ? 'imports' : 'exports'}" field module resolution of the package at ${
109+
pjsonPath}${base ? ` imported from ${fileURLToPath(base)}` : ''}.`,
110110
'DeprecationWarning',
111111
'DEP0166'
112112
);
@@ -372,7 +372,7 @@ function resolvePackageTargetString(
372372
const resolvedTarget = pattern ?
373373
RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) :
374374
target;
375-
emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, base);
375+
emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, internal, base, true);
376376
}
377377
} else {
378378
throwInvalidPackageTarget(match, target, packageJSONUrl, internal, base);
@@ -395,7 +395,7 @@ function resolvePackageTargetString(
395395
const resolvedTarget = pattern ?
396396
RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) :
397397
target;
398-
emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, base);
398+
emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, internal, base, false);
399399
}
400400
} else {
401401
throwInvalidSubpath(request, match, packageJSONUrl, internal, base);
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)