-
-
Notifications
You must be signed in to change notification settings - Fork 32
feat(plugins/x): add 'no-use-context', closes #930 #931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3cd6465
feat(plugins/x): add 'no-use-context', closes #930
Rel1cx f842c7e
test(no-use-context): add test case for 'useContext' with generic typ…
Rel1cx cc0aeaf
docs(no-use-context): update examples
Rel1cx b008553
fix: 'isUseImported' checks all imports
Rel1cx bb9836b
docs: update 'no-use-context' features and version requirement
Rel1cx 709b4b7
docs(no-use-context): add link to React blog for new feature use
Rel1cx f47de61
fix(no-use-context): use 'settings.version' instead of 'getSettingsFr…
Rel1cx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
packages/plugins/eslint-plugin-react-x/src/rules/no-use-context.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| import { ruleTester } from "../../../../../test"; | ||
| import rule, { RULE_NAME } from "./no-use-context"; | ||
|
|
||
| ruleTester.run(RULE_NAME, rule, { | ||
| invalid: [ | ||
| { | ||
| code: /* tsx */ ` | ||
| import { useContext } from 'react' | ||
|
|
||
| export const Component = () => { | ||
| const value = useContext(MyContext) | ||
| return <div>{value}</div> | ||
| } | ||
| `, | ||
| errors: [ | ||
| { messageId: "noUseContext" }, | ||
| { messageId: "noUseContext" }, | ||
| ], | ||
| output: /* tsx */ ` | ||
| import { use } from 'react' | ||
|
|
||
| export const Component = () => { | ||
| const value = use(MyContext) | ||
| return <div>{value}</div> | ||
| } | ||
| `, | ||
| settings: { | ||
| "react-x": { | ||
| version: "19.0.0", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| code: /* tsx */ ` | ||
| import { useContext } from 'react' | ||
|
|
||
| export const Component = () => { | ||
| const value = useContext<MyContext>(MyContext) | ||
| return <div>{value}</div> | ||
| } | ||
| `, | ||
| errors: [ | ||
| { messageId: "noUseContext" }, | ||
| { messageId: "noUseContext" }, | ||
| ], | ||
| output: /* tsx */ ` | ||
| import { use } from 'react' | ||
|
|
||
| export const Component = () => { | ||
| const value = use<MyContext>(MyContext) | ||
| return <div>{value}</div> | ||
| } | ||
| `, | ||
| settings: { | ||
| "react-x": { | ||
| version: "19.0.0", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| code: /* tsx */ ` | ||
| import { use, useContext } from 'react' | ||
|
|
||
| export const Component = () => { | ||
| const value = useContext(MyContext) | ||
| return <div>{value}</div> | ||
| } | ||
| `, | ||
| errors: [ | ||
| { messageId: "noUseContext" }, | ||
| { messageId: "noUseContext" }, | ||
| ], | ||
| output: /* tsx */ ` | ||
| import { use, } from 'react' | ||
|
|
||
| export const Component = () => { | ||
| const value = use(MyContext) | ||
| return <div>{value}</div> | ||
| } | ||
| `, | ||
| settings: { | ||
| "react-x": { | ||
| version: "19.0.0", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| code: /* tsx */ ` | ||
| import React from 'react' | ||
|
|
||
| export const Component = () => { | ||
| const value = React.useContext(MyContext) | ||
| return <div>{value}</div> | ||
| } | ||
| `, | ||
| errors: [ | ||
| { messageId: "noUseContext" }, | ||
| ], | ||
| output: /* tsx */ ` | ||
| import React from 'react' | ||
|
|
||
| export const Component = () => { | ||
| const value = React.use(MyContext) | ||
| return <div>{value}</div> | ||
| } | ||
| `, | ||
| settings: { | ||
| "react-x": { | ||
| version: "19.0.0", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| code: /* tsx */ ` | ||
| import { use, useContext as useCtx } from 'react' | ||
|
|
||
| export const Component = () => { | ||
| const value = useCtx(MyContext) | ||
| return <div>{value}</div> | ||
| } | ||
| `, | ||
| errors: [ | ||
| { messageId: "noUseContext" }, | ||
| { messageId: "noUseContext" }, | ||
| ], | ||
| output: /* tsx */ ` | ||
| import { use, useContext as useCtx } from 'react' | ||
|
|
||
| export const Component = () => { | ||
| const value = use(MyContext) | ||
| return <div>{value}</div> | ||
| } | ||
| `, | ||
| settings: { | ||
| "react-x": { | ||
| version: "19.0.0", | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| valid: [ | ||
| { | ||
| code: /* tsx */ ` | ||
| import { useContext } from 'react' | ||
|
|
||
| export const Component = () => { | ||
| const value = useContext(MyContext) | ||
| return <div>{value}</div> | ||
| } | ||
| `, | ||
| settings: { | ||
| "react-x": { | ||
| version: "18.3.1", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| code: /* tsx */ ` | ||
| import { use } from 'react' | ||
|
|
||
| export const Component = () => { | ||
| const value = use(MyContext) | ||
| return <div>{value}</div> | ||
| } | ||
| `, | ||
| settings: { | ||
| "react-x": { | ||
| version: "19.0.0", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| code: /* tsx */ ` | ||
| import React from 'react' | ||
|
|
||
| export const Component = () => { | ||
| const value = React.use(MyContext) | ||
| return <div>{value}</div> | ||
| } | ||
| `, | ||
| settings: { | ||
| "react-x": { | ||
| version: "19.0.0", | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }); |
100 changes: 100 additions & 0 deletions
100
packages/plugins/eslint-plugin-react-x/src/rules/no-use-context.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { isReactHookCall, isReactHookCallWithNameAlias } from "@eslint-react/core"; | ||
| import type { RuleFeature } from "@eslint-react/shared"; | ||
| import { getSettingsFromContext } from "@eslint-react/shared"; | ||
| import { AST_NODE_TYPES as T } from "@typescript-eslint/types"; | ||
| import { compare } from "compare-versions"; | ||
| import type { CamelCase } from "string-ts"; | ||
| import { isMatching } from "ts-pattern"; | ||
|
|
||
| import { createRule } from "../utils"; | ||
|
|
||
| export const RULE_NAME = "no-use-context"; | ||
|
|
||
| export const RULE_FEATURES = [ | ||
| "CHK", | ||
| "MOD", | ||
| ] as const satisfies RuleFeature[]; | ||
|
|
||
| export type MessageID = CamelCase<typeof RULE_NAME>; | ||
|
|
||
| export default createRule<[], MessageID>({ | ||
| meta: { | ||
| type: "problem", | ||
| docs: { | ||
| description: "disallow the use of 'useContext'", | ||
| [Symbol.for("rule_features")]: RULE_FEATURES, | ||
| }, | ||
| fixable: "code", | ||
| messages: { | ||
| noUseContext: "In React 19, 'use' is preferred over 'useContext' because it is more flexible.", | ||
| }, | ||
| schema: [], | ||
| }, | ||
| name: RULE_NAME, | ||
| create(context) { | ||
| const settings = getSettingsFromContext(context); | ||
| const useContextAlias = new Set<string>(); | ||
|
|
||
| if (!context.sourceCode.text.includes("useContext")) { | ||
| return {}; | ||
| } | ||
| if (compare(settings.version, "19.0.0", "<")) { | ||
| return {}; | ||
| } | ||
| return { | ||
| CallExpression(node) { | ||
| if (!isReactHookCall(node)) { | ||
| return; | ||
| } | ||
| if (!isReactHookCallWithNameAlias("useContext", context, [...useContextAlias])(node)) { | ||
| return; | ||
| } | ||
| context.report({ | ||
| messageId: "noUseContext", | ||
| node, | ||
| fix(fixer) { | ||
| switch (node.callee.type) { | ||
| case T.Identifier: | ||
| return fixer.replaceText(node.callee, "use"); | ||
| case T.MemberExpression: | ||
| return fixer.replaceText(node.callee.property, "use"); | ||
| } | ||
| return null; | ||
| }, | ||
| }); | ||
| }, | ||
| ImportDeclaration(node) { | ||
| if (node.source.value !== settings.importSource) { | ||
| return; | ||
| } | ||
| const isUseImported = node.specifiers | ||
| .some(isMatching({ local: { type: T.Identifier, name: "use" } })); | ||
| for (const specifier of node.specifiers) { | ||
| if (specifier.type !== T.ImportSpecifier) continue; | ||
| if (specifier.imported.type !== T.Identifier) continue; | ||
| if (specifier.imported.name === "useContext") { | ||
| if (specifier.local.name !== "useContext") { | ||
| useContextAlias.add(specifier.local.name); | ||
| context.report({ | ||
| messageId: "noUseContext", | ||
| node: specifier, | ||
| }); | ||
| return; | ||
| } | ||
| context.report({ | ||
| messageId: "noUseContext", | ||
| node: specifier, | ||
| fix(fixer) { | ||
Rel1cx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (isUseImported) { | ||
| return fixer.replaceText(specifier, " ".repeat(specifier.range[1] - specifier.range[0])); | ||
| } | ||
| return fixer.replaceText(specifier.imported, "use"); | ||
| }, | ||
| }); | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| defaultOptions: [], | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.