Skip to content

[Fix] static-property-placement: don't report a typed context field - #4060

Merged
ljharb merged 2 commits into
jsx-eslint:masterfrom
b84330808:static-property-placement-typed-context
Nov 3, 2026
Merged

ljharb merged 2 commits into
jsx-eslint:masterfrom
b84330808:static-property-placement-typed-context

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

Fixes #3467

static-property-placement reports declare context: React.ContextType<typeof MyContext> (the usual way to type this.context in a TS class component) as "'contextTypes' should be declared as a static class property."

The cause is the "Flow support" branch in isContextTypesDeclaration (lib/util/props.js), which returned true for any class field named context with a type annotation. A typed context field declares the type of this.context, not contextTypes, so this removes that branch, as agreed in the issue.

forbid-prop-types with checkContextTypes uses the same helper, so it also stops treating an annotated context instance field as contextTypes.

Tests: valid cases for declare context next to static contextType and next to static contextTypes, plus a plain annotated context: field. They fail without the change.

Side note: the matching props branch in isPropTypesDeclaration makes this rule report props: Props; / declare props: Props; as a misplaced propTypes too. I left it alone because the prop-types rules rely on it for Flow, but I can follow up if you'd like this rule to handle it.

I used Claude Code to write this change.

@codecov

codecov Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.75%. Comparing base (bd381cd) to head (2dd0415).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4060   +/-   ##
=======================================
  Coverage   97.74%   97.75%           
=======================================
  Files         137      137           
  Lines       10299    10296    -3     
  Branches     3834     3832    -2     
=======================================
- Hits        10067    10065    -2     
+ Misses        232      231    -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@b84330808
b84330808 force-pushed the static-property-placement-typed-context branch from 2dd0415 to 70b8070 Compare October 24, 2026 09:21
ljharb and others added 2 commits November 3, 2026 13:11
`isContextTypesDeclaration` returned true for any class field named `context` that has a type annotation.
That branch was marked "Flow support" and was copied from the `props` check in `isPropTypesDeclaration`,
but a typed `context` field declares the type of `this.context`, not `contextTypes`.

So TypeScript's `declare context: React.ContextType<typeof MyContext>`
was reported as "'contextTypes' should be declared as a static class property."

Only a property named `contextTypes` counts as a contextTypes declaration now.

Fixes jsx-eslint#3467

Co-authored-by: b84330808 <[email protected]>
Co-authored-by: Jordan Harband <[email protected]>

Assisted-By: Claude Opus 5.5 <[email protected]>
@ljharb
ljharb force-pushed the static-property-placement-typed-context branch from 70b8070 to ea87c86 Compare November 3, 2026 19:44
@ljharb
ljharb merged commit ea87c86 into jsx-eslint:master Nov 3, 2026
522 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Declaring context type on class component breaks static-property-placement

2 participants