Skip to content

Commit a09e8cd

Browse files
committed
feat(core): add 'COMPONENT_DISPLAY_NAME_ASSIGNMENT_SELECTOR' and 'isComponentDisplayNameAssignment' function
1 parent e7be885 commit a09e8cd

File tree

8 files changed

+79
-30
lines changed

8 files changed

+79
-30
lines changed

packages/core/docs/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
## Variables
3131

32+
- [COMPONENT\_DISPLAY\_NAME\_ASSIGNMENT\_SELECTOR](variables/COMPONENT_DISPLAY_NAME_ASSIGNMENT_SELECTOR.md)
3233
- [DEFAULT\_COMPONENT\_HINT](variables/DEFAULT_COMPONENT_HINT.md)
3334
- [ERClassComponentFlag](variables/ERClassComponentFlag.md)
3435
- [ERComponentHint](variables/ERComponentHint.md)
@@ -60,6 +61,7 @@
6061
- [isCloneElementCall](functions/isCloneElementCall.md)
6162
- [isComponentDidCatch](functions/isComponentDidCatch.md)
6263
- [isComponentDidMount](functions/isComponentDidMount.md)
64+
- [isComponentDisplayNameAssignment](functions/isComponentDisplayNameAssignment.md)
6365
- [isComponentName](functions/isComponentName.md)
6466
- [isComponentWillUnmount](functions/isComponentWillUnmount.md)
6567
- [isCreateContext](functions/isCreateContext.md)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[**@eslint-react/core**](../README.md)
2+
3+
***
4+
5+
[@eslint-react/core](../README.md) / isComponentDisplayNameAssignment
6+
7+
# Function: isComponentDisplayNameAssignment()
8+
9+
> **isComponentDisplayNameAssignment**(`node`): `node is AssignmentExpression`
10+
11+
Check if the node is a component display name assignment expression
12+
13+
## Parameters
14+
15+
### node
16+
17+
The AST node
18+
19+
`undefined` | `Node`
20+
21+
## Returns
22+
23+
`node is AssignmentExpression`
24+
25+
`true` if the node is a component display name assignment

packages/core/docs/functions/useComponentCollector.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,6 @@ The component collector
9898

9999
`void`
100100

101-
#### listeners.AssignmentExpression\[type\]\[operator='='\]\[left.type='MemberExpression'\]\[left.property.name='displayName'\]()?
102-
103-
##### Parameters
104-
105-
###### node
106-
107-
`AssignmentExpression` & `object`
108-
109-
##### Returns
110-
111-
`void`
112-
113101
#### listeners.CallExpression\[type\]:exit()?
114102

115103
##### Parameters
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[**@eslint-react/core**](../README.md)
2+
3+
***
4+
5+
[@eslint-react/core](../README.md) / COMPONENT\_DISPLAY\_NAME\_ASSIGNMENT\_SELECTOR
6+
7+
# Variable: COMPONENT\_DISPLAY\_NAME\_ASSIGNMENT\_SELECTOR
8+
9+
> `const` **COMPONENT\_DISPLAY\_NAME\_ASSIGNMENT\_SELECTOR**: `string`
10+
11+
The ESQuery selector for a component display name assignment expression

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { ESLintUtils } from "@typescript-eslint/utils";
1010
import { isChildrenOfCreateElement } from "../element";
1111
import { isReactHookCall } from "../hook";
1212
import { DEFAULT_COMPONENT_HINT, ERComponentHint } from "./component-collector-hint";
13+
import { COMPONENT_DISPLAY_NAME_ASSIGNMENT_SELECTOR } from "./component-display-name";
1314
import { ERFunctionComponentFlag } from "./component-flag";
1415
import { getFunctionComponentIdentifier } from "./component-id";
1516
import { isFunctionOfRenderMethod } from "./component-lifecycle";
@@ -61,9 +62,6 @@ export interface ComponentCollectorOptions {
6162
collectHookCalls?: boolean;
6263
}
6364

64-
// dprint-ignore
65-
const displayNameAssignmentSelector = "AssignmentExpression[type][operator='='][left.type='MemberExpression'][left.property.name='displayName']";
66-
6765
/**
6866
* Get a ctx and listeners for the rule to collect function components
6967
* @param context The ESLint rule context
@@ -157,8 +155,9 @@ export function useComponentCollector(
157155
},
158156
...collectDisplayName
159157
? {
160-
[displayNameAssignmentSelector](node: TSESTree.AssignmentExpression & { left: TSESTree.MemberExpression }) {
158+
[COMPONENT_DISPLAY_NAME_ASSIGNMENT_SELECTOR](node: TSESTree.AssignmentExpression) {
161159
const { left, right } = node;
160+
if (left.type !== T.MemberExpression) return;
162161
const componentName = left.object.type === T.Identifier
163162
? left.object.name
164163
: _;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { _ } from "@eslint-react/eff";
2+
import { AST_NODE_TYPES as T, type TSESTree } from "@typescript-eslint/types";
3+
4+
/**
5+
* The ESQuery selector for a component display name assignment expression
6+
*/
7+
export const COMPONENT_DISPLAY_NAME_ASSIGNMENT_SELECTOR = [
8+
"AssignmentExpression",
9+
"[type]",
10+
"[operator='=']",
11+
"[left.type='MemberExpression']",
12+
"[left.property.name='displayName']",
13+
].join("");
14+
15+
/**
16+
* Check if the node is a component display name assignment expression
17+
* @param node The AST node
18+
* @returns `true` if the node is a component display name assignment
19+
*/
20+
export function isComponentDisplayNameAssignment(node: TSESTree.Node | _): node is TSESTree.AssignmentExpression {
21+
if (node == null) return false;
22+
return node.type === T.AssignmentExpression
23+
&& node.operator === "="
24+
&& node.left.type === T.MemberExpression
25+
&& node.left.property.type === T.Identifier
26+
&& node.left.property.name === "displayName";
27+
}

packages/core/src/component/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from "./component-collector";
22
export * from "./component-collector-hint";
33
export * from "./component-collector-legacy";
4+
export * from "./component-display-name";
45
export * from "./component-flag";
56
export * from "./component-id";
67
export type * from "./component-kind";

packages/utilities/ast/src/find-parent-node.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,20 @@ import { _ } from "@eslint-react/eff";
22
import { AST_NODE_TYPES as T, type TSESTree } from "@typescript-eslint/types";
33

44
/**
5-
* Find the parent node that satisfies the predicate function
5+
* Find the parent node that satisfies the test function
66
* @param node The AST node
7-
* @param predicate The predicate function
8-
* @returns The parent node that satisfies the predicate or `undefined` if not found
7+
* @param test The test function
8+
* @returns The parent node that satisfies the test function or `_` if not found
99
*/
10-
export function findParentNode<A extends TSESTree.Node>(
11-
node: TSESTree.Node | _,
12-
predicate: (n: TSESTree.Node) => n is A,
13-
): A | _;
10+
function findParentNode<A extends TSESTree.Node>(node: TSESTree.Node | _, test: (n: TSESTree.Node) => n is A): A | _;
1411
/**
15-
* Find the parent node that satisfies the test function
12+
* Find the parent node that satisfies the test function or `_` if not found
1613
* @param node The AST node
1714
* @param test The test function
18-
* @returns The parent node that satisfies the test or `undefined` if not found
15+
* @returns The parent node that satisfies the test function
1916
*/
20-
export function findParentNode(
21-
node: TSESTree.Node | _,
22-
test: (node: TSESTree.Node) => boolean,
23-
): TSESTree.Node | _;
24-
export function findParentNode<A extends TSESTree.Node>(
17+
function findParentNode(node: TSESTree.Node | _, test: (node: TSESTree.Node) => boolean): TSESTree.Node | _;
18+
function findParentNode<A extends TSESTree.Node>(
2519
node: TSESTree.Node | _,
2620
test: ((node: TSESTree.Node) => boolean) | ((n: TSESTree.Node) => n is A),
2721
): TSESTree.Node | A | _ {
@@ -35,3 +29,5 @@ export function findParentNode<A extends TSESTree.Node>(
3529
}
3630
return _;
3731
}
32+
33+
export { findParentNode };

0 commit comments

Comments
 (0)