Skip to content

Commit 0c169a5

Browse files
committed
refactor: reorganize element hierarchy functions and remove unused code
1 parent 0a5ee37 commit 0c169a5

File tree

8 files changed

+32
-110
lines changed

8 files changed

+32
-110
lines changed

packages/core/docs/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
- [isChildrenForEachCall](functions/isChildrenForEachCall.md)
6262
- [isChildrenMap](functions/isChildrenMap.md)
6363
- [isChildrenMapCall](functions/isChildrenMapCall.md)
64-
- [isChildrenOfCreateElement](functions/isChildrenOfCreateElement.md)
6564
- [isChildrenOnly](functions/isChildrenOnly.md)
6665
- [isChildrenOnlyCall](functions/isChildrenOnlyCall.md)
6766
- [isChildrenToArray](functions/isChildrenToArray.md)
@@ -95,7 +94,6 @@
9594
- [isGetDerivedStateFromError](functions/isGetDerivedStateFromError.md)
9695
- [isGetDerivedStateFromProps](functions/isGetDerivedStateFromProps.md)
9796
- [isInitializedFromReact](functions/isInitializedFromReact.md)
98-
- [isInsideCreateElementProps](functions/isInsideCreateElementProps.md)
9997
- [isInsideReactHook](functions/isInsideReactHook.md)
10098
- [isInsideRenderMethod](functions/isInsideRenderMethod.md)
10199
- [isInversePhase](functions/isInversePhase.md)

packages/core/docs/functions/isChildrenOfCreateElement.md

Lines changed: 0 additions & 31 deletions
This file was deleted.

packages/core/docs/functions/isInsideCreateElementProps.md

Lines changed: 0 additions & 31 deletions
This file was deleted.

packages/core/src/component/hierarchy.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { type RuleContext } from "@eslint-react/shared";
44
import type { TSESTree } from "@typescript-eslint/types";
55
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
66

7-
import { isChildrenOfCreateElement } from "../element";
7+
import { isCreateElementCall } from "../utils";
88
import { ERComponentHint } from "./component-collector-hint";
99
import { isFunctionOfRenderMethod } from "./component-lifecycle";
1010

@@ -38,6 +38,21 @@ export function hasValidHierarchy(context: RuleContext, node: AST.TSESTreeFuncti
3838
return boundaryNode == null || boundaryNode.type !== T.JSXExpressionContainer;
3939
}
4040

41+
/**
42+
* Determines whether inside `createElement`'s children.
43+
* @param context The rule context
44+
* @param node The AST node to check
45+
* @returns `true` if the node is inside createElement's children
46+
*/
47+
function isChildrenOfCreateElement(context: RuleContext, node: TSESTree.Node) {
48+
const parent = node.parent;
49+
if (parent == null || parent.type !== T.CallExpression) return false;
50+
if (!isCreateElementCall(context, parent)) return false;
51+
return parent.arguments
52+
.slice(2)
53+
.some((arg) => arg === node);
54+
}
55+
4156
function isFunctionOfClassMethod(node: TSESTree.Node): node is
4257
| TSESTree.ArrowFunctionExpression
4358
| TSESTree.FunctionExpression

packages/core/src/element/hierarchy.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

packages/core/src/element/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/core/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export * from "./component";
22
export * from "./effect";
3-
export * from "./element";
43
export * from "./hook";
54
export type * from "./semantic-entry";
65
export type * from "./semantic-node";

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import * as AST from "@eslint-react/ast";
22
import {
33
ERComponentHint,
4+
isCreateElementCall,
45
isDeclaredInRenderPropLoose,
56
isDirectValueOfRenderPropertyLoose,
6-
isInsideCreateElementProps,
77
isInsideRenderMethod,
88
useComponentCollector,
99
useComponentCollectorLegacy,
1010
} from "@eslint-react/core";
1111
import { _ } from "@eslint-react/eff";
1212
import * as JSX from "@eslint-react/jsx";
13-
import type { RuleFeature } from "@eslint-react/shared";
13+
import type { RuleContext, RuleFeature } from "@eslint-react/shared";
1414
import type { TSESTree } from "@typescript-eslint/types";
1515
import { AST_NODE_TYPES as T } from "@typescript-eslint/types";
1616

@@ -156,3 +156,17 @@ export default createRule<[], MessageID>({
156156
},
157157
defaultOptions: [],
158158
});
159+
160+
/**
161+
* Determines whether inside `createElement`'s props.
162+
* @param context The rule context
163+
* @param node The AST node to check
164+
* @returns `true` if the node is inside createElement's props
165+
*/
166+
function isInsideCreateElementProps(context: RuleContext, node: TSESTree.Node) {
167+
const call = AST.findParentNode(node, isCreateElementCall(context));
168+
if (call == null) return false;
169+
const prop = AST.findParentNode(node, AST.is(T.ObjectExpression));
170+
if (prop == null) return false;
171+
return prop === call.arguments[1];
172+
}

0 commit comments

Comments
 (0)