JS-1731 Fix S6551 typeof guard false positives#7404
Conversation
This comment has been minimized.
This comment has been minimized.
Code Review 👍 Approved with suggestions 1 resolved / 2 findingsSuppresses false positives in S6551 by accounting for typeof guard scopes, resolving the previous issue with reassignments. Ensure consistency regarding closure and hoisted writes, as the recent removal of isFunctionNode leads to unintended recursion into nested functions. 💡 Edge Case: Closure/hoisted writes handled inconsistently after isFunctionNode removal📄 packages/analysis/src/jsts/rules/S6551/decorator.ts:396-410 Removing the
Both are narrow edge cases and the direction of #1 is safe (errs toward reporting), but the behavior around nested-function writes is now range-based rather than execution-based, which is easy to trip over. Consider only counting writes that are lexically in the same function scope as the usage (ignoring nested function bodies entirely) so the semantics are consistent, or add fixtures covering these two shapes to pin the intended behavior. ✅ 1 resolved✅ Edge Case: hasWriteBefore misses reassignments in nested functions/after use
🤖 Prompt for agentsOptionsAuto-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 |
862b2cd to
12d1db1
Compare
(cherry picked from commit b19fe6a)
Treat destructuring assignment targets (`[value] = arr`,
`({ value } = obj)`) as invalidating writes so a proven-primitive
value reassigned to an object inside a typeof-guarded branch keeps
reporting instead of being suppressed.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
e08ae90 to
c10802b
Compare
Review feedbackReviewed the diff — logic checks out overall (scope resolution, shadowing, reassignment/destructuring/loop invalidation, and the conjunction/disjunction semantics in
Both are minor/non-blocking — no correctness bugs found, and test coverage (fixture Compliant/Noncompliant cases + upstream sentinel tests) is solid. |
|
Code Review ✅ Approved 3 resolved / 3 findingsRefines S6551 false-positive suppression by adding support for guarded type checks and destructuring reassignments, resolving previous issues with hoisted writes and inconsistent closure handling. ✅ 3 resolved✅ Edge Case: hasWriteBefore misses reassignments in nested functions/after use
✅ Edge Case: Closure/hoisted writes handled inconsistently after isFunctionNode removal
✅ Edge Case: typeof guard ignores reassignments inside the if condition
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 |




Summary
Fixes JS-1731 by adding a narrow S6551 decorator suppression for identifiers used in implicit string contexts when an enclosing
ifconsequent is guarded bytypeofchecks proving the same bound variable is primitive.The suppression handles primitive equality checks, reversed comparisons,
typeof value !== "object", conjunctions, and disjunctions. It resolves identifiers through ESLint scope so same-name shadowing does not suppress incorrectly, and it rejects suppression after reassignment before the report site.Why
@typescript-eslint/no-base-to-stringcan still report after guards such astypeof value !== "object"because TypeScript may expose a broad narrowed type like{}/{} | undefined, even though runtime object stringification cannot occur in the guarded branch.Tests
npx tsx --tsconfig packages/tsconfig.test.json --test --test-reporter=spec --test-reporter-destination=stdout packages/analysis/src/jsts/rules/S6551/cb.test.tsnpm run bridge:compilegit diff --checknpx prettier --check packages/analysis/src/jsts/rules/S6551/decorator.ts packages/analysis/src/jsts/rules/S6551/cb.fixture.tsSummary by Gitar
build.ymlto export and validate JavaScript coverage reports viaactions/upload-artifactandactions/download-artifact.false-positives/module to improve maintainability and support complex conditional checks.This will update automatically on new commits.