Skip to content

Commit 8cc5447

Browse files
committed
fix: minor fixes & improvements in 'no-nested-component-definitions' rule
1 parent ef84669 commit 8cc5447

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,11 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
8484
&& classComponents.some((component) => component.node === node);
8585
};
8686
for (const { name, node: component } of functionComponents) {
87-
// Do not mark objects containing render methods
88-
if (isDirectValueOfRenderPropertyLoose(component)) {
89-
continue;
90-
}
9187
// Do not mark anonymous function components to reduce false positives
92-
if (name == null) {
93-
continue;
94-
}
95-
const isInsideProperty = component.parent.type === T.Property;
96-
const isInsideJSXPropValue = component.parent.type === T.JSXAttribute
97-
|| JSX.findParentAttribute(node, (n) => n.value?.type === T.JSXExpressionContainer) != null;
98-
if (isInsideJSXPropValue) {
88+
if (name == null) continue;
89+
// Do not mark objects containing render methods
90+
if (isDirectValueOfRenderPropertyLoose(component)) continue;
91+
if (isInsideJSXAttributeValue(component)) {
9992
if (!isDeclaredInRenderPropLoose(component)) {
10093
context.report({
10194
messageId: "noNestedComponentDefinitionInProps",
@@ -120,11 +113,9 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
120113
continue;
121114
}
122115
const parentComponent = AST.findParentNode(component, isFunctionComponent);
123-
const isParentComponentNotDirectValueOfRenderProperty = parentComponent != null
124-
&& !isDirectValueOfRenderPropertyLoose(parentComponent);
125-
if (isParentComponentNotDirectValueOfRenderProperty) {
116+
if (parentComponent != null && !isDirectValueOfRenderPropertyLoose(parentComponent)) {
126117
context.report({
127-
messageId: isInsideProperty
118+
messageId: component.parent.type === T.Property
128119
? "noNestedComponentDefinitionInProps"
129120
: "noNestedComponentDefinition",
130121
node: component,
@@ -161,6 +152,16 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
161152
};
162153
}
163154

155+
/**
156+
* Determines whether the node is inside JSX attribute value
157+
* @param node The AST node to check
158+
* @returns `true` if the node is inside JSX attribute value
159+
*/
160+
function isInsideJSXAttributeValue(node: AST.TSESTreeFunction) {
161+
return node.parent.type === T.JSXAttribute
162+
|| JSX.findParentAttribute(node, (n) => n.value?.type === T.JSXExpressionContainer) != null;
163+
}
164+
164165
/**
165166
* Determines whether inside `createElement`'s props.
166167
* @param context The rule context

0 commit comments

Comments
 (0)