Repository navigation
fix: don't throw when a coerce function returns a non-primitive - #569
Closed
naveentehrpariya wants to merge 1 commit into
Closed
naveentehrpariya wants to merge 1 commit into
naveentehrpariya wants to merge 1 commit into
Conversation
`looksLikeNumber` converts its argument to a string to run its regex
checks. A coerce function can return any value, and converting one to a
string throws for objects with no `toString` -- for example the null
prototype object returned by `pg-connection-string`'s `parse`:
TypeError: Cannot convert object to primitive value
Return `false` for anything that is neither a number nor a string: such a
value can never be parsed as a number anyway.
Fixes yargs#352
naveentehrpariya
force-pushed
the
fix-looks-like-number-non-primitive
branch
from
November 8, 2026 10:43
d038a5d to
74a5ef2
Compare
Member
|
Unsolicited Pull Requests may be closed without discussion. For more see Contributing and AI Policy. |
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 #352.
The bug
looksLikeNumberconverts its argument to a string to run its regex checks:A
coercefunction is user-supplied and can return any value, andmaybeCoerceNumberpasses that return value straight intolooksLikeNumber. For an object with no reachabletoString— such as the null prototype object returned bypg-connection-string'sparse— that conversion throws:which yargs then surfaces as an opaque
YError, with the original stack lost.The fix
Return
falsefor anything that is neither a number nor a string. Such a value can never be parsed as a number, so there is nothing to lose by skipping the string checks — this is the direction both @bcoe and @groner suggested on the issue.Tests
looksLikeNumberreturnsfalseinstead of throwing for a null prototype object and for an object whosetoStringthrows.Both fail on
mainwithTypeError: Cannot convert object to primitive value. With the fix,npm testis 364 passing / 0 failing,npm run test:typescript20 passing, andnpm run checkis clean.