@barefootjs/jsx 0.40.0 → 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 +42 -0
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -0
- package/dist/adapters/attr-plan.d.ts.map +1 -1
- package/dist/adapters/boolean-carrier.d.ts +31 -0
- package/dist/adapters/boolean-carrier.d.ts.map +1 -0
- 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/component-props.d.ts +22 -7
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +28 -1
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +39 -1
- 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 +45 -0
- package/dist/adapters/number-value.d.ts.map +1 -0
- 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 +11 -0
- package/dist/analyzer-context.d.ts.map +1 -1
- package/dist/analyzer.d.ts +34 -0
- 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 +24 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5671 -2586
- 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-component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts +29 -0
- 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-lazy-row.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/build-lazy-row.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/plan/build-reactive-effects.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/inner-loop.d.ts +5 -4
- package/dist/ir-to-client-js/control-flow/plan/inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +15 -0
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/reactive-effects.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/reactive-effects.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 +22 -0
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/lazy-row.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts +3 -2
- 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 +37 -19
- 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/reactivity.d.ts.map +1 -1
- package/dist/ir-to-client-js/safe-html.d.ts +9 -3
- package/dist/ir-to-client-js/safe-html.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/ir-to-client-js/types.d.ts +30 -0
- package/dist/ir-to-client-js/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +8 -0
- package/dist/ir-to-client-js/utils.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 +95 -10
- 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 +27 -3
- 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 +100 -4
- 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 +16 -0
- package/dist/scope/root-prop-alias.d.ts.map +1 -1
- package/dist/signal-prop-name-collision.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts +4 -2
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +14 -0
- package/dist/ssr-seed-plan.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-parts.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 +120 -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-ratchet.test.ts +12 -2
- 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-carrier.test.ts +74 -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__/component-props.test.ts +14 -0
- package/src/__tests__/computed-const.test.ts +188 -0
- package/src/__tests__/emit-to-string-call.test.ts +2 -1
- package/src/__tests__/expression-parser.test.ts +24 -0
- 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 +143 -0
- 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-prop-read.test.ts +111 -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 +137 -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 +231 -2
- package/src/__tests__/primitive-plan.test.ts +416 -0
- package/src/__tests__/profile-binding-ids.test.ts +14 -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 +79 -1
- package/src/__tests__/signal-prop-name-collision.test.ts +102 -2
- package/src/__tests__/source-file-cache.test.ts +105 -0
- package/src/__tests__/ssr-seed-plan.test.ts +43 -0
- 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__/template-parts-literal-boundary.test.ts +19 -0
- package/src/adapters/ambiguous-nil-arithmetic.ts +103 -0
- package/src/adapters/attr-plan.ts +6 -2
- package/src/adapters/boolean-carrier.ts +76 -0
- 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/component-props.ts +28 -8
- package/src/adapters/computed-const.ts +443 -10
- package/src/adapters/dsl-template-adapter.ts +79 -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 +108 -0
- 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 +14 -0
- package/src/analyzer.ts +692 -85
- 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 +74 -2
- package/src/index.ts +45 -9
- 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 +100 -67
- package/src/ir-to-client-js/compute-inlinability.ts +1 -1
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +15 -1
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +85 -5
- package/src/ir-to-client-js/control-flow/plan/build-lazy-row.ts +3 -0
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +30 -28
- package/src/ir-to-client-js/control-flow/plan/build-reactive-effects.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/inner-loop.ts +5 -4
- package/src/ir-to-client-js/control-flow/plan/loop-child-arm.ts +2 -0
- package/src/ir-to-client-js/control-flow/plan/loop.ts +12 -0
- package/src/ir-to-client-js/control-flow/plan/reactive-effects.ts +2 -0
- package/src/ir-to-client-js/control-flow/shared.ts +23 -24
- package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +29 -0
- package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +8 -2
- package/src/ir-to-client-js/control-flow/stringify/event-listener.ts +8 -1
- package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +8 -7
- package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +8 -7
- package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +4 -4
- package/src/ir-to-client-js/control-flow/stringify/loop.ts +3 -3
- package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +6 -6
- package/src/ir-to-client-js/emit-reactive.ts +1 -1
- package/src/ir-to-client-js/html-template.ts +95 -38
- 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 +42 -15
- package/src/ir-to-client-js/plan/static-array-child-init.ts +34 -19
- package/src/ir-to-client-js/prop-handling.ts +22 -3
- package/src/ir-to-client-js/reactivity.ts +14 -0
- package/src/ir-to-client-js/safe-html.ts +11 -4
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +30 -12
- package/src/ir-to-client-js/types.ts +23 -0
- package/src/ir-to-client-js/utils.ts +15 -1
- package/src/jsx-to-ir.ts +510 -58
- package/src/nullable-signal-attr.ts +366 -28
- package/src/prop-rewrite.ts +30 -6
- package/src/props-binding.ts +54 -3
- package/src/props-object-whole-read.ts +87 -0
- package/src/rich-type-refusal.ts +2 -1
- package/src/scope/binding-scope.ts +203 -11
- package/src/scope/lexical-scope-walk.ts +4 -3
- package/src/scope/root-prop-alias.ts +23 -0
- package/src/signal-prop-name-collision.ts +113 -22
- package/src/ssr-defaults.ts +10 -4
- package/src/ssr-seed-plan.ts +82 -17
- package/src/strip-types.ts +31 -3
- package/src/template-parts.ts +21 -1
- package/src/template-position-walk.ts +57 -28
- package/src/types.ts +117 -0
|
@@ -16,7 +16,9 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import type { ParsedExpr } from './expression-parser.ts'
|
|
19
|
-
import
|
|
19
|
+
import { isBooleanResultParsed } from './adapters/boolean-result.ts'
|
|
20
|
+
import { BindingScope } from './scope/binding-scope.ts'
|
|
21
|
+
import type { ComponentIR, IRMetadata, MemoInfo, SignalInfo, TypeInfo } from './types.ts'
|
|
20
22
|
|
|
21
23
|
function isNullishType(type: TypeInfo): boolean {
|
|
22
24
|
return type.kind === 'primitive' && (type.primitive === 'undefined' || type.primitive === 'null')
|
|
@@ -101,6 +103,57 @@ function collectNullablePropReads(ir: ComponentIR): ReadonlyMap<string, string>
|
|
|
101
103
|
return names
|
|
102
104
|
}
|
|
103
105
|
|
|
106
|
+
/** A required string / number / boolean, or a union of them: a value that is never nullish. */
|
|
107
|
+
function isNonNullishScalar(type: TypeInfo | undefined): boolean {
|
|
108
|
+
if (!type) return false
|
|
109
|
+
if (type.kind === 'union') return (type.unionTypes ?? []).length > 0 && (type.unionTypes ?? []).every(isNonNullishScalar)
|
|
110
|
+
return type.kind === 'primitive' && (type.primitive === 'string' || type.primitive === 'number' || type.primitive === 'boolean')
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Whether `expr` is never `undefined` / `null` at SSR, so JS `expr ?? y` is
|
|
115
|
+
* `expr`, even when it is `false`, `''` or `0`: a boolean-result expression
|
|
116
|
+
* (a comparison, `!`, a boolean literal, `isBooleanResultParsed`), or a read
|
|
117
|
+
* of a required (not optional, not rest) prop declared as a string, number
|
|
118
|
+
* or boolean, or a union of them, read as its binding (`flag`) or as
|
|
119
|
+
* `<propsObjectName>.<key>`, with no binding shadowing the read.
|
|
120
|
+
*/
|
|
121
|
+
export function isNeverNullishOperand(
|
|
122
|
+
expr: ParsedExpr,
|
|
123
|
+
metadata: Pick<IRMetadata, 'propsParams' | 'propsObjectName'>,
|
|
124
|
+
isBound: (name: string) => boolean,
|
|
125
|
+
): boolean {
|
|
126
|
+
if (isBooleanResultParsed(expr)) return true
|
|
127
|
+
const objectName = metadata.propsObjectName
|
|
128
|
+
let matches: (p: IRMetadata['propsParams'][number]) => boolean
|
|
129
|
+
if (objectName && expr.kind === 'member' && !expr.computed && expr.object.kind === 'identifier' && expr.object.name === objectName) {
|
|
130
|
+
if (isBound(objectName)) return false
|
|
131
|
+
matches = p => (p.sourceName ?? p.name) === expr.property
|
|
132
|
+
} else if (!objectName && expr.kind === 'identifier') {
|
|
133
|
+
if (isBound(expr.name)) return false
|
|
134
|
+
matches = p => p.name === expr.name
|
|
135
|
+
} else {
|
|
136
|
+
return false
|
|
137
|
+
}
|
|
138
|
+
const param = metadata.propsParams.find(matches)
|
|
139
|
+
return !!param && !param.optional && !param.isRest && isNonNullishScalar(param.declaredType ?? param.type)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* The params of `nullableProps` (`NullishAttrContext.nullableProps`) that
|
|
144
|
+
* `expr`'s value can be read whole from (`collectSeedPropReads`): the props
|
|
145
|
+
* whose absence an attribute bound to `expr` omits it for. A backend that
|
|
146
|
+
* types a prop by value (Go) makes these props nillable.
|
|
147
|
+
*/
|
|
148
|
+
export function nullablePropsYieldedBy(
|
|
149
|
+
expr: ParsedExpr | undefined,
|
|
150
|
+
nullableProps: ReadonlyMap<string, string>,
|
|
151
|
+
): Set<string> {
|
|
152
|
+
const out = new Set<string>()
|
|
153
|
+
collectSeedPropReads(expr, nullableProps, out)
|
|
154
|
+
return out
|
|
155
|
+
}
|
|
156
|
+
|
|
104
157
|
/**
|
|
105
158
|
* Add to `out` the params of `nullableProps` a seed's value can be read
|
|
106
159
|
* whole from: a bare read, a ternary branch, the right operand of `??` /
|
|
@@ -175,11 +228,27 @@ export interface NullishAttrContext {
|
|
|
175
228
|
* (`arithmeticOperandIsUndefined`). */
|
|
176
229
|
readonly undefinedSeededSignals?: ReadonlySet<string>
|
|
177
230
|
readonly memos: ReadonlyMap<string, MemoInfo>
|
|
231
|
+
/** Props that can be absent or `null` at SSR (no default, optional or
|
|
232
|
+
* typed with `undefined` / `null`), by the name a read uses
|
|
233
|
+
* (`collectNullablePropReads`). */
|
|
234
|
+
readonly nullableProps?: ReadonlyMap<string, string>
|
|
178
235
|
/** Props declared as a number that may be `null` or absent (`n: number |
|
|
179
236
|
* null`, `n?: number`), by the name a read uses: the binding name, and
|
|
180
237
|
* `<propsObjectName>.<key>` for a member read (#3474), mapped to what the
|
|
181
238
|
* engine's nil stands for there (`NullableNumberPropNil`, #3533). */
|
|
182
239
|
readonly nullableNumberProps?: ReadonlyMap<string, NullableNumberPropNil>
|
|
240
|
+
/** Props declared as a string or a boolean that may be `null` or absent
|
|
241
|
+
* (`s?: string`, `b: boolean | null`), keyed like {@link nullableNumberProps}.
|
|
242
|
+
* Read only for a relational operand (#3824 step 3): JS converts it with
|
|
243
|
+
* ToNumber unless both sides are strings, so its nil's reading decides the
|
|
244
|
+
* comparison (`undefined < 1` is false, `null < 1` is true). */
|
|
245
|
+
readonly nullableScalarProps?: ReadonlyMap<string, NullableScalarProp>
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** A string or boolean prop's nil reading ({@link NullishAttrContext.nullableScalarProps}). */
|
|
249
|
+
export interface NullableScalarProp {
|
|
250
|
+
readonly family: 'string' | 'boolean'
|
|
251
|
+
readonly nil: NullableNumberPropNil
|
|
183
252
|
}
|
|
184
253
|
|
|
185
254
|
/**
|
|
@@ -189,10 +258,12 @@ export interface NullishAttrContext {
|
|
|
189
258
|
* decides. A prop whose type admits `null` (`n: number | null`) reads its
|
|
190
259
|
* nil as `null`. A prop that can only be absent (`n?: number`,
|
|
191
260
|
* `n: number | undefined`) reads it as `undefined`; a present value, `0`
|
|
192
|
-
* included, is unaffected. A prop that admits both (`n?: number | null
|
|
193
|
-
*
|
|
261
|
+
* included, is unaffected. A prop that admits both (`n?: number | null`,
|
|
262
|
+
* `n: number | null | undefined`) is `'either'`: the type cannot say which
|
|
263
|
+
* one its nil stands for, so arithmetic over it is refused
|
|
264
|
+
* (`arithmeticOperandNilIsAmbiguous`).
|
|
194
265
|
*/
|
|
195
|
-
export type NullableNumberPropNil = 'null' | 'undefined'
|
|
266
|
+
export type NullableNumberPropNil = 'null' | 'undefined' | 'either'
|
|
196
267
|
|
|
197
268
|
export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
|
|
198
269
|
const signals = ir.metadata.signals ?? []
|
|
@@ -205,7 +276,9 @@ export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
|
|
|
205
276
|
signals.filter(s => s.parsed?.kind === 'identifier' && s.parsed.name === 'undefined').map(s => s.getter),
|
|
206
277
|
),
|
|
207
278
|
memos: new Map((ir.metadata.memos ?? []).map(m => [m.name, m])),
|
|
279
|
+
nullableProps: collectNullablePropReads(ir),
|
|
208
280
|
nullableNumberProps: collectNullableNumberProps(ir),
|
|
281
|
+
nullableScalarProps: collectNullableScalarProps(ir),
|
|
209
282
|
}
|
|
210
283
|
}
|
|
211
284
|
|
|
@@ -213,29 +286,49 @@ export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
|
|
|
213
286
|
* What the nil of a number prop that admits `null` / `undefined` and no other
|
|
214
287
|
* family stands for (`NullableNumberPropNil`); `null` for any other type.
|
|
215
288
|
*/
|
|
216
|
-
function
|
|
289
|
+
function nullablePrimitivePropNil(
|
|
290
|
+
type: TypeInfo | undefined,
|
|
291
|
+
optional: boolean,
|
|
292
|
+
): { family: string; nil: NullableNumberPropNil } | null {
|
|
217
293
|
if (!type) return null
|
|
218
294
|
const members = type.kind === 'union' ? (type.unionTypes ?? []) : [type]
|
|
219
295
|
const families = new Set(members.map(m => (m.kind === 'primitive' ? m.primitive : m.kind)))
|
|
220
296
|
const admitsNull = families.has('null')
|
|
221
|
-
const
|
|
297
|
+
const admitsAbsent = optional || families.has('undefined')
|
|
222
298
|
families.delete('null')
|
|
223
299
|
families.delete('undefined')
|
|
224
|
-
if (!
|
|
225
|
-
|
|
300
|
+
if (!(admitsNull || admitsAbsent) || families.size !== 1) return null
|
|
301
|
+
const [family] = [...families] as [string]
|
|
302
|
+
if (admitsNull && admitsAbsent) return { family, nil: 'either' }
|
|
303
|
+
return { family, nil: admitsNull ? 'null' : 'undefined' }
|
|
226
304
|
}
|
|
227
305
|
|
|
228
|
-
|
|
229
|
-
|
|
306
|
+
/** Each no-default prop whose declared type `pick` accepts, keyed by the name a read uses. */
|
|
307
|
+
function collectNullablePrimitiveProps<T>(
|
|
308
|
+
ir: ComponentIR,
|
|
309
|
+
pick: (read: { family: string; nil: NullableNumberPropNil }) => T | null,
|
|
310
|
+
): ReadonlyMap<string, T> {
|
|
311
|
+
const names = new Map<string, T>()
|
|
230
312
|
const objectName = ir.metadata.propsObjectName
|
|
231
313
|
for (const p of ir.metadata.propsParams) {
|
|
232
314
|
if (p.isRest || p.defaultValue !== undefined) continue
|
|
233
|
-
const
|
|
234
|
-
|
|
315
|
+
const read = nullablePrimitivePropNil(p.declaredType ?? p.type, p.optional)
|
|
316
|
+
const picked = read === null ? null : pick(read)
|
|
317
|
+
if (picked !== null) names.set(objectName ? `${objectName}.${p.sourceName ?? p.name}` : p.name, picked)
|
|
235
318
|
}
|
|
236
319
|
return names
|
|
237
320
|
}
|
|
238
321
|
|
|
322
|
+
function collectNullableNumberProps(ir: ComponentIR): ReadonlyMap<string, NullableNumberPropNil> {
|
|
323
|
+
return collectNullablePrimitiveProps(ir, read => (read.family === 'number' ? read.nil : null))
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
function collectNullableScalarProps(ir: ComponentIR): ReadonlyMap<string, NullableScalarProp> {
|
|
327
|
+
return collectNullablePrimitiveProps(ir, ({ family, nil }) =>
|
|
328
|
+
family === 'string' || family === 'boolean' ? { family, nil } : null,
|
|
329
|
+
)
|
|
330
|
+
}
|
|
331
|
+
|
|
239
332
|
/**
|
|
240
333
|
* Whether an attribute value's SSR value can be `undefined` / `null`, so a
|
|
241
334
|
* DSL adapter must guard the attribute on a nil check to omit it the way
|
|
@@ -244,11 +337,14 @@ function collectNullableNumberProps(ir: ComponentIR): ReadonlyMap<string, Nullab
|
|
|
244
337
|
* nullish value through:
|
|
245
338
|
*
|
|
246
339
|
* - a read of a nullable signal or of a memo whose type or body can be
|
|
247
|
-
* nullish (`label()` with `label = createMemo(() => s())`)
|
|
340
|
+
* nullish (`label()` with `label = createMemo(() => s())`), or of a prop
|
|
341
|
+
* that can be absent (`props.on && 'y'`);
|
|
248
342
|
* - an optional member read (`user()?.name`), an index or computed-key read
|
|
249
343
|
* (`pair[1]`, `row[key]`), or a member read of a value that can itself be
|
|
250
344
|
* nullish;
|
|
251
345
|
* - a ternary branch, or an `??` / `||` / `&&` operand the result can be;
|
|
346
|
+
* - a method call that reads `undefined` when nothing is there
|
|
347
|
+
* (`word.at(-1)`, `items.find(...)`, {@link methodMayReturnUndefined});
|
|
252
348
|
* - a literal `undefined` / `null`.
|
|
253
349
|
*
|
|
254
350
|
* Like the bare-signal guard it is presence-only: a non-nil value renders
|
|
@@ -267,6 +363,7 @@ export function attrValueMayBeNullish(
|
|
|
267
363
|
// loop bindings at the top level, none inside a memo body — its getters
|
|
268
364
|
// resolve where the memo is declared, not where it is read.
|
|
269
365
|
const walk = (expr: ParsedExpr, shadowed: (name: string) => boolean): boolean => {
|
|
366
|
+
if (readsUndefinedLeaf(expr, ctx, shadowed)) return true
|
|
270
367
|
switch (expr.kind) {
|
|
271
368
|
case 'identifier':
|
|
272
369
|
return expr.name === 'undefined'
|
|
@@ -303,6 +400,40 @@ export function attrValueMayBeNullish(
|
|
|
303
400
|
return parsed !== undefined && walk(parsed, isShadowed)
|
|
304
401
|
}
|
|
305
402
|
|
|
403
|
+
/** A method call that can miss (`methodMayReturnUndefined`), or a read of a prop that can be absent. */
|
|
404
|
+
function readsUndefinedLeaf(expr: ParsedExpr, ctx: NullishAttrContext, shadowed: (name: string) => boolean): boolean {
|
|
405
|
+
return methodMayReturnUndefined(expr) || readsNullableProp(expr, ctx, shadowed)
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* A read of a prop that can be absent (`NullishAttrContext.nullableProps`):
|
|
410
|
+
* its binding (`on`) or `props.<key>`, unless a binding shadows the name.
|
|
411
|
+
*/
|
|
412
|
+
function readsNullableProp(expr: ParsedExpr, ctx: NullishAttrContext, shadowed: (name: string) => boolean): boolean {
|
|
413
|
+
if (!ctx.nullableProps?.size) return false
|
|
414
|
+
if (expr.kind === 'identifier') return !shadowed(expr.name) && ctx.nullableProps.has(expr.name)
|
|
415
|
+
if (expr.kind !== 'member' || expr.computed || expr.object.kind !== 'identifier' || shadowed(expr.object.name)) return false
|
|
416
|
+
return ctx.nullableProps.has(`${expr.object.name}.${expr.property}`)
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** Array / string methods that read `undefined` when nothing matches. */
|
|
420
|
+
const UNDEFINED_ON_MISS_METHODS: ReadonlySet<string> = new Set(['find', 'findLast'])
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Whether `expr` is a method call that reads `undefined` for some receiver:
|
|
424
|
+
* `.at(i)` out of range (an empty string or array included, `''.at(-1)`),
|
|
425
|
+
* or `.find()` / `.findLast()` with no matching element.
|
|
426
|
+
*/
|
|
427
|
+
function methodMayReturnUndefined(expr: ParsedExpr): boolean {
|
|
428
|
+
if (expr.kind === 'array-method') return expr.method === 'at'
|
|
429
|
+
return (
|
|
430
|
+
expr.kind === 'call' &&
|
|
431
|
+
expr.callee.kind === 'member' &&
|
|
432
|
+
!expr.callee.computed &&
|
|
433
|
+
UNDEFINED_ON_MISS_METHODS.has(expr.callee.property)
|
|
434
|
+
)
|
|
435
|
+
}
|
|
436
|
+
|
|
306
437
|
/**
|
|
307
438
|
* Whether an arithmetic operand's SSR value is certainly `null` — a literal
|
|
308
439
|
* `null`, a read of a signal seeded with `null`, or a memo whose body is
|
|
@@ -317,7 +448,8 @@ export function arithmeticOperandIsNull(
|
|
|
317
448
|
ctx: NullishAttrContext,
|
|
318
449
|
isShadowed: (name: string) => boolean = () => false,
|
|
319
450
|
): boolean {
|
|
320
|
-
|
|
451
|
+
const seed = arithmeticOperandSeed(parsed, ctx, isShadowed)
|
|
452
|
+
return seed === 'null' || seed === 'either'
|
|
321
453
|
}
|
|
322
454
|
|
|
323
455
|
/**
|
|
@@ -339,9 +471,11 @@ export function arithmeticOperandIsUndefined(
|
|
|
339
471
|
* What an arithmetic operand's SSR nil stands for: `'null'` / `'undefined'`
|
|
340
472
|
* when the operand is that value (a nullable number prop's `null` reading
|
|
341
473
|
* included), `'absent'` when it is a number prop that may be absent
|
|
342
|
-
* (`n?: number`), whose nil is `undefined` but which may hold a number
|
|
474
|
+
* (`n?: number`), whose nil is `undefined` but which may hold a number,
|
|
475
|
+
* `'either'` when it is a number prop whose nil may be either
|
|
476
|
+
* (`n?: number | null`).
|
|
343
477
|
*/
|
|
344
|
-
type OperandSeed = 'null' | 'undefined' | 'absent' | null
|
|
478
|
+
type OperandSeed = 'null' | 'undefined' | 'absent' | 'either' | null
|
|
345
479
|
|
|
346
480
|
function arithmeticOperandSeed(
|
|
347
481
|
parsed: ParsedExpr | undefined,
|
|
@@ -353,7 +487,7 @@ function arithmeticOperandSeed(
|
|
|
353
487
|
if (expr.kind === 'literal') return expr.literalType === 'null' ? 'null' : null
|
|
354
488
|
if (expr.kind === 'identifier' && expr.name === 'undefined' && !shadowed('undefined')) return 'undefined'
|
|
355
489
|
const prop = nullableNumberPropRead(expr, ctx, shadowed)
|
|
356
|
-
if (prop !== null) return prop === 'undefined' ? 'absent' :
|
|
490
|
+
if (prop !== null) return prop === 'undefined' ? 'absent' : prop
|
|
357
491
|
const getter = expr.kind === 'identifier' ? expr.name : bareGetterCallName(expr)
|
|
358
492
|
if (getter === null || shadowed(getter)) return null
|
|
359
493
|
if (ctx.nullSeededSignals?.has(getter)) return 'null'
|
|
@@ -377,14 +511,50 @@ function nullableNumberPropRead(
|
|
|
377
511
|
ctx: NullishAttrContext,
|
|
378
512
|
shadowed: (name: string) => boolean,
|
|
379
513
|
): NullableNumberPropNil | null {
|
|
380
|
-
|
|
381
|
-
|
|
514
|
+
return propReadIn(ctx.nullableNumberProps, expr, shadowed)
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** The entry of `props` (keyed as {@link NullishAttrContext.nullableNumberProps}) that `expr` reads, or `null`. */
|
|
518
|
+
function propReadIn<T>(
|
|
519
|
+
props: ReadonlyMap<string, T> | undefined,
|
|
520
|
+
expr: ParsedExpr,
|
|
521
|
+
shadowed: (name: string) => boolean,
|
|
522
|
+
): T | null {
|
|
523
|
+
if (!props?.size) return null
|
|
524
|
+
if (expr.kind === 'identifier') return shadowed(expr.name) ? null : (props.get(expr.name) ?? null)
|
|
382
525
|
if (expr.kind === 'member' && !expr.computed && expr.object.kind === 'identifier' && !shadowed(expr.object.name)) {
|
|
383
|
-
return
|
|
526
|
+
return props.get(`${expr.object.name}.${expr.property}`) ?? null
|
|
384
527
|
}
|
|
385
528
|
return null
|
|
386
529
|
}
|
|
387
530
|
|
|
531
|
+
/**
|
|
532
|
+
* The string or boolean prop (`NullishAttrContext.nullableScalarProps`) a
|
|
533
|
+
* relational operand reads, directly or through a memo whose body is such a
|
|
534
|
+
* read, or `null`.
|
|
535
|
+
*/
|
|
536
|
+
function scalarPropRead(
|
|
537
|
+
parsed: ParsedExpr | undefined,
|
|
538
|
+
ctx: NullishAttrContext,
|
|
539
|
+
isShadowed: (name: string) => boolean,
|
|
540
|
+
): NullableScalarProp | null {
|
|
541
|
+
if (!ctx.nullableScalarProps?.size || parsed === undefined) return null
|
|
542
|
+
const visiting = new Set<string>()
|
|
543
|
+
const walk = (expr: ParsedExpr, shadowed: (name: string) => boolean): NullableScalarProp | null => {
|
|
544
|
+
const prop = propReadIn(ctx.nullableScalarProps, expr, shadowed)
|
|
545
|
+
if (prop !== null) return prop
|
|
546
|
+
const getter = expr.kind === 'identifier' ? expr.name : bareGetterCallName(expr)
|
|
547
|
+
if (getter === null || shadowed(getter)) return null
|
|
548
|
+
const memo = ctx.memos.get(getter)
|
|
549
|
+
if (!memo?.parsed || visiting.has(getter)) return null
|
|
550
|
+
visiting.add(getter)
|
|
551
|
+
const result = walk(memo.parsed, () => false)
|
|
552
|
+
visiting.delete(getter)
|
|
553
|
+
return result
|
|
554
|
+
}
|
|
555
|
+
return walk(parsed, isShadowed)
|
|
556
|
+
}
|
|
557
|
+
|
|
388
558
|
/**
|
|
389
559
|
* Arithmetic operators JS applies `ToNumber` to, so a `null` operand reads
|
|
390
560
|
* as `0` (`null * 2` is `0`). A template engine's native operator rejects a
|
|
@@ -397,8 +567,15 @@ export const NULL_COERCING_ARITHMETIC_OPS: ReadonlySet<string> = new Set(['-', '
|
|
|
397
567
|
/** How a DSL adapter coerces an arithmetic operand (#3474): `'null'` reads a
|
|
398
568
|
* nil as `0` (JS `ToNumber(null)`), `'undefined'` reads it as `NaN`
|
|
399
569
|
* (`ToNumber(undefined)`), `null` leaves the operand as is. Either coercion
|
|
400
|
-
* leaves a non-nil operand's value as it is
|
|
401
|
-
|
|
570
|
+
* leaves a non-nil operand's value as it is: the operand is a number, so the
|
|
571
|
+
* runtime's JS `Number()` may spell `'undefined'`.
|
|
572
|
+
*
|
|
573
|
+
* `'undefined-keep'` (#3824 step 3) also reads a nil as `NaN`, but the
|
|
574
|
+
* operand is a string or boolean relational operand, which must reach the
|
|
575
|
+
* relational helper unconverted (`'a' < 'b'` compares strings), so the
|
|
576
|
+
* adapter spells it as a nil test: `NaN` when nil, the operand otherwise.
|
|
577
|
+
* `NaN` makes the comparison false, as `undefined < x` is in JS for any `x`. */
|
|
578
|
+
export type ArithmeticCoercion = 'null' | 'undefined' | 'undefined-keep' | null
|
|
402
579
|
|
|
403
580
|
/**
|
|
404
581
|
* The one decision behind every DSL adapter's arithmetic over a nullish
|
|
@@ -408,9 +585,10 @@ export type ArithmeticCoercion = 'null' | 'undefined' | null
|
|
|
408
585
|
* (`NullableNumberPropNil`, #3533), is coerced under the operators JS
|
|
409
586
|
* applies `ToNumber` to ({@link NULL_COERCING_ARITHMETIC_OPS}), and under
|
|
410
587
|
* `+` when the other operand is a number literal, where `+` cannot
|
|
411
|
-
* concatenate. Unary minus
|
|
412
|
-
* `other`. Each adapter only spells the
|
|
413
|
-
* reads nil as `0` spells `'null'` as the
|
|
588
|
+
* concatenate. Unary minus and unary plus apply `ToNumber` too: each passes
|
|
589
|
+
* its operator (`'-'` / `'+'`) with no `other`. Each adapter only spells the
|
|
590
|
+
* coercion; one whose engine already reads nil as `0` spells `'null'` as the
|
|
591
|
+
* operand itself.
|
|
414
592
|
*/
|
|
415
593
|
export function arithmeticOperandCoercion(
|
|
416
594
|
op: string,
|
|
@@ -419,10 +597,170 @@ export function arithmeticOperandCoercion(
|
|
|
419
597
|
ctx: NullishAttrContext,
|
|
420
598
|
isShadowed: (name: string) => boolean = () => false,
|
|
421
599
|
): ArithmeticCoercion {
|
|
422
|
-
|
|
423
|
-
if (!
|
|
600
|
+
if (RELATIONAL_OPS.has(op)) return relationalOperandCoercion(operand, ctx, isShadowed)
|
|
601
|
+
if (!isNumericArithmetic(op, other)) return null
|
|
424
602
|
const seed = arithmeticOperandSeed(operand, ctx, isShadowed)
|
|
425
|
-
|
|
603
|
+
if (seed === 'absent') return 'undefined'
|
|
604
|
+
// `'either'` is refused (`arithmeticOperandNilIsAmbiguous`); the spelling
|
|
605
|
+
// keeps the `null` reading so the refused template stays well-formed.
|
|
606
|
+
return seed === 'either' ? 'null' : seed
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
/** The relational operators, whose operands JS converts with ToNumber unless both are strings. */
|
|
610
|
+
const RELATIONAL_OPS: ReadonlySet<string> = new Set(['<', '<=', '>', '>='])
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* The coercion of a relational operand (#3824 step 3). The runtime's
|
|
614
|
+
* relational helpers read a nil operand as JS `null` (`0`), so only an operand
|
|
615
|
+
* whose nil reads as `undefined` needs spelling: it goes through `NaN`, which
|
|
616
|
+
* makes the comparison false, as `undefined < 1` is in JS. A nil whose
|
|
617
|
+
* reading the type leaves open (`n?: number | null`) is refused as it is in
|
|
618
|
+
* arithmetic (`arithmeticOperandNilIsAmbiguous`, #3700); the spelling keeps
|
|
619
|
+
* the helper's `null` reading so the refused template stays well-formed.
|
|
620
|
+
*/
|
|
621
|
+
function relationalOperandCoercion(
|
|
622
|
+
operand: ParsedExpr | undefined,
|
|
623
|
+
ctx: NullishAttrContext,
|
|
624
|
+
isShadowed: (name: string) => boolean,
|
|
625
|
+
): ArithmeticCoercion {
|
|
626
|
+
const seed = arithmeticOperandSeed(operand, ctx, isShadowed)
|
|
627
|
+
if (seed === 'absent' || seed === 'undefined') return 'undefined'
|
|
628
|
+
if (seed !== null) return null
|
|
629
|
+
// A string or boolean prop that may only be absent (`s?: string`): its nil
|
|
630
|
+
// is `undefined` too, but a present value must reach the helper as it is.
|
|
631
|
+
return scalarPropRead(operand, ctx, isShadowed)?.nil === 'undefined' ? 'undefined-keep' : null
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* Whether an arithmetic operand reads a number prop whose type admits both
|
|
636
|
+
* `null` and absence (`n?: number | null`), directly or through a memo
|
|
637
|
+
* (#3700). JS reads the omitted prop as `undefined` (`NaN` in arithmetic)
|
|
638
|
+
* and a `null` one as `0`; a template engine holds both as one nil and the
|
|
639
|
+
* type cannot say which, so no spelling of {@link arithmeticOperandCoercion}
|
|
640
|
+
* renders both. Template adapters refuse such an operand instead of
|
|
641
|
+
* rendering one reading silently. Same operators and scope as
|
|
642
|
+
* {@link arithmeticOperandCoercion}, relational operators included: they
|
|
643
|
+
* apply ToNumber too (`undefined < 1` is false, `null < 1` is true). Under a
|
|
644
|
+
* relational operator a string or boolean prop admitting both
|
|
645
|
+
* (`s?: string | null`) is ambiguous for the same reason
|
|
646
|
+
* ({@link ambiguousNilFamily}).
|
|
647
|
+
*/
|
|
648
|
+
export function arithmeticOperandNilIsAmbiguous(
|
|
649
|
+
op: string,
|
|
650
|
+
operand: ParsedExpr | undefined,
|
|
651
|
+
other: ParsedExpr | undefined,
|
|
652
|
+
ctx: NullishAttrContext,
|
|
653
|
+
isShadowed: (name: string) => boolean = () => false,
|
|
654
|
+
): boolean {
|
|
655
|
+
return ambiguousNilFamily(op, operand, other, ctx, isShadowed) !== null
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/** The declared family of a prop whose nil an operator cannot read ({@link ambiguousNilFamily}). */
|
|
659
|
+
export type AmbiguousNilFamily = 'number' | 'string' | 'boolean'
|
|
660
|
+
|
|
661
|
+
/**
|
|
662
|
+
* The declared family of the prop behind an operand that
|
|
663
|
+
* {@link arithmeticOperandNilIsAmbiguous} matches, or `null` when it does
|
|
664
|
+
* not match: `'number'` for a numeric `either` operand of arithmetic or a
|
|
665
|
+
* comparison, `'string'` / `'boolean'` for a string or boolean `either`
|
|
666
|
+
* operand of a comparison (#3824 step 3).
|
|
667
|
+
*/
|
|
668
|
+
export function ambiguousNilFamily(
|
|
669
|
+
op: string,
|
|
670
|
+
operand: ParsedExpr | undefined,
|
|
671
|
+
other: ParsedExpr | undefined,
|
|
672
|
+
ctx: NullishAttrContext,
|
|
673
|
+
isShadowed: (name: string) => boolean = () => false,
|
|
674
|
+
): AmbiguousNilFamily | null {
|
|
675
|
+
const relational = RELATIONAL_OPS.has(op)
|
|
676
|
+
if (!relational && !isNumericArithmetic(op, other)) return null
|
|
677
|
+
if (arithmeticOperandSeed(operand, ctx, isShadowed) === 'either') return 'number'
|
|
678
|
+
if (!relational) return null
|
|
679
|
+
const scalar = scalarPropRead(operand, ctx, isShadowed)
|
|
680
|
+
return scalar?.nil === 'either' ? scalar.family : null
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* Every arithmetic operand in `expr` that {@link arithmeticOperandNilIsAmbiguous}
|
|
685
|
+
* matches, for a whole expression a template adapter lowers without visiting
|
|
686
|
+
* each operator through its emitter (a memo body, a `.filter()` predicate, an
|
|
687
|
+
* expression the runtime evaluator runs). Operands of binary operators and of
|
|
688
|
+
* unary minus and plus count. `scope` is where the expression is read; each arrow in
|
|
689
|
+
* it enters its params as a callback frame (`BindingScope.enterCallback`),
|
|
690
|
+
* so they shadow within its body only.
|
|
691
|
+
*/
|
|
692
|
+
export function ambiguousNilArithmeticOperands(
|
|
693
|
+
expr: ParsedExpr,
|
|
694
|
+
ctx: NullishAttrContext,
|
|
695
|
+
scope: BindingScope = BindingScope.EMPTY,
|
|
696
|
+
): ParsedExpr[] {
|
|
697
|
+
return ambiguousNilOperands(expr, ctx, scope).map(found => found.operand)
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
/** {@link ambiguousNilArithmeticOperands}, each operand with its prop's family ({@link ambiguousNilFamily}). */
|
|
701
|
+
export function ambiguousNilOperands(
|
|
702
|
+
expr: ParsedExpr,
|
|
703
|
+
ctx: NullishAttrContext,
|
|
704
|
+
scope: BindingScope = BindingScope.EMPTY,
|
|
705
|
+
): Array<{ operand: ParsedExpr; family: AmbiguousNilFamily }> {
|
|
706
|
+
const found: Array<{ operand: ParsedExpr; family: AmbiguousNilFamily }> = []
|
|
707
|
+
const visit = (e: ParsedExpr, at: BindingScope): void => {
|
|
708
|
+
if (e.kind === 'arrow') {
|
|
709
|
+
visit(e.body, at.enterCallback(e.params))
|
|
710
|
+
return
|
|
711
|
+
}
|
|
712
|
+
for (const [op, operand, other] of arithmeticOperandsOf(e)) {
|
|
713
|
+
const family = ambiguousNilFamily(op, operand, other, ctx, at.asShadowPredicate())
|
|
714
|
+
if (family !== null) found.push({ operand, family })
|
|
715
|
+
}
|
|
716
|
+
for (const child of parsedExprChildren(e)) visit(child, at)
|
|
717
|
+
}
|
|
718
|
+
if (!ctx.nullableNumberProps?.size && !ctx.nullableScalarProps?.size) return found
|
|
719
|
+
visit(expr, scope)
|
|
720
|
+
return found
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
/** The `[op, operand, other]` triples of an arithmetic node's operands. A
|
|
724
|
+
* unary minus or plus applies ToNumber to its operand and has no `other`. */
|
|
725
|
+
function arithmeticOperandsOf(e: ParsedExpr): Array<[string, ParsedExpr, ParsedExpr | undefined]> {
|
|
726
|
+
if (e.kind === 'binary') return [[e.op, e.left, e.right], [e.op, e.right, e.left]]
|
|
727
|
+
if (e.kind === 'unary' && (e.op === '-' || e.op === '+')) return [[e.op, e.argument, undefined]]
|
|
728
|
+
return []
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/** The value-position sub-expressions of `e` (an arrow's body included). */
|
|
732
|
+
function parsedExprChildren(e: ParsedExpr): ParsedExpr[] {
|
|
733
|
+
switch (e.kind) {
|
|
734
|
+
case 'call':
|
|
735
|
+
return [e.callee, ...e.args]
|
|
736
|
+
case 'member':
|
|
737
|
+
return [e.object]
|
|
738
|
+
case 'index-access':
|
|
739
|
+
return [e.object, e.index]
|
|
740
|
+
case 'binary':
|
|
741
|
+
case 'logical':
|
|
742
|
+
return [e.left, e.right]
|
|
743
|
+
case 'unary':
|
|
744
|
+
return [e.argument]
|
|
745
|
+
case 'conditional':
|
|
746
|
+
return [e.test, e.consequent, e.alternate]
|
|
747
|
+
case 'template-literal':
|
|
748
|
+
return e.parts.flatMap(p => (p.type === 'expression' ? [p.expr] : []))
|
|
749
|
+
case 'array-literal':
|
|
750
|
+
return e.elements
|
|
751
|
+
case 'array-method':
|
|
752
|
+
return [e.object, ...e.args, ...(e.method === 'flat' && e.depthExpr ? [e.depthExpr] : [])]
|
|
753
|
+
case 'object-literal':
|
|
754
|
+
return e.properties.map(p => (p.kind === 'spread' ? p.expr : p.value))
|
|
755
|
+
default:
|
|
756
|
+
return []
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/** The operators {@link arithmeticOperandCoercion} applies `ToNumber` under:
|
|
761
|
+
* a `+` with no `other` is unary plus, which always does. */
|
|
762
|
+
function isNumericArithmetic(op: string, other: ParsedExpr | undefined): boolean {
|
|
763
|
+
return NULL_COERCING_ARITHMETIC_OPS.has(op) || (op === '+' && (other === undefined || isNumberLiteral(other)))
|
|
426
764
|
}
|
|
427
765
|
|
|
428
766
|
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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import ts from 'typescript'
|
|
2
|
-
import type { ConstantInfo, ParamInfo, PropUsage, SignalPropSeed } from './types.ts'
|
|
2
|
+
import type { ConstantInfo, FunctionInfo, MemoInfo, ParamInfo, PropUsage, SignalInfo, SignalPropSeed } from './types.ts'
|
|
3
3
|
import type { ParsedExpr } from './expression-parser.ts'
|
|
4
4
|
import { propHasPropertyAccess } from './ir-to-client-js/compute-prop-usage.ts'
|
|
5
5
|
import { parsePropReadInitializer } from './ir-to-client-js/prune-unused-prop-extractions.ts'
|
|
@@ -115,8 +115,8 @@ export function isPropsAliasConst(
|
|
|
115
115
|
* binding into a live prop read: the init body's alias rewrite
|
|
116
116
|
* (`resolveBodyPropAliases`) and parameter rewrite
|
|
117
117
|
* (`rewriteDestructuredPropReads`), the reactive attribute rewrite
|
|
118
|
-
* (`rewriteDestructuredPropsInExpr`), and the template's prop
|
|
119
|
-
* (`
|
|
118
|
+
* (`rewriteDestructuredPropsInExpr`), and the template's prop read set
|
|
119
|
+
* (`templatePropReadParams`, #3579).
|
|
120
120
|
*/
|
|
121
121
|
export function keepsDeclaredValue(
|
|
122
122
|
b: { declarationKind?: ConstantInfo['declarationKind']; mutatedAfterDeclaration?: boolean; reassigned?: boolean },
|
|
@@ -167,6 +167,57 @@ export function isRenamedBodyPropParam(
|
|
|
167
167
|
return propsParams.some(p => !p.isRest && p.name === c.name && p.sourceName === key)
|
|
168
168
|
}
|
|
169
169
|
|
|
170
|
+
/** The component bindings {@link templatePropReadParams} reads. */
|
|
171
|
+
export interface TemplatePropReadBindings {
|
|
172
|
+
propsObjectName: string | null
|
|
173
|
+
propsParams: readonly ParamInfo[]
|
|
174
|
+
signals: readonly Pick<SignalInfo, 'getter' | 'setter'>[]
|
|
175
|
+
memos: readonly Pick<MemoInfo, 'name'>[]
|
|
176
|
+
localFunctions: readonly Pick<FunctionInfo, 'name'>[]
|
|
177
|
+
localConstants: readonly ConstantInfo[]
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The props bindings a client `template:` reads as their live prop
|
|
182
|
+
* (`livePropReadExpr`, default included). The one set the Phase-1 markup
|
|
183
|
+
* rewrite and a signal seed's or memo body's template text share, so a
|
|
184
|
+
* binding read through a memo takes the same default as one read in markup.
|
|
185
|
+
*
|
|
186
|
+
* In a destructured-parameter component that is every parameter. In a
|
|
187
|
+
* `(props)`-arg component a bare name is a prop only where the body binds it
|
|
188
|
+
* as one, so the set is exactly the body bindings that hold their prop
|
|
189
|
+
* ({@link holdsProp}) and that no other component binding (a signal, memo,
|
|
190
|
+
* local function or other local) shares a name with. A typed prop the body
|
|
191
|
+
* does not bind is not in scope as a bare name: a bare `value` there is a
|
|
192
|
+
* local function, a module binding or a global, never `_p.value`.
|
|
193
|
+
*/
|
|
194
|
+
export function templatePropReadParams(b: TemplatePropReadBindings): ParamInfo[] {
|
|
195
|
+
if (!b.propsObjectName) return [...b.propsParams]
|
|
196
|
+
const held = new Set<string>()
|
|
197
|
+
const other = new Set<string>()
|
|
198
|
+
for (const s of b.signals) {
|
|
199
|
+
other.add(s.getter)
|
|
200
|
+
if (s.setter) other.add(s.setter)
|
|
201
|
+
}
|
|
202
|
+
for (const m of b.memos) other.add(m.name)
|
|
203
|
+
for (const f of b.localFunctions) other.add(f.name)
|
|
204
|
+
for (const c of b.localConstants) (holdsProp(c, b.propsObjectName, b.propsParams) ? held : other).add(c.name)
|
|
205
|
+
return b.propsParams.filter(p => held.has(p.name) && !other.has(p.name))
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Whether a `(props)`-arg component's body binding is the prop itself: an
|
|
210
|
+
* alias (`const { b } = props`, `const b = props.b`) or a renamed body
|
|
211
|
+
* binding with a default, which reads as its own prop entry like the
|
|
212
|
+
* parameter form (#3579). A body destructure binding that is reassigned or
|
|
213
|
+
* mutated no longer holds its prop, renamed or not, so the template keeps it
|
|
214
|
+
* a local.
|
|
215
|
+
*/
|
|
216
|
+
function holdsProp(c: ConstantInfo, propsObjectName: string, propsParams: readonly ParamInfo[]): boolean {
|
|
217
|
+
if (c.propsDestructure && !keepsDeclaredValue(c)) return false
|
|
218
|
+
return isPropsAliasConst(c, propsObjectName) || isRenamedBodyPropParam(c, propsParams)
|
|
219
|
+
}
|
|
220
|
+
|
|
170
221
|
/**
|
|
171
222
|
* How a parsed signal initializer reads a prop: a member chain rooted at the
|
|
172
223
|
* props object (`props.<name>`, `props.row.count`, `props.items[0]`) or a
|