Skip to content

Commit 02f5efe

Browse files
committed
fix(lint): popup rule sees through fragments and false/undefined branches
1 parent e3593e6 commit 02f5efe

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

‎oxlint-plugin-t3code/rules/no-popup-last-in-spaced-container.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,27 @@ const el = <div className={cn("sm:divide-y", active && "ring")}><p>Before</p><Po
9090
const el = <div className="space-y-2"><Popover />{error ? <p>{error}</p> : null}</div>;`,
9191
);
9292

93+
rule.invalid(
94+
"reports a popup last when a later ternary renders false or undefined",
95+
`${imports}
96+
const a = <div className="space-y-2"><Popover />{error ? <p>{error}</p> : false}</div>;
97+
const b = <div className="space-y-2"><Popover />{error ? <p>{error}</p> : undefined}</div>;`,
98+
(output) => {
99+
assert.strictEqual(output.match(/<Popover> can be the last child/g)?.length, 2);
100+
},
101+
);
102+
103+
rule.invalid(
104+
"reports a popup last inside a direct or nested fragment",
105+
`${imports}
106+
const a = <div className="space-y-3"><p>Before</p><><Menu /></></div>;
107+
const b = <div className="space-y-3"><p>Before</p><><p /><><Popover /></></></div>;`,
108+
(output) => {
109+
assert.match(output, /<Menu> can be the last child/);
110+
assert.match(output, /<Popover> can be the last child/);
111+
},
112+
);
113+
93114
rule.invalid(
94115
"reports a local component that renders a popup",
95116
`${imports}

‎oxlint-plugin-t3code/rules/no-popup-last-in-spaced-container.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,18 @@ function returnedBy(body: ESTree.Node): ESTree.Node[] {
109109
return found;
110110
}
111111

112+
const rendersNothing = (branch: ESTree.Node) =>
113+
(branch.type === "Literal" && (branch.value === null || branch.value === false)) ||
114+
(branch.type === "Identifier" && branch.name === "undefined");
115+
112116
/** Whether a child can render nothing, letting an earlier sibling end up last. */
113117
function mayRenderNothing(child: JsxChild) {
114118
if (child.type !== "JSXExpressionContainer") return false;
115119
const { expression } = child;
116120
if (expression.type === "LogicalExpression" || expression.type === "CallExpression") return true;
117121
return (
118122
expression.type === "ConditionalExpression" &&
119-
[expression.consequent, expression.alternate].some(
120-
(branch) => branch.type === "Literal" && branch.value === null,
121-
)
123+
[expression.consequent, expression.alternate].some(rendersNothing)
122124
);
123125
}
124126

@@ -132,9 +134,7 @@ function possiblyLast(children: ReadonlyArray<JsxChild>): ESTree.Node[] {
132134
const found: ESTree.Node[] = [];
133135
for (let index = meaningful.length - 1; index >= 0; index--) {
134136
const child = meaningful[index]!;
135-
found.push(
136-
...(child.type === "JSXExpressionContainer" ? renderedBy(child.expression) : [child]),
137-
);
137+
found.push(...renderedBy(child.type === "JSXExpressionContainer" ? child.expression : child));
138138
if (!mayRenderNothing(child)) break;
139139
}
140140
return found;

0 commit comments

Comments
 (0)