@barefootjs/jsx 0.41.0 → 0.41.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/dist/adapters/attr-plan.d.ts +8 -0
  2. package/dist/adapters/attr-plan.d.ts.map +1 -1
  3. package/dist/adapters/component-props.d.ts +11 -1
  4. package/dist/adapters/component-props.d.ts.map +1 -1
  5. package/dist/adapters/computed-const.d.ts +19 -0
  6. package/dist/adapters/computed-const.d.ts.map +1 -1
  7. package/dist/adapters/dsl-template-adapter.d.ts +2 -1
  8. package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
  9. package/dist/adapters/inherited-const-server-read.d.ts +37 -0
  10. package/dist/adapters/inherited-const-server-read.d.ts.map +1 -0
  11. package/dist/adapters/loop-row-type.d.ts +18 -0
  12. package/dist/adapters/loop-row-type.d.ts.map +1 -1
  13. package/dist/adapters/non-primitive-equality.d.ts +8 -2
  14. package/dist/adapters/non-primitive-equality.d.ts.map +1 -1
  15. package/dist/adapters/number-value.d.ts +2 -1
  16. package/dist/adapters/number-value.d.ts.map +1 -1
  17. package/dist/adapters/unapplied-prop-default.d.ts +34 -0
  18. package/dist/adapters/unapplied-prop-default.d.ts.map +1 -0
  19. package/dist/adapters/uninlinable-seed-call.d.ts +29 -0
  20. package/dist/adapters/uninlinable-seed-call.d.ts.map +1 -0
  21. package/dist/analyzer.d.ts.map +1 -1
  22. package/dist/augment-inherited-props.d.ts +4 -22
  23. package/dist/augment-inherited-props.d.ts.map +1 -1
  24. package/dist/const-identity.d.ts +38 -0
  25. package/dist/const-identity.d.ts.map +1 -0
  26. package/dist/index.d.ts +10 -5
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +2500 -1475
  29. package/dist/ir-to-client-js/child-props-object.d.ts +39 -41
  30. package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -1
  31. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  32. package/dist/ir-to-client-js/component-scope.d.ts +13 -2
  33. package/dist/ir-to-client-js/component-scope.d.ts.map +1 -1
  34. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
  35. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
  36. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  37. package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
  38. package/dist/jsx-to-ir.d.ts.map +1 -1
  39. package/dist/local-call-body.d.ts +112 -0
  40. package/dist/local-call-body.d.ts.map +1 -0
  41. package/dist/nullable-signal-attr.d.ts +10 -0
  42. package/dist/nullable-signal-attr.d.ts.map +1 -1
  43. package/dist/prop-default-resolution.d.ts +52 -0
  44. package/dist/prop-default-resolution.d.ts.map +1 -0
  45. package/dist/props-binding.d.ts +15 -0
  46. package/dist/props-binding.d.ts.map +1 -1
  47. package/dist/server-read-walk.d.ts +66 -0
  48. package/dist/server-read-walk.d.ts.map +1 -0
  49. package/dist/ssr-defaults.d.ts.map +1 -1
  50. package/dist/ssr-seed-plan.d.ts +24 -1
  51. package/dist/ssr-seed-plan.d.ts.map +1 -1
  52. package/dist/static-const-member.d.ts +92 -0
  53. package/dist/static-const-member.d.ts.map +1 -0
  54. package/dist/types.d.ts +59 -0
  55. package/dist/types.d.ts.map +1 -1
  56. package/package.json +2 -2
  57. package/src/__tests__/computed-const.test.ts +7 -1
  58. package/src/__tests__/const-identity-child-prop.test.ts +127 -0
  59. package/src/__tests__/inherited-const-server-read.test.ts +116 -0
  60. package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +8 -3
  61. package/src/__tests__/issue-3714-open-rest-spread-child.test.ts +188 -0
  62. package/src/__tests__/local-call-body.test.ts +101 -0
  63. package/src/__tests__/prop-default-resolution.test.ts +185 -0
  64. package/src/__tests__/props-spread-shadowed.test.ts +113 -0
  65. package/src/__tests__/server-read-walk.test.ts +125 -0
  66. package/src/__tests__/ssr-defaults.test.ts +67 -7
  67. package/src/__tests__/static-const-child-prop.test.ts +85 -0
  68. package/src/__tests__/static-record-literal-array.test.ts +82 -11
  69. package/src/adapters/attr-plan.ts +12 -1
  70. package/src/adapters/component-props.ts +19 -3
  71. package/src/adapters/computed-const.ts +108 -12
  72. package/src/adapters/dsl-template-adapter.ts +17 -4
  73. package/src/adapters/inherited-const-server-read.ts +174 -0
  74. package/src/adapters/loop-row-type.ts +69 -2
  75. package/src/adapters/non-primitive-equality.ts +17 -2
  76. package/src/adapters/number-value.ts +26 -2
  77. package/src/adapters/unapplied-prop-default.ts +83 -0
  78. package/src/adapters/uninlinable-seed-call.ts +62 -0
  79. package/src/analyzer.ts +43 -3
  80. package/src/augment-inherited-props.ts +4 -49
  81. package/src/compiler.ts +4 -4
  82. package/src/const-identity.ts +189 -0
  83. package/src/index.ts +10 -5
  84. package/src/ir-to-client-js/child-props-object.ts +94 -62
  85. package/src/ir-to-client-js/collect-elements.ts +28 -22
  86. package/src/ir-to-client-js/component-scope.ts +16 -2
  87. package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +1 -0
  88. package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +1 -0
  89. package/src/ir-to-client-js/html-template.ts +24 -4
  90. package/src/ir-to-client-js/index.ts +2 -2
  91. package/src/ir-to-client-js/prop-handling.ts +4 -0
  92. package/src/jsx-to-ir.ts +121 -60
  93. package/src/local-call-body.ts +191 -0
  94. package/src/nullable-signal-attr.ts +39 -17
  95. package/src/prop-default-resolution.ts +195 -0
  96. package/src/props-binding.ts +16 -0
  97. package/src/server-read-walk.ts +174 -0
  98. package/src/ssr-defaults.ts +93 -10
  99. package/src/ssr-seed-plan.ts +104 -18
  100. package/src/static-const-member.ts +243 -0
  101. package/src/types.ts +56 -0
@@ -0,0 +1,195 @@
1
+ /**
2
+ * What a computed destructure default evaluates, once the declarations it
3
+ * reads are resolved (#3725, #3791).
4
+ *
5
+ * A backend applies a computed default when it renders (the SSR seed plan's
6
+ * prop steps, Go's props constructor), and it receives the props, not the JS
7
+ * scope: a default naming a const (`const DEF = 'z'`, `{ b =
8
+ * DEF.toUpperCase() }`) or calling a function has nothing to read there.
9
+ * This is the one resolution of those names into the expression the default
10
+ * stands for, each name read lexically (`localBindings`):
11
+ *
12
+ * - A body destructure (`const { b = DEF } = props`) reads the component
13
+ * scope, where a component const hides a module const of its name.
14
+ * - A parameter destructure (`function C({ b = DEF })`) reads the module
15
+ * scope: a parameter default cannot see the body's declarations.
16
+ *
17
+ * A `const` is replaced by its own value resolved in the scope it is
18
+ * declared in, and a zero-argument call of a function a template inlines
19
+ * (`localCall`) by the expression it returns. A module value is taken only
20
+ * when nothing is left to read in it, so no name it reads can meet a
21
+ * component binding where it lands. What stays free is a prop, the props
22
+ * object, a global, or a module name no backend computes.
23
+ *
24
+ * A component-scope declaration that has no such value (a signal, a memo, a
25
+ * `let`, a mutated const, a function with parameters or statements, a const
26
+ * declared after the destructure, which JS has not initialized when the
27
+ * default runs) leaves the default with nothing a template can compute: it
28
+ * is `blocked`, and the adapters refuse it where the server render reads the
29
+ * prop (`refuseUnappliedPropDefaults`).
30
+ */
31
+
32
+ import { freeIdentifiers, mapParsedExpr, type ParsedExpr } from './expression-parser.ts'
33
+ import { localBindings, localCall, type LocalBinding, type LocalBindingResolver, type LocalScope } from './local-call-body.ts'
34
+ import { templatePropReadParams } from './props-binding.ts'
35
+ import type { IRMetadata, ParamInfo, Position, SourceLocation } from './types.ts'
36
+
37
+ /** A computed destructure default with the declarations it reads resolved. */
38
+ export interface ResolvedPropDefault {
39
+ /** The default as the expression it stands for. */
40
+ expr: ParsedExpr
41
+ /** Whether a declaration the default reads was resolved into `expr`. */
42
+ readsDeclaration: boolean
43
+ /** Where a body destructure binds the prop; unset for a parameter default. */
44
+ loc: SourceLocation | undefined
45
+ /** The component declarations it reads that have no value a template can compute. */
46
+ blocked: readonly string[]
47
+ }
48
+
49
+ /** The metadata {@link propDefaultResolver} reads. */
50
+ export type PropDefaultBindings = Pick<
51
+ IRMetadata,
52
+ 'propsParams' | 'propsObjectName' | 'restPropsName' | 'signals' | 'memos' | 'localFunctions' | 'localConstants'
53
+ >
54
+
55
+ /** One expression resolved in one scope. */
56
+ interface Resolution {
57
+ expr: ParsedExpr
58
+ blocked: Set<string>
59
+ /** Whether a declaration was resolved into `expr`. */
60
+ replaced: boolean
61
+ }
62
+
63
+ /** What a free name of an expression is replaced by. */
64
+ type Replacement =
65
+ | { kind: 'keep' }
66
+ | { kind: 'blocked' }
67
+ /** Every free read of the name. */
68
+ | { kind: 'value'; value: Resolution }
69
+ /** Only a call of the name with no arguments; any other read of it is `otherwise`. */
70
+ | { kind: 'call'; value: Resolution; otherwise: 'keep' | 'blocked' }
71
+
72
+ /**
73
+ * The resolver of a component's destructure defaults: `p`'s default as the
74
+ * expression it stands for ({@link ResolvedPropDefault}), or `null` for a
75
+ * prop with no parsed default.
76
+ */
77
+ export function propDefaultResolver(metadata: PropDefaultBindings): (p: ParamInfo) => ResolvedPropDefault | null {
78
+ const bindings = { ...metadata, localConstants: metadata.localConstants ?? [] }
79
+ const resolve = localBindings(bindings)
80
+ const props = new Set<string>(templatePropReadParams(bindings).map(p => p.name))
81
+ if (metadata.propsObjectName) props.add(metadata.propsObjectName)
82
+ if (metadata.restPropsName) props.add(metadata.restPropsName)
83
+ const bodyBound = new Map<string, SourceLocation>()
84
+ for (const c of bindings.localConstants) if (!c.isModule && c.propsDestructure) bodyBound.set(c.name, c.loc)
85
+ return (p) => {
86
+ if (p.isRest || !p.parsed) return null
87
+ const loc = bodyBound.get(p.name)
88
+ const resolver = new DeclarationResolver(resolve, props, loc?.start)
89
+ const { expr, blocked, replaced } = resolver.resolveExpr(p.parsed, loc ? 'component' : 'module', true)
90
+ return { expr, blocked: [...blocked], readsDeclaration: replaced, loc }
91
+ }
92
+ }
93
+
94
+ /** Resolves one default through the declarations it reads, each declaration once. */
95
+ class DeclarationResolver {
96
+ private readonly values = new Map<LocalBinding['scope'], Map<string, Resolution | null>>([
97
+ ['component', new Map()],
98
+ ['module', new Map()],
99
+ ])
100
+
101
+ constructor(
102
+ private readonly resolve: LocalBindingResolver,
103
+ /** The names a template reads as props, which hide a module declaration of the name. */
104
+ private readonly props: ReadonlySet<string>,
105
+ /** Where the body destructure holding the default starts: a component const declared from there on is not initialized yet. */
106
+ private readonly runsAt: Position | undefined,
107
+ ) {}
108
+
109
+ /**
110
+ * `expr` with each free name read from `scope` replaced by what it
111
+ * denotes. `propsInScope` is whether the expression sits where the props
112
+ * are bound (a default, a component declaration), not in a module one.
113
+ */
114
+ resolveExpr(expr: ParsedExpr, scope: LocalScope, propsInScope: boolean): Resolution {
115
+ const blocked = new Set<string>()
116
+ const free = freeIdentifiers(expr)
117
+ if (!free) return { expr, blocked, replaced: false }
118
+ const replacements = new Map<string, Replacement>()
119
+ for (const name of free) {
120
+ const replacement = propsInScope && this.props.has(name) ? KEEP : this.replacementOf(name, scope)
121
+ if (replacement.kind !== 'keep') replacements.set(name, replacement)
122
+ }
123
+ if (replacements.size === 0) return { expr, blocked, replaced: false }
124
+ let replaced = false
125
+ // A value landing under a callback param of one of its own names would read the param.
126
+ const take = (name: string, value: Resolution, enclosing: ReadonlySet<string>): ParsedExpr | null => {
127
+ const reads = freeIdentifiers(value.expr)
128
+ if (!reads || [...reads].some(read => enclosing.has(read))) {
129
+ blocked.add(name)
130
+ return null
131
+ }
132
+ for (const b of value.blocked) blocked.add(b)
133
+ replaced = true
134
+ return value.expr
135
+ }
136
+ const resolved = mapParsedExpr(expr, (e, enclosing) => {
137
+ if (e.kind === 'call' && e.args.length === 0 && e.callee.kind === 'identifier' && !enclosing.has(e.callee.name)) {
138
+ const r = replacements.get(e.callee.name)
139
+ if (r?.kind === 'call') return take(e.callee.name, r.value, enclosing)
140
+ }
141
+ if (e.kind !== 'identifier' || enclosing.has(e.name)) return null
142
+ const r = replacements.get(e.name)
143
+ if (r?.kind === 'value') return take(e.name, r.value, enclosing)
144
+ if (r?.kind === 'blocked' || (r?.kind === 'call' && r.otherwise === 'blocked')) blocked.add(e.name)
145
+ return null
146
+ })
147
+ return { expr: replaced ? resolved : expr, blocked, replaced }
148
+ }
149
+
150
+ /** What a free read of `name` from `scope` is replaced by. */
151
+ private replacementOf(name: string, scope: LocalScope): Replacement {
152
+ const binding = this.resolve(name, scope)
153
+ if (!binding || binding.kind === 'opaque') return KEEP
154
+ // A module declaration no backend computes stays a name, as before.
155
+ const otherwise = binding.scope === 'component' ? 'blocked' : 'keep'
156
+ if (binding.kind === 'const' && binding.scope === 'component' && !this.isInitialized(binding.info.loc?.start)) return { kind: 'blocked' }
157
+ const call = localCall(binding)
158
+ if (call) {
159
+ const value = call.kind === 'inline' ? this.valueOf(name, binding, call.body) : null
160
+ return value ? { kind: 'call', value, otherwise } : { kind: otherwise }
161
+ }
162
+ const value = binding.kind === 'const' && isStableConst(binding) ? this.valueOf(name, binding, binding.info.parsed!) : null
163
+ return value ? { kind: 'value', value } : { kind: otherwise }
164
+ }
165
+
166
+ /** Whether a component const declared at `declared` holds its value when the default runs. */
167
+ private isInitialized(declared: Position | undefined): boolean {
168
+ const at = this.runsAt
169
+ return !at || !declared || declared.line < at.line || (declared.line === at.line && declared.column < at.column)
170
+ }
171
+
172
+ /**
173
+ * The value `binding` (declared as `name`) holds, resolved in its own
174
+ * scope: `null` for a module value that still reads something, or one
175
+ * that reads itself.
176
+ */
177
+ private valueOf(name: string, binding: LocalBinding, expr: ParsedExpr): Resolution | null {
178
+ const known = this.values.get(binding.scope)!
179
+ if (known.has(name)) return known.get(name) ?? null
180
+ known.set(name, null)
181
+ const value = this.resolveExpr(expr, binding.scope, binding.scope === 'component')
182
+ const closed = binding.scope === 'component' || freeIdentifiers(value.expr)?.size === 0
183
+ const result = closed ? value : null
184
+ known.set(name, result)
185
+ return result
186
+ }
187
+ }
188
+
189
+ const KEEP: Replacement = { kind: 'keep' }
190
+
191
+ /** A `const` holding the one value its initializer gives it, which a template can read in its place. */
192
+ function isStableConst(binding: Extract<LocalBinding, { kind: 'const' }>): boolean {
193
+ const c = binding.info
194
+ return c.declarationKind === 'const' && !c.mutatedAfterDeclaration && c.parsed !== undefined && !c.isJsx && !c.isJsxFunction
195
+ }
@@ -619,6 +619,22 @@ export interface RestSpreadBindings {
619
619
  propsObjectName: string | null
620
620
  }
621
621
 
622
+ /**
623
+ * The caller keys the component's rest bag (`...rest`) leaves out: the keys
624
+ * its destructure takes before the rest element, spelled as the caller
625
+ * passes them. A body destructure of the props object (`const { a, ...rest }
626
+ * = props`) records them itself (`restPropsOmittedKeys`); a signature rest
627
+ * takes every destructured param, by its source key (`{ value: v }` takes
628
+ * `value`). The one definition `SpreadAttr.restOmittedKeys` is built from.
629
+ */
630
+ export function restBagOmittedKeys(bindings: {
631
+ propsParams: ReadonlyArray<{ name: string; sourceName?: string }>
632
+ restPropsOmittedKeys?: readonly string[] | null
633
+ }): string[] {
634
+ if (bindings.restPropsOmittedKeys) return [...bindings.restPropsOmittedKeys]
635
+ return bindings.propsParams.map(p => p.sourceName ?? p.name)
636
+ }
637
+
622
638
  /**
623
639
  * Walks a bare `const x__alias = <name>` hop chain recorded in
624
640
  * `constantValues` (name → initializer text) until `terminal` recognizes
@@ -0,0 +1,174 @@
1
+ /**
2
+ * The declarations a component's server render reads (#3721).
3
+ *
4
+ * A template renders a position from the values it reads there, and each of
5
+ * those from the values its own expression reads: a signal's seed, a memo's
6
+ * body, a const's initializer, the expression a called function returns.
7
+ * This is the one walk of that relation. It starts at every template
8
+ * position (`walkTemplatePositions`, which skips `/* @client *\/` reads) and
9
+ * follows the names each expression reads free, each resolved lexically
10
+ * (`localBindings`): a position's names in the scope it is read in, so a
11
+ * loop row, a callback param or a preamble local of a name is not the
12
+ * component's declaration of it (`BindingScope`); a declaration's names in
13
+ * the scope it is declared in, so a module function's body never reads a
14
+ * component binding.
15
+ *
16
+ * A function is read only where it is called with no arguments and a
17
+ * template inlines the call (`localCall`): its returned expression is then
18
+ * followed like any other. A call a template cannot inline ends the walk
19
+ * there and is reported on the declaration that makes it
20
+ * (`ServerRead.refusedCalls`). A function passed on uncalled, an event
21
+ * handler prop and a module-scope signal or memo (which never renders on the
22
+ * server) read nothing here.
23
+ */
24
+
25
+ import { freeIdentifiers, mapParsedExpr, type ParsedExpr, type ParsedStatement } from './expression-parser.ts'
26
+ import { isEventHandlerName } from './event-handler-name.ts'
27
+ import { localBindings, localCall, type LocalBinding, type LocalBindingResolver, type LocalScope } from './local-call-body.ts'
28
+ import { BindingScope } from './scope/binding-scope.ts'
29
+ import { walkTemplatePositions, type TemplatePosition } from './template-position-walk.ts'
30
+ import type { ComponentIR, SourceLocation } from './types.ts'
31
+
32
+ /** A declaration the server render reads. */
33
+ export interface ServerRead {
34
+ name: string
35
+ kind: 'signal' | 'memo' | 'const' | 'function'
36
+ /** The scope the declaration sits in. */
37
+ scope: LocalScope
38
+ loc: SourceLocation
39
+ /**
40
+ * The expression its value comes from (a called function's returned
41
+ * expression), read in `scope`. Unset for a declaration with no
42
+ * expression tree (a block-bodied memo no single expression stands for).
43
+ */
44
+ expr: ParsedExpr | undefined
45
+ /**
46
+ * The statements of a block-bodied memo with no `expr`, as far as its
47
+ * parse represents them. No server value is computed from them, but they
48
+ * are what the browser evaluates for the same slot.
49
+ */
50
+ block: readonly ParsedStatement[] | undefined
51
+ /**
52
+ * The functions its expression calls with no arguments that a template
53
+ * cannot inline, by the name they are called under. Empty for a
54
+ * declaration with no expression tree (a block-bodied memo).
55
+ */
56
+ refusedCalls: readonly string[]
57
+ }
58
+
59
+ /** What one expression reads: its free names, and those it calls with no arguments. */
60
+ interface Reads {
61
+ names: Iterable<string>
62
+ called: ReadonlySet<string>
63
+ }
64
+
65
+ /** A declaration the walk follows, with the expression its value comes from. */
66
+ interface Followed {
67
+ kind: ServerRead['kind']
68
+ loc: SourceLocation
69
+ expr: ParsedExpr | undefined
70
+ block?: readonly ParsedStatement[]
71
+ /** The names the declaration reads when its expression has no tree. */
72
+ fallback: ReadonlySet<string> | undefined
73
+ }
74
+
75
+ /** What {@link walkServerReads} reports besides the declarations, and where it starts. */
76
+ export interface ServerReadWalkOptions {
77
+ /**
78
+ * Sees each name read that has no declaration to follow: a prop in scope
79
+ * by its bare name, the props object, the rest props.
80
+ */
81
+ visitProp?: (name: string) => void
82
+ /** The positions the walk starts from; every position when unset. */
83
+ startsAt?: (position: TemplatePosition) => boolean
84
+ }
85
+
86
+ /** Visit each declaration the server render of `ir` reads, once. */
87
+ export function walkServerReads(ir: ComponentIR, visit: (read: ServerRead) => void, options: ServerReadWalkOptions = {}): void {
88
+ const { visitProp, startsAt } = options
89
+ const resolve = localBindings({ ...ir.metadata, localConstants: ir.metadata.localConstants ?? [] })
90
+ const seen = new Set<LocalBinding>()
91
+ const pending: Array<{ reads: Reads; from: LocalScope }> = []
92
+
93
+ const follow = (name: string, binding: LocalBinding): void => {
94
+ const followed = followedDeclaration(binding)
95
+ if (!followed || seen.has(binding)) return
96
+ seen.add(binding)
97
+ const reads = readsOf(followed.expr, followed.fallback)
98
+ const refusedCalls = [...reads.called].filter(fn => localCall(resolve(fn, binding.scope))?.kind === 'refuse')
99
+ visit({ name, kind: followed.kind, scope: binding.scope, loc: followed.loc, expr: followed.expr, block: followed.block, refusedCalls })
100
+ pending.push({ reads, from: binding.scope })
101
+ }
102
+ const followReads = (reads: Reads, from: LocalScope, isBound: (name: string) => boolean): void => {
103
+ for (const name of reads.names) {
104
+ if (isBound(name)) continue
105
+ const binding = resolve(name, from)
106
+ if (binding?.kind === 'opaque') visitProp?.(name)
107
+ else if (binding && isRead(binding, reads.called.has(name))) follow(name, binding)
108
+ }
109
+ }
110
+
111
+ const component = BindingScope.forComponent(ir.metadata.propsObjectName ?? null)
112
+ walkTemplatePositions(ir.root, (position) => {
113
+ const { expr, scope, attrName } = position
114
+ if (attrName !== undefined && isEventHandlerName(attrName)) return
115
+ if (startsAt && !startsAt(position)) return
116
+ followReads(readsOf(expr, undefined), 'component', name => (scope ?? component).isBound(name))
117
+ }, component)
118
+ for (let next = pending.pop(); next; next = pending.pop()) followReads(next.reads, next.from, () => false)
119
+ }
120
+
121
+ /** Whether reading `binding` reads its declaration: a function only where it is called. */
122
+ function isRead(binding: LocalBinding, called: boolean): boolean {
123
+ if (binding.kind === 'function' || (binding.kind === 'const' && binding.info.parsed?.kind === 'arrow')) {
124
+ return called && localCall(binding)?.kind === 'inline'
125
+ }
126
+ return true
127
+ }
128
+
129
+ /** {@link Followed} for `binding`, or `null` for one with nothing the server render computes. */
130
+ function followedDeclaration(binding: LocalBinding): Followed | null {
131
+ switch (binding.kind) {
132
+ case 'opaque':
133
+ return null
134
+ case 'signal': {
135
+ const s = binding.info
136
+ if (s.isModule || s.envReader) return null
137
+ return { kind: 'signal', loc: s.loc, expr: s.parsed, fallback: s.initialFreeIdentifiers }
138
+ }
139
+ case 'memo': {
140
+ const m = binding.info
141
+ if (m.isModule) return null
142
+ const block = m.parsed ? undefined : m.parsedBlock
143
+ return { kind: 'memo', loc: m.loc, expr: m.parsed, block, fallback: m.computationFreeIdentifiers }
144
+ }
145
+ case 'const': {
146
+ const c = binding.info
147
+ // JSX is inlined into the markup, where the positions already read it.
148
+ if (c.isJsx || c.isJsxFunction) return null
149
+ const call = localCall(binding)
150
+ const expr = call?.kind === 'inline' ? call.body : c.parsed
151
+ return { kind: call ? 'function' : 'const', loc: c.loc, expr, fallback: c.freeIdentifiers }
152
+ }
153
+ case 'function': {
154
+ const call = localCall(binding)
155
+ if (call?.kind !== 'inline') return null
156
+ return { kind: 'function', loc: binding.info.loc, expr: call.body, fallback: undefined }
157
+ }
158
+ }
159
+ }
160
+
161
+ /** {@link Reads} of `expr`, or of `fallback` (calling nothing) when it has no analyzable tree. */
162
+ function readsOf(expr: ParsedExpr | undefined, fallback: ReadonlySet<string> | undefined): Reads {
163
+ const names = expr && freeIdentifiers(expr)
164
+ // An IR that went through JSON holds no `Set`: such a declaration reads nothing here.
165
+ if (!expr || !names) return { names: fallback instanceof Set ? fallback : [], called: new Set() }
166
+ const called = new Set<string>()
167
+ mapParsedExpr(expr, (e, enclosing) => {
168
+ if (e.kind === 'call' && e.args.length === 0 && e.callee.kind === 'identifier' && !enclosing.has(e.callee.name)) {
169
+ called.add(e.callee.name)
170
+ }
171
+ return null
172
+ })
173
+ return { names, called }
174
+ }
@@ -32,7 +32,9 @@
32
32
  import ts from 'typescript'
33
33
  import { extractFreeIdentifiersFromNode } from './analyzer.ts'
34
34
  import { resolveGetterAliases, collectAliasableGetterNames } from './ir-to-client-js/csr-substitute.ts'
35
- import { resolveBodyDestructuredPropAliases } from './props-binding.ts'
35
+ import { resolveBodyDestructuredPropAliases, templatePropReadParams } from './props-binding.ts'
36
+ import { localBindings, localCall, type LocalBindingResolver, type LocalScope } from './local-call-body.ts'
37
+ import { stringifyParsedExpr } from './expression-parser.ts'
36
38
  import { computeSsrSeedPlan } from './ssr-seed-plan.ts'
37
39
  import type { IRMetadata } from './types.ts'
38
40
  import { isElidedFactoryGetter } from './types.ts'
@@ -174,12 +176,83 @@ type EvalResult = unknown | typeof UNRESOLVED
174
176
  // distinct from `UNRESOLVED` (couldn't evaluate) so a falsy guard
175
177
  // (`if (!key) return X`) continues evaluation instead of bailing.
176
178
  const NO_RETURN = Symbol('no-return')
179
+ /** A zero-argument call of a name nothing static is bound to. */
180
+ const NOT_BOUND = Symbol('not-bound')
177
181
 
178
182
  interface EvalContext {
179
183
  /** Identifier name → previously-resolved value (signal getters, memos). */
180
184
  bindings: Record<string, EvalResult>
181
185
  /** Names whose `<name>.X` / `<name>[X]` reads should resolve to `undefined`. */
182
186
  propsLike: ReadonlySet<string>
187
+ /**
188
+ * What a call of `name` with no arguments returns, for a name that
189
+ * denotes a local function where this context's expression is written
190
+ * (`localCall`); `NOT_BOUND` for any other name.
191
+ */
192
+ calls?: (name: string) => EvalResult | typeof NOT_BOUND
193
+ }
194
+
195
+ /**
196
+ * The context a signal seed or memo body is evaluated in: the props in
197
+ * scope by their bare name (`templatePropReadParams`, so a `(props)`-arg
198
+ * component's typed prop the body never binds is not one) and the props
199
+ * object, the component-scope consts that evaluate without reading a prop,
200
+ * and the zero-argument calls of local functions.
201
+ *
202
+ * Each name is read in the scope it is written in (`localBindings`, #3721).
203
+ * `bindings` arrives holding the module consts and leaves as the component
204
+ * scope: a module const the component scope shadows is dropped, the
205
+ * component's consts are added. A called function's returned expression is
206
+ * evaluated against the bindings of the scope the function is declared in,
207
+ * so a module function reads the module's `label` beside a component const
208
+ * of that name.
209
+ */
210
+ function componentEvalScope(metadata: IRMetadata, bindings: Record<string, EvalResult>): Omit<EvalContext, 'bindings'> {
211
+ const propsLike = new Set(templatePropReadParams(metadata).map(p => p.name))
212
+ if (metadata.propsObjectName) propsLike.add(metadata.propsObjectName)
213
+ const noProps: ReadonlySet<string> = new Set()
214
+ const constants = metadata.localConstants ?? []
215
+ const scopes: Record<LocalScope, Record<string, EvalResult>> = { component: bindings, module: { ...bindings } }
216
+ const calls = scopedCallEvaluator(localBindings({ ...metadata, localConstants: constants }), scopes, noProps)
217
+ for (const c of constants) {
218
+ if (c.isModule && c.shadowedInComponent) delete bindings[c.name]
219
+ }
220
+ for (const c of constants) {
221
+ if (c.isModule || c.value === undefined || c.declarationKind !== 'const' || c.mutatedAfterDeclaration) continue
222
+ // A function-valued const is not its return value; a call of it is (`calls`).
223
+ if (propsLike.has(c.name) || c.containsArrow || c.isFunctionValue) continue
224
+ const v = tryStaticEval(c.value, { bindings, propsLike: noProps, calls: calls('component') })
225
+ if (v !== UNRESOLVED) bindings[c.name] = v
226
+ }
227
+ return { propsLike, calls: calls('component') }
228
+ }
229
+
230
+ /**
231
+ * {@link EvalContext.calls} for an expression written in a given scope: the
232
+ * called name is resolved from that scope, and the returned expression is
233
+ * evaluated in the declaring scope's bindings, whose own calls resolve from
234
+ * there. A function a template cannot inline, and one that calls itself,
235
+ * have no static value.
236
+ */
237
+ function scopedCallEvaluator(
238
+ resolve: LocalBindingResolver,
239
+ scopes: Readonly<Record<LocalScope, Record<string, EvalResult>>>,
240
+ propsLike: ReadonlySet<string>,
241
+ ): (from: LocalScope) => NonNullable<EvalContext['calls']> {
242
+ const running = new Set<unknown>()
243
+ const callsFrom = (from: LocalScope) => (name: string): EvalResult | typeof NOT_BOUND => {
244
+ const binding = resolve(name, from)
245
+ const call = localCall(binding)
246
+ if (!binding || !call) return NOT_BOUND
247
+ if (call.kind === 'refuse' || running.has(binding)) return UNRESOLVED
248
+ running.add(binding)
249
+ try {
250
+ return tryStaticEval(stringifyParsedExpr(call.body), { bindings: scopes[binding.scope], propsLike, calls: callsFrom(binding.scope) })
251
+ } finally {
252
+ running.delete(binding)
253
+ }
254
+ }
255
+ return callsFrom
183
256
  }
184
257
 
185
258
  /**
@@ -290,6 +363,8 @@ export function extractSsrDefaults(metadata: IRMetadata): Record<string, SsrDefa
290
363
  const v = tryStaticEval(c.value, { bindings, propsLike })
291
364
  if (v !== UNRESOLVED) bindings[c.name] = v
292
365
  }
366
+ // Signals and memos read the names their component scope binds (#3721).
367
+ const scope = componentEvalScope(metadata, bindings)
293
368
  for (const sig of metadata.signals) {
294
369
  if (!sig.getter || sig.isModule) continue
295
370
  // Env signals (#2057) have no static SSR default — their value is the
@@ -305,7 +380,7 @@ export function extractSsrDefaults(metadata: IRMetadata): Record<string, SsrDefa
305
380
  // getter-elided form is unaffected, a pre-existing, separate question
306
381
  // this issue doesn't cover.
307
382
  if (isElidedFactoryGetter(sig)) continue
308
- const value = tryStaticEval(sig.initialValue, { bindings, propsLike })
383
+ const value = tryStaticEval(sig.initialValue, { ...scope, bindings })
309
384
  // Self-derivation collision (#2669): a signal whose getter shares its
310
385
  // name with the prop its OWN initializer derives from
311
386
  // (`const [label] = createSignal(props.label ?? 'Default')`) gets a
@@ -375,7 +450,7 @@ export function extractSsrDefaults(metadata: IRMetadata): Record<string, SsrDefa
375
450
 
376
451
  for (const memo of metadata.memos) {
377
452
  if (memo.isModule) continue
378
- const value = tryStaticEval(memo.computation, { bindings, propsLike })
453
+ const value = tryStaticEval(memo.computation, { ...scope, bindings })
379
454
  // Same self-derivation collision as signals above, for a memo whose
380
455
  // computation derives from a same-named prop
381
456
  // (`createMemo(() => (props.label ?? 'Default') + n())`). See the
@@ -757,6 +832,19 @@ function parseExpression(expr: string): ts.Expression | null {
757
832
  return inner
758
833
  }
759
834
 
835
+ /**
836
+ * What a zero-argument call of a bound name returns: a function's result
837
+ * (`ctx.calls`, #3721), or a binding's value, which resolves the signal
838
+ * getter pattern (`count() * 2` in a memo). `NOT_BOUND` for any other call.
839
+ */
840
+ function zeroArgCallResult(node: ts.CallExpression, ctx: EvalContext): EvalResult | typeof NOT_BOUND {
841
+ if (node.arguments.length > 0 || !ts.isIdentifier(node.expression)) return NOT_BOUND
842
+ const name = node.expression.text
843
+ const called = ctx.calls ? ctx.calls(name) : NOT_BOUND
844
+ if (called !== NOT_BOUND) return called
845
+ return name in ctx.bindings ? ctx.bindings[name] : NOT_BOUND
846
+ }
847
+
760
848
  function evalNode(node: ts.Expression, ctx: EvalContext): EvalResult {
761
849
  // Strip parentheses / `as` / `satisfies` — they don't affect the value.
762
850
  if (ts.isParenthesizedExpression(node)) return evalNode(node.expression, ctx)
@@ -886,13 +974,8 @@ function evalNode(node: ts.Expression, ctx: EvalContext): EvalResult {
886
974
  if (ts.isCallExpression(node)) {
887
975
  // Zero-arg call of a bound identifier — resolves the signal getter
888
976
  // pattern (`count() * 2` in a memo).
889
- if (
890
- node.arguments.length === 0 &&
891
- ts.isIdentifier(node.expression) &&
892
- node.expression.text in ctx.bindings
893
- ) {
894
- return ctx.bindings[node.expression.text]
895
- }
977
+ const zeroArg = zeroArgCallResult(node, ctx)
978
+ if (zeroArg !== NOT_BOUND) return zeroArg
896
979
  // `<array>.map(cb)` — a single-param, expression-bodied arrow over a
897
980
  // resolved array receiver; anything else (block body, multi-param,
898
981
  // non-array receiver) stays `UNRESOLVED`, same conservative narrowing as