Skip to content

Commit 0b53c39

Browse files
authored
feat(hooks-extra): update no-unnecessary-use-prefix to skip well-known hooks (#1072)
1 parent 26145cb commit 0b53c39

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-unnecessary-use-prefix.md

+8
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ function useAuth() {
114114
}
115115
```
116116

117+
```tsx
118+
export function useMDXComponents(components) {
119+
return {
120+
...components,
121+
};
122+
}
123+
```
124+
117125
## Implementation
118126

119127
- [Rule source](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-unnecessary-use-prefix.ts)

packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-unnecessary-use-prefix.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,14 @@ ruleTester.run(RULE_NAME, rule, {
186186
return TEST_USER;
187187
}
188188
`,
189+
tsx`
190+
import type { MDXComponents } from 'mdx/types'
191+
192+
export function useMDXComponents(components: MDXComponents): MDXComponents {
193+
return {
194+
...components,
195+
}
196+
}
197+
`,
189198
],
190199
});

packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-unnecessary-use-prefix.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ export const RULE_FEATURES = [] as const satisfies RuleFeature[];
1313

1414
export type MessageID = CamelCase<typeof RULE_NAME>;
1515

16-
function isNodeContainsUseCallComments(
17-
context: RuleContext,
18-
node: TSESTree.Node,
19-
) {
16+
const WELL_KNOWN_HOOKS = [
17+
"useMDXComponents",
18+
];
19+
20+
function containsUseComments(context: RuleContext, node: TSESTree.Node) {
2021
return context.sourceCode
2122
.getCommentsInside(node)
22-
.some((comment) => /use\w+\(/u.test(comment.value));
23+
.some(({ value }) => /use\([\s\S]*?\)/u.test(value) || /use[A-Z0-9]\w*\([\s\S]*?\)/u.test(value));
2324
}
2425

2526
export default createRule<[], MessageID>({
@@ -47,6 +48,10 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
4748
"Program:exit"(program) {
4849
const allHooks = ctx.getAllHooks(program);
4950
for (const { id, name, node, hookCalls } of allHooks.values()) {
51+
// Skip well-known hooks
52+
if (WELL_KNOWN_HOOKS.includes(name)) {
53+
continue;
54+
}
5055
// Skip empty functions
5156
if (AST.isEmptyFunction(node)) {
5257
continue;
@@ -56,7 +61,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
5661
continue;
5762
}
5863
// Skip hooks with comments that contain calls to other hooks
59-
if (isNodeContainsUseCallComments(context, node)) {
64+
if (containsUseComments(context, node)) {
6065
continue;
6166
}
6267
context.report({

pnpm-lock.yaml

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)