JS-1993 S5906: clarify that length matchers apply beyond collections#7434
Merged
Conversation
Reviewers have flagged expect(x.length).toBe(n) -> expect(x).toHaveLength(n) rewrites as incorrect when x isn't a collection, but toHaveLength/toHaveSize/ lengthOf work on any value exposing a numeric length/size property. Tag these suggestions and use a dedicated message that states this explicitly.
Contributor
guillemsarda
reviewed
Jul 2, 2026
guillemsarda
left a comment
Contributor
There was a problem hiding this comment.
I left two suggestions. Lmkwyt
Lets report() forward the message id directly instead of re-deriving it from a suggestion "kind", per review feedback.
Drop the "reads better, reports clearer failures" rationale from preferSpecificLengthAssertion per review feedback, keeping only the action and the length-property clarification.
Use "for better reporting" as a compact nod to the shared rationale with preferSpecificAssertion, and restore "numeric" to match the RSPEC wording.
|
Code Review ✅ ApprovedUpdates length-based matcher suggestions to include a dedicated message clarifying that these assertions apply to any value with a numeric length property, not just collections. No issues found. OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
francois-mora-sonarsource
pushed a commit
that referenced
this pull request
Jul 7, 2026
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.





Summary
Reviewers have reported the S5906 rewrite
expect(x.length).toBe(n)→expect(x).toHaveLength(n)(and the Jasmine/Chai equivalentstoHaveSize/lengthOf) as a false positive whenxisn't literally a collection, e.g.expect(diagram.length).toBe(1400)wherediagramis a rendered string.These matchers are actually generic: Jest/Vitest's
toHaveLengthand Jasmine'stoHaveSizepass whenever the tested value exposes a numericlength/sizeproperty with the expected value, whatever that value represents (Vitest's own test suite even assertsexpect({ length: 3 }).toHaveLength(3)). The rewrite is behaviorally identical regardless of whatxis, so this isn't a detection bug — it's a communication gap.This PR:
kind: 'length'on theSuggestiontype (assertion-utils.ts), set at every call site that builds a.length/.sizerewrite acrossrule.tsandassertion-suggestions.ts.preferSpecificLengthAssertionmessage that explicitly states the matcher works on any value with a numeric length property, not only arrays or collections, used instead of the genericpreferSpecificAssertionmessage for these cases.unit.test.ts,chai-bdd.unit.test.ts, andintegrations.unit.test.tsto assert the new message id.RSPEC companion PR (adds the same clarification plus a string-based example to "Why is this an issue?"): https://github.com/SonarSource/rspec/pull/7279
Test plan
npx tsx --test packages/analysis/src/jsts/rules/S5906/*.test.ts— all 3 suites passnpx tsc --noEmiton the analysis package — no new errorsnpx prettier --checkon changed files