@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.
- 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 +11 -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/number-value.d.ts +2 -1
- package/dist/adapters/number-value.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/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 +2500 -1475
- 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/types.d.ts +59 -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__/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-const-child-prop.test.ts +85 -0
- 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 +19 -3
- 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/number-value.ts +26 -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/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 +28 -22
- 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 +121 -60
- 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/types.ts +56 -0
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)
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a read of a literal const's member (`QS[0]`, `QS[9]`, `REC['k']`,
|
|
3
|
+
* `NEST.a.c`) sees, decided from the const's parsed literal value — the ONE
|
|
4
|
+
* decision behind every template adapter's compile-time member lookup of a
|
|
5
|
+
* module or component-local array / object literal const.
|
|
6
|
+
*
|
|
7
|
+
* The template receives the props, not the module's consts, so a read the
|
|
8
|
+
* lookup does not answer falls back to a lowering that reads the const as a
|
|
9
|
+
* template variable. That variable does not exist: some engines render it
|
|
10
|
+
* empty by accident, others fail the render (Perl `strict`, Go's field
|
|
11
|
+
* lookup). So a key the literal provably lacks is answered here too, as
|
|
12
|
+
* `undefined`, which every emitter already spells. A key the literal lacks
|
|
13
|
+
* but its prototype has (`REC['toString']`, `QS['map']`) reads a function
|
|
14
|
+
* in JS, which no template can render; that read is `inherited`, and
|
|
15
|
+
* `spellStaticConstMember` refuses it with BF101 where the adapter spells
|
|
16
|
+
* it; `refuseInheritedConstServerReads` does for a value the server render
|
|
17
|
+
* computes without an emitter.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { resolveStaticConstBinding } from './augment-inherited-props.ts'
|
|
21
|
+
import { exprToString, type ObjectLiteralProperty, type ParsedExpr } from './expression-parser.ts'
|
|
22
|
+
import { literalMemberIndex } from './literal-member-index.ts'
|
|
23
|
+
import type { CompilerError, IRMetadata, SourceLocation } from './types.ts'
|
|
24
|
+
|
|
25
|
+
type StaticConstants = IRMetadata['localConstants'] | undefined
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A literal const member read:
|
|
29
|
+
*
|
|
30
|
+
* - `value`: the read's value as a typed `ParsedExpr` — a string or number
|
|
31
|
+
* `literal`, a unary `-` over a number literal, or the identifier
|
|
32
|
+
* `undefined` for a key the literal provably lacks;
|
|
33
|
+
* - `inherited`: a key only the prototype has (a function in JS).
|
|
34
|
+
*
|
|
35
|
+
* `null` when the read is not a member of a literal const, or the literal
|
|
36
|
+
* cannot answer it (a hole or spread before the element, a non-literal
|
|
37
|
+
* value, an `__proto__` key); the caller falls back to its generic lowering.
|
|
38
|
+
*/
|
|
39
|
+
export type StaticConstMemberRead =
|
|
40
|
+
| { kind: 'value'; expr: ParsedExpr }
|
|
41
|
+
| { kind: 'inherited'; key: string }
|
|
42
|
+
|
|
43
|
+
const UNDEFINED_EXPR: ParsedExpr = { kind: 'identifier', name: 'undefined' }
|
|
44
|
+
|
|
45
|
+
/** The literal key of an index (`QS[-1]`, `QS[-0]`), as JS converts it to a property key. */
|
|
46
|
+
export function staticIndexKey(index: ParsedExpr): string | null {
|
|
47
|
+
if (index.kind === 'literal' && (index.literalType === 'string' || index.literalType === 'number')) {
|
|
48
|
+
return String(index.value)
|
|
49
|
+
}
|
|
50
|
+
if (
|
|
51
|
+
index.kind === 'unary' && index.op === '-' &&
|
|
52
|
+
index.argument.kind === 'literal' && index.argument.literalType === 'number' &&
|
|
53
|
+
typeof index.argument.value === 'number'
|
|
54
|
+
) {
|
|
55
|
+
return String(-index.argument.value)
|
|
56
|
+
}
|
|
57
|
+
return null
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Read `key` of `object`, where `object` is a literal const (`QS`) or a
|
|
62
|
+
* literal-valued member of one (`NEST.a`). `isShadowed` is REQUIRED for the
|
|
63
|
+
* same reason as in `lookupStaticRecordLiteral`.
|
|
64
|
+
*/
|
|
65
|
+
export function readStaticConstMember(
|
|
66
|
+
object: ParsedExpr,
|
|
67
|
+
key: string,
|
|
68
|
+
constants: StaticConstants,
|
|
69
|
+
isShadowed: (name: string) => boolean,
|
|
70
|
+
): StaticConstMemberRead | null {
|
|
71
|
+
const receiver = staticLiteralValue(object, constants, isShadowed)
|
|
72
|
+
if (receiver === null) return null
|
|
73
|
+
const read = readLiteralMember(receiver, key)
|
|
74
|
+
if (read === null || read.kind === 'inherited') return read
|
|
75
|
+
return isRenderableScalar(read.expr) ? read : null
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The value of a literal const member read, or `null` when the read has no
|
|
80
|
+
* compile-time value (including an `inherited` read, refused separately).
|
|
81
|
+
*/
|
|
82
|
+
export function lookupStaticConstMember(
|
|
83
|
+
object: ParsedExpr,
|
|
84
|
+
key: string,
|
|
85
|
+
constants: StaticConstants,
|
|
86
|
+
isShadowed: (name: string) => boolean,
|
|
87
|
+
): ParsedExpr | null {
|
|
88
|
+
const read = readStaticConstMember(object, key, constants, isShadowed)
|
|
89
|
+
return read?.kind === 'value' ? read.expr : null
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The place of an `inherited` read, as far as the caller of `spellStaticConstMember` knows it. */
|
|
93
|
+
export interface InheritedReadPlace {
|
|
94
|
+
loc: SourceLocation
|
|
95
|
+
/** `false` when `/* @client *\/` cannot defer the read. */
|
|
96
|
+
deferrable?: boolean
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Where a template adapter records the refusal of an `inherited` read. */
|
|
100
|
+
export interface StaticConstMemberSink {
|
|
101
|
+
readonly errors: CompilerError[]
|
|
102
|
+
readonly componentName: string
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The value a template adapter spells for a literal const member read it is
|
|
107
|
+
* emitting, as `lookupStaticConstMember`; and, for a key only the literal's
|
|
108
|
+
* prototype has (`REC['toString']`, `QS['map']`), BF101 recorded in `sink`.
|
|
109
|
+
* Every template adapter's member and index emitters (value, condition and
|
|
110
|
+
* child-prop position, and a constant or function body the adapter inlines)
|
|
111
|
+
* read through this, so the refusal fires exactly where a template would
|
|
112
|
+
* spell the read: a position deferred with `/* @client *\/`, a method the
|
|
113
|
+
* template never renders, or a name a binding shadows never reaches it.
|
|
114
|
+
* `where` is what the caller knows about the read's place: its location,
|
|
115
|
+
* and whether `/* @client *\/` can defer it (a child component's prop
|
|
116
|
+
* cannot be: the component renders on the server either way).
|
|
117
|
+
*/
|
|
118
|
+
export function spellStaticConstMember(
|
|
119
|
+
object: ParsedExpr,
|
|
120
|
+
key: string,
|
|
121
|
+
constants: StaticConstants,
|
|
122
|
+
isShadowed: (name: string) => boolean,
|
|
123
|
+
sink: StaticConstMemberSink,
|
|
124
|
+
where?: InheritedReadPlace,
|
|
125
|
+
): ParsedExpr | null {
|
|
126
|
+
const read = readStaticConstMember(object, key, constants, isShadowed)
|
|
127
|
+
if (read?.kind === 'inherited') {
|
|
128
|
+
recordInheritedRead(exprToString({ kind: 'member', object, property: key, computed: false, optional: false }), sink, where)
|
|
129
|
+
return null
|
|
130
|
+
}
|
|
131
|
+
return read?.kind === 'value' ? read.expr : null
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function recordInheritedRead(read: string, sink: StaticConstMemberSink, where: InheritedReadPlace | undefined): void {
|
|
135
|
+
const message =
|
|
136
|
+
`\`${read}\` reads a key the const's literal value does not have but its prototype does, so JS reads an inherited ` +
|
|
137
|
+
'function there. A template holds the literal value without a prototype, so the server render cannot give the JS answer.'
|
|
138
|
+
if (sink.errors.some(e => e.code === 'BF101' && e.message === message)) return
|
|
139
|
+
sink.errors.push({
|
|
140
|
+
code: 'BF101',
|
|
141
|
+
severity: 'error',
|
|
142
|
+
message,
|
|
143
|
+
loc: where?.loc ?? { file: `${sink.componentName}.tsx`, start: { line: 1, column: 0 }, end: { line: 1, column: 0 } },
|
|
144
|
+
suggestion: where?.deferrable === false
|
|
145
|
+
? { message: 'Read a key the literal lists.' }
|
|
146
|
+
: {
|
|
147
|
+
message: 'Read a key the literal lists, or defer the read to the client with /* @client */.',
|
|
148
|
+
escape: [{ kind: 'client-directive' }],
|
|
149
|
+
},
|
|
150
|
+
})
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* The expression a literal const, or a member of one read by literal keys
|
|
155
|
+
* (`H.get`, `NEST.a[0]`), holds: whatever the literal lists there, a
|
|
156
|
+
* function included. `null` when no literal const answers the read.
|
|
157
|
+
*/
|
|
158
|
+
export function staticConstValue(
|
|
159
|
+
expr: ParsedExpr,
|
|
160
|
+
constants: StaticConstants,
|
|
161
|
+
isShadowed: (name: string) => boolean,
|
|
162
|
+
): ParsedExpr | null {
|
|
163
|
+
return staticLiteralValue(expr, constants, isShadowed)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** The literal (array / object / scalar) `expr` evaluates to, read through literal consts. */
|
|
167
|
+
function staticLiteralValue(
|
|
168
|
+
expr: ParsedExpr,
|
|
169
|
+
constants: StaticConstants,
|
|
170
|
+
isShadowed: (name: string) => boolean,
|
|
171
|
+
): ParsedExpr | null {
|
|
172
|
+
if (expr.kind === 'identifier') {
|
|
173
|
+
const value = resolveStaticConstBinding(expr.name, constants, isShadowed)?.parsed
|
|
174
|
+
return value?.kind === 'array-literal' || value?.kind === 'object-literal' ? value : null
|
|
175
|
+
}
|
|
176
|
+
const key = expr.kind === 'member' ? expr.property : expr.kind === 'index-access' ? staticIndexKey(expr.index) : null
|
|
177
|
+
if (key === null || (expr.kind !== 'member' && expr.kind !== 'index-access')) return null
|
|
178
|
+
const receiver = staticLiteralValue(expr.object, constants, isShadowed)
|
|
179
|
+
if (receiver === null) return null
|
|
180
|
+
const read = readLiteralMember(receiver, key)
|
|
181
|
+
return read?.kind === 'value' ? read.expr : null
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* `key` of an array or object literal: its value, `undefined` for a key it
|
|
186
|
+
* provably lacks, `inherited` for a key only its prototype has, or `null`
|
|
187
|
+
* when the literal cannot answer.
|
|
188
|
+
*/
|
|
189
|
+
export function readLiteralMember(receiver: ParsedExpr, key: string): StaticConstMemberRead | null {
|
|
190
|
+
if (receiver.kind === 'array-literal') return readArrayMember(receiver.elements, key)
|
|
191
|
+
if (receiver.kind === 'object-literal') return readObjectMember(receiver.properties, key)
|
|
192
|
+
return null
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* An array literal's `length`, element, or missing key. A hole or spread
|
|
197
|
+
* (`unsupported`) at or before an index makes the element unknown, and
|
|
198
|
+
* anywhere makes the length (so whether an index is past the end) unknown.
|
|
199
|
+
*/
|
|
200
|
+
function readArrayMember(elements: readonly ParsedExpr[], key: string): StaticConstMemberRead | null {
|
|
201
|
+
const unknownAt = elements.findIndex(e => e.kind === 'unsupported')
|
|
202
|
+
if (key === 'length') {
|
|
203
|
+
return unknownAt >= 0 ? null : { kind: 'value', expr: numberLiteral(elements.length) }
|
|
204
|
+
}
|
|
205
|
+
const index = literalMemberIndex(key)
|
|
206
|
+
if (index.kind === 'literal' && typeof index.value === 'number') {
|
|
207
|
+
if (unknownAt >= 0 && unknownAt <= index.value) return null
|
|
208
|
+
if (index.value < elements.length) return { kind: 'value', expr: elements[index.value] }
|
|
209
|
+
return unknownAt >= 0 ? null : { kind: 'value', expr: UNDEFINED_EXPR }
|
|
210
|
+
}
|
|
211
|
+
// Not an array index (`'01'`, `'-1'`, `'foo'`): no element holds it, and a
|
|
212
|
+
// spread or hole adds none.
|
|
213
|
+
if (key in Array.prototype) return { kind: 'inherited', key }
|
|
214
|
+
return { kind: 'value', expr: UNDEFINED_EXPR }
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* An object literal's own key, or a key it provably lacks. A spread may
|
|
219
|
+
* bring any key, and an `__proto__` entry sets the prototype instead of a
|
|
220
|
+
* key, so either makes a missing key unknown. A later entry of the same key
|
|
221
|
+
* overrides an earlier one.
|
|
222
|
+
*/
|
|
223
|
+
function readObjectMember(properties: readonly ObjectLiteralProperty[], key: string): StaticConstMemberRead | null {
|
|
224
|
+
if (properties.some(p => p.kind === 'spread' || p.key === '__proto__')) return null
|
|
225
|
+
const own = properties.filter(p => p.kind === 'prop' && p.key === key).at(-1)
|
|
226
|
+
if (own?.kind === 'prop') return { kind: 'value', expr: own.value }
|
|
227
|
+
if (key in Object.prototype) return { kind: 'inherited', key }
|
|
228
|
+
return { kind: 'value', expr: UNDEFINED_EXPR }
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** A value every template emitter spells as the source would: a string or number, or `undefined`. */
|
|
232
|
+
function isRenderableScalar(expr: ParsedExpr): boolean {
|
|
233
|
+
if (expr.kind === 'identifier') return expr.name === 'undefined'
|
|
234
|
+
if (expr.kind === 'literal') return expr.literalType === 'string' || expr.literalType === 'number'
|
|
235
|
+
return (
|
|
236
|
+
expr.kind === 'unary' && expr.op === '-' &&
|
|
237
|
+
expr.argument.kind === 'literal' && expr.argument.literalType === 'number'
|
|
238
|
+
)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function numberLiteral(value: number): ParsedExpr {
|
|
242
|
+
return { kind: 'literal', value, literalType: 'number', raw: String(value) }
|
|
243
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1487,6 +1487,17 @@ export interface ExpressionAttr {
|
|
|
1487
1487
|
* differ, and the adapter then falls back to `expr`.
|
|
1488
1488
|
*/
|
|
1489
1489
|
typed?: TypedExprSource
|
|
1490
|
+
/**
|
|
1491
|
+
* Set on a component prop whose value is a bare read of a const holding a
|
|
1492
|
+
* fully static array or object literal (`<Kid xs={QS} />` beside
|
|
1493
|
+
* `const QS = ['a', 'b']`): that literal, as source text and parsed tree.
|
|
1494
|
+
* A template receives the props, not the module's or the component's
|
|
1495
|
+
* consts, so a template adapter passes the literal, as if the source had
|
|
1496
|
+
* written it in place (`templatePropValue`). JS-runtime consumers (Hono,
|
|
1497
|
+
* the client JS) keep `expr`, so the child receives the const's own value
|
|
1498
|
+
* and its identity.
|
|
1499
|
+
*/
|
|
1500
|
+
staticConstLiteral?: { expr: JsSourceText; parsed: ParsedExpr }
|
|
1490
1501
|
}
|
|
1491
1502
|
|
|
1492
1503
|
/** See {@link ExpressionAttr.typed}. */
|
|
@@ -1551,6 +1562,26 @@ export interface SpreadAttr {
|
|
|
1551
1562
|
* carry the caller's whole bag (`ClientAnalysis.readsCallerBag`, #3845).
|
|
1552
1563
|
*/
|
|
1553
1564
|
callerBag?: 'props' | 'rest'
|
|
1565
|
+
/**
|
|
1566
|
+
* The caller keys a `callerBag: 'rest'` spread leaves out: the keys the
|
|
1567
|
+
* component destructured before the rest element, as the caller spells
|
|
1568
|
+
* them (`{ title: t, ...rest }` omits `title`), from the signature or
|
|
1569
|
+
* from a body destructure of the props object (`restBagOmittedKeys`).
|
|
1570
|
+
* The client holds the rest bag as the caller's whole props object
|
|
1571
|
+
* (`_p`), so every reader of the bag (the child props object, the
|
|
1572
|
+
* registration template, `applyRestAttrs`) takes these keys out
|
|
1573
|
+
* (#3714). Set exactly when `callerBag` is `'rest'`.
|
|
1574
|
+
*/
|
|
1575
|
+
restOmittedKeys?: readonly string[]
|
|
1576
|
+
/**
|
|
1577
|
+
* Set when the spread's identifier is spelled like the component's props
|
|
1578
|
+
* object but a loop row, callback parameter, destructured field or
|
|
1579
|
+
* preamble local binds that name at the spread (`rows.map((props) => <li
|
|
1580
|
+
* {...props} />)`, #3727). It spreads that binding, not the props object, so
|
|
1581
|
+
* `spreadsPropsObject` is false for it. Decided once in Phase 1; unset for
|
|
1582
|
+
* every other spread.
|
|
1583
|
+
*/
|
|
1584
|
+
shadowsPropsObject?: true
|
|
1554
1585
|
}
|
|
1555
1586
|
|
|
1556
1587
|
export interface JsxChildrenAttr {
|
|
@@ -2287,6 +2318,20 @@ export interface FunctionInfo {
|
|
|
2287
2318
|
freeIdentifiers?: ReadonlySet<string>
|
|
2288
2319
|
/** When true, declared at module level (outside the component function). */
|
|
2289
2320
|
isModule?: boolean
|
|
2321
|
+
/**
|
|
2322
|
+
* When true, the declaration sits inside the component function's body. A
|
|
2323
|
+
* `function` declaration there is collected with `isModule` set too (the
|
|
2324
|
+
* module walk is the one that reaches it, and client-JS scope placement
|
|
2325
|
+
* demotes it later), so this is what says where its body's names resolve.
|
|
2326
|
+
*/
|
|
2327
|
+
inComponentBody?: boolean
|
|
2328
|
+
/**
|
|
2329
|
+
* What a call with no arguments returns, for a function that takes no
|
|
2330
|
+
* parameters and whose body is a single `return <expr>`: the returned
|
|
2331
|
+
* expression's tree. A template inlines such a call (`value()` → `1`)
|
|
2332
|
+
* where it reads the function (`localCallBodies`).
|
|
2333
|
+
*/
|
|
2334
|
+
returnParsed?: ParsedExpr
|
|
2290
2335
|
/** When true, this function returns JSX and is inlined at call sites (#569). */
|
|
2291
2336
|
isJsxFunction?: boolean
|
|
2292
2337
|
/**
|
|
@@ -2377,6 +2422,17 @@ export interface ConstantInfo {
|
|
|
2377
2422
|
* was supposed to do.
|
|
2378
2423
|
*/
|
|
2379
2424
|
mutatedAfterDeclaration?: boolean
|
|
2425
|
+
/**
|
|
2426
|
+
* When true, the initializer creates a new value each time it is evaluated
|
|
2427
|
+
* (an object or array literal, `new X()`, a call, a function expression)
|
|
2428
|
+
* and reads nothing reactive (no signal, memo, prop or `Reactive<T>`
|
|
2429
|
+
* brand), so every read must see the one value the declaration created. A
|
|
2430
|
+
* read site references the binding; it never re-evaluates the initializer
|
|
2431
|
+
* in its place, which would hand each read a different object (#3868).
|
|
2432
|
+
* Set by `markIdentityBoundConstants` (`const-identity.ts`). Consumed by
|
|
2433
|
+
* `expandDynamicPropValue`, which builds a child component's prop getters.
|
|
2434
|
+
*/
|
|
2435
|
+
identityBound?: boolean
|
|
2380
2436
|
/**
|
|
2381
2437
|
* When true, this module-level const is shadowed inside the component: a
|
|
2382
2438
|
* destructured prop, the props object, or a component-scope const,
|