|
1 | 1 | import { |
| 2 | + is, |
2 | 3 | isFunctionOfClassMethod, |
3 | 4 | isFunctionOfClassProperty, |
4 | 5 | isFunctionOfObjectMethod, |
| 6 | + isOneOf, |
5 | 7 | NodeType, |
| 8 | + traverseUp, |
6 | 9 | type TSESTreeFunction, |
7 | 10 | unsafeIsMapCall, |
8 | 11 | } from "@eslint-react/ast"; |
9 | 12 | import { getPragmaFromContext, isChildrenOfCreateElement, isJSXValue } from "@eslint-react/jsx"; |
10 | | -import { M, MutList, MutRef, O } from "@eslint-react/tools"; |
| 13 | +import { F, M, MutList, MutRef, O } from "@eslint-react/tools"; |
11 | 14 | import type { RuleContext } from "@eslint-react/types"; |
12 | 15 | import { uid } from "@eslint-react/utils"; |
13 | 16 | import { type TSESTree } from "@typescript-eslint/types"; |
@@ -39,7 +42,23 @@ function hasValidHierarchy(node: TSESTreeFunction, context: RuleContext, hint: b |
39 | 42 | return false; |
40 | 43 | } |
41 | 44 |
|
42 | | - return !(hint & ERComponentCollectorHint.SkipClassProperty && isFunctionOfClassProperty(node.parent)); |
| 45 | + if (hint & ERComponentCollectorHint.SkipClassProperty && isFunctionOfClassProperty(node.parent)) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + return !F.pipe( |
| 50 | + traverseUp( |
| 51 | + node, |
| 52 | + isOneOf([ |
| 53 | + NodeType.JSXExpressionContainer, |
| 54 | + NodeType.ArrowFunctionExpression, |
| 55 | + NodeType.FunctionExpression, |
| 56 | + NodeType.Property, |
| 57 | + NodeType.ClassBody, |
| 58 | + ]), |
| 59 | + ), |
| 60 | + O.exists(is(NodeType.JSXExpressionContainer)), |
| 61 | + ); |
43 | 62 | } |
44 | 63 |
|
45 | 64 | function getComponentFlag(initPath: ERFunctionComponent["initPath"], pragma: string) { |
@@ -107,14 +126,14 @@ export function componentCollector( |
107 | 126 | MutList.pop(functionStack); |
108 | 127 | MutList.append(functionStack, [currentFn, true, []]); |
109 | 128 |
|
| 129 | + const initPath = getComponentInitPath(currentFn); |
110 | 130 | const id = getFunctionComponentIdentifier(currentFn, context); |
111 | | - const key = uid.rnd(); |
112 | 131 | const name = O.flatMapNullable( |
113 | 132 | id, |
114 | 133 | getComponentNameFromIdentifier, |
115 | 134 | ); |
116 | | - const initPath = getComponentInitPath(currentFn); |
117 | 135 |
|
| 136 | + const key = uid.rnd(); |
118 | 137 | components.set(key, { |
119 | 138 | _: key, |
120 | 139 | id, |
@@ -148,14 +167,14 @@ export function componentCollector( |
148 | 167 | return; |
149 | 168 | } |
150 | 169 |
|
| 170 | + const initPath = getComponentInitPath(currentFn); |
151 | 171 | const id = getFunctionComponentIdentifier(currentFn, context); |
152 | | - const key = uid.rnd(); |
153 | 172 | const name = O.flatMapNullable( |
154 | 173 | id, |
155 | 174 | getComponentNameFromIdentifier, |
156 | 175 | ); |
157 | | - const initPath = getComponentInitPath(currentFn); |
158 | 176 |
|
| 177 | + const key = uid.rnd(); |
159 | 178 | components.set(key, { |
160 | 179 | _: key, |
161 | 180 | id, |
|
0 commit comments