Skip to content

Commit 8c871ae

Browse files
committed
Merge branch 'fix/ignore-jsx-callbacks'
2 parents 91ee2db + 6a98e66 commit 8c871ae

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

docs/components-return-once.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ function Component(props) {
106106
return props.primary || <div>{props.secondaryText}</div>;
107107
}
108108

109+
HOC(() => {
110+
if (condition) {
111+
return <div />;
112+
}
113+
return <div />;
114+
});
115+
109116
```
110117

111118
### Valid Examples
@@ -131,5 +138,12 @@ function notAComponent() {
131138
return <div />;
132139
}
133140

141+
callback(() => {
142+
if (condition) {
143+
return <div />;
144+
}
145+
return <div />;
146+
});
147+
134148
```
135149
<!-- AUTO-GENERATED-CONTENT:END -->

src/rules/components-return-once.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ const rule: TSESLint.RuleModule<"noEarlyReturn" | "noConditionalReturn", []> = {
6262
const onFunctionExit = (node: FunctionNode) => {
6363
if (
6464
(node.type === "FunctionDeclaration" && node.id?.name?.match(/^[a-z]/)) ||
65-
node.parent?.type === "JSXExpressionContainer" // "render props" aren't components
65+
// "render props" aren't components
66+
node.parent?.type === "JSXExpressionContainer" ||
67+
// ignore createMemo(() => conditional JSX), report HOC(() => conditional JSX)
68+
(node.parent?.type === "CallExpression" &&
69+
node.parent.arguments.some((n) => n === node) &&
70+
!(node.parent.callee as T.Identifier).name?.match(/^[A-Z]/))
6671
) {
6772
currentFunction().isComponent = false;
6873
}

test/rules/components-return-once.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export const cases = run("components-return-once", rule, {
1818
}
1919
return <div />;
2020
}`,
21+
`callback(() => {
22+
if (condition) {
23+
return <div />;
24+
}
25+
return <div />;
26+
});`,
2127
],
2228
invalid: [
2329
// Early returns
@@ -111,5 +117,15 @@ export const cases = run("components-return-once", rule, {
111117
}`,
112118
errors: [{ messageId: "noConditionalReturn" }],
113119
},
120+
// HOCs
121+
{
122+
code: `HOC(() => {
123+
if (condition) {
124+
return <div />;
125+
}
126+
return <div />;
127+
});`,
128+
errors: [{ messageId: "noEarlyReturn" }],
129+
},
114130
],
115131
});

0 commit comments

Comments
 (0)