From 2e42ccf1a51b00bfb77d81da6e27f77c55974b4b Mon Sep 17 00:00:00 2001 From: Bradley Farias Date: Fri, 11 Mar 2022 11:20:57 -0600 Subject: [PATCH] esm: improve typings and code coverage PR-URL: https://github.com/nodejs/node/pull/42305 Reviewed-By: Geoffrey Booth Reviewed-By: James M Snell Reviewed-By: Mestery Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Jacob Smith Reviewed-By: Darshan Sen --- lib/internal/modules/esm/formats.js | 4 ++++ lib/internal/modules/esm/get_format.js | 21 +++++++++++++++++++++ lib/internal/modules/esm/resolve.js | 1 + test/es-module/test-esm-basic-imports.mjs | 7 +++++-- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/internal/modules/esm/formats.js b/lib/internal/modules/esm/formats.js index 6f145931a1eed4..f9da01402e7f62 100644 --- a/lib/internal/modules/esm/formats.js +++ b/lib/internal/modules/esm/formats.js @@ -29,6 +29,10 @@ if (experimentalWasmModules) { extensionFormatMap['.wasm'] = legacyExtensionFormatMap['.wasm'] = 'wasm'; } +/** + * @param {string} mime + * @returns {string | null} + */ function mimeToFormat(mime) { if ( RegExpPrototypeTest( diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index 825fbae3f5931f..ec3ab0c6c68527 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -41,6 +41,12 @@ function getDataProtocolModuleFormat(parsed) { return mimeToFormat(mime); } +/** + * @param {URL} url + * @param {{parentURL: string}} context + * @param {boolean} ignoreErrors + * @returns {string} + */ function getFileProtocolModuleFormat(url, context, ignoreErrors) { const ext = extname(url.pathname); if (ext === '.js') { @@ -59,6 +65,11 @@ function getFileProtocolModuleFormat(url, context, ignoreErrors) { return getLegacyExtensionFormat(ext) ?? null; } +/** + * @param {URL} url + * @param {{parentURL: string}} context + * @returns {Promise | undefined} only works when enabled + */ function getHttpProtocolModuleFormat(url, context) { if (experimentalNetworkImports) { return PromisePrototypeThen( @@ -70,6 +81,11 @@ function getHttpProtocolModuleFormat(url, context) { } } +/** + * @param {URL | URL['href']} url + * @param {{parentURL: string}} context + * @returns {Promise | string | undefined} only works when enabled + */ function defaultGetFormatWithoutErrors(url, context) { const parsed = new URL(url); if (!ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol)) @@ -77,6 +93,11 @@ function defaultGetFormatWithoutErrors(url, context) { return protocolHandlers[parsed.protocol](parsed, context, true); } +/** + * @param {URL | URL['href']} url + * @param {{parentURL: string}} context + * @returns {Promise | string | undefined} only works when enabled + */ function defaultGetFormat(url, context) { const parsed = new URL(url); return ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol) ? diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index d870907d6e0f6c..4ac3c7a9763fe8 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -79,6 +79,7 @@ const DEFAULT_CONDITIONS_SET = new SafeSet(DEFAULT_CONDITIONS); * @typedef {string | string[] | Record} Exports * @typedef {'module' | 'commonjs'} PackageType * @typedef {{ + * pjsonPath: string, * exports?: ExportConfig; * name?: string; * main?: string; diff --git a/test/es-module/test-esm-basic-imports.mjs b/test/es-module/test-esm-basic-imports.mjs index 5009fbadb39657..173f80ac955ff1 100644 --- a/test/es-module/test-esm-basic-imports.mjs +++ b/test/es-module/test-esm-basic-imports.mjs @@ -1,7 +1,10 @@ import '../common/index.mjs'; import assert from 'assert'; import ok from '../fixtures/es-modules/test-esm-ok.mjs'; -import okShebang from './test-esm-shebang.mjs'; +import * as okShebangNs from './test-esm-shebang.mjs'; +// encode the '.' +import * as okShebangPercentNs from './test-esm-shebang%2emjs'; assert(ok); -assert(okShebang); +assert(okShebangNs.default); +assert.strict.equal(okShebangNs, okShebangPercentNs);