Skip to content

Commit c22239b

Browse files
authored
Require Context suffix for context providers, closes #1295 (#1296)
1 parent b11e2fe commit c22239b

File tree

6 files changed

+25
-67
lines changed

6 files changed

+25
-67
lines changed

packages/plugins/eslint-plugin-react-x/src/rules/no-context-provider.mdx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ Replaces usages of `<Context.Provider>` with `<Context>`.
3434

3535
In React 19, you can render `<Context>` as a provider instead of `<Context.Provider>`.
3636

37-
In addition, it is recommended to enable the [`naming-convention/context-name`](./naming-convention-context-name) rule to enforce consistent naming conventions for contexts.
37+
<Callout type="warning">
38+
This rule depends on the [`naming-convention/context-name`](./naming-convention-context-name) rule to identify context components. Make sure to enable that rule to ensure this rule works correctly.
39+
</Callout>
3840

3941
## Examples
4042

@@ -81,7 +83,7 @@ function App({ children }) {
8183

8284
## See Also
8385

84-
- [`no-forward-ref`](./no-forward-ref)\
85-
Replaces usages of `forwardRef` with passing `ref` as a prop.
8686
- [`no-use-context`](./no-use-context)\
8787
Replaces usages of `useContext` with `use`.
88+
- [`naming-convention/context-name`](./naming-convention-context-name)\
89+
Enforces consistent naming conventions for contexts.

packages/plugins/eslint-plugin-react-x/src/rules/no-context-provider.spec.ts

Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,6 @@ import rule, { RULE_NAME } from "./no-context-provider";
55

66
ruleTester.run(RULE_NAME, rule, {
77
invalid: [
8-
{
9-
code: tsx`<context.Provider />`,
10-
errors: [
11-
{
12-
messageId: "noContextProvider",
13-
},
14-
],
15-
settings: {
16-
"react-x": {
17-
version: "19.0.0",
18-
},
19-
},
20-
},
218
{
229
code: tsx`<Context.Provider />`,
2310
errors: [
@@ -60,47 +47,6 @@ ruleTester.run(RULE_NAME, rule, {
6047
},
6148
},
6249
},
63-
{
64-
code: tsx`<Foo.Bar.Provider>{children}</Foo.Bar.Provider>`,
65-
errors: [
66-
{
67-
messageId: "noContextProvider",
68-
},
69-
],
70-
output: tsx`<Foo.Bar>{children}</Foo.Bar>`,
71-
settings: {
72-
"react-x": {
73-
version: "19.0.0",
74-
},
75-
},
76-
},
77-
{
78-
code: tsx`<foo.Bar.Provider>{children}</foo.Bar.Provider>`,
79-
errors: [
80-
{
81-
messageId: "noContextProvider",
82-
},
83-
],
84-
output: tsx`<foo.Bar>{children}</foo.Bar>`,
85-
settings: {
86-
"react-x": {
87-
version: "19.0.0",
88-
},
89-
},
90-
},
91-
{
92-
code: tsx`<foo.bar.Provider>{children}</foo.bar.Provider>`,
93-
errors: [
94-
{
95-
messageId: "noContextProvider",
96-
},
97-
],
98-
settings: {
99-
"react-x": {
100-
version: "19.0.0",
101-
},
102-
},
103-
},
10450
],
10551
valid: [
10652
{
@@ -165,5 +111,19 @@ ruleTester.run(RULE_NAME, rule, {
165111
},
166112
},
167113
},
114+
{
115+
code: tsx`
116+
import { Tooltip } from '@base-ui-components/react/tooltip'
117+
118+
function Component() {
119+
return <Tooltip.Provider>hello world</Tooltip.Provider>;
120+
}
121+
`,
122+
settings: {
123+
"react-x": {
124+
version: "19.0.0",
125+
},
126+
},
127+
},
168128
],
169129
});

packages/plugins/eslint-plugin-react-x/src/rules/no-context-provider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
4949
const contextSelfName = parts.pop();
5050
// Exit if the element is not a "Provider"
5151
if (selfName !== "Provider") return;
52-
// Exit if there is no context name
53-
if (contextSelfName == null) return;
52+
// Exit if there is no context name or it doesn't end with "Context"
53+
if (contextSelfName == null || !contextSelfName.endsWith("Context")) return;
5454
context.report({
5555
messageId: "noContextProvider",
5656
node,

packages/plugins/eslint-plugin-react-x/src/rules/no-forward-ref.mdx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,3 @@ function MyInput({
112112

113113
- [`no-useless-forward-ref`](./no-useless-forward-ref)\
114114
Enforces that `forwardRef` is only used when a `ref` parameter is declared.
115-
- [`no-context-provider`](./no-context-provider)\
116-
Replaces usages of `<Context.Provider>` with `<Context>`.
117-
- [`no-use-context`](./no-use-context)\
118-
Replaces usages of `useContext` with `use`.

packages/plugins/eslint-plugin-react-x/src/rules/no-use-context.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,5 @@ function Button() {
7676

7777
- [`no-context-provider`](./no-context-provider)\
7878
Replaces usages of `<Context.Provider>` with `<Context>`.
79-
- [`no-forward-ref`](./no-forward-ref)\
80-
Replace usages of `forwardRef` with passing `ref` as a prop.
79+
- [`naming-convention/context-name`](./naming-convention-context-name)\
80+
Enforces consistent naming conventions for contexts.

packages/plugins/eslint-plugin/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ ESLint React is not affiliated with Meta Corporation or [facebook/react](https:/
186186

187187
Contributions are welcome!
188188

189-
Please follow our [contributing guidelines](https://github.com/Rel1cx/eslint-react/tree/main/.github/CONTRIBUTING.md).
189+
Please follow our [contributing guidelines](https://github.com/Rel1cx/eslint-react/tree/no-context-provider/.github/CONTRIBUTING.md).
190190

191191
## License
192192

193-
This project is licensed under the MIT License - see the [LICENSE](https://github.com/Rel1cx/eslint-react/tree/main/LICENSE) file for details.
193+
This project is licensed under the MIT License - see the [LICENSE](https://github.com/Rel1cx/eslint-react/tree/no-context-provider/LICENSE) file for details.

0 commit comments

Comments
 (0)