module: deprecate trailing slash pattern mappings

PR-URL: https://github.com/nodejs/node/pull/40039
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com>
This commit is contained in:
Guy Bedford committed 2021-09-14 23:58:56 -07:00
1 parent 798c0cb6bc
commit 7216fb118d
9 files changed
+57 -1

No files matched your search

+14
View File
@@ -2814,6 +2814,20 @@ Type: Documentation-only (supports [`--pending-deprecation`][])
The `'hash'` and `'mgf1Hash'` options are replaced with `'hashAlgorithm'`
and `'mgf1HashAlgorithm'`.
### DEP0155: Trailing slashes in pattern specifier resolutions
<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/40039
description: Documentation-only deprecation
with `--pending-deprecation` support.
-->
Type: Documentation-only (supports [`--pending-deprecation`][])
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
+2
View File
@@ -1205,6 +1205,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
+20
View File
@@ -40,6 +40,7 @@ const { sep, relative, resolve } = require('path');
const preserveSymlinks = getOptionValue('--preserve-symlinks');
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
const typeFlag = getOptionValue('--input-type');
const pendingDeprecation = getOptionValue('--pending-deprecation');
const { URL, pathToFileURL, fileURLToPath } = require('internal/url');
const {
ERR_INPUT_TYPE_NOT_ALLOWED,
@@ -106,6 +107,22 @@ function emitFolderMapDeprecation(match, pjsonUrl, isExports, base) {
);
}
function emitTrailingSlashPatternDeprecation(match, pjsonUrl, isExports, base) {
if (!pendingDeprecation) return;
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
@@ -639,6 +656,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) &&
@@ -1,3 +1,4 @@
// Flags: --pending-deprecation
import { mustCall } from '../common/index.mjs';
import assert from 'assert';
@@ -5,7 +6,10 @@ let curWarning = 0;
const expectedWarnings = [
'"./sub/"',
'"./fallbackdir/"',
'"./trailing-pattern-slash/"',
'"./subpath/"',
'"./subpath/dir1/"',
'"./subpath/dir2/"',
'no_exports',
'default_index',
];
+6
View File
@@ -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
@@ -1,3 +1,5 @@
// Flags: --pending-deprecation
import '../common/index.mjs';
import assert from 'assert';
import fixtures from '../common/fixtures.js';
@@ -9,10 +11,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) => {
@@ -28,5 +34,6 @@ process.on('exit', () => {
(async () => {
await import(pathToFileURL(selfDeprecatedFolders));
await import(pathToFileURL(deprecatedFoldersIgnore));
await import(pathToFileURL(deprecatedTrailingSlashPattern));
})()
.catch((err) => console.error(err));
+1
View File
@@ -0,0 +1 @@
import 'pkgexports/trailing-pattern-slash/';
+2 -1
View File
@@ -61,6 +61,7 @@
"./subpath/": "./subpath/",
"./subpath/sub-*": "./subpath/dir1/*.js",
"./subpath/sub-*.js": "./subpath/dir1/*.js",
"./features/*": "./subpath/*/*.js"
"./features/*": "./subpath/*/*.js",
"./trailing-pattern-slash*": "./trailing-pattern-slash*index.js"
}
}
@@ -0,0 +1 @@
module.exports = 'trailing-pattern-slash';