fix(ext/node): Support returning tokens and option defaults in node:util.parseArgs (#23192)

Fixes #23179.
Fixes #22454.

Enables passing `{tokens: true}` to `parseArgs` and setting default
values for options.

With this PR, the observable framework works with deno out of the box
(no unstable flags needed).

The existing code was basically copied straight from node, so this PR
mostly just updates that (out of date) vendored code. Also fixes some
issues with error exports (before this PR, in certain error cases we
were attempting to construct error classes that weren't actually in
scope).

The last change (in the second commit) adds a small hack so that we
actually exercise the `test-parse-args.js` node_compat test, previously
it was reported as passing though it should have failed. That test now
passes.

---------

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
Nathan Whitaker 2024-04-02 16:20:48 -07:00 committed by GitHub
parent 8eb2b6c61f
commit 219a27dde5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 305 additions and 143 deletions

View file

@ -173,6 +173,18 @@ function findLongOptionForShort(shortOption, options) {
return longOptionEntry?.[0] ?? shortOption;
}
/**
* Check if the given option includes a default value
* and that option has not been set by the input args.
* @param {string} longOption - long option name e.g. 'foo'
* @param {object} optionConfig - the option configuration properties
* @param {object} values - option values returned in `values` by parseArgs
*/
function useDefaultValueOption(longOption, optionConfig, values) {
return objectGetOwn(optionConfig, "default") !== undefined &&
values[longOption] === undefined;
}
export {
findLongOptionForShort,
isLoneLongOption,
@ -184,4 +196,5 @@ export {
isShortOptionGroup,
objectGetOwn,
optionsGetOwn,
useDefaultValueOption,
};