fix: array type flags (number/boolean/string) should respect their value#533
Open
chatman-media wants to merge 1 commit into
Open
fix: array type flags (number/boolean/string) should respect their value#533chatman-media wants to merge 1 commit into
chatman-media wants to merge 1 commit into
Conversation
When an array option was configured with an object such as
{ key: 'foo', number: false }, the parser enabled number coercion
regardless of the value because it only checked for the presence of
the key rather than whether it was truthy. As a result, number: false,
number: undefined, boolean: false and string: false all changed parsing
instead of being treated as if the flag were absent.
Only enable the corresponding coercion when the type flag is === true.
Closes yargs#415
Member
|
(Will be a while before I review this. Both the original code and replacement code are somewhat opaque, and other issues waiting.) |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #415.
Problem
When an
arrayoption is configured with an object, the parser decides whether to coerce its values tonumber/boolean/stringbased on the presence of the corresponding key, ignoring its value. As a result a falsy type flag still enables coercion:The README documents enabling coercion with
number: true, and the type definition declares these properties asboolean?, sofalse/undefinedshould behave as if the flag were absent. The same bug affectsboolean: false(values wrongly coerced to booleans) andstring: false(numbers wrongly kept as strings).Cause
In
lib/yargs-parser.ts, the assignment was derived fromObject.keys(opt), which only checks key existence:Fix
Only enable a coercion flag when its value is strictly
=== true. (Also guards the object-vs-stringoptcase explicitly, instead of relying on a string`s numeric character indices never matching a flag name.)Tests
Added four tests covering
number: false,number: undefined,boolean: false, andstring: false. All four fail onmainand pass with the fix; the full suite (npm test) stays green (366 passing) with 100% coverage onyargs-parser.ts, andnpm run check(gts lint) is clean.