Repository navigation
fix(compiler): a parameter shadows an outer const in SSR and class-field output - #5127
Conversation
🦋 Changeset detectedLatest commit: a98dce9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Merging this PR will degrade performance by 12.58%3 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
Summary
The native compiler's SSR output inlines an outer
constinto the server HTML even where a parameter of the same name shadows it:With
@solidjs/compiler2.0.2 this renders<ul><li>outer</li><li>outer</li></ul>on the server, where babel-plugin rendersaandb. The same happens for function parameters, destructured, default and rest parameters, attribute values (title={label}), and methods. The client compiles the parameter correctly, so server HTML and client render disagree, and hydration keeps the wrong server text.Constant folding asks the binding table whether a name refers to a known constant, and the table resolves a name to its innermost declaration. The DOM and universal transforms declare each function's parameters when they enter it (
declare_function_params), but the SSR transform has its own function and arrow visitors and kept the trait's no-op default. So inside an SSR function body a parameter name resolved to the outerconst, or to the module-constant fallback from #4799, and was folded. The SSR transform now declares parameters in both visitors, in the same order the shared DOM/universal visitors use.An expression-bodied arrow in a class field (
class A { row = label => <li>{label}</li> }) is lowered bylower_class_field_value, which skips the visitors. That path had the same gap in SSR and in DOM output, and now opens a function scope and declares the arrow's parameters before lowering its body. The universal transform already visited these.The
JsxTransform::declare_function_paramsdoc said "(dom only)", which was already out of date (universal overrides it too), so I dropped those words.Not changed here, and the same on
mainin all three modes:catch (label)andfor (const label of …)heads still fold an outerconst, because the binding table never declares those names. That is a separate fix. While testing I also saw DOM class-field output that differs from babel without any shadowing: the no-capture path flattens babel's setup IIFE (same behavior), a non-arrowel = <li>{label}</li>field is not folded (same behavior, less optimized), and a fragment-bodied class-field arrow that readsthisreferences_g2$without declaring it (row = y => <>{this.n}<li title={y} /></>throws a ReferenceError). I left those alone.Public API changes
None.
How did you test this change?
packages/compiler/__tests__/parity-probes.test.js: a function parameter, a.mapcallback parameter, a destructured parameter, a parameter in an attribute, a class-field arrow parameter, and a class-field arrow with a conditional body. Withmain's binary 26 checks fail (the first four inssr,ssr-hydratableandtsrx-ssr; the class-field case in every DOM and SSR mode; the conditional case in the SSR modes). With the fix all 4200 pass.cargo test,cargo clippy --all-targets -- -D warningsand rustfmt on the changed files pass. The compiler vitest suite (7027 passed, 2 skipped), babel-plugin (432) andpnpm testfrom the repo root (39 of 39 turbo tasks) pass.Verification
A script that compiles each case with babel-plugin and the native compiler and renders it with
renderToString. Published@solidjs/compiler2.0.2:This branch:
Code that does not shadow anything compiles to byte-identical output before and after.