Repository navigation
Conversation
`processValue` replaces the value of a count argument with `increment()`,
which returns the number `1`. `setKey` then tested `value === increment()`
to decide whether to increment the running count, so any option receiving
the literal value `1` took the counter branch:
parser('-x 3 -x 1') // => { _: [], x: 4 }
Use a dedicated symbol as the marker instead, so the check identifies a
count argument rather than the value 1. Reversing the order (`-x 1 -x 3`)
already worked, which is why the result looked arbitrary.
Fixes yargs#506
Member
|
This looks like a drive-by AI contribution. The account created 28 Pull Requests on 28 repositories so far this month. This may get looked at and used when the issue is prioritised. The human maintainer does not have time to review all AI heavy PRs that are opened. |
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 #506.
The bug
An option that is not declared as a counter is incremented when it receives the literal value
1:duplicate-arguments-array: falsedoes not help either — it also yields4.Root cause
processValuereplaces the value of a count argument with the result ofincrement(), which is the number1:setKeythen decides whether to increment the running count by comparing that value back:So the check is really
value === 1, and any option whose value happens to be1takes the counter branch. That explains why the result looks arbitrary:-x 3 -x 1computes3 + 1, while-x 1 -x 3takes the branch on the first occurrence (whereo.xis stillundefined, so it stays1) and then falls through normally for3.As @shadowspawn noted in the issue, this regressed in the TypeScript conversion:
index.jsused theincrementfunction itself as a sentinel value (value === increment, a reference comparison) rather than its return value.The fix
Restore the sentinel semantics with a dedicated symbol, so the condition identifies "this is a count argument" instead of "this value is 1":
A symbol is used rather than the function reference so it cannot collide with a user-supplied value. The marker never escapes the parser:
setKeyis the only consumer and it always replaces it with a number.Behaviour
-x 3 -x 1x: 4x: [3, 1]-x 1 -x 3x: [1, 3]x: [1, 3]-x 3 -x 1,duplicate-arguments-array: falsex: 4x: 1-v -v -vwithcount: ["v"]v: 3v: 3-v 1withcount: ["v"]v: 1, _: [1]v: 1, _: [1]Counter behaviour is unchanged, including the existing
should increment regardless of arg valuecase.Tests
Two regression tests added to the
countdescribe block. Both fail onmainand pass with the fix.Validation
npm test— 364 passing (362 before, no existing test changed)npm run test:typescript— 20 passingnpm run check(gts lint) — cleannpm run compile— clean