-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
9 changed files
with
363 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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) { | ||
if (isUseImported) { | ||
return fixer.replaceText(specifier, " ".repeat(specifier.range[1] - specifier.range[0])); | ||
} | ||
return fixer.replaceText(specifier.imported, "use"); | ||
}, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
defaultOptions: [], | ||
}); |
This file contains 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 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 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.