module: fix segment deprecation for imports field

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Guy Bedford 2022-10-03 15:32:04 -07:00
parent 03fb789fb9
commit 15f10515e3
4 changed files with 38 additions and 7 deletions

View file

@ -98,15 +98,15 @@ function emitTrailingSlashPatternDeprecation(match, pjsonUrl, base) {
const doubleSlashRegEx = /[/\\][/\\]/;
function emitInvalidSegmentDeprecation(target, request, match, pjsonUrl, base) {
function emitInvalidSegmentDeprecation(target, request, match, pjsonUrl, internal, base, isTarget) {
const pjsonPath = fileURLToPath(pjsonUrl);
const double = RegExpPrototypeExec(doubleSlashRegEx, target) !== null;
const double = RegExpPrototypeExec(doubleSlashRegEx, isTarget ? target : request) !== null;
process.emitWarning(
`Use of deprecated ${double ? 'double slash' :
'leading or trailing slash matching'} resolving "${target}" for module ` +
`request "${request}" ${request !== match ? `matched to "${match}" ` : ''
}in the "exports" field module resolution of the package at ${pjsonPath}${
base ? ` imported from ${fileURLToPath(base)}` : ''}.`,
}in the "${internal ? 'imports' : 'exports'}" field module resolution of the package at ${
pjsonPath}${base ? ` imported from ${fileURLToPath(base)}` : ''}.`,
'DeprecationWarning',
'DEP0166'
);
@ -372,7 +372,7 @@ function resolvePackageTargetString(
const resolvedTarget = pattern ?
RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) :
target;
emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, base);
emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, internal, base, true);
}
} else {
throwInvalidPackageTarget(match, target, packageJSONUrl, internal, base);
@ -395,7 +395,7 @@ function resolvePackageTargetString(
const resolvedTarget = pattern ?
RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) :
target;
emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, base);
emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, internal, base, false);
}
} else {
throwInvalidSubpath(request, match, packageJSONUrl, internal, base);

View file

@ -0,0 +1,27 @@
// Flags: --pending-deprecation
import { mustCall } from '../common/index.mjs';
import assert from 'assert';
let curWarning = 0;
const expectedWarnings = [
'Use of deprecated double slash',
'Use of deprecated double slash',
'./sub//null',
'./sub/////null',
'./sub//internal/test',
'./sub//internal//test',
'#subpath/////internal',
'#subpath//asdf.asdf',
'#subpath/as//df.asdf',
'./sub//null',
'./sub/////null',
'./sub//internal/test',
'./sub//internal//test',
'#subpath/////internal',
];
process.addListener('warning', mustCall((warning) => {
assert(warning.stack.includes(expectedWarnings[curWarning++]), warning.stack);
}, expectedWarnings.length));
await import('./test-esm-imports.mjs');

View file

@ -22,6 +22,10 @@ const { requireImport, importImport } = importer;
['#external/subpath/asdf.js', { default: 'asdf' }],
// Trailing pattern imports
['#subpath/asdf.asdf', { default: 'test' }],
// Leading slash
['#subpath//asdf.asdf', { default: 'test' }],
// Double slash
['#subpath/as//df.asdf', { default: 'test' }],
]);
for (const [validSpecifier, expected] of internalImports) {

View file

@ -1,6 +1,5 @@
{
"imports": {
"#test": "./test.js",
"#branch": {
"import": "./importbranch.js",
"require": "./requirebranch.js"
@ -26,6 +25,7 @@
},
"#subpath/nullshadow/*": [null],
"#": "./test.js",
"#*est": "./*est.js",
"#/initialslash": "./test.js",
"#notfound": "./notfound.js",
"#encodedslash": "./..%2F/x.js",