Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ Replaces usages of `<Context.Provider>` with `<Context>`.

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

In addition, it is recommended to enable the [`naming-convention/context-name`](./naming-convention-context-name) rule to enforce consistent naming conventions for contexts.
<Callout type="info">
This rule works best in codebases that follow the [`naming-convention/context-name`](./naming-convention-context-name) rule.
</Callout>

## Examples

Expand Down Expand Up @@ -81,7 +83,7 @@ function App({ children }) {

## See Also

- [`no-forward-ref`](./no-forward-ref)\
Replaces usages of `forwardRef` with passing `ref` as a prop.
- [`no-use-context`](./no-use-context)\
Replaces usages of `useContext` with `use`.
- [`naming-convention/context-name`](./naming-convention-context-name)\
Enforces consistent naming conventions for contexts.
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,6 @@ import rule, { RULE_NAME } from "./no-context-provider";

ruleTester.run(RULE_NAME, rule, {
invalid: [
{
code: tsx`<context.Provider />`,
errors: [
{
messageId: "noContextProvider",
},
],
settings: {
"react-x": {
version: "19.0.0",
},
},
},
{
code: tsx`<Context.Provider />`,
errors: [
Expand Down Expand Up @@ -60,47 +47,6 @@ ruleTester.run(RULE_NAME, rule, {
},
},
},
{
code: tsx`<Foo.Bar.Provider>{children}</Foo.Bar.Provider>`,
errors: [
{
messageId: "noContextProvider",
},
],
output: tsx`<Foo.Bar>{children}</Foo.Bar>`,
settings: {
"react-x": {
version: "19.0.0",
},
},
},
{
code: tsx`<foo.Bar.Provider>{children}</foo.Bar.Provider>`,
errors: [
{
messageId: "noContextProvider",
},
],
output: tsx`<foo.Bar>{children}</foo.Bar>`,
settings: {
"react-x": {
version: "19.0.0",
},
},
},
{
code: tsx`<foo.bar.Provider>{children}</foo.bar.Provider>`,
errors: [
{
messageId: "noContextProvider",
},
],
settings: {
"react-x": {
version: "19.0.0",
},
},
},
],
valid: [
{
Expand Down Expand Up @@ -165,5 +111,19 @@ ruleTester.run(RULE_NAME, rule, {
},
},
},
{
code: tsx`
import { Tooltip } from '@base-ui-components/react/tooltip'

function Component() {
return <Tooltip.Provider>hello world</Tooltip.Provider>;
}
`,
settings: {
"react-x": {
version: "19.0.0",
},
},
},
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
const contextSelfName = parts.pop();
// Exit if the element is not a "Provider"
if (selfName !== "Provider") return;
// Exit if there is no context name
if (contextSelfName == null) return;
// Exit if there is no context name or it doesn't end with "Context"
if (contextSelfName == null || !contextSelfName.endsWith("Context")) return;
context.report({
messageId: "noContextProvider",
node,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,3 @@ function MyInput({

- [`no-useless-forward-ref`](./no-useless-forward-ref)\
Enforces that `forwardRef` is only used when a `ref` parameter is declared.
- [`no-context-provider`](./no-context-provider)\
Replaces usages of `<Context.Provider>` with `<Context>`.
- [`no-use-context`](./no-use-context)\
Replaces usages of `useContext` with `use`.
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ function Button() {

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