Skip to content

Commit

Permalink
module: fix segment deprecation for imports field
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
guybedford authored and danielleadams committed Jan 3, 2023
1 parent 57f507f commit 2c0f7d4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
12 changes: 6 additions & 6 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ function emitTrailingSlashPatternDeprecation(match, pjsonUrl, base) {

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

function emitInvalidSegmentDeprecation(target, request, match, pjsonUrl, base) {
function emitInvalidSegmentDeprecation(target, request, match, pjsonUrl, internal, base, isTarget) {
if (!pendingDeprecation) { return; }
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 @@ -438,7 +438,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 @@ -461,7 +461,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
Binary file added snapshot.blob
Binary file not shown.
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

0 comments on commit 2c0f7d4

Please sign in to comment.