Skip to content

Commit 6ec9c05

Browse files
committed
fix(lint): popup rule skips text and reads logical operators by what they render
1 parent 08115fe commit 6ec9c05

2 files changed

Lines changed: 98 additions & 28 deletions

File tree

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,64 @@ const el = <div className="space-y-2"><Popover /><>{visible && <p />}<span /></>
134134
const el = <div className="space-y-2"><Popover />{null}{show ? (<p />) : <span />}</div>;`,
135135
);
136136

137+
rule.invalid(
138+
"reports a popup followed only by text, which is not an element child",
139+
`${imports}
140+
const a = <div className="space-y-2"><Popover />{"After"}</div>;
141+
const b = <div className="space-y-2"><Popover /> After {1}{\`x\`}{true}</div>;
142+
const c = <div className="space-y-2"><Popover /><>text</></div>;`,
143+
(output) => {
144+
assert.strictEqual(output.match(/<Popover> can be the last child/g)?.length, 3);
145+
},
146+
);
147+
148+
rule.valid(
149+
"allows a popup followed by a logical expression that always renders an element",
150+
`${imports}
151+
const a = <div className="space-y-2"><Popover />{(ready && <p />) || <span />}</div>;
152+
const b = <div className="space-y-2"><Popover />{<p /> ?? <span />}{<p /> && <span />}</div>;`,
153+
);
154+
155+
rule.invalid(
156+
"reports a popup when a later logical expression may render text or nothing",
157+
`${imports}
158+
const a = <div className="space-y-2"><Popover />{label || <span />}</div>;
159+
const b = <div className="space-y-2"><Popover />{ready && "Ready"}</div>;
160+
const c = <div className="space-y-2"><Popover />{items.map((item) => <p key={item} />)}</div>;
161+
const d = <div className="space-y-2"><Popover />{(ready && <p />) ?? <span />}</div>;`,
162+
(output) => {
163+
assert.strictEqual(output.match(/<Popover> can be the last child/g)?.length, 4);
164+
},
165+
);
166+
167+
rule.invalid(
168+
"reports a popup followed by an unknown value",
169+
`${imports}
170+
const a = <div className="space-y-2"><Popover />{children}</div>;
171+
const b = <div className="space-y-2"><Popover />{props.footer}</div>;`,
172+
(output) => {
173+
assert.strictEqual(output.match(/<Popover> can be the last child/g)?.length, 2);
174+
},
175+
);
176+
177+
rule.invalid(
178+
"reports a popup on the left of an or that can be last",
179+
`${imports}
180+
const el = <div className="space-y-2"><p />{(open && <Popover />) || <span />}</div>;`,
181+
);
182+
183+
rule.valid(
184+
"allows a popup before a nested ternary that always renders",
185+
`${imports}
186+
const el = <div className="space-y-2"><Popover />{a ? <p /> : b ? <span /> : <i />}</div>;`,
187+
);
188+
189+
rule.invalid(
190+
"reports a popup before a nested ternary with an empty branch",
191+
`${imports}
192+
const el = <div className="space-y-2"><Popover />{a ? <p /> : b ? "text" : <i />}</div>;`,
193+
);
194+
137195
rule.invalid(
138196
"reports a popup last inside a direct or nested fragment",
139197
`${imports}

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

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ function lastChildUtility(element: ESTree.JSXElement) {
6969
return undefined;
7070
}
7171

72-
/** JSX an expression can render: both branches of a ternary, the right of `&&`, a map callback's result. */
73-
function renderedBy(expression: ESTree.Node | null | undefined): ESTree.Node[] {
72+
/** JSX an expression can render: both branches of a ternary or `||`, the right of `&&`, a map callback's result. */
73+
function renderedBy(expression: ESTree.Node | JsxChild | null | undefined): ESTree.Node[] {
7474
if (!expression) return [];
7575
switch (expression.type) {
76+
case "JSXExpressionContainer":
7677
case "ParenthesizedExpression":
7778
return renderedBy(expression.expression);
7879
case "JSXElement":
@@ -82,7 +83,9 @@ function renderedBy(expression: ESTree.Node | null | undefined): ESTree.Node[] {
8283
case "ConditionalExpression":
8384
return [...renderedBy(expression.consequent), ...renderedBy(expression.alternate)];
8485
case "LogicalExpression":
85-
return renderedBy(expression.right);
86+
return expression.operator === "&&"
87+
? renderedBy(expression.right)
88+
: [...renderedBy(expression.left), ...renderedBy(expression.right)];
8689
case "CallExpression": {
8790
const callback = expression.arguments[0];
8891
if (callback?.type !== "ArrowFunctionExpression" && callback?.type !== "FunctionExpression")
@@ -109,44 +112,53 @@ function returnedBy(body: ESTree.Node): ESTree.Node[] {
109112
return found;
110113
}
111114

112-
/** Whether an expression can render nothing, letting an earlier sibling end up last. */
113-
function mayRenderNothing(expression: ESTree.Node | JsxChild): boolean {
115+
type Rendered = "element" | "maybe" | "none";
116+
117+
const either = (a: Rendered, b: Rendered): Rendered => (a === b ? a : "maybe");
118+
119+
/** Whether an expression, or only its truthy values, always, maybe, or never renders an element; text counts as none. */
120+
function renders(expression: ESTree.Node | JsxChild, truthy = false): Rendered {
114121
switch (expression.type) {
122+
case "JSXElement":
123+
return "element";
124+
case "JSXFragment": {
125+
const children = expression.children.map((child) => renders(child));
126+
if (children.includes("element")) return "element";
127+
return children.includes("maybe") ? "maybe" : "none";
128+
}
115129
case "JSXText":
116-
return expression.value.trim() === "";
117130
case "JSXEmptyExpression":
118-
return true;
131+
case "Literal":
132+
case "TemplateLiteral":
133+
return "none";
119134
case "JSXExpressionContainer":
120135
case "ParenthesizedExpression":
121-
return mayRenderNothing(expression.expression);
122-
case "Literal":
123-
return expression.value === null || expression.value === false;
136+
return renders(expression.expression, truthy);
124137
case "Identifier":
125-
return expression.name === "undefined";
126-
case "LogicalExpression":
127-
case "CallExpression":
128-
return true;
138+
return expression.name === "undefined" ? "none" : "maybe";
139+
case "LogicalExpression": {
140+
const right = renders(expression.right, truthy);
141+
if (expression.operator === "&&") {
142+
return truthy || renders(expression.left) === "element" ? right : either("none", right);
143+
}
144+
return either(renders(expression.left, expression.operator === "||"), right);
145+
}
129146
case "ConditionalExpression":
130-
return mayRenderNothing(expression.consequent) || mayRenderNothing(expression.alternate);
131-
case "JSXFragment":
132-
return expression.children.every(mayRenderNothing);
147+
return either(renders(expression.consequent, truthy), renders(expression.alternate, truthy));
133148
default:
134-
return false;
149+
return "maybe";
135150
}
136151
}
137152

138-
/** Nodes that can end up as the last rendered child. */
153+
/** Nodes that can end up as the last rendered element child. */
139154
function possiblyLast(children: ReadonlyArray<JsxChild>): ESTree.Node[] {
140-
const meaningful = children.filter(
141-
(child) =>
142-
!(child.type === "JSXText" && child.value.trim() === "") &&
143-
!(child.type === "JSXExpressionContainer" && child.expression.type === "JSXEmptyExpression"),
144-
);
145155
const found: ESTree.Node[] = [];
146-
for (let index = meaningful.length - 1; index >= 0; index--) {
147-
const child = meaningful[index]!;
148-
found.push(...renderedBy(child.type === "JSXExpressionContainer" ? child.expression : child));
149-
if (!mayRenderNothing(child)) break;
156+
for (let index = children.length - 1; index >= 0; index--) {
157+
const child = children[index]!;
158+
const rendered = renders(child);
159+
if (rendered === "none") continue;
160+
found.push(...renderedBy(child));
161+
if (rendered === "element") break;
150162
}
151163
return found;
152164
}

0 commit comments

Comments
 (0)