@barefootjs/jsx 0.40.1 → 0.41.0
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/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 +45 -5
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/component-props.d.ts +3 -4
- package/dist/adapters/component-props.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 +24 -12
- 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 +59 -0
- package/dist/adapters/loop-row-type.d.ts.map +1 -0
- package/dist/adapters/nil-reading.d.ts +63 -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 +80 -12
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/adapters/primitive-plan.d.ts +320 -0
- package/dist/adapters/primitive-plan.d.ts.map +1 -0
- package/dist/adapters/prop-classes.d.ts +5 -0
- package/dist/adapters/prop-classes.d.ts.map +1 -1
- 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 +20 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4903 -2311
- package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
- package/dist/ir-to-client-js/child-props-object.d.ts +106 -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/phases/props-extraction.d.ts +3 -3
- package/dist/ir-to-client-js/plan/build-declaration-emit.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/prune-unused-prop-extractions.d.ts +3 -1
- package/dist/ir-to-client-js/prune-unused-prop-extractions.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 +51 -34
- 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-binding.d.ts +58 -13
- package/dist/props-binding.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/ssr-defaults.d.ts +39 -3
- package/dist/ssr-defaults.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 +9 -9
- package/src/__tests__/binding-scope.test.ts +89 -1
- package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -0
- package/src/__tests__/body-destructured-prop-default-arrow.test.ts +2 -2
- package/src/__tests__/body-destructured-props-live.test.ts +5 -5
- 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__/client-js-generation.test.ts +3 -3
- package/src/__tests__/computed-const.test.ts +103 -0
- package/src/__tests__/destructure-default-reads-earlier-param.test.ts +7 -7
- package/src/__tests__/destructured-props-live.test.ts +1 -1
- 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__/issue-3861-object-spread-child.test.ts +102 -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__/memo-body-expr.test.ts +2 -2
- 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 +78 -0
- package/src/__tests__/nullable-signal-attr.test.ts +109 -48
- package/src/__tests__/primitive-plan.test.ts +494 -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__/prop-default-undefined-only.test.ts +115 -0
- package/src/__tests__/prop-references.test.ts +2 -2
- package/src/__tests__/props-destructuring.test.ts +17 -0
- package/src/__tests__/props-object-whole-read.test.ts +40 -0
- package/src/__tests__/prune-unused-prop-extractions.test.ts +3 -3
- package/src/__tests__/reactive-attrs-in-map.test.ts +1 -1
- package/src/__tests__/reactive-factory-inlining.test.ts +4 -1
- package/src/__tests__/renamed-body-destructure-template.test.ts +7 -7
- package/src/__tests__/rest-binding-gaps.test.ts +192 -0
- package/src/__tests__/rewrite-destructured-props.test.ts +1 -1
- package/src/__tests__/root-prop-alias.test.ts +53 -8
- package/src/__tests__/ssr-defaults.test.ts +113 -25
- 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/__tests__/written-binding-substitution.test.ts +8 -8
- package/src/adapters/boolean-result.ts +72 -66
- package/src/adapters/brace-delimiter-text.ts +25 -0
- package/src/adapters/callback-method.ts +159 -27
- package/src/adapters/component-props.ts +3 -4
- package/src/adapters/computed-const.ts +238 -4
- package/src/adapters/dsl-template-adapter.ts +62 -26
- package/src/adapters/loop-bound-names.ts +9 -1
- package/src/adapters/loop-plan.ts +1 -1
- package/src/adapters/loop-row-type.ts +149 -0
- package/src/adapters/nil-reading.ts +295 -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 +183 -19
- package/src/adapters/primitive-plan.ts +765 -0
- package/src/adapters/prop-classes.ts +16 -3
- package/src/analyzer-context.ts +6 -0
- package/src/analyzer.ts +532 -26
- 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 +35 -16
- package/src/ir-to-client-js/build-references.ts +69 -0
- package/src/ir-to-client-js/child-props-object.ts +240 -0
- package/src/ir-to-client-js/collect-elements.ts +31 -63
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +8 -1
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +31 -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 +43 -17
- package/src/ir-to-client-js/imports.ts +2 -2
- package/src/ir-to-client-js/index.ts +53 -11
- package/src/ir-to-client-js/phases/props-extraction.ts +11 -7
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +40 -7
- 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/prune-unused-prop-extractions.ts +58 -5
- package/src/ir-to-client-js/rewrite-destructured-props.ts +31 -1
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +26 -10
- package/src/jsx-to-ir.ts +459 -37
- package/src/nullable-signal-attr.ts +200 -110
- package/src/prop-rewrite.ts +30 -6
- package/src/props-binding.ts +84 -19
- 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/ssr-defaults.ts +100 -11
- package/src/strip-types.ts +31 -3
- package/src/template-position-walk.ts +57 -28
- package/src/types.ts +87 -0
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts +0 -41
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +0 -1
- package/src/adapters/ambiguous-nil-arithmetic.ts +0 -94
|
@@ -16,8 +16,8 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import type { ParsedExpr } from './expression-parser.ts'
|
|
19
|
-
import {
|
|
20
|
-
import type { ComponentIR, MemoInfo, SignalInfo, TypeInfo } from './types.ts'
|
|
19
|
+
import { isBooleanResultParsed } from './adapters/boolean-result.ts'
|
|
20
|
+
import type { ComponentIR, IRMetadata, MemoInfo, SignalInfo, TypeInfo } from './types.ts'
|
|
21
21
|
|
|
22
22
|
function isNullishType(type: TypeInfo): boolean {
|
|
23
23
|
return type.kind === 'primitive' && (type.primitive === 'undefined' || type.primitive === 'null')
|
|
@@ -102,6 +102,57 @@ function collectNullablePropReads(ir: ComponentIR): ReadonlyMap<string, string>
|
|
|
102
102
|
return names
|
|
103
103
|
}
|
|
104
104
|
|
|
105
|
+
/** A required string / number / boolean, or a union of them: a value that is never nullish. */
|
|
106
|
+
function isNonNullishScalar(type: TypeInfo | undefined): boolean {
|
|
107
|
+
if (!type) return false
|
|
108
|
+
if (type.kind === 'union') return (type.unionTypes ?? []).length > 0 && (type.unionTypes ?? []).every(isNonNullishScalar)
|
|
109
|
+
return type.kind === 'primitive' && (type.primitive === 'string' || type.primitive === 'number' || type.primitive === 'boolean')
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Whether `expr` is never `undefined` / `null` at SSR, so JS `expr ?? y` is
|
|
114
|
+
* `expr`, even when it is `false`, `''` or `0`: a boolean-result expression
|
|
115
|
+
* (a comparison, `!`, a boolean literal, `isBooleanResultParsed`), or a read
|
|
116
|
+
* of a required (not optional, not rest) prop declared as a string, number
|
|
117
|
+
* or boolean, or a union of them, read as its binding (`flag`) or as
|
|
118
|
+
* `<propsObjectName>.<key>`, with no binding shadowing the read.
|
|
119
|
+
*/
|
|
120
|
+
export function isNeverNullishOperand(
|
|
121
|
+
expr: ParsedExpr,
|
|
122
|
+
metadata: Pick<IRMetadata, 'propsParams' | 'propsObjectName'>,
|
|
123
|
+
isBound: (name: string) => boolean,
|
|
124
|
+
): boolean {
|
|
125
|
+
if (isBooleanResultParsed(expr)) return true
|
|
126
|
+
const objectName = metadata.propsObjectName
|
|
127
|
+
let matches: (p: IRMetadata['propsParams'][number]) => boolean
|
|
128
|
+
if (objectName && expr.kind === 'member' && !expr.computed && expr.object.kind === 'identifier' && expr.object.name === objectName) {
|
|
129
|
+
if (isBound(objectName)) return false
|
|
130
|
+
matches = p => (p.sourceName ?? p.name) === expr.property
|
|
131
|
+
} else if (!objectName && expr.kind === 'identifier') {
|
|
132
|
+
if (isBound(expr.name)) return false
|
|
133
|
+
matches = p => p.name === expr.name
|
|
134
|
+
} else {
|
|
135
|
+
return false
|
|
136
|
+
}
|
|
137
|
+
const param = metadata.propsParams.find(matches)
|
|
138
|
+
return !!param && !param.optional && !param.isRest && isNonNullishScalar(param.declaredType ?? param.type)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The params of `nullableProps` (`NullishAttrContext.nullableProps`) that
|
|
143
|
+
* `expr`'s value can be read whole from (`collectSeedPropReads`): the props
|
|
144
|
+
* whose absence an attribute bound to `expr` omits it for. A backend that
|
|
145
|
+
* types a prop by value (Go) makes these props nillable.
|
|
146
|
+
*/
|
|
147
|
+
export function nullablePropsYieldedBy(
|
|
148
|
+
expr: ParsedExpr | undefined,
|
|
149
|
+
nullableProps: ReadonlyMap<string, string>,
|
|
150
|
+
): Set<string> {
|
|
151
|
+
const out = new Set<string>()
|
|
152
|
+
collectSeedPropReads(expr, nullableProps, out)
|
|
153
|
+
return out
|
|
154
|
+
}
|
|
155
|
+
|
|
105
156
|
/**
|
|
106
157
|
* Add to `out` the params of `nullableProps` a seed's value can be read
|
|
107
158
|
* whole from: a bare read, a ternary branch, the right operand of `??` /
|
|
@@ -176,24 +227,39 @@ export interface NullishAttrContext {
|
|
|
176
227
|
* (`arithmeticOperandIsUndefined`). */
|
|
177
228
|
readonly undefinedSeededSignals?: ReadonlySet<string>
|
|
178
229
|
readonly memos: ReadonlyMap<string, MemoInfo>
|
|
230
|
+
/** Props that can be absent or `null` at SSR (no default, optional or
|
|
231
|
+
* typed with `undefined` / `null`), by the name a read uses
|
|
232
|
+
* (`collectNullablePropReads`). */
|
|
233
|
+
readonly nullableProps?: ReadonlyMap<string, string>
|
|
179
234
|
/** Props declared as a number that may be `null` or absent (`n: number |
|
|
180
235
|
* null`, `n?: number`), by the name a read uses: the binding name, and
|
|
181
236
|
* `<propsObjectName>.<key>` for a member read (#3474), mapped to what the
|
|
182
237
|
* engine's nil stands for there (`NullableNumberPropNil`, #3533). */
|
|
183
238
|
readonly nullableNumberProps?: ReadonlyMap<string, NullableNumberPropNil>
|
|
239
|
+
/** Props declared as a string or a boolean that may be `null` or absent
|
|
240
|
+
* (`s?: string`, `b: boolean | null`), keyed like {@link nullableNumberProps}.
|
|
241
|
+
* Read only for a relational operand (#3824 step 3): JS converts it with
|
|
242
|
+
* ToNumber unless both sides are strings, so its nil's reading decides the
|
|
243
|
+
* comparison (`undefined < 1` is false, `null < 1` is true). */
|
|
244
|
+
readonly nullableScalarProps?: ReadonlyMap<string, NullableScalarProp>
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** A string or boolean prop's nil reading ({@link NullishAttrContext.nullableScalarProps}). */
|
|
248
|
+
export interface NullableScalarProp {
|
|
249
|
+
readonly family: 'string' | 'boolean'
|
|
250
|
+
readonly nil: NullableNumberPropNil
|
|
184
251
|
}
|
|
185
252
|
|
|
186
253
|
/**
|
|
187
254
|
* What a nullable number prop's nil stands for at SSR (#3533). JS reads an
|
|
188
|
-
* absent prop as `undefined` (`NaN` in arithmetic) and a `null` one as `0
|
|
189
|
-
*
|
|
190
|
-
* decides. A prop whose type admits `null` (`n: number | null`) reads its
|
|
255
|
+
* absent prop as `undefined` (`NaN` in arithmetic) and a `null` one as `0`;
|
|
256
|
+
* the declared type decides the coercion an adapter spells statically. A prop whose type admits `null` (`n: number | null`) reads its
|
|
191
257
|
* nil as `null`. A prop that can only be absent (`n?: number`,
|
|
192
258
|
* `n: number | undefined`) reads it as `undefined`; a present value, `0`
|
|
193
259
|
* included, is unaffected. A prop that admits both (`n?: number | null`,
|
|
194
260
|
* `n: number | null | undefined`) is `'either'`: the type cannot say which
|
|
195
|
-
* one its nil stands for, so
|
|
196
|
-
* (
|
|
261
|
+
* one its nil stands for, so the operand is left to the runtime's helper,
|
|
262
|
+
* which reads the value itself (spec/js-undefined-value.md).
|
|
197
263
|
*/
|
|
198
264
|
export type NullableNumberPropNil = 'null' | 'undefined' | 'either'
|
|
199
265
|
|
|
@@ -208,7 +274,9 @@ export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
|
|
|
208
274
|
signals.filter(s => s.parsed?.kind === 'identifier' && s.parsed.name === 'undefined').map(s => s.getter),
|
|
209
275
|
),
|
|
210
276
|
memos: new Map((ir.metadata.memos ?? []).map(m => [m.name, m])),
|
|
277
|
+
nullableProps: collectNullablePropReads(ir),
|
|
211
278
|
nullableNumberProps: collectNullableNumberProps(ir),
|
|
279
|
+
nullableScalarProps: collectNullableScalarProps(ir),
|
|
212
280
|
}
|
|
213
281
|
}
|
|
214
282
|
|
|
@@ -216,7 +284,10 @@ export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
|
|
|
216
284
|
* What the nil of a number prop that admits `null` / `undefined` and no other
|
|
217
285
|
* family stands for (`NullableNumberPropNil`); `null` for any other type.
|
|
218
286
|
*/
|
|
219
|
-
function
|
|
287
|
+
function nullablePrimitivePropNil(
|
|
288
|
+
type: TypeInfo | undefined,
|
|
289
|
+
optional: boolean,
|
|
290
|
+
): { family: string; nil: NullableNumberPropNil } | null {
|
|
220
291
|
if (!type) return null
|
|
221
292
|
const members = type.kind === 'union' ? (type.unionTypes ?? []) : [type]
|
|
222
293
|
const families = new Set(members.map(m => (m.kind === 'primitive' ? m.primitive : m.kind)))
|
|
@@ -224,22 +295,38 @@ function nullableNumberPropNil(type: TypeInfo | undefined, optional: boolean): N
|
|
|
224
295
|
const admitsAbsent = optional || families.has('undefined')
|
|
225
296
|
families.delete('null')
|
|
226
297
|
families.delete('undefined')
|
|
227
|
-
if (!(admitsNull || admitsAbsent) || families.size !== 1
|
|
228
|
-
|
|
229
|
-
|
|
298
|
+
if (!(admitsNull || admitsAbsent) || families.size !== 1) return null
|
|
299
|
+
const [family] = [...families] as [string]
|
|
300
|
+
if (admitsNull && admitsAbsent) return { family, nil: 'either' }
|
|
301
|
+
return { family, nil: admitsNull ? 'null' : 'undefined' }
|
|
230
302
|
}
|
|
231
303
|
|
|
232
|
-
|
|
233
|
-
|
|
304
|
+
/** Each no-default prop whose declared type `pick` accepts, keyed by the name a read uses. */
|
|
305
|
+
function collectNullablePrimitiveProps<T>(
|
|
306
|
+
ir: ComponentIR,
|
|
307
|
+
pick: (read: { family: string; nil: NullableNumberPropNil }) => T | null,
|
|
308
|
+
): ReadonlyMap<string, T> {
|
|
309
|
+
const names = new Map<string, T>()
|
|
234
310
|
const objectName = ir.metadata.propsObjectName
|
|
235
311
|
for (const p of ir.metadata.propsParams) {
|
|
236
312
|
if (p.isRest || p.defaultValue !== undefined) continue
|
|
237
|
-
const
|
|
238
|
-
|
|
313
|
+
const read = nullablePrimitivePropNil(p.declaredType ?? p.type, p.optional)
|
|
314
|
+
const picked = read === null ? null : pick(read)
|
|
315
|
+
if (picked !== null) names.set(objectName ? `${objectName}.${p.sourceName ?? p.name}` : p.name, picked)
|
|
239
316
|
}
|
|
240
317
|
return names
|
|
241
318
|
}
|
|
242
319
|
|
|
320
|
+
function collectNullableNumberProps(ir: ComponentIR): ReadonlyMap<string, NullableNumberPropNil> {
|
|
321
|
+
return collectNullablePrimitiveProps(ir, read => (read.family === 'number' ? read.nil : null))
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function collectNullableScalarProps(ir: ComponentIR): ReadonlyMap<string, NullableScalarProp> {
|
|
325
|
+
return collectNullablePrimitiveProps(ir, ({ family, nil }) =>
|
|
326
|
+
family === 'string' || family === 'boolean' ? { family, nil } : null,
|
|
327
|
+
)
|
|
328
|
+
}
|
|
329
|
+
|
|
243
330
|
/**
|
|
244
331
|
* Whether an attribute value's SSR value can be `undefined` / `null`, so a
|
|
245
332
|
* DSL adapter must guard the attribute on a nil check to omit it the way
|
|
@@ -248,7 +335,8 @@ function collectNullableNumberProps(ir: ComponentIR): ReadonlyMap<string, Nullab
|
|
|
248
335
|
* nullish value through:
|
|
249
336
|
*
|
|
250
337
|
* - a read of a nullable signal or of a memo whose type or body can be
|
|
251
|
-
* nullish (`label()` with `label = createMemo(() => s())`)
|
|
338
|
+
* nullish (`label()` with `label = createMemo(() => s())`), or of a prop
|
|
339
|
+
* that can be absent (`props.on && 'y'`);
|
|
252
340
|
* - an optional member read (`user()?.name`), an index or computed-key read
|
|
253
341
|
* (`pair[1]`, `row[key]`), or a member read of a value that can itself be
|
|
254
342
|
* nullish;
|
|
@@ -273,7 +361,7 @@ export function attrValueMayBeNullish(
|
|
|
273
361
|
// loop bindings at the top level, none inside a memo body — its getters
|
|
274
362
|
// resolve where the memo is declared, not where it is read.
|
|
275
363
|
const walk = (expr: ParsedExpr, shadowed: (name: string) => boolean): boolean => {
|
|
276
|
-
if (
|
|
364
|
+
if (readsUndefinedLeaf(expr, ctx, shadowed)) return true
|
|
277
365
|
switch (expr.kind) {
|
|
278
366
|
case 'identifier':
|
|
279
367
|
return expr.name === 'undefined'
|
|
@@ -310,6 +398,22 @@ export function attrValueMayBeNullish(
|
|
|
310
398
|
return parsed !== undefined && walk(parsed, isShadowed)
|
|
311
399
|
}
|
|
312
400
|
|
|
401
|
+
/** A method call that can miss (`methodMayReturnUndefined`), or a read of a prop that can be absent. */
|
|
402
|
+
function readsUndefinedLeaf(expr: ParsedExpr, ctx: NullishAttrContext, shadowed: (name: string) => boolean): boolean {
|
|
403
|
+
return methodMayReturnUndefined(expr) || readsNullableProp(expr, ctx, shadowed)
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* A read of a prop that can be absent (`NullishAttrContext.nullableProps`):
|
|
408
|
+
* its binding (`on`) or `props.<key>`, unless a binding shadows the name.
|
|
409
|
+
*/
|
|
410
|
+
function readsNullableProp(expr: ParsedExpr, ctx: NullishAttrContext, shadowed: (name: string) => boolean): boolean {
|
|
411
|
+
if (!ctx.nullableProps?.size) return false
|
|
412
|
+
if (expr.kind === 'identifier') return !shadowed(expr.name) && ctx.nullableProps.has(expr.name)
|
|
413
|
+
if (expr.kind !== 'member' || expr.computed || expr.object.kind !== 'identifier' || shadowed(expr.object.name)) return false
|
|
414
|
+
return ctx.nullableProps.has(`${expr.object.name}.${expr.property}`)
|
|
415
|
+
}
|
|
416
|
+
|
|
313
417
|
/** Array / string methods that read `undefined` when nothing matches. */
|
|
314
418
|
const UNDEFINED_ON_MISS_METHODS: ReadonlySet<string> = new Set(['find', 'findLast'])
|
|
315
419
|
|
|
@@ -405,14 +509,50 @@ function nullableNumberPropRead(
|
|
|
405
509
|
ctx: NullishAttrContext,
|
|
406
510
|
shadowed: (name: string) => boolean,
|
|
407
511
|
): NullableNumberPropNil | null {
|
|
408
|
-
|
|
409
|
-
|
|
512
|
+
return propReadIn(ctx.nullableNumberProps, expr, shadowed)
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/** The entry of `props` (keyed as {@link NullishAttrContext.nullableNumberProps}) that `expr` reads, or `null`. */
|
|
516
|
+
function propReadIn<T>(
|
|
517
|
+
props: ReadonlyMap<string, T> | undefined,
|
|
518
|
+
expr: ParsedExpr,
|
|
519
|
+
shadowed: (name: string) => boolean,
|
|
520
|
+
): T | null {
|
|
521
|
+
if (!props?.size) return null
|
|
522
|
+
if (expr.kind === 'identifier') return shadowed(expr.name) ? null : (props.get(expr.name) ?? null)
|
|
410
523
|
if (expr.kind === 'member' && !expr.computed && expr.object.kind === 'identifier' && !shadowed(expr.object.name)) {
|
|
411
|
-
return
|
|
524
|
+
return props.get(`${expr.object.name}.${expr.property}`) ?? null
|
|
412
525
|
}
|
|
413
526
|
return null
|
|
414
527
|
}
|
|
415
528
|
|
|
529
|
+
/**
|
|
530
|
+
* The string or boolean prop (`NullishAttrContext.nullableScalarProps`) a
|
|
531
|
+
* relational operand reads, directly or through a memo whose body is such a
|
|
532
|
+
* read, or `null`.
|
|
533
|
+
*/
|
|
534
|
+
function scalarPropRead(
|
|
535
|
+
parsed: ParsedExpr | undefined,
|
|
536
|
+
ctx: NullishAttrContext,
|
|
537
|
+
isShadowed: (name: string) => boolean,
|
|
538
|
+
): NullableScalarProp | null {
|
|
539
|
+
if (!ctx.nullableScalarProps?.size || parsed === undefined) return null
|
|
540
|
+
const visiting = new Set<string>()
|
|
541
|
+
const walk = (expr: ParsedExpr, shadowed: (name: string) => boolean): NullableScalarProp | null => {
|
|
542
|
+
const prop = propReadIn(ctx.nullableScalarProps, expr, shadowed)
|
|
543
|
+
if (prop !== null) return prop
|
|
544
|
+
const getter = expr.kind === 'identifier' ? expr.name : bareGetterCallName(expr)
|
|
545
|
+
if (getter === null || shadowed(getter)) return null
|
|
546
|
+
const memo = ctx.memos.get(getter)
|
|
547
|
+
if (!memo?.parsed || visiting.has(getter)) return null
|
|
548
|
+
visiting.add(getter)
|
|
549
|
+
const result = walk(memo.parsed, () => false)
|
|
550
|
+
visiting.delete(getter)
|
|
551
|
+
return result
|
|
552
|
+
}
|
|
553
|
+
return walk(parsed, isShadowed)
|
|
554
|
+
}
|
|
555
|
+
|
|
416
556
|
/**
|
|
417
557
|
* Arithmetic operators JS applies `ToNumber` to, so a `null` operand reads
|
|
418
558
|
* as `0` (`null * 2` is `0`). A template engine's native operator rejects a
|
|
@@ -425,8 +565,15 @@ export const NULL_COERCING_ARITHMETIC_OPS: ReadonlySet<string> = new Set(['-', '
|
|
|
425
565
|
/** How a DSL adapter coerces an arithmetic operand (#3474): `'null'` reads a
|
|
426
566
|
* nil as `0` (JS `ToNumber(null)`), `'undefined'` reads it as `NaN`
|
|
427
567
|
* (`ToNumber(undefined)`), `null` leaves the operand as is. Either coercion
|
|
428
|
-
* leaves a non-nil operand's value as it is
|
|
429
|
-
|
|
568
|
+
* leaves a non-nil operand's value as it is: the operand is a number, so the
|
|
569
|
+
* runtime's JS `Number()` may spell `'undefined'`.
|
|
570
|
+
*
|
|
571
|
+
* `'undefined-keep'` (#3824 step 3) also reads a nil as `NaN`, but the
|
|
572
|
+
* operand is a string or boolean relational operand, which must reach the
|
|
573
|
+
* relational helper unconverted (`'a' < 'b'` compares strings), so the
|
|
574
|
+
* adapter spells it as a nil test: `NaN` when nil, the operand otherwise.
|
|
575
|
+
* `NaN` makes the comparison false, as `undefined < x` is in JS for any `x`. */
|
|
576
|
+
export type ArithmeticCoercion = 'null' | 'undefined' | 'undefined-keep' | null
|
|
430
577
|
|
|
431
578
|
/**
|
|
432
579
|
* The one decision behind every DSL adapter's arithmetic over a nullish
|
|
@@ -436,9 +583,10 @@ export type ArithmeticCoercion = 'null' | 'undefined' | null
|
|
|
436
583
|
* (`NullableNumberPropNil`, #3533), is coerced under the operators JS
|
|
437
584
|
* applies `ToNumber` to ({@link NULL_COERCING_ARITHMETIC_OPS}), and under
|
|
438
585
|
* `+` when the other operand is a number literal, where `+` cannot
|
|
439
|
-
* concatenate. Unary minus
|
|
440
|
-
* `other`. Each adapter only spells the
|
|
441
|
-
* reads nil as `0` spells `'null'` as the
|
|
586
|
+
* concatenate. Unary minus and unary plus apply `ToNumber` too: each passes
|
|
587
|
+
* its operator (`'-'` / `'+'`) with no `other`. Each adapter only spells the
|
|
588
|
+
* coercion; one whose engine already reads nil as `0` spells `'null'` as the
|
|
589
|
+
* operand itself.
|
|
442
590
|
*/
|
|
443
591
|
export function arithmeticOperandCoercion(
|
|
444
592
|
op: string,
|
|
@@ -447,103 +595,45 @@ export function arithmeticOperandCoercion(
|
|
|
447
595
|
ctx: NullishAttrContext,
|
|
448
596
|
isShadowed: (name: string) => boolean = () => false,
|
|
449
597
|
): ArithmeticCoercion {
|
|
598
|
+
if (RELATIONAL_OPS.has(op)) return relationalOperandCoercion(operand, ctx, isShadowed)
|
|
450
599
|
if (!isNumericArithmetic(op, other)) return null
|
|
451
600
|
const seed = arithmeticOperandSeed(operand, ctx, isShadowed)
|
|
452
601
|
if (seed === 'absent') return 'undefined'
|
|
453
|
-
// `'either'
|
|
454
|
-
//
|
|
455
|
-
|
|
602
|
+
// `'either'`: the type cannot say which nil the operand holds, so it is
|
|
603
|
+
// left as it is and the runtime's helper reads its value, `null` or
|
|
604
|
+
// `UNDEF`, exactly (spec/js-undefined-value.md).
|
|
605
|
+
return seed === 'either' ? null : seed
|
|
456
606
|
}
|
|
457
607
|
|
|
458
|
-
/**
|
|
459
|
-
|
|
460
|
-
* `null` and absence (`n?: number | null`), directly or through a memo
|
|
461
|
-
* (#3700). JS reads the omitted prop as `undefined` (`NaN` in arithmetic)
|
|
462
|
-
* and a `null` one as `0`; a template engine holds both as one nil and the
|
|
463
|
-
* type cannot say which, so no spelling of {@link arithmeticOperandCoercion}
|
|
464
|
-
* renders both. Template adapters refuse such an operand instead of
|
|
465
|
-
* rendering one reading silently. Same operators and scope as
|
|
466
|
-
* {@link arithmeticOperandCoercion}.
|
|
467
|
-
*/
|
|
468
|
-
export function arithmeticOperandNilIsAmbiguous(
|
|
469
|
-
op: string,
|
|
470
|
-
operand: ParsedExpr | undefined,
|
|
471
|
-
other: ParsedExpr | undefined,
|
|
472
|
-
ctx: NullishAttrContext,
|
|
473
|
-
isShadowed: (name: string) => boolean = () => false,
|
|
474
|
-
): boolean {
|
|
475
|
-
return isNumericArithmetic(op, other) && arithmeticOperandSeed(operand, ctx, isShadowed) === 'either'
|
|
476
|
-
}
|
|
608
|
+
/** The relational operators, whose operands JS converts with ToNumber unless both are strings. */
|
|
609
|
+
const RELATIONAL_OPS: ReadonlySet<string> = new Set(['<', '<=', '>', '>='])
|
|
477
610
|
|
|
478
611
|
/**
|
|
479
|
-
*
|
|
480
|
-
*
|
|
481
|
-
*
|
|
482
|
-
*
|
|
483
|
-
*
|
|
484
|
-
* it
|
|
485
|
-
*
|
|
612
|
+
* The coercion of a relational operand (#3824 step 3). The runtime's
|
|
613
|
+
* relational helpers read a nil operand as JS `null` (`0`), so only an operand
|
|
614
|
+
* whose nil reads as `undefined` needs spelling: it goes through `NaN`, which
|
|
615
|
+
* makes the comparison false, as `undefined < 1` is in JS. A nil whose
|
|
616
|
+
* reading the type leaves open (`n?: number | null`, `s?: string | null`) is
|
|
617
|
+
* left as it is: the relational helper reads its value, `null` or `UNDEF`,
|
|
618
|
+
* exactly.
|
|
486
619
|
*/
|
|
487
|
-
|
|
488
|
-
|
|
620
|
+
function relationalOperandCoercion(
|
|
621
|
+
operand: ParsedExpr | undefined,
|
|
489
622
|
ctx: NullishAttrContext,
|
|
490
|
-
|
|
491
|
-
):
|
|
492
|
-
const
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
for (const [op, operand, other] of arithmeticOperandsOf(e)) {
|
|
499
|
-
if (arithmeticOperandNilIsAmbiguous(op, operand, other, ctx, at.asShadowPredicate())) found.push(operand)
|
|
500
|
-
}
|
|
501
|
-
for (const child of parsedExprChildren(e)) visit(child, at)
|
|
502
|
-
}
|
|
503
|
-
if (!ctx.nullableNumberProps?.size) return found
|
|
504
|
-
visit(expr, scope)
|
|
505
|
-
return found
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
/** The `[op, operand, other]` triples of an arithmetic node's operands. */
|
|
509
|
-
function arithmeticOperandsOf(e: ParsedExpr): Array<[string, ParsedExpr, ParsedExpr | undefined]> {
|
|
510
|
-
if (e.kind === 'binary') return [[e.op, e.left, e.right], [e.op, e.right, e.left]]
|
|
511
|
-
if (e.kind === 'unary' && e.op === '-') return [['-', e.argument, undefined]]
|
|
512
|
-
return []
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
/** The value-position sub-expressions of `e` (an arrow's body included). */
|
|
516
|
-
function parsedExprChildren(e: ParsedExpr): ParsedExpr[] {
|
|
517
|
-
switch (e.kind) {
|
|
518
|
-
case 'call':
|
|
519
|
-
return [e.callee, ...e.args]
|
|
520
|
-
case 'member':
|
|
521
|
-
return [e.object]
|
|
522
|
-
case 'index-access':
|
|
523
|
-
return [e.object, e.index]
|
|
524
|
-
case 'binary':
|
|
525
|
-
case 'logical':
|
|
526
|
-
return [e.left, e.right]
|
|
527
|
-
case 'unary':
|
|
528
|
-
return [e.argument]
|
|
529
|
-
case 'conditional':
|
|
530
|
-
return [e.test, e.consequent, e.alternate]
|
|
531
|
-
case 'template-literal':
|
|
532
|
-
return e.parts.flatMap(p => (p.type === 'expression' ? [p.expr] : []))
|
|
533
|
-
case 'array-literal':
|
|
534
|
-
return e.elements
|
|
535
|
-
case 'array-method':
|
|
536
|
-
return [e.object, ...e.args, ...(e.method === 'flat' && e.depthExpr ? [e.depthExpr] : [])]
|
|
537
|
-
case 'object-literal':
|
|
538
|
-
return e.properties.map(p => (p.kind === 'spread' ? p.expr : p.value))
|
|
539
|
-
default:
|
|
540
|
-
return []
|
|
541
|
-
}
|
|
623
|
+
isShadowed: (name: string) => boolean,
|
|
624
|
+
): ArithmeticCoercion {
|
|
625
|
+
const seed = arithmeticOperandSeed(operand, ctx, isShadowed)
|
|
626
|
+
if (seed === 'absent' || seed === 'undefined') return 'undefined'
|
|
627
|
+
if (seed !== null) return null
|
|
628
|
+
// A string or boolean prop that may only be absent (`s?: string`): its nil
|
|
629
|
+
// is `undefined` too, but a present value must reach the helper as it is.
|
|
630
|
+
return scalarPropRead(operand, ctx, isShadowed)?.nil === 'undefined' ? 'undefined-keep' : null
|
|
542
631
|
}
|
|
543
632
|
|
|
544
|
-
/** The operators {@link arithmeticOperandCoercion} applies `ToNumber` under
|
|
633
|
+
/** The operators {@link arithmeticOperandCoercion} applies `ToNumber` under:
|
|
634
|
+
* a `+` with no `other` is unary plus, which always does. */
|
|
545
635
|
function isNumericArithmetic(op: string, other: ParsedExpr | undefined): boolean {
|
|
546
|
-
return NULL_COERCING_ARITHMETIC_OPS.has(op) || (op === '+' && isNumberLiteral(other))
|
|
636
|
+
return NULL_COERCING_ARITHMETIC_OPS.has(op) || (op === '+' && (other === undefined || isNumberLiteral(other)))
|
|
547
637
|
}
|
|
548
638
|
|
|
549
639
|
function isNumberLiteral(expr: ParsedExpr | undefined): boolean {
|
package/src/prop-rewrite.ts
CHANGED
|
@@ -307,7 +307,35 @@ export function rewriteBarePropRefs(
|
|
|
307
307
|
propAliases?: ReadonlyMap<string, string>,
|
|
308
308
|
replacementFor?: (localName: string, callerKey: string) => string,
|
|
309
309
|
): string | undefined {
|
|
310
|
-
|
|
310
|
+
const foundPropRefs = foundBarePropRefs(node, propNames, extraPropRefs)
|
|
311
|
+
if (foundPropRefs.size === 0) return undefined
|
|
312
|
+
return (
|
|
313
|
+
applyScopedPropRefRewrite(text, foundPropRefs, propAliases, replacementFor) ??
|
|
314
|
+
applyRegexPropRefRewrite(text, foundPropRefs, propAliases, replacementFor)
|
|
315
|
+
)
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* `rewriteBarePropRefs` without the regex fallback: `null` when `text`
|
|
320
|
+
* does not parse as an expression, so the caller refuses the shape instead
|
|
321
|
+
* of splicing names the scope-blind regex would also hit inside an inner
|
|
322
|
+
* binding (`const one = 'a'` → `const _p.one = 'a'`, #3845).
|
|
323
|
+
*/
|
|
324
|
+
export function rewriteBarePropRefsStructural(
|
|
325
|
+
text: string,
|
|
326
|
+
node: ts.Node,
|
|
327
|
+
propNames: Set<string>,
|
|
328
|
+
extraPropRefs?: ReadonlySet<string>,
|
|
329
|
+
propAliases?: ReadonlyMap<string, string>,
|
|
330
|
+
replacementFor?: (localName: string, callerKey: string) => string,
|
|
331
|
+
): string | undefined | null {
|
|
332
|
+
const foundPropRefs = foundBarePropRefs(node, propNames, extraPropRefs)
|
|
333
|
+
if (foundPropRefs.size === 0) return undefined
|
|
334
|
+
return applyScopedPropRefRewrite(text, foundPropRefs, propAliases, replacementFor)
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** The prop names `node` reads as values, plus the `extraPropRefs` that are props. */
|
|
338
|
+
function foundBarePropRefs(node: ts.Node, propNames: Set<string>, extraPropRefs?: ReadonlySet<string>): Set<string> {
|
|
311
339
|
const foundPropRefs = new Set<string>()
|
|
312
340
|
collectAstPropRefs(node, propNames, foundPropRefs)
|
|
313
341
|
if (extraPropRefs) {
|
|
@@ -315,9 +343,5 @@ export function rewriteBarePropRefs(
|
|
|
315
343
|
if (propNames.has(ref)) foundPropRefs.add(ref)
|
|
316
344
|
}
|
|
317
345
|
}
|
|
318
|
-
|
|
319
|
-
return (
|
|
320
|
-
applyScopedPropRefRewrite(text, foundPropRefs, propAliases, replacementFor) ??
|
|
321
|
-
applyRegexPropRefRewrite(text, foundPropRefs, propAliases, replacementFor)
|
|
322
|
-
)
|
|
346
|
+
return foundPropRefs
|
|
323
347
|
}
|
package/src/props-binding.ts
CHANGED
|
@@ -455,7 +455,14 @@ const EMPTY_SET: ReadonlySet<string> = new Set()
|
|
|
455
455
|
* never bound a parameter — so callers synthesize one of these instead of a
|
|
456
456
|
* full `ParamInfo` (which would need a fabricated `type`/`optional`).
|
|
457
457
|
*/
|
|
458
|
-
export type LivePropReadInfo = Pick<ParamInfo, 'name' | 'sourceName' | 'defaultValue' | 'defaultContainsArrow' | 'defaultParts'>
|
|
458
|
+
export type LivePropReadInfo = Pick<ParamInfo, 'name' | 'sourceName' | 'defaultValue' | 'defaultContainsArrow' | 'defaultParts'> & {
|
|
459
|
+
/**
|
|
460
|
+
* The fallback is the author's own `??` (`const label = props.label ??
|
|
461
|
+
* 'x'`, a body alias), which replaces `null` too, not a destructure
|
|
462
|
+
* default, which replaces only `undefined`.
|
|
463
|
+
*/
|
|
464
|
+
readonly coalescesNull?: boolean
|
|
465
|
+
}
|
|
459
466
|
|
|
460
467
|
/** `_p.<callerKey>` — `_p` is always keyed by the caller-facing name (#2524 CSR half). */
|
|
461
468
|
export function propReadBase(p: LivePropReadInfo): string {
|
|
@@ -463,8 +470,8 @@ export function propReadBase(p: LivePropReadInfo): string {
|
|
|
463
470
|
}
|
|
464
471
|
|
|
465
472
|
/**
|
|
466
|
-
* The text a destructure default contributes
|
|
467
|
-
*
|
|
473
|
+
* The text a destructure default contributes to a prop read
|
|
474
|
+
* ({@link composePropRead}). An arrow (or function-expression) default needs its own
|
|
468
475
|
* parens — `??` binds tighter than an arrow head, so `_p.onInput ?? () =>
|
|
469
476
|
* {}` is a SyntaxError. The one implementation of that decision, shared by
|
|
470
477
|
* `propReadFallback` below (every live `_p.X` read) and `collectConstant`'s
|
|
@@ -477,11 +484,24 @@ export function coalesceDefaultText(defaultValue: string, defaultContainsArrow:
|
|
|
477
484
|
}
|
|
478
485
|
|
|
479
486
|
/**
|
|
480
|
-
*
|
|
481
|
-
*
|
|
487
|
+
* What a prop read falls back to:
|
|
488
|
+
* - `default`: the prop's destructure default, which JS applies to an
|
|
489
|
+
* absent or `undefined` prop only, never to `null`
|
|
490
|
+
* (`spec/js-undefined-value.md`, "Decisions" (6)).
|
|
491
|
+
* - `guard`: a value the compiler supplies for a defaultless prop so the
|
|
492
|
+
* read cannot throw (`[]` for a loop array, `{}` for a member read); it
|
|
493
|
+
* replaces either nil.
|
|
494
|
+
*/
|
|
495
|
+
export interface PropReadFallback {
|
|
496
|
+
readonly kind: 'default' | 'guard'
|
|
497
|
+
readonly text: string
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* The fallback of a prop read, or null when the bare read is correct.
|
|
502
|
+
* Precedence:
|
|
482
503
|
* 1. an explicit destructure default (`{ x = 1 }`) — an arrow default gets
|
|
483
|
-
* an extra paren, since `??`
|
|
484
|
-
* `_p.onInput ?? () => {}` would be a syntax error.
|
|
504
|
+
* an extra paren, since `??` / `?:` bind tighter than the arrow head.
|
|
485
505
|
* 2. `usedAsLoopArray` → `[]`.
|
|
486
506
|
* 3. property/index access → `{}`, UNLESS the prop also guards a
|
|
487
507
|
* conditional: `{}` is truthy, so that would always render the branch.
|
|
@@ -494,28 +514,72 @@ export function propReadFallback(
|
|
|
494
514
|
p: LivePropReadInfo,
|
|
495
515
|
usage: PropUsage | undefined,
|
|
496
516
|
usedAsCondition: boolean,
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
if (
|
|
501
|
-
if (
|
|
517
|
+
retained?: ReadonlySet<string>,
|
|
518
|
+
): PropReadFallback | null {
|
|
519
|
+
const defaultText = propDefaultText(p, retained)
|
|
520
|
+
if (defaultText) return { kind: p.coalescesNull ? 'guard' : 'default', text: coalesceDefaultText(defaultText, p.defaultContainsArrow) }
|
|
521
|
+
if (usage?.usedAsLoopArray) return { kind: 'guard', text: '[]' }
|
|
522
|
+
if (propHasPropertyAccess(usage) && !usedAsCondition) return { kind: 'guard', text: '{}' }
|
|
502
523
|
return null
|
|
503
524
|
}
|
|
504
525
|
|
|
526
|
+
/**
|
|
527
|
+
* A prop read `base` (a plain property read, `_p.x` / `props.x`, so reading
|
|
528
|
+
* it twice is safe) with its fallback, unparenthesized: a destructure
|
|
529
|
+
* default as `base === undefined ? default : base`, which keeps an explicit
|
|
530
|
+
* `null`, as JS does; a guard as `base ?? guard`. The one spelling every
|
|
531
|
+
* client-JS prop read and the body-destructure expansion share, and the
|
|
532
|
+
* shape `parsePropReadInitializer` recognizes.
|
|
533
|
+
*/
|
|
534
|
+
export function composePropRead(base: string, fallback: PropReadFallback | null): string {
|
|
535
|
+
if (fallback === null) return base
|
|
536
|
+
return fallback.kind === 'default' ? `${base} === undefined ? ${fallback.text} : ${base}` : `${base} ?? ${fallback.text}`
|
|
537
|
+
}
|
|
538
|
+
|
|
505
539
|
/**
|
|
506
540
|
* A prop's destructure default as client JS. A default that reads an earlier
|
|
507
541
|
* binding of its own pattern (`{ off = false, on = off }`, #3531) renders each
|
|
508
|
-
* read as that prop's own live read (`_p.off
|
|
509
|
-
* scope binds no `off`; any other default is its
|
|
510
|
-
* the
|
|
542
|
+
* read as that prop's own live read (`_p.off === undefined ? false :
|
|
543
|
+
* _p.off`) where the emitted scope binds no `off`; any other default is its
|
|
544
|
+
* source text. `retained` names the earlier bindings that ARE locals of the
|
|
545
|
+
* emitted scope (`retainedPropLocals`): a read of one stays a read of that
|
|
546
|
+
* local, as in JS, so a default sharing an earlier binding's array, or
|
|
547
|
+
* reading a value a default assigned to it, sees that binding. Undefined
|
|
548
|
+
* when the prop has no default.
|
|
511
549
|
*/
|
|
512
|
-
export function propDefaultText(p: LivePropReadInfo): string | undefined {
|
|
550
|
+
export function propDefaultText(p: LivePropReadInfo, retained: ReadonlySet<string> = EMPTY_SET): string | undefined {
|
|
513
551
|
if (!p.defaultParts) return p.defaultValue
|
|
514
552
|
return p.defaultParts
|
|
515
|
-
.map(part =>
|
|
553
|
+
.map(part => {
|
|
554
|
+
if (part.kind === 'text') return part.text
|
|
555
|
+
return retained.has(part.param.name) ? part.param.name : livePropReadExpr(part.param, undefined, false, retained)
|
|
556
|
+
})
|
|
516
557
|
.join('')
|
|
517
558
|
}
|
|
518
559
|
|
|
560
|
+
/**
|
|
561
|
+
* The destructured prop bindings the init body keeps as locals, by local
|
|
562
|
+
* name: a written or mutated parameter (`let start = …`, `emitPropsExtraction`)
|
|
563
|
+
* and a body destructure of the props object that does not keep its declared
|
|
564
|
+
* value (`let { a } = props`, or one mutated later). Every other destructured
|
|
565
|
+
* binding is read live (`keepsDeclaredValue`). A default reading one of these
|
|
566
|
+
* reads the local ({@link propDefaultText}).
|
|
567
|
+
*/
|
|
568
|
+
export function retainedPropLocals(b: {
|
|
569
|
+
propsObjectName: string | null
|
|
570
|
+
propsParams: readonly ParamInfo[]
|
|
571
|
+
localConstants: readonly ConstantInfo[]
|
|
572
|
+
}): ReadonlySet<string> {
|
|
573
|
+
const names = new Set<string>()
|
|
574
|
+
if (b.propsObjectName === null) {
|
|
575
|
+
for (const p of b.propsParams) if (!p.isRest && !keepsDeclaredValue(p)) names.add(p.name)
|
|
576
|
+
}
|
|
577
|
+
for (const c of b.localConstants) {
|
|
578
|
+
if (c.propsDestructure && !c.isModule && !keepsDeclaredValue(c)) names.add(c.name)
|
|
579
|
+
}
|
|
580
|
+
return names
|
|
581
|
+
}
|
|
582
|
+
|
|
519
583
|
/**
|
|
520
584
|
* The live read of a destructured prop inside init scope, in the same shape
|
|
521
585
|
* props-object mode emits for `props.X`. The one formula every live-read
|
|
@@ -536,10 +600,11 @@ export function livePropReadExpr(
|
|
|
536
600
|
p: LivePropReadInfo,
|
|
537
601
|
usage: PropUsage | undefined,
|
|
538
602
|
usedAsCondition: boolean,
|
|
603
|
+
retained?: ReadonlySet<string>,
|
|
539
604
|
): string {
|
|
540
605
|
const base = propReadBase(p)
|
|
541
|
-
const fallback = propReadFallback(p, usage, usedAsCondition)
|
|
542
|
-
return fallback === null ? base : `(${base
|
|
606
|
+
const fallback = propReadFallback(p, usage, usedAsCondition, retained)
|
|
607
|
+
return fallback === null ? base : `(${composePropRead(base, fallback)})`
|
|
543
608
|
}
|
|
544
609
|
|
|
545
610
|
/**
|