Skip to content

Commit 6fe9101

Browse files
authored
fix: fixed potential false negatives in no-leaked-conditional-rendering when logical expressions are wrapped by type expressions (#1068)
1 parent d50055f commit 6fe9101

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

packages/plugins/eslint-plugin-react-x/src/rules/no-leaked-conditional-rendering.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ export default createRule<[], MessageID>({
192192
});
193193

194194
export function create(context: RuleContext<MessageID, []>): RuleListener {
195-
if (!context.sourceCode.text.includes("&&") && !context.sourceCode.text.includes("?")) return {};
196-
195+
if (!context.sourceCode.text.includes("&&")) return {};
197196
const { version } = getSettingsFromContext(context);
198197

199198
// Allowed left node type variants
@@ -213,10 +212,18 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
213212
] as const satisfies VariantType[];
214213

215214
const services = ESLintUtils.getParserServices(context, false);
216-
function getReportDescriptor(node: TSESTree.Expression | _): ReportDescriptor<MessageID> | _ {
215+
function getReportDescriptor(
216+
node:
217+
| _
218+
| TSESTree.Expression
219+
| TSESTree.JSXExpressionContainer
220+
| TSESTree.JSXExpressionContainer["expression"],
221+
): ReportDescriptor<MessageID> | _ {
217222
if (node == null) return _;
223+
if (AST.is(T.JSXExpressionContainer)(node)) return getReportDescriptor(node.expression);
224+
if (AST.isJSX(node)) return _;
225+
if (AST.isTypeExpression(node)) return getReportDescriptor(node.expression);
218226
return match<typeof node, ReportDescriptor<MessageID> | _>(node)
219-
.when(AST.isJSX, () => _)
220227
.with({ type: T.LogicalExpression, operator: "&&" }, ({ left, right }) => {
221228
const isLeftUnaryNot = left.type === T.UnaryExpression && left.operator === "!";
222229
if (isLeftUnaryNot) {
@@ -260,10 +267,8 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
260267
})
261268
.otherwise(() => _);
262269
}
263-
const visitorFunction = flow(getReportDescriptor, Report.make(context).send);
264270
return {
265-
"JSXExpressionContainer > ConditionalExpression": visitorFunction,
266-
"JSXExpressionContainer > LogicalExpression": visitorFunction,
271+
JSXExpressionContainer: flow(getReportDescriptor, Report.make(context).send),
267272
};
268273
}
269274

0 commit comments

Comments
 (0)