File tree Expand file tree Collapse file tree
oxlint-plugin-t3code/rules Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -90,6 +90,27 @@ const el = <div className={cn("sm:divide-y", active && "ring")}><p>Before</p><Po
9090const 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 ( / < P o p o v e r > c a n b e t h e l a s t c h i l d / 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 , / < M e n u > c a n b e t h e l a s t c h i l d / ) ;
110+ assert . match ( output , / < P o p o v e r > c a n b e t h e l a s t c h i l d / ) ;
111+ } ,
112+ ) ;
113+
93114 rule . invalid (
94115 "reports a local component that renders a popup" ,
95116 `${ imports }
Original file line number Diff line number Diff 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. */
113117function 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 ;
You can’t perform that action at this time.
0 commit comments