-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
esm: improve typings and code coverage #42305
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<string> | undefined} only works when enabled | ||
*/ | ||
function getHttpProtocolModuleFormat(url, context) { | ||
if (experimentalNetworkImports) { | ||
return PromisePrototypeThen( | ||
|
@@ -70,13 +85,23 @@ function getHttpProtocolModuleFormat(url, context) { | |
} | ||
} | ||
|
||
/** | ||
* @param {URL | URL['href']} url | ||
* @param {{parentURL: string}} context | ||
* @returns {Promise<string> | string | undefined} only works when enabled | ||
*/ | ||
function defaultGetFormatWithoutErrors(url, context) { | ||
const parsed = new URL(url); | ||
if (!ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol)) | ||
return null; | ||
return protocolHandlers[parsed.protocol](parsed, context, true); | ||
} | ||
|
||
/** | ||
* @param {URL | URL['href']} url | ||
* @param {{parentURL: string}} context | ||
* @returns {Promise<string> | string | undefined} only works when enabled | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Copy-pasta? |
||
*/ | ||
function defaultGetFormat(url, context) { | ||
const parsed = new URL(url); | ||
return ObjectPrototypeHasOwnProperty(protocolHandlers, parsed.protocol) ? | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,7 @@ const DEFAULT_CONDITIONS_SET = new SafeSet(DEFAULT_CONDITIONS); | |
* @typedef {string | string[] | Record<string, unknown>} Exports | ||
* @typedef {'module' | 'commonjs'} PackageType | ||
* @typedef {{ | ||
* pjsonPath: string, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* exports?: ExportConfig; | ||
* name?: string; | ||
* main?: string; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
context
exists in a bunch of places, so it would be better to create a typedef and then reference that. I'd be happy to do in a follow-up too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it's Bradley code, I feel inclined to land it as is (if I can get at least another 👍 on #42305 (comment)), and let you make a follow up PR since this one as already several approvals.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Along these lines, and also to consider in the follow-up PR rather than here, shouldn’t we be using true JSDoc syntax rather than TypeScript inside brackets? In other words, instead of:
We would write:
Besides being correct JSDoc (in my understanding) the latter lets us add descriptions to each property of the object, like
parentURL
in this case.