From 5b76b990b9d5b444afb3b5d6b00700381eb51f75 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 --- lib/internal/modules/esm/formats.js | 4 ++++ lib/internal/modules/esm/get_format.js | 25 +++++++++++++++++++++++ lib/internal/modules/esm/resolve.js | 1 + test/es-module/test-esm-basic-imports.mjs | 7 +++++-- 4 files changed, 35 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..91f3c9edeb10f5 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -32,6 +32,10 @@ const protocolHandlers = ObjectAssign(ObjectCreate(null), { 'node:'() { return 'builtin'; }, }); +/** + * @param {URL} parsed + * @returns {string | null} + */ function getDataProtocolModuleFormat(parsed) { const { 1: mime } = RegExpPrototypeExec( /^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/, @@ -41,6 +45,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 +69,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 +85,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 +97,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 7751fa756e198b..5e00b53a65f7f5 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);