Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -114,6 +114,14 @@ function useAuth() {
}
```

```tsx
export function useMDXComponents(components) {
return {
...components,
};
}
```

## Implementation

- [Rule source](https://github.com/Rel1cx/eslint-react/tree/main/packages/plugins/eslint-plugin-react-hooks-extra/src/rules/no-unnecessary-use-prefix.ts)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,14 @@ ruleTester.run(RULE_NAME, rule, {
return TEST_USER;
}
`,
tsx`
import type { MDXComponents } from 'mdx/types'

export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
...components,
}
}
`,
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ export const RULE_FEATURES = [] as const satisfies RuleFeature[];

export type MessageID = CamelCase<typeof RULE_NAME>;

function isNodeContainsUseCallComments(
context: RuleContext,
node: TSESTree.Node,
) {
const WELL_KNOWN_HOOKS = [
"useMDXComponents",
];

function containsUseComments(context: RuleContext, node: TSESTree.Node) {
return context.sourceCode
.getCommentsInside(node)
.some((comment) => /use\w+\(/u.test(comment.value));
.some(({ value }) => /use\([\s\S]*?\)/u.test(value) || /use[A-Z0-9]\w*\([\s\S]*?\)/u.test(value));
}

export default createRule<[], MessageID>({
Expand Down Expand Up @@ -47,6 +48,10 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
"Program:exit"(program) {
const allHooks = ctx.getAllHooks(program);
for (const { id, name, node, hookCalls } of allHooks.values()) {
// Skip well-known hooks
if (WELL_KNOWN_HOOKS.includes(name)) {
continue;
}
// Skip empty functions
if (AST.isEmptyFunction(node)) {
continue;
Expand All @@ -56,7 +61,7 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
continue;
}
// Skip hooks with comments that contain calls to other hooks
if (isNodeContainsUseCallComments(context, node)) {
if (containsUseComments(context, node)) {
continue;
}
context.report({
Expand Down
1 change: 0 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.