Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: fix invalid segment deprecation for imports field #44883

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,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'
);
Expand Down Expand Up @@ -436,7 +436,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);
Expand All @@ -459,7 +459,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);
Expand Down
27 changes: 27 additions & 0 deletions test/es-module/test-esm-imports-deprecations.mjs
Original file line number Diff line number Diff line change
@@ -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');
4 changes: 4 additions & 0 deletions test/es-module/test-esm-imports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/es-modules/pkgimports/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"imports": {
"#test": "./test.js",
"#branch": {
"import": "./importbranch.js",
"require": "./requirebranch.js"
Expand All @@ -26,6 +25,7 @@
},
"#subpath/nullshadow/*": [null],
"#": "./test.js",
"#*est": "./*est.js",
"#/initialslash": "./test.js",
"#notfound": "./notfound.js",
"#encodedslash": "./..%2F/x.js",
Expand Down