Skip to content

Commit

Permalink
module: deprecate trailing slash pattern mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Sep 8, 2021
1 parent 861c3e0 commit 819aa31
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 1 deletion.
7 changes: 7 additions & 0 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2814,6 +2814,13 @@ Type: Documentation-only (supports [`--pending-deprecation`][])
The `'hash'` and `'mgf1Hash'` options are replaced with `'hashAlgorithm'`
and `'mgf1HashAlgorithm'`.

### DEP0155: Trailing slashes in pattern specifier resolutions

Type: Runtime

The remapping of specifiers ending in `"/"` like `import 'pkg/x/'` is deprecated
for package `"exports"` and `"imports"` pattern resolutions.

[Legacy URL API]: url.md#legacy-url-api
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
[RFC 6066]: https://tools.ietf.org/html/rfc6066#section-3
Expand Down
2 changes: 2 additions & 0 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,8 @@ _isImports_, _conditions_)
> _expansionKey_ up to but excluding the first _"*"_ character.
> 1. If _patternBase_ is not **null** and _matchKey_ starts with but is not
> equal to _patternBase_, then
> 1. If _matchKey_ ends with _"/"_, throw an _Invalid Module Specifier_
> error.
> 1. Let _patternTrailer_ be the substring of _expansionKey_ from the
> index after the first _"*"_ character.
> 1. If _patternTrailer_ has zero length, or if _matchKey_ ends with
Expand Down
18 changes: 18 additions & 0 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,21 @@ function emitFolderMapDeprecation(match, pjsonUrl, isExports, base) {
);
}

function emitTrailingSlashPatternDeprecation(match, pjsonUrl, isExports, base) {
const pjsonPath = fileURLToPath(pjsonUrl);
if (emittedPackageWarnings.has(pjsonPath + '|' + match))
return;
emittedPackageWarnings.add(pjsonPath + '|' + match);
process.emitWarning(
`Use of deprecated trailing slash pattern mapping "${match}" in the ${
isExports ? '"exports"' : '"imports"'} field module resolution of the ` +
`package at ${pjsonPath}${base ? ` imported from ${fileURLToPath(base)}` :
''}. Mapping specifiers ending in "/" is no longer supported.`,
'DeprecationWarning',
'DEP0155'
);
}

/**
* @param {URL} url
* @param {URL} packageJSONUrl
Expand Down Expand Up @@ -630,6 +645,9 @@ function packageExportsResolve(
if (patternIndex !== -1 &&
StringPrototypeStartsWith(packageSubpath,
StringPrototypeSlice(key, 0, patternIndex))) {
if (StringPrototypeEndsWith(packageSubpath, '/'))
emitTrailingSlashPatternDeprecation(packageSubpath, packageJSONUrl,
true, base);
const patternTrailer = StringPrototypeSlice(key, patternIndex + 1);
if (packageSubpath.length >= key.length &&
StringPrototypeEndsWith(packageSubpath, patternTrailer) &&
Expand Down
6 changes: 6 additions & 0 deletions test/es-module/test-esm-exports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,19 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
['pkgexports/dir2/dir2/trailer', { default: 'index' }],
['pkgexports/a/dir1/dir1', { default: 'main' }],
['pkgexports/a/b/dir1/dir1', { default: 'main' }],

// Deprecated:
['pkgexports/trailing-pattern-slash/',
{ default: 'trailing-pattern-slash' }],
]);

if (isRequire) {
validSpecifiers.set('pkgexports/subpath/file', { default: 'file' });
validSpecifiers.set('pkgexports/subpath/dir1', { default: 'main' });
// Deprecated:
validSpecifiers.set('pkgexports/subpath/dir1/', { default: 'main' });
validSpecifiers.set('pkgexports/subpath/dir2', { default: 'index' });
// Deprecated:
validSpecifiers.set('pkgexports/subpath/dir2/', { default: 'index' });
} else {
// No exports or main field
Expand Down
5 changes: 5 additions & 0 deletions test/es-module/test-esm-local-deprecations.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ const selfDeprecatedFolders =
const deprecatedFoldersIgnore =
fixtures.path('/es-modules/deprecated-folders-ignore/main.js');

const deprecatedTrailingSlashPattern =
fixtures.path('/es-modules/pattern-trailing-slash.mjs');

const expectedWarnings = [
'"./" in the "exports" field',
'"#self/" in the "imports" field',
'"./folder/" in the "exports" field',
'"./trailing-pattern-slash/" in the "exports" field',
];

process.addListener('warning', (warning) => {
Expand All @@ -28,5 +32,6 @@ process.on('exit', () => {
(async () => {
await import(pathToFileURL(selfDeprecatedFolders));
await import(pathToFileURL(deprecatedFoldersIgnore));
await import(pathToFileURL(deprecatedTrailingSlashPattern));
})()
.catch((err) => console.error(err));
1 change: 1 addition & 0 deletions test/fixtures/es-modules/pattern-trailing-slash.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'pkgexports/trailing-pattern-slash/';
3 changes: 2 additions & 1 deletion test/fixtures/node_modules/pkgexports/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 819aa31

Please sign in to comment.