JS-1394 Handle S6767 React class non-props generics#7050
JS-1394 Handle S6767 React class non-props generics#7050francois-mora-sonarsource wants to merge 34 commits into
Conversation
Ruling ReportCode no longer flagged (2 issues)S6767ant-design/components/anchor/Anchor.tsx:70 68 |
69 | export interface AnchorState {
> 70 | activeLink: null | string;
71 | }
72 | S2094 348 |
349 |
> 350 | class Foo {
351 | constructor(name, options = {}) {
352 | this.name = name;New issues flagged (4 issues)S5845 266 | function (x) { results3.push(x); },
267 | function (err) {
> 268 | expect(false).to.equal('should not throw error: ' + err.toString());
269 | }
270 | );rxjs/spec/observables/IteratorObservable-spec.ts:152 150 | IteratorObservable.create('foo')
151 | .subscribe(
> 152 | (x: number) => { expect(x).to.equal(expected.shift()); },
153 | (x) => {
154 | done(new Error('should not be called'));vitest/test/coverage-test/fixtures/test/mocking-in-js-file.test.js:9 7 |
8 | test('mocking in Javascript test should not break sourcemaps', () => {
> 9 | expect(sum(1, 2)).toBe('mocked')
10 | })
11 | vitest/test/browser/fixtures/mocking/mocked-nested.test.ts:14 12 | test('adds', () => {
13 | child.mockReturnValue(42)
> 14 | expect(parent()).toBe(42)
15 | })
16 | |
SummaryWhat changed: This PR fixes false-positive S6767 reports on React class non-props generics (state/snapshot) by introducing per-component suppression logic and a new TypeScript-based check. When React classes declare all three type parameters (
Why: The per-component approach makes suppression logic context-aware—a prop can be a false positive for one component (where it's state) but real for another (where it's props). This isolates the new non-props check from unrelated escapes and prevents incorrect suppressions. What reviewers should knowWhere to start:
Key decisions:
Watch for:
|
26d646e to
69ffa29
Compare
57f915f to
83bbc8a
Compare
69ffa29 to
629141e
Compare
There was a problem hiding this comment.
The new commits are a pure rename refactoring — ReportedEnclosingType → ReportedType in reported-type.ts, propagated to all consumers (react.ts, react/member-ownership.ts, react/type-ownership.ts, react-non-props-usage.ts). No functional logic was changed.
The previously flagged duplication (REACT_COMPONENT_WRAPPER_CALLEES, isReactComponentWrapperCallee, isWrappedInReactComponentCall, getOutermostReactWrapperParent, isAnonymousDefaultExportComponent duplicated between react.ts:50 and react/component-analysis.ts:36) is still present and was not addressed by these commits.
This reverts commit 9347324.
|
Co-authored-by: Gitar <noreply@gitar.ai>
Code Review ✅ Approved 2 resolved / 2 findingsRefactors React component analysis to better handle S6767 non-props generics and resolves misleading prop-independent conditions in ✅ 2 resolved✅ Quality: isPropUsed has misleading prop-independent conditions inside .some()
✅ Quality: getReportedTypeUsageCacheKey uses unsafe type cast for cache key
OptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change:
Was this helpful? React with 👍 / 👎 | Gitar |




Summary
This stacked PR updates the S6767 false-positive remediation for React class non-props generics.
It does four things:
props,non-props,mixed, orother, so state/snapshot-only declarations can be filtered without hiding real props-side ownersReact.Component<Props, State, Snapshot>cases where upstream still reports the second generic as an unused propant-designruling by removingcomponents/anchor/Anchor.tsx:70Why
The upstream
eslint-plugin-reactrule still misreports some TypeScript React class declarations when the reported type is only used through non-props generic slots. The motivating case is a class declared asReact.Component<Props, State, Snapshot>, where aStatemember can still be treated as an unused prop. The SonarJS decorator also needs to keep the report when the same declaration is still used as props elsewhere in the file.Verification
node --import=tsx --test --test-reporter=spec packages/analysis/src/jsts/rules/S6767/unit.test.ts packages/analysis/src/jsts/rules/S6767/unit.upstream.test.ts packages/analysis/src/jsts/rules/S6767/unit.typescript.test.tsnode --import=tsx --test --test-reporter=spec packages/analysis/tests/jsts/rules/helpers/react.test.ts packages/analysis/tests/jsts/rules/helpers/react/component-analysis.test.ts packages/analysis/tests/jsts/rules/helpers/react/type-ownership.test.ts packages/analysis/tests/jsts/rules/helpers/reported-type.test.tsThe focused S6767 suites and the changed React helper suites pass locally. The branch also removes the existing
ant-designcomponents/anchor/Anchor.tsx:70ruling entry, which matches the targeted state-only pattern.Manual validation
components/anchor/Anchor.tsxdeclaresclass Anchor extends React.Component<InternalAnchorProps, AnchorState, ConfigConsumerProps>and usesAnchorState.activeLinkthroughthis.state, so the removed ruling entry is in scopeSummary by Gitar
rspec-secretsvault access in.github/workflows/build.ymlto prevent Windows build failures.This will update automatically on new commits.