@barefootjs/jsx 0.40.1 → 0.40.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/ambiguous-nil-arithmetic.d.ts +4 -3
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -1
- package/dist/adapters/boolean-result.d.ts +32 -10
- package/dist/adapters/boolean-result.d.ts.map +1 -1
- package/dist/adapters/brace-delimiter-text.d.ts +18 -0
- package/dist/adapters/brace-delimiter-text.d.ts.map +1 -0
- package/dist/adapters/callback-method.d.ts +38 -5
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +11 -1
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +21 -0
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/loop-bound-names.d.ts +9 -1
- package/dist/adapters/loop-bound-names.d.ts.map +1 -1
- package/dist/adapters/loop-row-type.d.ts +52 -0
- package/dist/adapters/loop-row-type.d.ts.map +1 -0
- package/dist/adapters/nil-reading.d.ts +68 -0
- package/dist/adapters/nil-reading.d.ts.map +1 -0
- package/dist/adapters/non-primitive-equality.d.ts +33 -0
- package/dist/adapters/non-primitive-equality.d.ts.map +1 -0
- package/dist/adapters/number-value.d.ts +5 -3
- package/dist/adapters/number-value.d.ts.map +1 -1
- package/dist/adapters/operand-typing.d.ts +126 -0
- package/dist/adapters/operand-typing.d.ts.map +1 -0
- package/dist/adapters/parsed-expr-emitter.d.ts +70 -12
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/adapters/primitive-plan.d.ts +291 -0
- package/dist/adapters/primitive-plan.d.ts.map +1 -0
- package/dist/analyzer-context.d.ts +4 -0
- package/dist/analyzer-context.d.ts.map +1 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +63 -8
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/compiler.d.ts.map +1 -1
- package/dist/event-handler-name.d.ts +12 -0
- package/dist/event-handler-name.d.ts.map +1 -1
- package/dist/expression-parser.d.ts +8 -0
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/index.d.ts +19 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4628 -2198
- package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
- package/dist/ir-to-client-js/child-props-object.d.ts +64 -0
- package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -0
- package/dist/ir-to-client-js/collect-elements.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/control-flow/shared.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +4 -2
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/imports.d.ts +2 -2
- package/dist/ir-to-client-js/imports.d.ts.map +1 -1
- package/dist/ir-to-client-js/index.d.ts +2 -6
- package/dist/ir-to-client-js/index.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +30 -18
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +3 -2
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts +1 -0
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +68 -9
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/prop-rewrite.d.ts +7 -0
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-object-whole-read.d.ts +15 -0
- package/dist/props-object-whole-read.d.ts.map +1 -0
- package/dist/rich-type-refusal.d.ts.map +1 -1
- package/dist/scope/binding-scope.d.ts +85 -10
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/scope/lexical-scope-walk.d.ts +3 -2
- package/dist/scope/lexical-scope-walk.d.ts.map +1 -1
- package/dist/scope/root-prop-alias.d.ts +10 -7
- package/dist/scope/root-prop-alias.d.ts.map +1 -1
- package/dist/strip-types.d.ts +13 -0
- package/dist/strip-types.d.ts.map +1 -1
- package/dist/template-position-walk.d.ts +21 -3
- package/dist/template-position-walk.d.ts.map +1 -1
- package/dist/types.d.ts +90 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +3 -3
- package/src/__tests__/binding-scope.test.ts +89 -1
- package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -0
- package/src/__tests__/boolean-result.test.ts +22 -5
- package/src/__tests__/brace-delimiter-text.test.ts +26 -0
- package/src/__tests__/callback-method.test.ts +62 -2
- package/src/__tests__/child-components-in-map.test.ts +1 -1
- package/src/__tests__/computed-const.test.ts +103 -0
- package/src/__tests__/emit-to-string-call.test.ts +2 -1
- package/src/__tests__/hydration-payload-analysis.test.ts +166 -0
- package/src/__tests__/issue-2724-prop-alias-loop-not-static.test.ts +7 -7
- package/src/__tests__/issue-2754-rest-spread-needs-slot.test.ts +5 -2
- package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +1 -1
- package/src/__tests__/issue-2835-props-type-member-name-collision.test.ts +4 -4
- package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +1 -1
- package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +2 -1
- package/src/__tests__/issue-3713-props-spread-loop-row.test.ts +65 -0
- package/src/__tests__/lookup-literal-const.test.ts +9 -3
- package/src/__tests__/loop-index-reactivity.test.ts +2 -2
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +1 -1
- package/src/__tests__/loop-plan.test.ts +1 -1
- package/src/__tests__/loop-row-const-initializer-shadow.test.ts +146 -0
- package/src/__tests__/loop-row-type.test.ts +64 -0
- package/src/__tests__/lowering-value-position.test.ts +4 -0
- package/src/__tests__/module-const-destructure-default.test.ts +156 -0
- package/src/__tests__/nested-loop-index-param.test.ts +1 -1
- package/src/__tests__/nested-loop-reactive-attrs.test.ts +2 -2
- package/src/__tests__/nested-row-shadowing.test.ts +2 -2
- package/src/__tests__/nil-reading.test.ts +77 -0
- package/src/__tests__/nullable-signal-attr.test.ts +134 -0
- package/src/__tests__/primitive-plan.test.ts +416 -0
- package/src/__tests__/profile-loop-binding-ids.test.ts +2 -2
- package/src/__tests__/profile-nested-binding-ids.test.ts +2 -2
- package/src/__tests__/props-object-whole-read.test.ts +40 -0
- package/src/__tests__/reactive-attrs-in-map.test.ts +1 -1
- package/src/__tests__/reactive-factory-inlining.test.ts +4 -1
- package/src/__tests__/rest-binding-gaps.test.ts +192 -0
- package/src/__tests__/root-prop-alias.test.ts +53 -8
- package/src/__tests__/static-loop-csr-materialize.test.ts +2 -2
- package/src/__tests__/static-record-literal-array.test.ts +171 -0
- package/src/__tests__/string-concat-identifier.test.ts +40 -1
- package/src/adapters/ambiguous-nil-arithmetic.ts +22 -13
- package/src/adapters/boolean-result.ts +72 -66
- package/src/adapters/brace-delimiter-text.ts +25 -0
- package/src/adapters/callback-method.ts +151 -27
- package/src/adapters/computed-const.ts +238 -4
- package/src/adapters/dsl-template-adapter.ts +40 -2
- package/src/adapters/loop-bound-names.ts +9 -1
- package/src/adapters/loop-plan.ts +1 -1
- package/src/adapters/loop-row-type.ts +139 -0
- package/src/adapters/nil-reading.ts +297 -0
- package/src/adapters/non-primitive-equality.ts +92 -0
- package/src/adapters/number-value.ts +17 -4
- package/src/adapters/operand-typing.ts +522 -0
- package/src/adapters/parsed-expr-emitter.ts +154 -19
- package/src/adapters/primitive-plan.ts +696 -0
- package/src/analyzer-context.ts +6 -0
- package/src/analyzer.ts +505 -19
- package/src/augment-inherited-props.ts +108 -10
- package/src/compiler.ts +17 -0
- package/src/event-handler-name.ts +15 -0
- package/src/expression-parser.ts +70 -1
- package/src/index.ts +33 -7
- package/src/ir-to-client-js/build-references.ts +69 -0
- package/src/ir-to-client-js/child-props-object.ts +106 -0
- package/src/ir-to-client-js/collect-elements.ts +30 -55
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +6 -1
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +29 -28
- package/src/ir-to-client-js/control-flow/shared.ts +20 -22
- package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +5 -3
- package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +2 -2
- package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +1 -1
- package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +2 -2
- package/src/ir-to-client-js/control-flow/stringify/loop.ts +1 -1
- package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +3 -3
- package/src/ir-to-client-js/html-template.ts +25 -8
- package/src/ir-to-client-js/imports.ts +1 -1
- package/src/ir-to-client-js/index.ts +53 -11
- package/src/ir-to-client-js/plan/build-static-array-child-init.ts +31 -14
- package/src/ir-to-client-js/plan/static-array-child-init.ts +27 -18
- package/src/ir-to-client-js/prop-handling.ts +22 -3
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +26 -10
- package/src/jsx-to-ir.ts +446 -37
- package/src/nullable-signal-attr.ts +246 -29
- package/src/prop-rewrite.ts +30 -6
- package/src/props-object-whole-read.ts +87 -0
- package/src/rich-type-refusal.ts +2 -1
- package/src/scope/binding-scope.ts +182 -18
- package/src/scope/lexical-scope-walk.ts +4 -3
- package/src/scope/root-prop-alias.ts +15 -8
- package/src/strip-types.ts +31 -3
- package/src/template-position-walk.ts +57 -28
- package/src/types.ts +87 -0
|
@@ -15,9 +15,9 @@
|
|
|
15
15
|
* shadow the name are respected.
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
import { collectContextConsumers, lookupLiteralConst } from '../augment-inherited-props.ts'
|
|
19
|
-
import { freeIdentifiers, inlineBinding, parseExpression, stringifyParsedExpr, type ParsedExpr } from '../expression-parser.ts'
|
|
20
|
-
import type { ConstantInfo, ExpressionAttr, IRMetadata } from '../types.ts'
|
|
18
|
+
import { collectContextConsumers, lookupLiteralConst, resolveStaticConstBinding } from '../augment-inherited-props.ts'
|
|
19
|
+
import { freeIdentifiers, inlineBinding, mapParsedExpr, parseExpression, stringifyParsedExpr, type ParsedExpr } from '../expression-parser.ts'
|
|
20
|
+
import type { ConstantInfo, ExpressionAttr, IRMetadata, ParamInfo } from '../types.ts'
|
|
21
21
|
import { componentBindings } from '../relocate.ts'
|
|
22
22
|
import { PROPS_OBJECT_IDENTIFIER } from '../scope/binding-scope.ts'
|
|
23
23
|
|
|
@@ -46,6 +46,8 @@ export interface ComputedConstContext {
|
|
|
46
46
|
* (`function C(props: { label: string })`) binds no name.
|
|
47
47
|
*/
|
|
48
48
|
componentNames: ReadonlySet<string>
|
|
49
|
+
/** The component's props params and props object name, which type an index read (`keyedValue`). */
|
|
50
|
+
props: { params: readonly ParamInfo[]; objectName: string | null }
|
|
49
51
|
/** Whether a loop row or callback binds `name` where the expression is read. */
|
|
50
52
|
isShadowed(name: string): boolean
|
|
51
53
|
/** Whether the adapter binds the const `name` itself, so a read of it stays as written. */
|
|
@@ -73,6 +75,7 @@ export function computedConstContext(
|
|
|
73
75
|
propReads: propReadKeys(metadata, bindings),
|
|
74
76
|
localFunctions: new Set(metadata.localFunctions.filter(f => !f.isModule).map(f => f.name)),
|
|
75
77
|
componentNames: componentScopeNames(metadata),
|
|
78
|
+
props: { params: metadata.propsParams, objectName: metadata.propsObjectName },
|
|
76
79
|
isShadowed,
|
|
77
80
|
isAdapterBound,
|
|
78
81
|
isGlobal: name => !bindings.has(name) && name !== metadata.restPropsName && !isShadowed(name),
|
|
@@ -108,7 +111,9 @@ function propsBindingNames(metadata: IRMetadata): Set<string> {
|
|
|
108
111
|
* {@link ComputedConstContext.componentNames}. Not `componentBindings`, which
|
|
109
112
|
* also lists every typed prop of a props object as a `prop` binding.
|
|
110
113
|
*/
|
|
111
|
-
function componentScopeNames(
|
|
114
|
+
export function componentScopeNames(
|
|
115
|
+
metadata: Pick<IRMetadata, 'propsObjectName' | 'propsParams' | 'restPropsName' | 'localConstants' | 'localFunctions' | 'signals' | 'memos'>,
|
|
116
|
+
): Set<string> {
|
|
112
117
|
const names = new Set<string>()
|
|
113
118
|
if (metadata.propsObjectName !== null) names.add(metadata.propsObjectName)
|
|
114
119
|
// With no props object the signature destructures, so every param is bound.
|
|
@@ -175,6 +180,11 @@ function inlineOnce(expr: ParsedExpr, ctx: ComputedConstContext): ParsedExpr {
|
|
|
175
180
|
if (!free) return expr
|
|
176
181
|
let result = expr
|
|
177
182
|
for (const name of free) {
|
|
183
|
+
const object = objectConstInitializer(name, ctx)
|
|
184
|
+
if (object) {
|
|
185
|
+
result = projectObjectReads(result, name, object, ctx) ?? result
|
|
186
|
+
continue
|
|
187
|
+
}
|
|
178
188
|
const init = inlinableInitializer(name, ctx)
|
|
179
189
|
if (!init) continue
|
|
180
190
|
result = inlineBinding(result, name, init) ?? inlineUnderCallbacks(result, name, init, ctx) ?? result
|
|
@@ -382,3 +392,227 @@ function callsOnlyGetters(init: ParsedExpr, getters: ReadonlySet<string>): boole
|
|
|
382
392
|
visit(init)
|
|
383
393
|
return ok
|
|
384
394
|
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* The initializer of a `const` holding an object literal (`const flags = { a:
|
|
398
|
+
* n > 1, label: 'L' + n }`, `const labels = { ok: 'Fine', err: 'Broken' }`)
|
|
399
|
+
* that a read of `name` stands for, resolved in the scope the const is
|
|
400
|
+
* declared in, or `null` when the read stays as it is. The same visibility
|
|
401
|
+
* rules as {@link inlinableInitializer}, with the const a read reaches decided
|
|
402
|
+
* by `resolveStaticConstBinding`; the literal must list plain
|
|
403
|
+
* `key: value` entries only (no spread), so each key's value is known.
|
|
404
|
+
*/
|
|
405
|
+
function objectConstInitializer(name: string, ctx: ComputedConstContext): ObjectLiteral | null {
|
|
406
|
+
if (ctx.componentNames.has(name) && !componentConst(name, ctx)) return null
|
|
407
|
+
const constant = resolveStaticConstBinding(name, ctx.constants, n => ctx.isShadowed(n))
|
|
408
|
+
const init = constant?.parsed
|
|
409
|
+
if (!constant || init?.kind !== 'object-literal' || constant.systemConstructKind) return null
|
|
410
|
+
if (literalValues(init) === null) return null
|
|
411
|
+
if (ctx.isShadowed(name) || ctx.getters.has(name) || ctx.contextConsumers.has(name) || ctx.propsBindings.has(name)) return null
|
|
412
|
+
if (ctx.isAdapterBound(name)) return null
|
|
413
|
+
if (!callsOnlyGetters(init, constant.isModule ? new Set() : ctx.getters)) return null
|
|
414
|
+
const resolved = resolveInitializer(init, ctx, n => ctx.isShadowed(n), constant.isModule ? 'module' : 'component')
|
|
415
|
+
return resolved?.kind === 'object-literal' ? resolved : null
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* `expr` with each read of a property of the object const `name` replaced by
|
|
420
|
+
* what JS reads there (#3844): `flags.label` by the value the literal gives
|
|
421
|
+
* `label` (`undefined` for a key it does not list), and `labels[kind]` by
|
|
422
|
+
* the conditional that picks the value of the key `kind` equals
|
|
423
|
+
* ({@link keyedValue}). A const that holds an object has no binding in a
|
|
424
|
+
* template, the same as a computed const.
|
|
425
|
+
*
|
|
426
|
+
* An index whose keys are not all known at compile time is read off the
|
|
427
|
+
* literal itself, which the adapter refuses (BF101). `null` (the read stays
|
|
428
|
+
* as written) when `expr` reads `name` whole or reads a key
|
|
429
|
+
* `Object.prototype` holds.
|
|
430
|
+
*/
|
|
431
|
+
function projectObjectReads(
|
|
432
|
+
expr: ParsedExpr,
|
|
433
|
+
name: string,
|
|
434
|
+
object: ObjectLiteral,
|
|
435
|
+
ctx: ComputedConstContext,
|
|
436
|
+
): ParsedExpr | null {
|
|
437
|
+
const objectFree = freeIdentifiers(object) ?? new Set<string>()
|
|
438
|
+
let ok = true
|
|
439
|
+
// What a read chain rooted at `name` (`flags`, `conf.inner`, `labels[k]`)
|
|
440
|
+
// holds, or null for a chain rooted elsewhere.
|
|
441
|
+
const resolve = (e: ParsedExpr, enclosing: ReadonlySet<string>): ParsedExpr | null => {
|
|
442
|
+
if (e.kind === 'identifier') return e.name === name && !enclosing.has(name) ? object : null
|
|
443
|
+
if (e.kind !== 'member' && e.kind !== 'index-access') return null
|
|
444
|
+
const base = resolve(e.object, enclosing)
|
|
445
|
+
if (base === null) return null
|
|
446
|
+
// A read off a value that is not an object (`flags.label.length`) is
|
|
447
|
+
// left to the walk, which projects the inner read.
|
|
448
|
+
if (base.kind !== 'object-literal') return null
|
|
449
|
+
const values = literalValues(base)
|
|
450
|
+
const value = values === null ? undefined : e.kind === 'member' ? memberValue(values, e.property) : keyedValue(values, e.index, enclosing, ctx)
|
|
451
|
+
if (value === undefined) ok = false
|
|
452
|
+
// An index whose keys are not known reads the literal itself, which no
|
|
453
|
+
// template lowers: the adapter refuses it (BF101) instead of reading an
|
|
454
|
+
// unbound name.
|
|
455
|
+
if (value === UNPROVABLE_INDEX && e.kind === 'index-access') return { kind: 'index-access', object: base, index: e.index }
|
|
456
|
+
return value ?? null
|
|
457
|
+
}
|
|
458
|
+
const result = mapParsedExpr(expr, (e, enclosing) => {
|
|
459
|
+
const value = resolve(e, enclosing)
|
|
460
|
+
if (value === null) return null
|
|
461
|
+
// A value read where a callback param rebinds one of its names, or an
|
|
462
|
+
// object read whole, has no projection.
|
|
463
|
+
if (value.kind === 'object-literal' || [...enclosing].some(p => objectFree.has(p))) ok = false
|
|
464
|
+
return value
|
|
465
|
+
})
|
|
466
|
+
return ok ? foldUndefinedTests(result) : null
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* `expr` with each conditional whose test compares two operands known at
|
|
471
|
+
* compile time against `undefined` (`'x' === undefined`, `undefined ===
|
|
472
|
+
* undefined`) replaced by the branch it takes. A projected read of a key
|
|
473
|
+
* the literal does or does not list leaves exactly this behind in a
|
|
474
|
+
* destructure default (`conf.tone === undefined ? 'x' : conf.tone`), and a
|
|
475
|
+
* backend need not spell `undefined` the same in every position.
|
|
476
|
+
*/
|
|
477
|
+
function foldUndefinedTests(expr: ParsedExpr): ParsedExpr {
|
|
478
|
+
return mapParsedExpr(expr, e => {
|
|
479
|
+
if (e.kind !== 'conditional' || e.test.kind !== 'binary' || (e.test.op !== '===' && e.test.op !== '!==')) return null
|
|
480
|
+
const equal = undefinedEquality(e.test.left, e.test.right)
|
|
481
|
+
if (equal === null) return null
|
|
482
|
+
return foldUndefinedTests((equal === (e.test.op === '===')) ? e.consequent : e.alternate)
|
|
483
|
+
})
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/** Whether `a === b` holds, when one side is `undefined` and the other a literal or `undefined`; null otherwise. */
|
|
487
|
+
function undefinedEquality(a: ParsedExpr, b: ParsedExpr): boolean | null {
|
|
488
|
+
const isUndefined = (e: ParsedExpr) => e.kind === 'identifier' && e.name === 'undefined'
|
|
489
|
+
const known = (e: ParsedExpr) => isUndefined(e) || e.kind === 'literal'
|
|
490
|
+
if (!known(a) || !known(b) || (!isUndefined(a) && !isUndefined(b))) return null
|
|
491
|
+
return isUndefined(a) && isUndefined(b)
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
type ObjectLiteral = Extract<ParsedExpr, { kind: 'object-literal' }>
|
|
495
|
+
|
|
496
|
+
/** The value of each key `object` lists (the last one for a repeated key), or null when it spreads another object. */
|
|
497
|
+
function literalValues(object: ObjectLiteral): Map<string, ParsedExpr> | null {
|
|
498
|
+
const values = new Map<string, ParsedExpr>()
|
|
499
|
+
for (const p of object.properties) {
|
|
500
|
+
if (p.kind === 'spread') return null
|
|
501
|
+
values.set(p.key, p.value)
|
|
502
|
+
}
|
|
503
|
+
return values
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* What `object.<key>` reads: the value the literal gives `key`, and
|
|
508
|
+
* `undefined` for a key it does not list. `undefined` (no projection) for a
|
|
509
|
+
* key `Object.prototype` holds, which JS reads from there.
|
|
510
|
+
*/
|
|
511
|
+
function memberValue(values: ReadonlyMap<string, ParsedExpr>, key: string): ParsedExpr | undefined {
|
|
512
|
+
const value = values.get(key)
|
|
513
|
+
if (value) return value
|
|
514
|
+
return key in Object.prototype ? undefined : { kind: 'identifier', name: 'undefined' }
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** {@link keyedValue}'s answer for an index whose keys are not all known. */
|
|
518
|
+
const UNPROVABLE_INDEX: ParsedExpr = { kind: 'unsupported', raw: '', reason: 'index keys not known at compile time' }
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* `values[index]` as a conditional over the keys the index can hold, with
|
|
522
|
+
* `undefined` for one the literal does not list ({@link projectObjectReads}).
|
|
523
|
+
* Sound only when every key the index can hold is known: its type is a
|
|
524
|
+
* union of string literals (plus `null` / `undefined`) — a prop typed
|
|
525
|
+
* `'ok' | 'err'` — so strict equality with each key is the property lookup
|
|
526
|
+
* JS does. {@link UNPROVABLE_INDEX} otherwise, and also when the index calls
|
|
527
|
+
* something other than a signal or memo getter (the conditional repeats
|
|
528
|
+
* it), when a key it can hold is inherited from `Object.prototype`
|
|
529
|
+
* (`labels['toString']` reads a function), or when it can be `null` /
|
|
530
|
+
* `undefined` and the literal lists `'null'` / `'undefined'`.
|
|
531
|
+
*/
|
|
532
|
+
function keyedValue(
|
|
533
|
+
values: ReadonlyMap<string, ParsedExpr>,
|
|
534
|
+
index: ParsedExpr,
|
|
535
|
+
enclosing: ReadonlySet<string>,
|
|
536
|
+
ctx: ComputedConstContext,
|
|
537
|
+
): ParsedExpr {
|
|
538
|
+
if (!readsOnly(index, ctx.getters)) return UNPROVABLE_INDEX
|
|
539
|
+
// A computed const in the index (`const variant = props.variant ?? 'x'`)
|
|
540
|
+
// is typed as what it holds.
|
|
541
|
+
const domain = indexKeyDomain(inlineComputedConsts(index, ctx), enclosing, ctx)
|
|
542
|
+
if (domain === null) return UNPROVABLE_INDEX
|
|
543
|
+
if (domain.keys.some(k => !values.has(k) && k in Object.prototype)) return UNPROVABLE_INDEX
|
|
544
|
+
if (domain.nullish.some(k => values.has(k))) return UNPROVABLE_INDEX
|
|
545
|
+
let result: ParsedExpr = { kind: 'identifier', name: 'undefined' }
|
|
546
|
+
for (const key of [...domain.keys].reverse()) {
|
|
547
|
+
const value = values.get(key)
|
|
548
|
+
if (!value) continue
|
|
549
|
+
result = {
|
|
550
|
+
kind: 'conditional',
|
|
551
|
+
test: { kind: 'binary', op: '===', left: index, right: { kind: 'literal', value: key, literalType: 'string' } },
|
|
552
|
+
consequent: value,
|
|
553
|
+
alternate: result,
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
return result
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/** The keys an index can hold ({@link keyedValue}): string keys, and whether it can be `null` / `undefined`. */
|
|
560
|
+
interface KeyDomain {
|
|
561
|
+
keys: string[]
|
|
562
|
+
nullish: Array<'null' | 'undefined'>
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* The keys an index read can hold, from its declared type: a prop read
|
|
567
|
+
* (`kind`, `props.kind`) whose declared type admits only string literals,
|
|
568
|
+
* `null` and `undefined` (`ParamInfo.literalKeys`; an optional prop adds
|
|
569
|
+
* `undefined`), or `a ?? 'x'` over such a read. `null` for any other index, including a name a loop row or callback
|
|
570
|
+
* binds.
|
|
571
|
+
*/
|
|
572
|
+
function indexKeyDomain(index: ParsedExpr, enclosing: ReadonlySet<string>, ctx: ComputedConstContext): KeyDomain | null {
|
|
573
|
+
if (index.kind === 'logical' && index.op === '??') {
|
|
574
|
+
const left = indexKeyDomain(index.left, enclosing, ctx)
|
|
575
|
+
const right = index.right.kind === 'literal' && typeof index.right.value === 'string' ? index.right.value : null
|
|
576
|
+
if (!left || right === null) return left === null || right === null ? null : left
|
|
577
|
+
return left.nullish.length === 0 ? left : { keys: [...new Set([...left.keys, right])], nullish: [] }
|
|
578
|
+
}
|
|
579
|
+
const param = indexPropParam(index, enclosing, ctx)
|
|
580
|
+
if (!param) return null
|
|
581
|
+
if (!param.literalKeys) return null
|
|
582
|
+
const domain: KeyDomain = { keys: [...param.literalKeys.keys], nullish: [...param.literalKeys.nullish] }
|
|
583
|
+
const readsDefault = index.kind === 'identifier' && param.defaultValue !== undefined
|
|
584
|
+
if (param.optional && !readsDefault && !domain.nullish.includes('undefined')) domain.nullish.push('undefined')
|
|
585
|
+
return domain
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/** The prop param an index read reads whole (`kind`, `props.kind`), or undefined. */
|
|
589
|
+
function indexPropParam(index: ParsedExpr, enclosing: ReadonlySet<string>, ctx: ComputedConstContext): ParamInfo | undefined {
|
|
590
|
+
const visible = (n: string) => !enclosing.has(n) && !ctx.isShadowed(n)
|
|
591
|
+
const params = ctx.props.params.filter(p => !p.isRest)
|
|
592
|
+
if (index.kind === 'identifier') {
|
|
593
|
+
if (!visible(index.name)) return undefined
|
|
594
|
+
if (ctx.props.objectName !== null && !ctx.propsBindings.has(index.name)) return undefined
|
|
595
|
+
return params.find(p => p.name === index.name)
|
|
596
|
+
}
|
|
597
|
+
if (index.kind !== 'member' || index.object.kind !== 'identifier' || index.object.name !== ctx.props.objectName) return undefined
|
|
598
|
+
if (!visible(index.object.name)) return undefined
|
|
599
|
+
return params.find(p => p.sourceName === undefined && p.name === index.property)
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/** Whether evaluating `expr` twice reads the same value: it calls nothing but signal and memo getters, and holds no function. */
|
|
603
|
+
function readsOnly(expr: ParsedExpr, getters: ReadonlySet<string>): boolean {
|
|
604
|
+
let ok = true
|
|
605
|
+
const visit = (node: unknown): void => {
|
|
606
|
+
if (!ok || node === null || typeof node !== 'object') return
|
|
607
|
+
if (Array.isArray(node)) {
|
|
608
|
+
node.forEach(visit)
|
|
609
|
+
return
|
|
610
|
+
}
|
|
611
|
+
const e = node as ParsedExpr
|
|
612
|
+
if (e.kind === 'arrow' || e.kind === 'unsupported') ok = false
|
|
613
|
+
else if (e.kind === 'call' && !(e.callee.kind === 'identifier' && e.args.length === 0 && getters.has(e.callee.name))) ok = false
|
|
614
|
+
else for (const value of Object.values(e)) visit(value)
|
|
615
|
+
}
|
|
616
|
+
visit(expr)
|
|
617
|
+
return ok
|
|
618
|
+
}
|
|
@@ -16,6 +16,8 @@ import {
|
|
|
16
16
|
type NullishAttrContext,
|
|
17
17
|
} from '../nullable-signal-attr.ts'
|
|
18
18
|
import { BindingScope } from '../scope/binding-scope.ts'
|
|
19
|
+
import { declaredValueTypes, loopRowType, type DeclaredValueTypes } from './loop-row-type.ts'
|
|
20
|
+
import { collectOperandTyping, EMPTY_OPERAND_TYPING, type OperandTyping, type ScopedOperandTyping } from './operand-typing.ts'
|
|
19
21
|
import { rootPropAliasesForLoop, rootPropAliasNames } from '../scope/root-prop-alias.ts'
|
|
20
22
|
import type {
|
|
21
23
|
CompilerError,
|
|
@@ -32,6 +34,7 @@ import {
|
|
|
32
34
|
refuseAmbiguousNilMemoArithmetic,
|
|
33
35
|
type AmbiguousNilArithmeticSink,
|
|
34
36
|
} from './ambiguous-nil-arithmetic.ts'
|
|
37
|
+
import { refuseNonPrimitiveStrictEquality } from './non-primitive-equality.ts'
|
|
35
38
|
import { collectImportedLoopChildComponentErrors } from './component-tree.ts'
|
|
36
39
|
import { computedConstContext, type ComputedConstContext } from './computed-const.ts'
|
|
37
40
|
import { searchParamsLocalNames } from './env-signal.ts'
|
|
@@ -128,6 +131,14 @@ export abstract class DslTemplateAdapter extends BaseAdapter {
|
|
|
128
131
|
protected booleanTypedProps: Set<string> = new Set()
|
|
129
132
|
/** Names (signal getters + props) whose value is a string (`stringValueNamesFor`). */
|
|
130
133
|
protected stringValueNames: Set<string> = new Set()
|
|
134
|
+
/** The declared types a loop row's type is read from (`loopRowType`, #3762). */
|
|
135
|
+
protected declaredTypes: DeclaredValueTypes | null = null
|
|
136
|
+
/**
|
|
137
|
+
* The component's operand typing (`collectOperandTyping`), which the
|
|
138
|
+
* emitters' primitive plans read through the scope at the emission site
|
|
139
|
+
* (`operandTypingAt`, #3824).
|
|
140
|
+
*/
|
|
141
|
+
protected operandTyping: OperandTyping = EMPTY_OPERAND_TYPING
|
|
131
142
|
/**
|
|
132
143
|
* Module-scope pure-string consts (`const x = 'literal'`), keyed by name →
|
|
133
144
|
* unescaped value. A className template literal that references such a
|
|
@@ -221,6 +232,28 @@ export abstract class DslTemplateAdapter extends BaseAdapter {
|
|
|
221
232
|
*/
|
|
222
233
|
protected resetAdapterState(_ir: ComponentIR): void {}
|
|
223
234
|
|
|
235
|
+
/**
|
|
236
|
+
* Whether `name` (a signal getter, prop, local constant or loop binding)
|
|
237
|
+
* holds a string value at `scope`, which picks the template's string
|
|
238
|
+
* concatenation for a JS `+` (`isStringConcatBinary`). A loop row or
|
|
239
|
+
* callback binding of the same name shadows the outer typing; a row field
|
|
240
|
+
* read (`path`: `row.id`) takes the row type's declared field type.
|
|
241
|
+
* `componentProperty`: read as `props.<name>`.
|
|
242
|
+
*/
|
|
243
|
+
protected isStringValueNameAt(
|
|
244
|
+
name: string,
|
|
245
|
+
componentProperty = false,
|
|
246
|
+
path: readonly string[] = [],
|
|
247
|
+
scope: BindingScope = this.scope,
|
|
248
|
+
): boolean {
|
|
249
|
+
return scope.isStringValueName(name, n => this.stringValueNames.has(n), componentProperty, path, this.declaredTypes?.namedFields)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** The component's operand typing at `scope` (the emission site's, by default). */
|
|
253
|
+
protected operandTypingAt(scope: BindingScope = this.scope): ScopedOperandTyping {
|
|
254
|
+
return this.operandTyping.at(scope)
|
|
255
|
+
}
|
|
256
|
+
|
|
224
257
|
/** `render()` under `scope`, the current scope restored afterwards. */
|
|
225
258
|
protected renderInScope<T>(scope: BindingScope, render: () => T): T {
|
|
226
259
|
const outer = this.scope
|
|
@@ -317,7 +350,8 @@ export abstract class DslTemplateAdapter extends BaseAdapter {
|
|
|
317
350
|
*/
|
|
318
351
|
protected renderLoopRow(loop: IRLoop, spelling: LoopRowSpelling): LoopRow {
|
|
319
352
|
const outside = this.scope
|
|
320
|
-
const
|
|
353
|
+
const rowType = this.declaredTypes ? loopRowType(this.declaredTypes, loop, outside) : null
|
|
354
|
+
const row = outside.enterLoopRow(rowType ? { ...loop, rowType } : loop)
|
|
321
355
|
return this.renderInScope(row, () => {
|
|
322
356
|
spelling.enter?.(row, outside)
|
|
323
357
|
const preamble = (loop.preamble?.declarations ?? []).map(d => spelling.preamble(d))
|
|
@@ -404,11 +438,13 @@ export abstract class DslTemplateAdapter extends BaseAdapter {
|
|
|
404
438
|
this.booleanTypedProps = collectBooleanTypedProps(ir)
|
|
405
439
|
this.localConstants = ir.metadata.localConstants ?? []
|
|
406
440
|
this.rootPropAliases = rootPropAliasNames(ir, this.propsParams.map(p => p.name))
|
|
407
|
-
this.scope = BindingScope.
|
|
441
|
+
this.scope = BindingScope.forComponent(this.propsObjectName)
|
|
408
442
|
this.nullableOptionalProps = collectNullableOptionalProps(ir)
|
|
409
443
|
this.nullableSignalGetters = collectNullableSignalGetters(ir)
|
|
410
444
|
this.nullishAttrCtx = collectNullishAttrContext(ir)
|
|
411
445
|
this.stringValueNames = this.stringValueNamesFor(ir)
|
|
446
|
+
this.declaredTypes = declaredValueTypes(ir)
|
|
447
|
+
this.operandTyping = collectOperandTyping(ir.metadata, { namedFields: this.declaredTypes.namedFields })
|
|
412
448
|
this.moduleStringConsts = collectModuleStringConsts(ir.metadata.localConstants)
|
|
413
449
|
this._searchParamsLocals = searchParamsLocalNames(ir.metadata)
|
|
414
450
|
this._loweringMatchers = prepareLoweringMatchers(ir.metadata)
|
|
@@ -436,5 +472,7 @@ export abstract class DslTemplateAdapter extends BaseAdapter {
|
|
|
436
472
|
this.errors.push(...collectImportedLoopChildComponentErrors(ir, this.componentName, this.loopChildDiagnosticName))
|
|
437
473
|
}
|
|
438
474
|
refuseAmbiguousNilMemoArithmetic(ir.metadata.memos ?? [], this.nullishAttrCtx, this.ambiguousNilSink())
|
|
475
|
+
// `===` / `!==` over an object or array (#3774, #3824 step 8).
|
|
476
|
+
refuseNonPrimitiveStrictEquality(ir, this.operandTyping, this.ambiguousNilSink())
|
|
439
477
|
}
|
|
440
478
|
}
|
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Every name a `.map()`/`.filter()` loop callback binds as its item or
|
|
3
3
|
* index parameter, anywhere in a component's IR tree (#2212 review
|
|
4
|
-
* finding).
|
|
4
|
+
* finding).
|
|
5
|
+
*
|
|
6
|
+
* The string-typing use described below is retired: every template adapter
|
|
7
|
+
* now resolves a name's string typing at the read through the shared
|
|
8
|
+
* `BindingScope` (#3762), and no longer subtracts this set. The remaining
|
|
9
|
+
* callers use it as a whole-component "may be shadowed somewhere" set
|
|
10
|
+
* (root prop aliases, Go's static loop sources, Xslate's memo seeds).
|
|
11
|
+
*
|
|
12
|
+
* Original rationale: `isStringTypedOperand`'s identifier arm resolves a bare
|
|
5
13
|
* identifier against `collectStringValueNames`'s flat, scope-BLIND
|
|
6
14
|
* `Set<string>` — so a loop callback whose param happens to reuse a
|
|
7
15
|
* string prop's name (`items.map((name) => ... 1 + name ...)`, where the
|
|
@@ -298,7 +298,7 @@ export function planLoop<Spelled>(loop: IRLoop, ctx: LoopPlanContext<Spelled>):
|
|
|
298
298
|
destructure,
|
|
299
299
|
staticSource,
|
|
300
300
|
unresolvableConst: staticSource === null ? unresolvableConst(loop, ctx) : null,
|
|
301
|
-
sort: loop.sortComparator ? lowerSortComparator('sort', loop.sortComparator.arrow, ctx.callbackLowering.adapterName) : null,
|
|
301
|
+
sort: loop.sortComparator ? lowerSortComparator('sort', loop.sortComparator.arrow, ctx.callbackLowering.adapterName, ctx.scope.propsObject) : null,
|
|
302
302
|
captureAliases,
|
|
303
303
|
filter: filterPlan(loop, captureAliases, ctx.callbackLowering.adapterName),
|
|
304
304
|
itemVar: loop.iterationShape === 'keys' || destructure === 'lowerable' ? '__bf_item' : loop.param,
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The declared element type of the array a loop ranges over, read from the
|
|
3
|
+
* component's IR: a prop (`props.rows`, a destructured `rows`), a signal or
|
|
4
|
+
* memo getter (`items()`), a row field of an enclosing loop (`r.tags`), an
|
|
5
|
+
* index into one of those, and a `filter` / `slice` / `sort` chain over them.
|
|
6
|
+
* A template adapter enters a loop row with it as
|
|
7
|
+
* `LoopBindingSource.rowType`, so the row's bindings resolve their field types
|
|
8
|
+
* at the read (`row.id`, a destructured `id`) through the shared
|
|
9
|
+
* `BindingScope` (#3762). The IR's `itemType` carries scalar evidence only.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { ParsedExpr } from '../expression-parser.ts'
|
|
13
|
+
import type { ComponentIR, TypeInfo } from '../types.ts'
|
|
14
|
+
import {
|
|
15
|
+
arrayElementType,
|
|
16
|
+
loopValueTypeAt,
|
|
17
|
+
unionOfLoopValueTypes,
|
|
18
|
+
type BindingScope,
|
|
19
|
+
type LoopValueType,
|
|
20
|
+
type NamedTypeFields,
|
|
21
|
+
} from '../scope/binding-scope.ts'
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Array methods whose result has the receiver's element type. `concat` is
|
|
25
|
+
* not one: its result also holds its arguments' elements ({@link concatType}).
|
|
26
|
+
*/
|
|
27
|
+
const SAME_ELEMENT_METHODS: ReadonlySet<string> = new Set([
|
|
28
|
+
'filter', 'slice', 'sort', 'toSorted', 'reverse', 'toReversed',
|
|
29
|
+
])
|
|
30
|
+
|
|
31
|
+
/** What a component declares about the types of the values it reads. */
|
|
32
|
+
export interface DeclaredValueTypes {
|
|
33
|
+
readonly propsParams: readonly {
|
|
34
|
+
readonly name: string
|
|
35
|
+
readonly type: TypeInfo
|
|
36
|
+
readonly declaredType?: TypeInfo
|
|
37
|
+
readonly isRest?: boolean
|
|
38
|
+
}[]
|
|
39
|
+
readonly signals: readonly { readonly getter: string; readonly type: TypeInfo }[]
|
|
40
|
+
readonly memos: readonly { readonly name: string; readonly type: TypeInfo }[]
|
|
41
|
+
/** The identifier the component reads its props object as (`props`); null for a destructured signature. */
|
|
42
|
+
readonly propsObjectName: string | null
|
|
43
|
+
readonly namedFields: NamedTypeFields
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** {@link DeclaredValueTypes} of a component. */
|
|
47
|
+
export function declaredValueTypes(ir: ComponentIR): DeclaredValueTypes {
|
|
48
|
+
const { propsParams, signals, memos, typeDefinitions, propsObjectName } = ir.metadata
|
|
49
|
+
return {
|
|
50
|
+
propsParams,
|
|
51
|
+
signals,
|
|
52
|
+
memos,
|
|
53
|
+
propsObjectName: propsObjectName ?? null,
|
|
54
|
+
namedFields: name => typeDefinitions.find(td => td.name === name)?.properties,
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The element type of the array `loop` ranges over; null when it is not declared. */
|
|
59
|
+
export function loopRowType(
|
|
60
|
+
types: DeclaredValueTypes,
|
|
61
|
+
loop: { readonly arrayParsed?: ParsedExpr; readonly iterationShape?: string; readonly objectIteration?: unknown },
|
|
62
|
+
scope: BindingScope,
|
|
63
|
+
): LoopValueType | null {
|
|
64
|
+
// An `.entries()` / `Object.entries(…)` row is a tuple, not an element.
|
|
65
|
+
if (loop.iterationShape !== undefined || loop.objectIteration || !loop.arrayParsed) return null
|
|
66
|
+
const type = valueType(types, loop.arrayParsed, scope)
|
|
67
|
+
return arrayElementType(type) ?? null
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function valueType(types: DeclaredValueTypes, expr: ParsedExpr, scope: BindingScope): LoopValueType | null {
|
|
71
|
+
switch (expr.kind) {
|
|
72
|
+
case 'identifier': {
|
|
73
|
+
const hit = scope.lookup(expr.name)
|
|
74
|
+
if (!hit) return propType(types, expr.name)
|
|
75
|
+
const row = hit.binding.row
|
|
76
|
+
return row ? loopValueTypeAt(row.itemType, row.segments, types.namedFields) : null
|
|
77
|
+
}
|
|
78
|
+
case 'member': {
|
|
79
|
+
if (expr.computed) return null
|
|
80
|
+
if (expr.object.kind === 'identifier' && expr.object.name === types.propsObjectName && !scope.isBound(expr.object.name)) {
|
|
81
|
+
return propType(types, expr.property)
|
|
82
|
+
}
|
|
83
|
+
const object = valueType(types, expr.object, scope)
|
|
84
|
+
return object && loopValueTypeAt(object, [{ kind: 'field', key: expr.property, isIdent: true }], types.namedFields)
|
|
85
|
+
}
|
|
86
|
+
case 'index-access': {
|
|
87
|
+
const object = valueType(types, expr.object, scope)
|
|
88
|
+
return arrayElementType(object) ?? null
|
|
89
|
+
}
|
|
90
|
+
case 'call':
|
|
91
|
+
return callType(types, expr, scope)
|
|
92
|
+
case 'array-method':
|
|
93
|
+
if (expr.method === 'concat') {
|
|
94
|
+
return concatType(valueType(types, expr.object, scope), expr.args.map(arg => valueType(types, arg, scope)))
|
|
95
|
+
}
|
|
96
|
+
return SAME_ELEMENT_METHODS.has(expr.method) ? valueType(types, expr.object, scope) : null
|
|
97
|
+
default:
|
|
98
|
+
return null
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The type of `receiver.concat(...args)`: an array whose element type is the
|
|
104
|
+
* union of the receiver's element type and each argument's (an array
|
|
105
|
+
* argument's elements, or the argument itself). A field read on a row of it
|
|
106
|
+
* is then a definite string only when it is one in every member
|
|
107
|
+
* (`loopValueTypeAt`), so an `any[]` argument or a field one member lacks
|
|
108
|
+
* types nothing (#3762 review). An argument of unknown type leaves the
|
|
109
|
+
* result with no element type.
|
|
110
|
+
*/
|
|
111
|
+
export function concatType<T extends LoopValueType>(receiver: T | null, args: readonly (T | null)[]): T | null {
|
|
112
|
+
const element = arrayElementType(receiver)
|
|
113
|
+
if (!receiver || !element) return receiver
|
|
114
|
+
const asArray = (elementType: T | undefined): T => ({ ...receiver, kind: 'array', unionTypes: undefined, elementType })
|
|
115
|
+
if (args.some(arg => arg === null)) return asArray(undefined)
|
|
116
|
+
const members = [element, ...(args as T[]).map(arg => arrayElementType(arg) ?? arg)]
|
|
117
|
+
return asArray(unionOfLoopValueTypes(members))
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** A prop's declared type; a destructured prop whose type stays `unknown` carries it as `declaredType`. */
|
|
121
|
+
/** A signal or memo getter's type, or the receiver's type through an element-keeping array method. */
|
|
122
|
+
function callType(types: DeclaredValueTypes, expr: Extract<ParsedExpr, { kind: 'call' }>, scope: BindingScope): LoopValueType | null {
|
|
123
|
+
const callee = expr.callee
|
|
124
|
+
if (callee.kind === 'identifier' && expr.args.length === 0 && !scope.isBound(callee.name)) {
|
|
125
|
+
return types.signals.find(s => s.getter === callee.name)?.type ??
|
|
126
|
+
types.memos.find(m => m.name === callee.name)?.type ?? null
|
|
127
|
+
}
|
|
128
|
+
if (callee.kind !== 'member' || callee.computed) return null
|
|
129
|
+
if (callee.property === 'concat') {
|
|
130
|
+
return concatType(valueType(types, callee.object, scope), expr.args.map(arg => valueType(types, arg, scope)))
|
|
131
|
+
}
|
|
132
|
+
return SAME_ELEMENT_METHODS.has(callee.property) ? valueType(types, callee.object, scope) : null
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function propType(types: DeclaredValueTypes, name: string): LoopValueType | null {
|
|
136
|
+
const param = types.propsParams.find(p => !p.isRest && p.name === name)
|
|
137
|
+
if (!param) return null
|
|
138
|
+
return param.type.kind === 'unknown' ? param.declaredType ?? null : param.type
|
|
139
|
+
}
|