Skip to content

Commit 0a5ee37

Browse files
committed
refactor: update useComponentCollector function parameters and related documentation
1 parent 751a7b7 commit 0a5ee37

File tree

9 files changed

+23
-20
lines changed

9 files changed

+23
-20
lines changed

packages/core/docs/functions/isFunctionOfUseEffectCleanup.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
### node
1414

15-
[`Node`](../-internal-/type-aliases/Node.md)
15+
`undefined` | [`Node`](../-internal-/type-aliases/Node.md)
1616

1717
## Returns
1818

packages/core/docs/functions/useComponentCollector.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Function: useComponentCollector()
88

9-
> **useComponentCollector**(`context`, `hint`, `options`): [`ReturnType`](../namespaces/useComponentCollector/type-aliases/ReturnType.md)
9+
> **useComponentCollector**(`context`, `options`): [`ReturnType`](../namespaces/useComponentCollector/type-aliases/ReturnType.md)
1010
1111
Get a ctx and listeners for the rule to collect function components
1212

@@ -18,12 +18,6 @@ Get a ctx and listeners for the rule to collect function components
1818

1919
The ESLint rule context
2020

21-
### hint
22-
23-
`bigint` = `DEFAULT_COMPONENT_HINT`
24-
25-
The hint to use
26-
2721
### options
2822

2923
[`Options`](../namespaces/useComponentCollector/type-aliases/Options.md) = `{}`

packages/core/docs/namespaces/useComponentCollector/type-aliases/Options.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@
1717
### collectHookCalls?
1818

1919
> `optional` **collectHookCalls**: `boolean`
20+
21+
### hint?
22+
23+
> `optional` **hint**: [`ERComponentHint`](../../../type-aliases/ERComponentHint.md)

packages/core/src/component/component-collector.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type { ESLintUtils } from "@typescript-eslint/utils";
99

1010
import { isReactHookCall } from "../hook";
1111
import { DISPLAY_NAME_ASSIGNMENT_SELECTOR } from "../utils";
12+
import type { ERComponentHint } from "./component-collector-hint";
1213
import { DEFAULT_COMPONENT_HINT } from "./component-collector-hint";
1314
import { ERComponentFlag } from "./component-flag";
1415
import { getFunctionComponentIdentifier } from "./component-id";
@@ -27,6 +28,7 @@ export declare namespace useComponentCollector {
2728
type Options = {
2829
collectDisplayName?: boolean;
2930
collectHookCalls?: boolean;
31+
hint?: ERComponentHint;
3032
};
3133
type ReturnType = {
3234
ctx: {
@@ -41,16 +43,18 @@ export declare namespace useComponentCollector {
4143
/**
4244
* Get a ctx and listeners for the rule to collect function components
4345
* @param context The ESLint rule context
44-
* @param hint The hint to use
4546
* @param options The options to use
4647
* @returns The component collector
4748
*/
4849
export function useComponentCollector(
4950
context: RuleContext,
50-
hint = DEFAULT_COMPONENT_HINT,
5151
options: useComponentCollector.Options = {},
5252
): useComponentCollector.ReturnType {
53-
const { collectDisplayName = false, collectHookCalls = false } = options;
53+
const {
54+
collectDisplayName = false,
55+
collectHookCalls = false,
56+
hint = DEFAULT_COMPONENT_HINT,
57+
} = options;
5458

5559
const jsxCtx = { getScope: (node: TSESTree.Node) => context.sourceCode.getScope(node) } as const;
5660
const components = new Map<string, ERFunctionComponent>();

packages/core/src/effect/is.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ export function isFunctionOfUseEffectSetup(node: TSESTree.Node | _) {
1414
&& isUseEffectCallLoose(node.parent);
1515
}
1616

17-
export function isFunctionOfUseEffectCleanup(node: TSESTree.Node) {
18-
const nearReturn = AST.findParentNode(node, AST.is(T.ReturnStatement));
19-
const nearFunction = AST.findParentNode(node, AST.isFunction);
20-
const nearFunctionOfReturn = AST.findParentNode(nearReturn, AST.isFunction);
21-
return nearFunction === nearFunctionOfReturn && isFunctionOfUseEffectSetup(nearFunction);
17+
export function isFunctionOfUseEffectCleanup(node: TSESTree.Node | _) {
18+
if (node == null) return _;
19+
const returnStatement = AST.findParentNode(node, AST.is(T.ReturnStatement));
20+
const enclosingFunction = AST.findParentNode(node, AST.isFunction);
21+
const functionOfReturnStatement = AST.findParentNode(returnStatement, AST.isFunction);
22+
return enclosingFunction === functionOfReturnStatement && isFunctionOfUseEffectSetup(enclosingFunction);
2223
}

packages/plugins/eslint-plugin-react-debug/src/rules/function-component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ export default createRule<[], MessageID>({
2929
create(context) {
3030
const { ctx, listeners } = useComponentCollector(
3131
context,
32-
DEFAULT_COMPONENT_HINT,
3332
{
3433
collectDisplayName: true,
3534
collectHookCalls: true,
35+
hint: DEFAULT_COMPONENT_HINT,
3636
},
3737
);
3838
return {

packages/plugins/eslint-plugin-react-naming-convention/src/rules/use-state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export default createRule<[], MessageID>({
4747
listeners,
4848
} = useComponentCollector(
4949
context,
50-
DEFAULT_COMPONENT_HINT,
5150
{
51+
hint: DEFAULT_COMPONENT_HINT,
5252
collectDisplayName: false,
5353
collectHookCalls: true,
5454
},

packages/plugins/eslint-plugin-react-x/src/rules/no-missing-component-display-name.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export default createRule<[], MessageID>({
3535
listeners,
3636
} = useComponentCollector(
3737
context,
38-
DEFAULT_COMPONENT_HINT,
3938
{
39+
hint: DEFAULT_COMPONENT_HINT,
4040
collectDisplayName: true,
4141
collectHookCalls: false,
4242
},

packages/plugins/eslint-plugin-react-x/src/rules/no-nested-components.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export default createRule<[], MessageID>({
5050
| ERComponentHint.StrictLogical
5151
| ERComponentHint.StrictConditional;
5252

53-
const collector = useComponentCollector(context, hint);
53+
const collector = useComponentCollector(context, { hint });
5454
const collectorLegacy = useComponentCollectorLegacy();
5555

5656
return {

0 commit comments

Comments
 (0)