Repository navigation
perf(solid): lower a static @if condition without <Show> - #1129
Conversation
Solid lowers `@if`, `@for`, `@switch` and `@try` bodies itself and did
not count a `JSXCodeBlock` as a render child. A `@{ … }` block, as an
element's child or as the body itself, went into the branch's statement
list; the walker then lowered it to an IIFE there, so its value was
dropped and the branch returned `null` (tsrx-org#1071).
Treat the block as a render child and place it as an expression child,
where the walker's lowering returns its output.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The Solid `@switch` lowering cloned the discriminant into every
`<Match when={d === caseN}>`, so each case evaluated it again. With a
discriminant that has a side effect, such as `queue.shift()`, each case
compared a different value: one render consumed several entries and
could pick the fallback instead of the matching case, on the server and
in the browser (tsrx-org#508).
When the discriminant is not a plain name, literal or property read,
hold it in `createMemo` (imported as `__createMemo`) inside an IIFE
around the `<Switch>`, and compare each case with the memo's value.
The memo keeps the Switch reactive. The type-only output keeps the
inline comparisons the editor maps.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
`<Show>` exists to track a reactive condition. A literal or an `import.meta.env.*` read cannot change at runtime, so render `test ? a : b` instead (tsrx-org#84). Only an `@if` with at most an `@else`, whose branches each lower to an element, a fragment or a literal, takes this path. A branch that lowers to an IIFE (setup statements, a `@{ }` block, a nested `@switch`) keeps `<Show>`, which runs it untracked; as a conditional operand it would run inside the enclosing insert effect. An `@else if` chain keeps `<Switch>`. The type-only output is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A `@{ }` block that was the whole body of a `@case` or `@default` still
rendered nothing: a case body holds the block as an expression
statement, which Solid treated as a plain statement, so the IIFE's
value was dropped. And a whole-body block in an `@if` or `@else if`
became `<Show>{(() => { … })()}</Show>`; Solid's Show and Switch read a
non-function child inside their memo, so the block's setup was tracked
and re-ran, recreating its output, whenever a signal it read changed.
A body whose only node is a `@{ }` block now takes the block's setup
statements and output as its own, so it lowers like the same body
without the `@{ }`: a function child that Show and Match run untracked.
A block beside other nodes keeps its own scope as an expression child.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Solid 2.0's `createMemo` treats a promise result as async: it suspends, then holds the resolved value. So `@switch (getPromise())` no longer compared the promise itself, as a JavaScript switch (and every other target) does, and could take a case instead of `@default`. Box the value, `createMemo(() => [d])`, and compare `value()[0]`. The memo and its import also used fixed names (`__switch_value`, `__createMemo`) that an authored binding could shadow; a discriminant reading a parameter named `__switch_value` hit a TDZ error. Name them with the reserved `_$_` prefix, which authored code cannot declare. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Brings in the review fixes from tsrx-org#1128: whole-body `@{ }` blocks lower like their body, and the memoized `@switch` discriminant is boxed and uses reserved names. Co-Authored-By: Claude Opus 5.5 <[email protected]>
main now has tsrx-org#1128's squash commit; merged against the parent commit this branch has so git doesn't redo that work. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit fb186fe. Configure here.
| : create_null_literal(); | ||
| if (!is_plain_output(consequent) || !is_plain_output(alternate)) { | ||
| return null; | ||
| } |
There was a problem hiding this comment.
Rejected static if lowers bodies twice
Low Severity
build_static_conditional always lowers both branch bodies before checking is_plain_output, then returns null and throws that tree away when a branch is not plain output. if_statement_to_jsx_child then lowers the same bodies again for <Show>. Nested diagnostics, generated identifiers, and transform_context side effects therefore run twice for a static @if that still needs <Show> (setup statements, a sole nested @switch IIFE, and similar).
Additional Locations (1)
Reviewed by Cursor Bugbot for commit fb186fe. Configure here.
main now has tsrx-org#1129's squash commit; merged against the parent commit this branch has so git doesn't redo that work. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
…ests This branch imports Solid's Show, For, Switch, Match, Errored and Loading and Vue's VaporFor under `__` names. Tests main added since (the static @if fallbacks from tsrx-org#1129, the @{ } block bodies from tsrx-org#1128, the memoized @switch, the untracked @Try body, and the keyed-array @for from tsrx-org#1135) matched the public names; they now match the private ones. The memoized @switch also imports `createMemo` from solid-js, so the generated import no longer fits on one line and the printer wraps it. The shared generated-import check and the @switch memo test now read an import across lines. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46
…e names (#1222) * fix(solid,vue): import generated control-flow components privately Solid imported Show, For, Switch, Match, Errored and Loading from solid-js under their public names, and Vue imported VaporFor the same way. An authored binding with one of those names either collided with the generated import (another module's import, a type-only import) or replaced the generated element (a module or setup local, or a `@for (const Match of rows)` loop variable). Import them as __Show, __For, __Switch, __Match, __Errored, __Loading and __VaporFor, the way #1175 imports Suspense, Fragment and TsrxErrorBoundary. Solid's compiler treats the aliased tags as plain components, and vue-jsx-vapor compiles <__VaporFor> exactly like <VaporFor> in DOM, SSR, HMR and interop output. defineVaporComponent keeps its public name: vue-jsx-vapor's macros, HMR registration, SSR registration and interop compile all find components by that callee name. * fix(vue): report authored defineVaporComponent bindings (TSRX2032) vue-jsx-vapor finds components by calls to `defineVaporComponent`, so the compiler's import of it can't take a private name the way VaporFor now does. An authored binding of that name either collided with the generated import (an import from another module) or replaced it (a module function or const), and vue-jsx-vapor's macros treat every call of the name as a component definition anyway. Report each such binding with a new code, TSRX2032: an import from anywhere but `vue-jsx-vapor`, a module function, const or class, and a local or parameter in any scope. In collect and editor mode a module-level binding keeps the name, and the compiler adds no import that would declare it again. A type-only import of `defineVaporComponent` from `vue-jsx-vapor` itself becomes the value import the components need. The error has an entry in the error tables, a row in the specification and an example the content mapper checks through TypeScript 7, which now covers Vue as well as React and Solid. * fix(core): treat any tag not starting with a-z as a component The previous commit named Solid's generated control-flow components `__Show`, `__Switch`, `__Loading` and so on. The compiler counted a tag as a component only when it started with A-Z, so those looked like host tags. Solid vetoes hoisting a subtree that renders a component, and without the veto a static `@if`, `@switch` or `@pending` subtree went to one module-level node: a component rendered twice showed one copy. Decide as JSX does instead: a tag is a host element only when its first character is a lowercase ASCII letter. Compiled on every .tsrx file in the repo, React, Preact, Hono and Vue output is byte-identical before and after, and Solid output matches #1175's apart from the private names. * fix(vue): cover the TSRX2032 edge cases - `declare function defineVaporComponent` declares the name, so it is reported too. - After any TSRX2032 the compiler adds no `defineVaporComponent` import. A `var` in a top-level block hoists to module scope, so the import redeclared it (TS2440) even though the binding is not at module level. - A namespace of types only binds no value, so it is not reported. - A quoted specifier, `{ "defineVaporComponent" as defineVaporComponent }` from `vue-jsx-vapor`, is the compiler's own import: it is not reported, and the compiler reuses it instead of adding the name again. * docs(changeset): note the component-name rule * test: expect the private Solid and Vue control-flow names in main's tests This branch imports Solid's Show, For, Switch, Match, Errored and Loading and Vue's VaporFor under `__` names. Tests main added since (the static @if fallbacks from #1129, the @{ } block bodies from #1128, the memoized @switch, the untracked @Try body, and the keyed-array @for from #1135) matched the public names; they now match the private ones. The memoized @switch also imports `createMemo` from solid-js, so the generated import no longer fits on one line and the printer wraps it. The shared generated-import check and the @switch memo test now read an import across lines. * test(vue): import defineVaporComponent from vue-jsx-vapor in the props params test Main's #1231 added a runtime test that imports `defineVaporComponent` from `vue` and calls it directly, so vue-jsx-vapor's props macro sees the patterns as written. This branch reports every binding of that name other than an import from `vue-jsx-vapor` (TSRX2032), because the compiler imports it from there for each component it wraps and a second binding would collide. Import it from `vue-jsx-vapor`, as the compiler does; the macro still finds the calls by name. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01FRKmwAwuyPbGQ1ejNqiJ46


Stacked on #1128; review commit b7211f7 (merge commit e9291ea brings in the #1128 review fixes); merge after #1128.
Summary
On Solid, an
@ifwhose condition cannot change at runtime no longer goes through<Show>:A condition counts as static when it is a literal, an
import.meta.env.*read (import.meta.env.DEV,import.meta.env.MODE), or the negation of either. Only an@ifwith at most an@elsetakes this path, and only when each branch lowers to an element, a fragment or a literal. A branch that lowers to an IIFE (setup statements, a@{ }block, a nested@switch) keeps<Show>, which runs its children untracked; as a conditional operand that IIFE would run inside the enclosing insert effect and track its reads. An@else ifchain keeps<Switch>.import.meta.urland otherimport.metareads are not treated as static. The type-only output is unchanged.Verification
packages/tsrx-solid/tests/basic.test.js: a literal, animport.meta.envread and a negated read compile to conditionals with noShowimport (failed before the change); a body with setup statements, an@else ifchain andimport.meta.urlkeep<Show>/<Switch>.packages/vite-plugin-solid/tests/runtime.test.tsrx:@if (true),@if (false)and@if (import.meta.env.MODE)render the selected branch.npx vitest run --testTimeout=60000at b7211f7 passed 172 of 177 files; the 5 failures were the three React typecheck fixtures at their 30s timeout, the TextMate grammar test and a CodeMirror highlighting test, all outside this change and all passing when rerun alone.pnpm typecheckclean,pnpm changeset:checkpasses.Related
Fixes #84
🤖 Generated with Claude Code
Note
Medium Risk
Changes Solid control-flow codegen for a common pattern; incorrect static detection or plain-output checks could alter tracking behavior or drop Show imports unexpectedly.
Overview
The Solid
@iflowering now skips<Show>when the condition is provably fixed at compile time and the shape is simple enough to emit a plain ternary in JSX.When it applies: literal conditions,
import.meta.env.*(including negated forms), at most an@else(no@else if), and each branch lowers to plain output only (element, fragment, or literal). Production output uses expressions like{true ? <Child /> : <Fallback />}instead of<Show>. Type-only transforms are unchanged; branches with setup/@{ }IIFEs or multi-branch chains still use<Show>/<Switch>so untracked branch execution is preserved. Non-static checks such asimport.meta.urlkeep<Show>.Patch release notes and compile/runtime tests cover the new emission and the fallback cases.
Reviewed by Cursor Bugbot for commit fb186fe. Bugbot is set up for automated code reviews on this repo. Configure here.