Skip to content

Commit e17db8f

Browse files
bmeckbengl
authored andcommitted
esm: improve typings and code coverage
PR-URL: #42305 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Mestery <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]> Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Darshan Sen <[email protected]>
1 parent 08e2d8a commit e17db8f

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

lib/internal/modules/esm/formats.js

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ if (experimentalWasmModules) {
2929
extensionFormatMap['.wasm'] = legacyExtensionFormatMap['.wasm'] = 'wasm';
3030
}
3131

32+
/**
33+
* @param {string} mime
34+
* @returns {string | null}
35+
*/
3236
function mimeToFormat(mime) {
3337
if (
3438
RegExpPrototypeTest(

lib/internal/modules/esm/get_format.js

+25
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ const protocolHandlers = ObjectAssign(ObjectCreate(null), {
3232
'node:'() { return 'builtin'; },
3333
});
3434

35+
/**
36+
* @param {URL} parsed
37+
* @returns {string | null}
38+
*/
3539
function getDataProtocolModuleFormat(parsed) {
3640
const { 1: mime } = RegExpPrototypeExec(
3741
/^([^/]+\/[^;,]+)(?:[^,]*?)(;base64)?,/,
@@ -41,6 +45,12 @@ function getDataProtocolModuleFormat(parsed) {
4145
return mimeToFormat(mime);
4246
}
4347

48+
/**
49+
* @param {URL} url
50+
* @param {{parentURL: string}} context
51+
* @param {boolean} ignoreErrors
52+
* @returns {string}
53+
*/
4454
function getFileProtocolModuleFormat(url, context, ignoreErrors) {
4555
const ext = extname(url.pathname);
4656
if (ext === '.js') {
@@ -59,6 +69,11 @@ function getFileProtocolModuleFormat(url, context, ignoreErrors) {
5969
return getLegacyExtensionFormat(ext) ?? null;
6070
}
6171

72+
/**
73+
* @param {URL} url
74+
* @param {{parentURL: string}} context
75+
* @returns {Promise<string> | undefined} only works when enabled
76+
*/
6277
function getHttpProtocolModuleFormat(url, context) {
6378
if (experimentalNetworkImports) {
6479
return PromisePrototypeThen(
@@ -70,13 +85,23 @@ function getHttpProtocolModuleFormat(url, context) {
7085
}
7186
}
7287

88+
/**
89+
* @param {URL | URL['href']} url
90+
* @param {{parentURL: string}} context
91+
* @returns {Promise<string> | string | undefined} only works when enabled
92+
*/
7393
function defaultGetFormatWithoutErrors(url, context) {
7494
const parsed = new URL(url);
7595
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol))
7696
return null;
7797
return protocolHandlers[parsed.protocol](parsed, context, true);
7898
}
7999

100+
/**
101+
* @param {URL | URL['href']} url
102+
* @param {{parentURL: string}} context
103+
* @returns {Promise<string> | string | undefined} only works when enabled
104+
*/
80105
function defaultGetFormat(url, context) {
81106
const parsed = new URL(url);
82107
return ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol) ?

lib/internal/modules/esm/resolve.js

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ const DEFAULT_CONDITIONS_SET = new SafeSet(DEFAULT_CONDITIONS);
7979
* @typedef {string | string[] | Record<string, unknown>} Exports
8080
* @typedef {'module' | 'commonjs'} PackageType
8181
* @typedef {{
82+
* pjsonPath: string,
8283
* exports?: ExportConfig;
8384
* name?: string;
8485
* main?: string;
+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import '../common/index.mjs';
22
import assert from 'assert';
33
import ok from '../fixtures/es-modules/test-esm-ok.mjs';
4-
import okShebang from './test-esm-shebang.mjs';
4+
import * as okShebangNs from './test-esm-shebang.mjs';
5+
// encode the '.'
6+
import * as okShebangPercentNs from './test-esm-shebang%2emjs';
57

68
assert(ok);
7-
assert(okShebang);
9+
assert(okShebangNs.default);
10+
assert.strict.equal(okShebangNs, okShebangPercentNs);

0 commit comments

Comments
 (0)