Skip to content

Commit 7165d27

Browse files
committed
refactor(jsx): simplify code
1 parent 0403041 commit 7165d27

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export function useComponentCollector(
5757
hint = DEFAULT_COMPONENT_DETECTION_HINT,
5858
} = options;
5959

60-
const jsxCtx = { getScope: (node: TSESTree.Node) => context.sourceCode.getScope(node) } as const;
6160
const components = new Map<string, FunctionComponent>();
6261
const functionEntries: FunctionEntry[] = [];
6362

@@ -76,7 +75,7 @@ export function useComponentCollector(
7675
.some((r) => {
7776
return context.sourceCode.getScope(r).block === entry.node
7877
&& r.argument != null
79-
&& !JSX.isJSXLike(r.argument, jsxCtx, hint);
78+
&& !JSX.isJsxLike(context.sourceCode, r.argument, hint);
8079
});
8180
if (shouldDrop) {
8281
components.delete(entry.key);
@@ -103,7 +102,7 @@ export function useComponentCollector(
103102
if (entry == null) return;
104103
const { body } = entry.node;
105104
const isComponent = hasNoneOrLooseComponentName(context, entry.node)
106-
&& JSX.isJSXLike(body, jsxCtx, hint)
105+
&& JSX.isJsxLike(context.sourceCode, body, hint)
107106
&& isValidComponentDefinition(context, entry.node, hint);
108107
if (!isComponent) return;
109108
const initPath = AST.getFunctionInitPath(entry.node);
@@ -152,7 +151,7 @@ export function useComponentCollector(
152151
const entry = getCurrentEntry();
153152
if (entry == null) return;
154153
const isComponent = hasNoneOrLooseComponentName(context, entry.node)
155-
&& JSX.isJSXLike(node.argument, jsxCtx, hint)
154+
&& JSX.isJsxLike(context.sourceCode, node.argument, hint)
156155
&& isValidComponentDefinition(context, entry.node, hint);
157156
if (!isComponent) return;
158157
entry.isComponent = true;

packages/core/src/component/component-render-prop.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@ export function isRenderFunctionLoose(context: RuleContext, node: AST.TSESTreeFu
2525
&& parent.parent.name.type === T.JSXIdentifier
2626
&& parent.parent.name.name.startsWith("render");
2727
}
28-
return JSX.isJSXLike(
28+
return JSX.isJsxLike(
29+
context.sourceCode,
2930
body,
30-
{
31-
getScope: (node: TSESTree.Node) => context.sourceCode.getScope(node),
32-
},
3331
JSX.JSXDetectionHint.SkipNullLiteral
3432
| JSX.JSXDetectionHint.SkipUndefined
3533
| JSX.JSXDetectionHint.StrictLogical

packages/utilities/jsx/src/is-jsx.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ import { DEFAULT_JSX_DETECTION_HINT, JSXDetectionHint } from "./jsx-detection-hi
99

1010
/**
1111
* Heuristic decision to determine if a node is a JSX-like node.
12+
* @param code The sourceCode object
13+
* @param code.getScope The function to get the scope of a node
1214
* @param node The AST node to check
13-
* @param jsxCtx The requirements for the check
14-
* @param jsxCtx.getScope The function to get the scope of a node
1515
* @param hint The `JSXDetectionHint` to use
1616
* @returns boolean
1717
*/
18-
export function isJSXLike(
18+
export function isJsxLike(
19+
code: { getScope: (node: TSESTree.Node) => Scope },
1920
node: TSESTree.Node | _ | null,
20-
jsxCtx: { getScope: (node: TSESTree.Node) => Scope },
2121
hint: JSXDetectionHint = DEFAULT_JSX_DETECTION_HINT,
2222
): boolean {
2323
switch (node?.type) {
@@ -59,15 +59,15 @@ export function isJSXLike(
5959
}
6060
case T.ArrayExpression: {
6161
if (hint & JSXDetectionHint.StrictArray) {
62-
return node.elements.every((n) => isJSXLike(n, jsxCtx, hint));
62+
return node.elements.every((n) => isJsxLike(code, n, hint));
6363
}
64-
return node.elements.some((n) => isJSXLike(n, jsxCtx, hint));
64+
return node.elements.some((n) => isJsxLike(code, n, hint));
6565
}
6666
case T.LogicalExpression: {
6767
if (hint & JSXDetectionHint.StrictLogical) {
68-
return isJSXLike(node.left, jsxCtx, hint) && isJSXLike(node.right, jsxCtx, hint);
68+
return isJsxLike(code, node.left, hint) && isJsxLike(code, node.right, hint);
6969
}
70-
return isJSXLike(node.left, jsxCtx, hint) || isJSXLike(node.right, jsxCtx, hint);
70+
return isJsxLike(code, node.left, hint) || isJsxLike(code, node.right, hint);
7171
}
7272
case T.ConditionalExpression: {
7373
function leftHasJSX(node: TSESTree.ConditionalExpression) {
@@ -76,14 +76,14 @@ export function isJSXLike(
7676
return !(hint & JSXDetectionHint.SkipEmptyArray);
7777
}
7878
if (hint & JSXDetectionHint.StrictArray) {
79-
return node.consequent.every((n: TSESTree.Expression) => isJSXLike(n, jsxCtx, hint));
79+
return node.consequent.every((n: TSESTree.Expression) => isJsxLike(code, n, hint));
8080
}
81-
return node.consequent.some((n: TSESTree.Expression) => isJSXLike(n, jsxCtx, hint));
81+
return node.consequent.some((n: TSESTree.Expression) => isJsxLike(code, n, hint));
8282
}
83-
return isJSXLike(node.consequent, jsxCtx, hint);
83+
return isJsxLike(code, node.consequent, hint);
8484
}
8585
function rightHasJSX(node: TSESTree.ConditionalExpression) {
86-
return isJSXLike(node.alternate, jsxCtx, hint);
86+
return isJsxLike(code, node.alternate, hint);
8787
}
8888
if (hint & JSXDetectionHint.StrictConditional) {
8989
return leftHasJSX(node) && rightHasJSX(node);
@@ -92,7 +92,7 @@ export function isJSXLike(
9292
}
9393
case T.SequenceExpression: {
9494
const exp = node.expressions.at(-1);
95-
return isJSXLike(exp, jsxCtx, hint);
95+
return isJsxLike(code, exp, hint);
9696
}
9797
case T.CallExpression: {
9898
if (hint & JSXDetectionHint.SkipCreateElement) {
@@ -114,11 +114,11 @@ export function isJSXLike(
114114
if (AST.isJSXTagNameExpression(node)) {
115115
return true;
116116
}
117-
const variable = VAR.findVariable(name, jsxCtx.getScope(node));
117+
const variable = VAR.findVariable(name, code.getScope(node));
118118
const variableNode = variable
119119
&& VAR.getVariableInitNode(variable, 0);
120120
return !!variableNode
121-
&& isJSXLike(variableNode, jsxCtx, hint);
121+
&& isJsxLike(code, variableNode, hint);
122122
}
123123
}
124124
return false;

0 commit comments

Comments
 (0)