From e6702345554d617012856d254fb6909bdacb734c Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Mon, 3 Oct 2022 15:32:04 -0700 Subject: [PATCH] module: fix invalid segment deprecation for imports field --- lib/internal/modules/esm/resolve.js | 12 ++++----- .../test-esm-imports-deprecations.mjs | 27 +++++++++++++++++++ test/es-module/test-esm-imports.mjs | 4 +++ .../es-modules/pkgimports/package.json | 2 +- 4 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 test/es-module/test-esm-imports-deprecations.mjs diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 91ac345e96df86..0359bc04dd9d0c 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -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' ); @@ -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); @@ -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); diff --git a/test/es-module/test-esm-imports-deprecations.mjs b/test/es-module/test-esm-imports-deprecations.mjs new file mode 100644 index 00000000000000..775152f29ecc68 --- /dev/null +++ b/test/es-module/test-esm-imports-deprecations.mjs @@ -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'); diff --git a/test/es-module/test-esm-imports.mjs b/test/es-module/test-esm-imports.mjs index a42738646fbaa8..2720f47974128a 100644 --- a/test/es-module/test-esm-imports.mjs +++ b/test/es-module/test-esm-imports.mjs @@ -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) { diff --git a/test/fixtures/es-modules/pkgimports/package.json b/test/fixtures/es-modules/pkgimports/package.json index 6971e8b6fbcdea..dbbbcd1ab01ea1 100644 --- a/test/fixtures/es-modules/pkgimports/package.json +++ b/test/fixtures/es-modules/pkgimports/package.json @@ -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",