@barefootjs/jsx 0.40.1 → 0.40.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts +4 -3
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -1
- package/dist/adapters/boolean-result.d.ts +32 -10
- package/dist/adapters/boolean-result.d.ts.map +1 -1
- package/dist/adapters/brace-delimiter-text.d.ts +18 -0
- package/dist/adapters/brace-delimiter-text.d.ts.map +1 -0
- package/dist/adapters/callback-method.d.ts +38 -5
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +11 -1
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +21 -0
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/loop-bound-names.d.ts +9 -1
- package/dist/adapters/loop-bound-names.d.ts.map +1 -1
- package/dist/adapters/loop-row-type.d.ts +52 -0
- package/dist/adapters/loop-row-type.d.ts.map +1 -0
- package/dist/adapters/nil-reading.d.ts +68 -0
- package/dist/adapters/nil-reading.d.ts.map +1 -0
- package/dist/adapters/non-primitive-equality.d.ts +33 -0
- package/dist/adapters/non-primitive-equality.d.ts.map +1 -0
- package/dist/adapters/number-value.d.ts +5 -3
- package/dist/adapters/number-value.d.ts.map +1 -1
- package/dist/adapters/operand-typing.d.ts +126 -0
- package/dist/adapters/operand-typing.d.ts.map +1 -0
- package/dist/adapters/parsed-expr-emitter.d.ts +70 -12
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/adapters/primitive-plan.d.ts +291 -0
- package/dist/adapters/primitive-plan.d.ts.map +1 -0
- package/dist/analyzer-context.d.ts +4 -0
- package/dist/analyzer-context.d.ts.map +1 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +63 -8
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/compiler.d.ts.map +1 -1
- package/dist/event-handler-name.d.ts +12 -0
- package/dist/event-handler-name.d.ts.map +1 -1
- package/dist/expression-parser.d.ts +8 -0
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/index.d.ts +19 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4628 -2198
- package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
- package/dist/ir-to-client-js/child-props-object.d.ts +64 -0
- package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -0
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +4 -2
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/imports.d.ts +2 -2
- package/dist/ir-to-client-js/imports.d.ts.map +1 -1
- package/dist/ir-to-client-js/index.d.ts +2 -6
- package/dist/ir-to-client-js/index.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +30 -18
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +3 -2
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts +1 -0
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +68 -9
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/prop-rewrite.d.ts +7 -0
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-object-whole-read.d.ts +15 -0
- package/dist/props-object-whole-read.d.ts.map +1 -0
- package/dist/rich-type-refusal.d.ts.map +1 -1
- package/dist/scope/binding-scope.d.ts +85 -10
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/scope/lexical-scope-walk.d.ts +3 -2
- package/dist/scope/lexical-scope-walk.d.ts.map +1 -1
- package/dist/scope/root-prop-alias.d.ts +10 -7
- package/dist/scope/root-prop-alias.d.ts.map +1 -1
- package/dist/strip-types.d.ts +13 -0
- package/dist/strip-types.d.ts.map +1 -1
- package/dist/template-position-walk.d.ts +21 -3
- package/dist/template-position-walk.d.ts.map +1 -1
- package/dist/types.d.ts +90 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +3 -3
- package/src/__tests__/binding-scope.test.ts +89 -1
- package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -0
- package/src/__tests__/boolean-result.test.ts +22 -5
- package/src/__tests__/brace-delimiter-text.test.ts +26 -0
- package/src/__tests__/callback-method.test.ts +62 -2
- package/src/__tests__/child-components-in-map.test.ts +1 -1
- package/src/__tests__/computed-const.test.ts +103 -0
- package/src/__tests__/emit-to-string-call.test.ts +2 -1
- package/src/__tests__/hydration-payload-analysis.test.ts +166 -0
- package/src/__tests__/issue-2724-prop-alias-loop-not-static.test.ts +7 -7
- package/src/__tests__/issue-2754-rest-spread-needs-slot.test.ts +5 -2
- package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +1 -1
- package/src/__tests__/issue-2835-props-type-member-name-collision.test.ts +4 -4
- package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +1 -1
- package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +2 -1
- package/src/__tests__/issue-3713-props-spread-loop-row.test.ts +65 -0
- package/src/__tests__/lookup-literal-const.test.ts +9 -3
- package/src/__tests__/loop-index-reactivity.test.ts +2 -2
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +1 -1
- package/src/__tests__/loop-plan.test.ts +1 -1
- package/src/__tests__/loop-row-const-initializer-shadow.test.ts +146 -0
- package/src/__tests__/loop-row-type.test.ts +64 -0
- package/src/__tests__/lowering-value-position.test.ts +4 -0
- package/src/__tests__/module-const-destructure-default.test.ts +156 -0
- package/src/__tests__/nested-loop-index-param.test.ts +1 -1
- package/src/__tests__/nested-loop-reactive-attrs.test.ts +2 -2
- package/src/__tests__/nested-row-shadowing.test.ts +2 -2
- package/src/__tests__/nil-reading.test.ts +77 -0
- package/src/__tests__/nullable-signal-attr.test.ts +134 -0
- package/src/__tests__/primitive-plan.test.ts +416 -0
- package/src/__tests__/profile-loop-binding-ids.test.ts +2 -2
- package/src/__tests__/profile-nested-binding-ids.test.ts +2 -2
- package/src/__tests__/props-object-whole-read.test.ts +40 -0
- package/src/__tests__/reactive-attrs-in-map.test.ts +1 -1
- package/src/__tests__/reactive-factory-inlining.test.ts +4 -1
- package/src/__tests__/rest-binding-gaps.test.ts +192 -0
- package/src/__tests__/root-prop-alias.test.ts +53 -8
- package/src/__tests__/static-loop-csr-materialize.test.ts +2 -2
- package/src/__tests__/static-record-literal-array.test.ts +171 -0
- package/src/__tests__/string-concat-identifier.test.ts +40 -1
- package/src/adapters/ambiguous-nil-arithmetic.ts +22 -13
- package/src/adapters/boolean-result.ts +72 -66
- package/src/adapters/brace-delimiter-text.ts +25 -0
- package/src/adapters/callback-method.ts +151 -27
- package/src/adapters/computed-const.ts +238 -4
- package/src/adapters/dsl-template-adapter.ts +40 -2
- package/src/adapters/loop-bound-names.ts +9 -1
- package/src/adapters/loop-plan.ts +1 -1
- package/src/adapters/loop-row-type.ts +139 -0
- package/src/adapters/nil-reading.ts +297 -0
- package/src/adapters/non-primitive-equality.ts +92 -0
- package/src/adapters/number-value.ts +17 -4
- package/src/adapters/operand-typing.ts +522 -0
- package/src/adapters/parsed-expr-emitter.ts +154 -19
- package/src/adapters/primitive-plan.ts +696 -0
- package/src/analyzer-context.ts +6 -0
- package/src/analyzer.ts +505 -19
- package/src/augment-inherited-props.ts +108 -10
- package/src/compiler.ts +17 -0
- package/src/event-handler-name.ts +15 -0
- package/src/expression-parser.ts +70 -1
- package/src/index.ts +33 -7
- package/src/ir-to-client-js/build-references.ts +69 -0
- package/src/ir-to-client-js/child-props-object.ts +106 -0
- package/src/ir-to-client-js/collect-elements.ts +30 -55
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +6 -1
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +29 -28
- package/src/ir-to-client-js/control-flow/shared.ts +20 -22
- package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +5 -3
- package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +2 -2
- package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +1 -1
- package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +2 -2
- package/src/ir-to-client-js/control-flow/stringify/loop.ts +1 -1
- package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +3 -3
- package/src/ir-to-client-js/html-template.ts +25 -8
- package/src/ir-to-client-js/imports.ts +1 -1
- package/src/ir-to-client-js/index.ts +53 -11
- package/src/ir-to-client-js/plan/build-static-array-child-init.ts +31 -14
- package/src/ir-to-client-js/plan/static-array-child-init.ts +27 -18
- package/src/ir-to-client-js/prop-handling.ts +22 -3
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +26 -10
- package/src/jsx-to-ir.ts +446 -37
- package/src/nullable-signal-attr.ts +246 -29
- package/src/prop-rewrite.ts +30 -6
- package/src/props-object-whole-read.ts +87 -0
- package/src/rich-type-refusal.ts +2 -1
- package/src/scope/binding-scope.ts +182 -18
- package/src/scope/lexical-scope-walk.ts +4 -3
- package/src/scope/root-prop-alias.ts +15 -8
- package/src/strip-types.ts +31 -3
- package/src/template-position-walk.ts +57 -28
- package/src/types.ts +87 -0
|
@@ -16,8 +16,9 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import type { ParsedExpr } from './expression-parser.ts'
|
|
19
|
+
import { isBooleanResultParsed } from './adapters/boolean-result.ts'
|
|
19
20
|
import { BindingScope } from './scope/binding-scope.ts'
|
|
20
|
-
import type { ComponentIR, MemoInfo, SignalInfo, TypeInfo } from './types.ts'
|
|
21
|
+
import type { ComponentIR, IRMetadata, MemoInfo, SignalInfo, TypeInfo } from './types.ts'
|
|
21
22
|
|
|
22
23
|
function isNullishType(type: TypeInfo): boolean {
|
|
23
24
|
return type.kind === 'primitive' && (type.primitive === 'undefined' || type.primitive === 'null')
|
|
@@ -102,6 +103,57 @@ function collectNullablePropReads(ir: ComponentIR): ReadonlyMap<string, string>
|
|
|
102
103
|
return names
|
|
103
104
|
}
|
|
104
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
|
+
|
|
105
157
|
/**
|
|
106
158
|
* Add to `out` the params of `nullableProps` a seed's value can be read
|
|
107
159
|
* whole from: a bare read, a ternary branch, the right operand of `??` /
|
|
@@ -176,11 +228,27 @@ export interface NullishAttrContext {
|
|
|
176
228
|
* (`arithmeticOperandIsUndefined`). */
|
|
177
229
|
readonly undefinedSeededSignals?: ReadonlySet<string>
|
|
178
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>
|
|
179
235
|
/** Props declared as a number that may be `null` or absent (`n: number |
|
|
180
236
|
* null`, `n?: number`), by the name a read uses: the binding name, and
|
|
181
237
|
* `<propsObjectName>.<key>` for a member read (#3474), mapped to what the
|
|
182
238
|
* engine's nil stands for there (`NullableNumberPropNil`, #3533). */
|
|
183
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
|
|
184
252
|
}
|
|
185
253
|
|
|
186
254
|
/**
|
|
@@ -208,7 +276,9 @@ export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
|
|
|
208
276
|
signals.filter(s => s.parsed?.kind === 'identifier' && s.parsed.name === 'undefined').map(s => s.getter),
|
|
209
277
|
),
|
|
210
278
|
memos: new Map((ir.metadata.memos ?? []).map(m => [m.name, m])),
|
|
279
|
+
nullableProps: collectNullablePropReads(ir),
|
|
211
280
|
nullableNumberProps: collectNullableNumberProps(ir),
|
|
281
|
+
nullableScalarProps: collectNullableScalarProps(ir),
|
|
212
282
|
}
|
|
213
283
|
}
|
|
214
284
|
|
|
@@ -216,7 +286,10 @@ export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
|
|
|
216
286
|
* What the nil of a number prop that admits `null` / `undefined` and no other
|
|
217
287
|
* family stands for (`NullableNumberPropNil`); `null` for any other type.
|
|
218
288
|
*/
|
|
219
|
-
function
|
|
289
|
+
function nullablePrimitivePropNil(
|
|
290
|
+
type: TypeInfo | undefined,
|
|
291
|
+
optional: boolean,
|
|
292
|
+
): { family: string; nil: NullableNumberPropNil } | null {
|
|
220
293
|
if (!type) return null
|
|
221
294
|
const members = type.kind === 'union' ? (type.unionTypes ?? []) : [type]
|
|
222
295
|
const families = new Set(members.map(m => (m.kind === 'primitive' ? m.primitive : m.kind)))
|
|
@@ -224,22 +297,38 @@ function nullableNumberPropNil(type: TypeInfo | undefined, optional: boolean): N
|
|
|
224
297
|
const admitsAbsent = optional || families.has('undefined')
|
|
225
298
|
families.delete('null')
|
|
226
299
|
families.delete('undefined')
|
|
227
|
-
if (!(admitsNull || admitsAbsent) || families.size !== 1
|
|
228
|
-
|
|
229
|
-
|
|
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' }
|
|
230
304
|
}
|
|
231
305
|
|
|
232
|
-
|
|
233
|
-
|
|
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>()
|
|
234
312
|
const objectName = ir.metadata.propsObjectName
|
|
235
313
|
for (const p of ir.metadata.propsParams) {
|
|
236
314
|
if (p.isRest || p.defaultValue !== undefined) continue
|
|
237
|
-
const
|
|
238
|
-
|
|
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)
|
|
239
318
|
}
|
|
240
319
|
return names
|
|
241
320
|
}
|
|
242
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
|
+
|
|
243
332
|
/**
|
|
244
333
|
* Whether an attribute value's SSR value can be `undefined` / `null`, so a
|
|
245
334
|
* DSL adapter must guard the attribute on a nil check to omit it the way
|
|
@@ -248,7 +337,8 @@ function collectNullableNumberProps(ir: ComponentIR): ReadonlyMap<string, Nullab
|
|
|
248
337
|
* nullish value through:
|
|
249
338
|
*
|
|
250
339
|
* - a read of a nullable signal or of a memo whose type or body can be
|
|
251
|
-
* 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'`);
|
|
252
342
|
* - an optional member read (`user()?.name`), an index or computed-key read
|
|
253
343
|
* (`pair[1]`, `row[key]`), or a member read of a value that can itself be
|
|
254
344
|
* nullish;
|
|
@@ -273,7 +363,7 @@ export function attrValueMayBeNullish(
|
|
|
273
363
|
// loop bindings at the top level, none inside a memo body — its getters
|
|
274
364
|
// resolve where the memo is declared, not where it is read.
|
|
275
365
|
const walk = (expr: ParsedExpr, shadowed: (name: string) => boolean): boolean => {
|
|
276
|
-
if (
|
|
366
|
+
if (readsUndefinedLeaf(expr, ctx, shadowed)) return true
|
|
277
367
|
switch (expr.kind) {
|
|
278
368
|
case 'identifier':
|
|
279
369
|
return expr.name === 'undefined'
|
|
@@ -310,6 +400,22 @@ export function attrValueMayBeNullish(
|
|
|
310
400
|
return parsed !== undefined && walk(parsed, isShadowed)
|
|
311
401
|
}
|
|
312
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
|
+
|
|
313
419
|
/** Array / string methods that read `undefined` when nothing matches. */
|
|
314
420
|
const UNDEFINED_ON_MISS_METHODS: ReadonlySet<string> = new Set(['find', 'findLast'])
|
|
315
421
|
|
|
@@ -405,14 +511,50 @@ function nullableNumberPropRead(
|
|
|
405
511
|
ctx: NullishAttrContext,
|
|
406
512
|
shadowed: (name: string) => boolean,
|
|
407
513
|
): NullableNumberPropNil | null {
|
|
408
|
-
|
|
409
|
-
|
|
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)
|
|
410
525
|
if (expr.kind === 'member' && !expr.computed && expr.object.kind === 'identifier' && !shadowed(expr.object.name)) {
|
|
411
|
-
return
|
|
526
|
+
return props.get(`${expr.object.name}.${expr.property}`) ?? null
|
|
412
527
|
}
|
|
413
528
|
return null
|
|
414
529
|
}
|
|
415
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
|
+
|
|
416
558
|
/**
|
|
417
559
|
* Arithmetic operators JS applies `ToNumber` to, so a `null` operand reads
|
|
418
560
|
* as `0` (`null * 2` is `0`). A template engine's native operator rejects a
|
|
@@ -425,8 +567,15 @@ export const NULL_COERCING_ARITHMETIC_OPS: ReadonlySet<string> = new Set(['-', '
|
|
|
425
567
|
/** How a DSL adapter coerces an arithmetic operand (#3474): `'null'` reads a
|
|
426
568
|
* nil as `0` (JS `ToNumber(null)`), `'undefined'` reads it as `NaN`
|
|
427
569
|
* (`ToNumber(undefined)`), `null` leaves the operand as is. Either coercion
|
|
428
|
-
* leaves a non-nil operand's value as it is
|
|
429
|
-
|
|
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
|
|
430
579
|
|
|
431
580
|
/**
|
|
432
581
|
* The one decision behind every DSL adapter's arithmetic over a nullish
|
|
@@ -436,9 +585,10 @@ export type ArithmeticCoercion = 'null' | 'undefined' | null
|
|
|
436
585
|
* (`NullableNumberPropNil`, #3533), is coerced under the operators JS
|
|
437
586
|
* applies `ToNumber` to ({@link NULL_COERCING_ARITHMETIC_OPS}), and under
|
|
438
587
|
* `+` 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
|
|
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.
|
|
442
592
|
*/
|
|
443
593
|
export function arithmeticOperandCoercion(
|
|
444
594
|
op: string,
|
|
@@ -447,6 +597,7 @@ export function arithmeticOperandCoercion(
|
|
|
447
597
|
ctx: NullishAttrContext,
|
|
448
598
|
isShadowed: (name: string) => boolean = () => false,
|
|
449
599
|
): ArithmeticCoercion {
|
|
600
|
+
if (RELATIONAL_OPS.has(op)) return relationalOperandCoercion(operand, ctx, isShadowed)
|
|
450
601
|
if (!isNumericArithmetic(op, other)) return null
|
|
451
602
|
const seed = arithmeticOperandSeed(operand, ctx, isShadowed)
|
|
452
603
|
if (seed === 'absent') return 'undefined'
|
|
@@ -455,6 +606,31 @@ export function arithmeticOperandCoercion(
|
|
|
455
606
|
return seed === 'either' ? 'null' : seed
|
|
456
607
|
}
|
|
457
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
|
+
|
|
458
634
|
/**
|
|
459
635
|
* Whether an arithmetic operand reads a number prop whose type admits both
|
|
460
636
|
* `null` and absence (`n?: number | null`), directly or through a memo
|
|
@@ -463,7 +639,11 @@ export function arithmeticOperandCoercion(
|
|
|
463
639
|
* type cannot say which, so no spelling of {@link arithmeticOperandCoercion}
|
|
464
640
|
* renders both. Template adapters refuse such an operand instead of
|
|
465
641
|
* rendering one reading silently. Same operators and scope as
|
|
466
|
-
* {@link arithmeticOperandCoercion}
|
|
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}).
|
|
467
647
|
*/
|
|
468
648
|
export function arithmeticOperandNilIsAmbiguous(
|
|
469
649
|
op: string,
|
|
@@ -472,7 +652,32 @@ export function arithmeticOperandNilIsAmbiguous(
|
|
|
472
652
|
ctx: NullishAttrContext,
|
|
473
653
|
isShadowed: (name: string) => boolean = () => false,
|
|
474
654
|
): boolean {
|
|
475
|
-
return
|
|
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
|
|
476
681
|
}
|
|
477
682
|
|
|
478
683
|
/**
|
|
@@ -480,7 +685,7 @@ export function arithmeticOperandNilIsAmbiguous(
|
|
|
480
685
|
* matches, for a whole expression a template adapter lowers without visiting
|
|
481
686
|
* each operator through its emitter (a memo body, a `.filter()` predicate, an
|
|
482
687
|
* expression the runtime evaluator runs). Operands of binary operators and of
|
|
483
|
-
* unary minus count. `scope` is where the expression is read; each arrow in
|
|
688
|
+
* unary minus and plus count. `scope` is where the expression is read; each arrow in
|
|
484
689
|
* it enters its params as a callback frame (`BindingScope.enterCallback`),
|
|
485
690
|
* so they shadow within its body only.
|
|
486
691
|
*/
|
|
@@ -489,26 +694,37 @@ export function ambiguousNilArithmeticOperands(
|
|
|
489
694
|
ctx: NullishAttrContext,
|
|
490
695
|
scope: BindingScope = BindingScope.EMPTY,
|
|
491
696
|
): ParsedExpr[] {
|
|
492
|
-
|
|
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 }> = []
|
|
493
707
|
const visit = (e: ParsedExpr, at: BindingScope): void => {
|
|
494
708
|
if (e.kind === 'arrow') {
|
|
495
709
|
visit(e.body, at.enterCallback(e.params))
|
|
496
710
|
return
|
|
497
711
|
}
|
|
498
712
|
for (const [op, operand, other] of arithmeticOperandsOf(e)) {
|
|
499
|
-
|
|
713
|
+
const family = ambiguousNilFamily(op, operand, other, ctx, at.asShadowPredicate())
|
|
714
|
+
if (family !== null) found.push({ operand, family })
|
|
500
715
|
}
|
|
501
716
|
for (const child of parsedExprChildren(e)) visit(child, at)
|
|
502
717
|
}
|
|
503
|
-
if (!ctx.nullableNumberProps?.size) return found
|
|
718
|
+
if (!ctx.nullableNumberProps?.size && !ctx.nullableScalarProps?.size) return found
|
|
504
719
|
visit(expr, scope)
|
|
505
720
|
return found
|
|
506
721
|
}
|
|
507
722
|
|
|
508
|
-
/** The `[op, operand, other]` triples of an arithmetic node's operands.
|
|
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`. */
|
|
509
725
|
function arithmeticOperandsOf(e: ParsedExpr): Array<[string, ParsedExpr, ParsedExpr | undefined]> {
|
|
510
726
|
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 [[
|
|
727
|
+
if (e.kind === 'unary' && (e.op === '-' || e.op === '+')) return [[e.op, e.argument, undefined]]
|
|
512
728
|
return []
|
|
513
729
|
}
|
|
514
730
|
|
|
@@ -541,9 +757,10 @@ function parsedExprChildren(e: ParsedExpr): ParsedExpr[] {
|
|
|
541
757
|
}
|
|
542
758
|
}
|
|
543
759
|
|
|
544
|
-
/** The operators {@link arithmeticOperandCoercion} applies `ToNumber` under
|
|
760
|
+
/** The operators {@link arithmeticOperandCoercion} applies `ToNumber` under:
|
|
761
|
+
* a `+` with no `other` is unary plus, which always does. */
|
|
545
762
|
function isNumericArithmetic(op: string, other: ParsedExpr | undefined): boolean {
|
|
546
|
-
return NULL_COERCING_ARITHMETIC_OPS.has(op) || (op === '+' && isNumberLiteral(other))
|
|
763
|
+
return NULL_COERCING_ARITHMETIC_OPS.has(op) || (op === '+' && (other === undefined || isNumberLiteral(other)))
|
|
547
764
|
}
|
|
548
765
|
|
|
549
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
|
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a component observes its props object as a whole: any read of the
|
|
3
|
+
* props parameter other than a member read with a known key (`props.x`,
|
|
4
|
+
* `props['x']`). A whole read — `{...props}` on an element or a component,
|
|
5
|
+
* `Object.keys(props)`, `props[key]`, passing `props` to a function,
|
|
6
|
+
* `const { a, ...rest } = props` — can see a key the props type does not
|
|
7
|
+
* declare, so a caller's undeclared prop is not dead there.
|
|
8
|
+
*
|
|
9
|
+
* Syntactic, over the component's TS AST: a nested function, catch clause or
|
|
10
|
+
* block that rebinds the name shadows it, and a type position is not a read.
|
|
11
|
+
* A shadow this misses only makes the answer `true` more often.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import ts from 'typescript'
|
|
15
|
+
|
|
16
|
+
export function observesWholePropsObject(
|
|
17
|
+
fn: ts.FunctionDeclaration | ts.ArrowFunction | ts.FunctionExpression,
|
|
18
|
+
name: string,
|
|
19
|
+
): boolean {
|
|
20
|
+
if (!fn.body) return false
|
|
21
|
+
let found = false
|
|
22
|
+
const visit = (node: ts.Node): void => {
|
|
23
|
+
if (found || ts.isTypeNode(node) || rebinds(node, name)) return
|
|
24
|
+
if (ts.isIdentifier(node) && node.text === name && isWholeRead(node)) {
|
|
25
|
+
found = true
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
ts.forEachChild(node, visit)
|
|
29
|
+
}
|
|
30
|
+
visit(fn.body)
|
|
31
|
+
return found
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** A member read with a key known at compile time (`props.x`, `props['x']`). */
|
|
35
|
+
function isKnownKeyMemberRead(id: ts.Identifier): boolean {
|
|
36
|
+
const parent = id.parent
|
|
37
|
+
if (ts.isPropertyAccessExpression(parent)) return parent.expression === id
|
|
38
|
+
if (ts.isElementAccessExpression(parent) && parent.expression === id) {
|
|
39
|
+
return ts.isStringLiteralLike(parent.argumentExpression) || ts.isNumericLiteral(parent.argumentExpression)
|
|
40
|
+
}
|
|
41
|
+
return false
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** An identifier in a name position (a property name, a declaration, a JSX attribute name), not a read. */
|
|
45
|
+
function isNamePosition(id: ts.Identifier): boolean {
|
|
46
|
+
const parent = id.parent
|
|
47
|
+
if (ts.isPropertyAccessExpression(parent)) return parent.name === id
|
|
48
|
+
if (ts.isPropertyAssignment(parent) || ts.isJsxAttribute(parent)) return parent.name === id
|
|
49
|
+
if (ts.isBindingElement(parent)) return parent.name === id || parent.propertyName === id
|
|
50
|
+
if (ts.isVariableDeclaration(parent) || ts.isParameter(parent)) return parent.name === id
|
|
51
|
+
if (ts.isFunctionDeclaration(parent) || ts.isMethodDeclaration(parent)) return parent.name === id
|
|
52
|
+
return false
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isWholeRead(id: ts.Identifier): boolean {
|
|
56
|
+
if (isNamePosition(id)) return false
|
|
57
|
+
if (isKnownKeyMemberRead(id)) return false
|
|
58
|
+
return !isRestFreeDestructure(id)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** `const { a, b } = props`: reads only the named keys. */
|
|
62
|
+
function isRestFreeDestructure(id: ts.Identifier): boolean {
|
|
63
|
+
const parent = id.parent
|
|
64
|
+
if (!ts.isVariableDeclaration(parent) || parent.initializer !== id) return false
|
|
65
|
+
const pattern = parent.name
|
|
66
|
+
return ts.isObjectBindingPattern(pattern) &&
|
|
67
|
+
pattern.elements.every(el => !el.dotDotDotToken && (!el.propertyName || !ts.isComputedPropertyName(el.propertyName)))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Whether `node` opens a scope that binds `name` again. */
|
|
71
|
+
function rebinds(node: ts.Node, name: string): boolean {
|
|
72
|
+
if (ts.isFunctionLike(node)) return node.parameters.some(p => bindsName(p.name, name))
|
|
73
|
+
if (ts.isCatchClause(node)) return !!node.variableDeclaration && bindsName(node.variableDeclaration.name, name)
|
|
74
|
+
if (ts.isBlock(node)) return node.statements.some(s => declaresInBlock(s, name))
|
|
75
|
+
return false
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function declaresInBlock(stmt: ts.Statement, name: string): boolean {
|
|
79
|
+
if (ts.isVariableStatement(stmt)) return stmt.declarationList.declarations.some(d => bindsName(d.name, name))
|
|
80
|
+
if (ts.isFunctionDeclaration(stmt) || ts.isClassDeclaration(stmt)) return stmt.name?.text === name
|
|
81
|
+
return false
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function bindsName(binding: ts.BindingName, name: string): boolean {
|
|
85
|
+
if (ts.isIdentifier(binding)) return binding.text === name
|
|
86
|
+
return binding.elements.some(el => !ts.isOmittedExpression(el) && bindsName(el.name, name))
|
|
87
|
+
}
|
package/src/rich-type-refusal.ts
CHANGED
|
@@ -65,6 +65,7 @@ import type { ParsedExpr } from './expression-parser.ts'
|
|
|
65
65
|
import { parseExpression } from './expression-parser.ts'
|
|
66
66
|
import { prepareLoweringMatchers, type LoweringMatcher } from './lowering-registry.ts'
|
|
67
67
|
import { ErrorCodes } from './errors.ts'
|
|
68
|
+
import { isHydrationPayloadPropName } from './event-handler-name.ts'
|
|
68
69
|
import {
|
|
69
70
|
resolveReceiverType,
|
|
70
71
|
baseTypeName,
|
|
@@ -119,7 +120,7 @@ export function checkRichTypePropSerialization(root: IRNode, metadata: IRMetadat
|
|
|
119
120
|
const usedProps = new Set(metadata.clientAnalysis.usedProps)
|
|
120
121
|
const loc = declLoc ?? root.loc
|
|
121
122
|
for (const param of metadata.propsParams) {
|
|
122
|
-
if (param.isRest ||
|
|
123
|
+
if (param.isRest || !isHydrationPayloadPropName(param.name)) continue
|
|
123
124
|
if (!usedProps.has(param.name)) continue
|
|
124
125
|
const declared = resolvePropDeclaredType(param.sourceName ?? param.name, metadata)
|
|
125
126
|
const typeName = jsonUnsafeTypeName(declared)
|