@barefootjs/jsx 0.41.1 → 0.41.3
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.
- package/dist/adapters/attr-plan.d.ts +8 -0
- package/dist/adapters/attr-plan.d.ts.map +1 -1
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +19 -0
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +2 -1
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/inherited-const-server-read.d.ts +37 -0
- package/dist/adapters/inherited-const-server-read.d.ts.map +1 -0
- package/dist/adapters/loop-row-type.d.ts +18 -0
- package/dist/adapters/loop-row-type.d.ts.map +1 -1
- package/dist/adapters/non-primitive-equality.d.ts +8 -2
- package/dist/adapters/non-primitive-equality.d.ts.map +1 -1
- package/dist/adapters/unapplied-prop-default.d.ts +34 -0
- package/dist/adapters/unapplied-prop-default.d.ts.map +1 -0
- package/dist/adapters/uninlinable-seed-call.d.ts +29 -0
- package/dist/adapters/uninlinable-seed-call.d.ts.map +1 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +4 -22
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/caller-value-reach.d.ts +57 -0
- package/dist/caller-value-reach.d.ts.map +1 -0
- package/dist/const-identity.d.ts +38 -0
- package/dist/const-identity.d.ts.map +1 -0
- package/dist/index.d.ts +10 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2685 -1381
- package/dist/ir-to-client-js/child-props-object.d.ts +39 -41
- package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -1
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/component-scope.d.ts +13 -2
- package/dist/ir-to-client-js/component-scope.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/local-call-body.d.ts +112 -0
- package/dist/local-call-body.d.ts.map +1 -0
- package/dist/nullable-signal-attr.d.ts +10 -0
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/prop-default-resolution.d.ts +52 -0
- package/dist/prop-default-resolution.d.ts.map +1 -0
- package/dist/props-binding.d.ts +15 -0
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/server-read-walk.d.ts +66 -0
- package/dist/server-read-walk.d.ts.map +1 -0
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +24 -1
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/static-const-member.d.ts +92 -0
- package/dist/static-const-member.d.ts.map +1 -0
- package/dist/static-literal.d.ts +21 -1
- package/dist/static-literal.d.ts.map +1 -1
- package/dist/types.d.ts +63 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/computed-const.test.ts +7 -1
- package/src/__tests__/const-identity-child-prop.test.ts +127 -0
- package/src/__tests__/inherited-const-server-read.test.ts +116 -0
- package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +8 -3
- package/src/__tests__/issue-3714-open-rest-spread-child.test.ts +188 -0
- package/src/__tests__/local-call-body.test.ts +101 -0
- package/src/__tests__/loop-branch-bare-expression-reactive-text.test.ts +212 -56
- package/src/__tests__/nested-loop-conditional.test.ts +16 -16
- package/src/__tests__/prop-default-resolution.test.ts +185 -0
- package/src/__tests__/props-spread-shadowed.test.ts +113 -0
- package/src/__tests__/server-read-walk.test.ts +125 -0
- package/src/__tests__/ssr-defaults.test.ts +67 -7
- package/src/__tests__/static-literal.test.ts +47 -2
- package/src/__tests__/static-record-literal-array.test.ts +82 -11
- package/src/adapters/attr-plan.ts +12 -1
- package/src/adapters/component-props.ts +2 -1
- package/src/adapters/computed-const.ts +108 -12
- package/src/adapters/dsl-template-adapter.ts +17 -4
- package/src/adapters/inherited-const-server-read.ts +174 -0
- package/src/adapters/loop-row-type.ts +69 -2
- package/src/adapters/non-primitive-equality.ts +17 -2
- package/src/adapters/unapplied-prop-default.ts +83 -0
- package/src/adapters/uninlinable-seed-call.ts +62 -0
- package/src/analyzer.ts +43 -3
- package/src/augment-inherited-props.ts +4 -49
- package/src/caller-value-reach.ts +436 -0
- package/src/compiler.ts +4 -4
- package/src/const-identity.ts +189 -0
- package/src/index.ts +10 -5
- package/src/ir-to-client-js/child-props-object.ts +94 -62
- package/src/ir-to-client-js/collect-elements.ts +53 -68
- package/src/ir-to-client-js/component-scope.ts +16 -2
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +1 -0
- package/src/ir-to-client-js/html-template.ts +24 -4
- package/src/ir-to-client-js/index.ts +2 -2
- package/src/ir-to-client-js/prop-handling.ts +4 -0
- package/src/jsx-to-ir.ts +40 -2
- package/src/local-call-body.ts +191 -0
- package/src/nullable-signal-attr.ts +39 -17
- package/src/prop-default-resolution.ts +195 -0
- package/src/props-binding.ts +16 -0
- package/src/server-read-walk.ts +174 -0
- package/src/ssr-defaults.ts +93 -10
- package/src/ssr-seed-plan.ts +104 -18
- package/src/static-const-member.ts +243 -0
- package/src/static-literal.ts +55 -1
- package/src/types.ts +63 -1
|
@@ -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
|
+
}
|
package/src/ssr-defaults.ts
CHANGED
|
@@ -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, {
|
|
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, {
|
|
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
|
-
|
|
890
|
-
|
|
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
|
package/src/ssr-seed-plan.ts
CHANGED
|
@@ -45,6 +45,8 @@ import {
|
|
|
45
45
|
type ParsedExpr,
|
|
46
46
|
} from './expression-parser.ts'
|
|
47
47
|
import { templatePropReadParams } from './props-binding.ts'
|
|
48
|
+
import { localCallBodies, type LocalCallBindings } from './local-call-body.ts'
|
|
49
|
+
import { propDefaultResolver, type PropDefaultBindings, type ResolvedPropDefault } from './prop-default-resolution.ts'
|
|
48
50
|
import type { IRMetadata, ParamInfo } from './types.ts'
|
|
49
51
|
import { isElidedFactoryGetter } from './types.ts'
|
|
50
52
|
|
|
@@ -145,6 +147,35 @@ function localConstExprsByName(metadata: IRMetadata): ReadonlyMap<string, Parsed
|
|
|
145
147
|
if (c.isModule || c.declarationKind !== 'const' || c.value === undefined) continue
|
|
146
148
|
out.set(c.name, c.parsed ?? parseExpression(c.value.trim()))
|
|
147
149
|
}
|
|
150
|
+
// A zero-argument call of a function returning one expression reads that
|
|
151
|
+
// expression (#3721): `inlineBinding` inlines a zero-param arrow's call as
|
|
152
|
+
// its body. The plan's scope is the component's, so a module function is
|
|
153
|
+
// listed as its body resolved in module scope, and only when nothing is
|
|
154
|
+
// left to read there.
|
|
155
|
+
const bindings = { ...metadata, localConstants: metadata.localConstants ?? [] }
|
|
156
|
+
const moduleScope = moduleScopeExprsByName(bindings)
|
|
157
|
+
for (const [name, { body, scope }] of localCallBodies(bindings)) {
|
|
158
|
+
if (out.has(name)) continue
|
|
159
|
+
const resolved = scope === 'module' ? resolveThroughLocalConsts(body, moduleScope) : body
|
|
160
|
+
if (scope === 'module' && freeIdentifiers(resolved)?.size !== 0) continue
|
|
161
|
+
out.set(name, { kind: 'arrow', params: [], body: resolved })
|
|
162
|
+
}
|
|
163
|
+
return out
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* What each module-scope name a module function's body can be resolved
|
|
168
|
+
* through holds: a `const` no statement reassigns, and a function a
|
|
169
|
+
* template inlines, read from the module scope (so no component binding of
|
|
170
|
+
* the name stands in for it).
|
|
171
|
+
*/
|
|
172
|
+
function moduleScopeExprsByName(bindings: LocalCallBindings): ReadonlyMap<string, ParsedExpr> {
|
|
173
|
+
const out = new Map<string, ParsedExpr>()
|
|
174
|
+
for (const c of bindings.localConstants) {
|
|
175
|
+
if (!c.isModule || c.declarationKind !== 'const' || c.mutatedAfterDeclaration || !c.parsed) continue
|
|
176
|
+
out.set(c.name, c.parsed)
|
|
177
|
+
}
|
|
178
|
+
for (const [name, { body }] of localCallBodies(bindings, 'module')) out.set(name, { kind: 'arrow', params: [], body })
|
|
148
179
|
return out
|
|
149
180
|
}
|
|
150
181
|
|
|
@@ -260,37 +291,78 @@ function isLiteralData(e: ParsedExpr): boolean {
|
|
|
260
291
|
|
|
261
292
|
/**
|
|
262
293
|
* A `derived` step per prop with a computed destructure default
|
|
263
|
-
* ({@link hasComputedDefault}): `on ?? <default
|
|
264
|
-
*
|
|
265
|
-
*
|
|
266
|
-
*
|
|
267
|
-
*
|
|
294
|
+
* ({@link hasComputedDefault}): `on ?? <default>`, the default being the
|
|
295
|
+
* expression it stands for once the consts and functions it reads are
|
|
296
|
+
* resolved (`propDefaultResolver`, #3725): a component const for a body
|
|
297
|
+
* destructure, a module const for either form. A default reading an earlier
|
|
298
|
+
* prop of the same pattern (`ParamInfo.defaultParts`, #3531) reads the
|
|
299
|
+
* earlier props' template variables. Every prop is already in `available`
|
|
300
|
+
* (baseScope), and the steps run in dependency order
|
|
301
|
+
* ({@link appliedPropDefaults}), so a chain (`{ a = 1, b = a, c = b }`) reads
|
|
268
302
|
* `b` after its own step re-bound it. A default the plan cannot lower adds
|
|
269
303
|
* no step and keeps the static ssr-defaults seed.
|
|
270
304
|
*/
|
|
271
305
|
function dependentPropDefaultSteps(metadata: IRMetadata, inlined: ReadonlySet<string>): SsrSeedStep[] {
|
|
272
|
-
const {
|
|
306
|
+
const { available, unshadowed } = propDefaultScope(metadata, inlined)
|
|
273
307
|
const steps: SsrSeedStep[] = []
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
const
|
|
277
|
-
|
|
308
|
+
const { order, defaults } = appliedPropDefaults(metadata)
|
|
309
|
+
for (const param of order) {
|
|
310
|
+
const resolved = defaults.get(param.name)
|
|
311
|
+
if (!resolved || resolved.blocked.length > 0) continue
|
|
312
|
+
const { expr } = resolved
|
|
313
|
+
const parsed: ParsedExpr = { kind: 'logical', op: '??', left: { kind: 'identifier', name: param.name }, right: expr }
|
|
314
|
+
const step = classify(param.name, 'prop', stringifyParsedExpr(parsed), parsed, available, unshadowed)
|
|
278
315
|
if (step.kind === 'derived') steps.push(step)
|
|
279
316
|
}
|
|
280
317
|
return steps
|
|
281
318
|
}
|
|
282
319
|
|
|
320
|
+
/** The computed destructure defaults of a component, as its backends apply them. */
|
|
321
|
+
export interface PropDefaultApplication {
|
|
322
|
+
/**
|
|
323
|
+
* Every prop, each after the props its default reads: the order a backend
|
|
324
|
+
* applying the defaults one prop at a time takes.
|
|
325
|
+
*/
|
|
326
|
+
order: readonly ParamInfo[]
|
|
327
|
+
/** By binding name, each computed default as the expression it stands for. */
|
|
328
|
+
defaults: ReadonlyMap<string, ResolvedPropDefault>
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* The computed destructure defaults ({@link hasComputedDefault}) of the
|
|
333
|
+
* props a template reads as their live prop (`templatePropReadParams`, so a
|
|
334
|
+
* binding that a local of the same name shadows has none), each as the
|
|
335
|
+
* expression it stands for (`propDefaultResolver`), and the order to apply
|
|
336
|
+
* them in. {@link inDefaultReadOrder} orders by the reads a default makes
|
|
337
|
+
* directly; a read through a const (`const L = a + '!'`, `{ b = L }`) shows
|
|
338
|
+
* only in the resolved expression, so the order follows those too.
|
|
339
|
+
*/
|
|
340
|
+
export function appliedPropDefaults(metadata: PropDefaultBindings): PropDefaultApplication {
|
|
341
|
+
const readable = new Set(templatePropReadParams({ ...metadata, localConstants: metadata.localConstants ?? [] }).map(p => p.name))
|
|
342
|
+
const resolveDefault = propDefaultResolver(metadata)
|
|
343
|
+
const defaults = new Map<string, ResolvedPropDefault>()
|
|
344
|
+
for (const param of metadata.propsParams) {
|
|
345
|
+
if (!hasComputedDefault(param) || !readable.has(param.name)) continue
|
|
346
|
+
const resolved = resolveDefault(param)
|
|
347
|
+
if (resolved) defaults.set(param.name, resolved)
|
|
348
|
+
}
|
|
349
|
+
const reads = (p: ParamInfo): Iterable<string> => {
|
|
350
|
+
const resolved = defaults.get(p.name)
|
|
351
|
+
return (resolved && freeIdentifiers(resolved.expr)) ?? []
|
|
352
|
+
}
|
|
353
|
+
return { order: inDefaultReadOrder(metadata.propsParams, reads), defaults }
|
|
354
|
+
}
|
|
355
|
+
|
|
283
356
|
/**
|
|
284
357
|
* The names a prop default step may read: the props a template reads as
|
|
285
|
-
* their live prop (`templatePropReadParams
|
|
286
|
-
*
|
|
287
|
-
*
|
|
288
|
-
* the body hides a module `DEF`, whatever the declaration order).
|
|
358
|
+
* their live prop (`templatePropReadParams`), the props object, and the
|
|
359
|
+
* module string consts no component-scope binding shadows (`const DEF = 'z'`
|
|
360
|
+
* in the body hides a module `DEF`, whatever the declaration order).
|
|
289
361
|
*/
|
|
290
362
|
function propDefaultScope(
|
|
291
363
|
metadata: IRMetadata,
|
|
292
364
|
inlined: ReadonlySet<string>,
|
|
293
|
-
): {
|
|
365
|
+
): { available: ReadonlySet<string>; unshadowed: ReadonlySet<string> } {
|
|
294
366
|
const readable = new Set(templatePropReadParams(metadata).map(p => p.name))
|
|
295
367
|
const locals = new Set<string>()
|
|
296
368
|
for (const c of metadata.localConstants ?? []) if (!c.isModule) locals.add(c.name)
|
|
@@ -303,7 +375,7 @@ function propDefaultScope(
|
|
|
303
375
|
const unshadowed = new Set([...inlined].filter(name => !locals.has(name)))
|
|
304
376
|
const available = new Set([...readable, ...unshadowed])
|
|
305
377
|
if (metadata.propsObjectName) available.add(metadata.propsObjectName)
|
|
306
|
-
return {
|
|
378
|
+
return { available, unshadowed }
|
|
307
379
|
}
|
|
308
380
|
|
|
309
381
|
/**
|
|
@@ -315,8 +387,13 @@ function propDefaultScope(
|
|
|
315
387
|
* EARLIER binding of the same pattern, so they form no cycle; otherwise the
|
|
316
388
|
* input order is kept. Every backend applying these defaults one prop at a
|
|
317
389
|
* time (the in-template re-binds here, Go's constructor) uses this order.
|
|
390
|
+
* `alsoReads` names further props a default reads, which its parts do not
|
|
391
|
+
* show ({@link appliedPropDefaults}).
|
|
318
392
|
*/
|
|
319
|
-
export function inDefaultReadOrder(
|
|
393
|
+
export function inDefaultReadOrder(
|
|
394
|
+
params: readonly ParamInfo[],
|
|
395
|
+
alsoReads?: (p: ParamInfo) => Iterable<string>,
|
|
396
|
+
): ParamInfo[] {
|
|
320
397
|
const byName = new Map(params.filter(p => !p.isRest).map(p => [p.name, p]))
|
|
321
398
|
const ordered: ParamInfo[] = []
|
|
322
399
|
const placed = new Set<ParamInfo>()
|
|
@@ -327,6 +404,10 @@ export function inDefaultReadOrder(params: readonly ParamInfo[]): ParamInfo[] {
|
|
|
327
404
|
const read = part.kind === 'param' ? byName.get(part.param.name) : undefined
|
|
328
405
|
if (read) place(read)
|
|
329
406
|
}
|
|
407
|
+
for (const name of alsoReads?.(p) ?? []) {
|
|
408
|
+
const read = byName.get(name)
|
|
409
|
+
if (read) place(read)
|
|
410
|
+
}
|
|
330
411
|
ordered.push(p)
|
|
331
412
|
}
|
|
332
413
|
for (const p of params) place(p)
|
|
@@ -334,7 +415,12 @@ export function inDefaultReadOrder(params: readonly ParamInfo[]): ParamInfo[] {
|
|
|
334
415
|
}
|
|
335
416
|
|
|
336
417
|
export function computeSsrSeedPlan(metadata: IRMetadata): SsrSeedPlan {
|
|
337
|
-
|
|
418
|
+
// A prop is in scope by its bare name only where the component binds it
|
|
419
|
+
// so: a destructured parameter, or a `(props)`-arg component's body
|
|
420
|
+
// binding that holds it. A typed prop the body never binds names nothing
|
|
421
|
+
// here; a local or module binding of that name is what the code reads
|
|
422
|
+
// (#3721).
|
|
423
|
+
const baseScope: string[] = templatePropReadParams(metadata).map(p => p.name)
|
|
338
424
|
if (metadata.propsObjectName) baseScope.push(metadata.propsObjectName)
|
|
339
425
|
const inlined = new Set(collectModuleStringConsts(metadata.localConstants).keys())
|
|
340
426
|
baseScope.push(...inlined)
|