@barefootjs/jsx 0.39.5 → 0.39.6
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/array-method.d.ts +93 -0
- package/dist/adapters/array-method.d.ts.map +1 -0
- package/dist/adapters/attr-plan.d.ts +135 -0
- package/dist/adapters/attr-plan.d.ts.map +1 -0
- package/dist/adapters/boolean-result.d.ts +84 -9
- package/dist/adapters/boolean-result.d.ts.map +1 -1
- package/dist/adapters/callback-method.d.ts +126 -0
- package/dist/adapters/callback-method.d.ts.map +1 -0
- package/dist/adapters/component-props.d.ts +99 -0
- package/dist/adapters/component-props.d.ts.map +1 -0
- package/dist/adapters/component-tree.d.ts +25 -0
- package/dist/adapters/component-tree.d.ts.map +1 -0
- package/dist/adapters/literal-value.d.ts +22 -0
- package/dist/adapters/literal-value.d.ts.map +1 -0
- package/dist/adapters/loop-plan.d.ts +53 -0
- package/dist/adapters/loop-plan.d.ts.map +1 -0
- package/dist/adapters/prop-classes.d.ts +37 -0
- package/dist/adapters/prop-classes.d.ts.map +1 -0
- package/dist/expression-parser.d.ts +38 -0
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/index.d.ts +17 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1765 -914
- package/dist/jsx-to-ir.d.ts +465 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +19 -0
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +1 -1
- package/src/__tests__/action-accessor.test.ts +6 -24
- package/src/__tests__/array-method-catalogue.test.ts +99 -0
- package/src/__tests__/attr-plan.test.ts +121 -0
- package/src/__tests__/binding-scope-ratchet.test.ts +12 -12
- package/src/__tests__/boolean-result-expr.test.ts +106 -0
- package/src/__tests__/boolean-result.test.ts +34 -1
- package/src/__tests__/callback-method.test.ts +189 -0
- package/src/__tests__/component-props.test.ts +148 -0
- package/src/__tests__/delegated-handler-preamble.test.ts +4 -4
- package/src/__tests__/dsl-shared-helpers.test.ts +166 -0
- package/src/__tests__/issue-3240-nested-static-map-event-delegation.test.ts +1 -1
- package/src/__tests__/loop-plan.test.ts +104 -0
- package/src/__tests__/map-call-decisions.test.ts +521 -0
- package/src/__tests__/nullable-signal-attr.test.ts +45 -0
- package/src/adapters/array-method.ts +193 -0
- package/src/adapters/attr-plan.ts +200 -0
- package/src/adapters/boolean-result.ts +208 -12
- package/src/adapters/callback-method.ts +301 -0
- package/src/adapters/component-props.ts +144 -0
- package/src/adapters/component-tree.ts +118 -0
- package/src/adapters/literal-value.ts +36 -0
- package/src/adapters/loop-plan.ts +88 -0
- package/src/adapters/prop-classes.ts +73 -0
- package/src/expression-parser.ts +537 -374
- package/src/index.ts +53 -2
- package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +9 -7
- package/src/jsx-to-ir.ts +1328 -991
- package/src/nullable-signal-attr.ts +78 -0
|
@@ -89,6 +89,10 @@ export interface NullishAttrContext {
|
|
|
89
89
|
* (`arithmeticOperandIsUndefined`). */
|
|
90
90
|
readonly undefinedSeededSignals?: ReadonlySet<string>
|
|
91
91
|
readonly memos: ReadonlyMap<string, MemoInfo>
|
|
92
|
+
/** Props declared as a number that may be `null` or absent (`n: number |
|
|
93
|
+
* null`, `n?: number`), by the name a read uses: the binding name, and
|
|
94
|
+
* `<propsObjectName>.<key>` for a member read (#3474). */
|
|
95
|
+
readonly nullableNumberProps?: ReadonlySet<string>
|
|
92
96
|
}
|
|
93
97
|
|
|
94
98
|
export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
|
|
@@ -102,9 +106,31 @@ export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
|
|
|
102
106
|
signals.filter(s => s.parsed?.kind === 'identifier' && s.parsed.name === 'undefined').map(s => s.getter),
|
|
103
107
|
),
|
|
104
108
|
memos: new Map((ir.metadata.memos ?? []).map(m => [m.name, m])),
|
|
109
|
+
nullableNumberProps: collectNullableNumberProps(ir),
|
|
105
110
|
}
|
|
106
111
|
}
|
|
107
112
|
|
|
113
|
+
/** Whether `type` is a number that admits `null` / `undefined` and no other family. */
|
|
114
|
+
function isNullableNumberType(type: TypeInfo | undefined, optional: boolean): boolean {
|
|
115
|
+
if (!type) return false
|
|
116
|
+
const members = type.kind === 'union' ? (type.unionTypes ?? []) : [type]
|
|
117
|
+
const families = new Set(members.map(m => (m.kind === 'primitive' ? m.primitive : m.kind)))
|
|
118
|
+
const nullable = optional || families.has('null') || families.has('undefined')
|
|
119
|
+
families.delete('null')
|
|
120
|
+
families.delete('undefined')
|
|
121
|
+
return nullable && families.size === 1 && families.has('number')
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function collectNullableNumberProps(ir: ComponentIR): ReadonlySet<string> {
|
|
125
|
+
const names = new Set<string>()
|
|
126
|
+
const objectName = ir.metadata.propsObjectName
|
|
127
|
+
for (const p of ir.metadata.propsParams) {
|
|
128
|
+
if (p.isRest || p.defaultValue !== undefined || !isNullableNumberType(p.type, p.optional)) continue
|
|
129
|
+
names.add(objectName ? `${objectName}.${p.sourceName ?? p.name}` : p.name)
|
|
130
|
+
}
|
|
131
|
+
return names
|
|
132
|
+
}
|
|
133
|
+
|
|
108
134
|
/**
|
|
109
135
|
* Whether an attribute value's SSR value can be `undefined` / `null`, so a
|
|
110
136
|
* DSL adapter must guard the attribute on a nil check to omit it the way
|
|
@@ -210,6 +236,8 @@ function arithmeticOperandSeed(
|
|
|
210
236
|
const walk = (expr: ParsedExpr, shadowed: (name: string) => boolean): 'null' | 'undefined' | null => {
|
|
211
237
|
if (expr.kind === 'literal') return expr.literalType === 'null' ? 'null' : null
|
|
212
238
|
if (expr.kind === 'identifier' && expr.name === 'undefined' && !shadowed('undefined')) return 'undefined'
|
|
239
|
+
const prop = nullableNumberPropRead(expr, ctx, shadowed)
|
|
240
|
+
if (prop) return 'null'
|
|
213
241
|
const getter = expr.kind === 'identifier' ? expr.name : bareGetterCallName(expr)
|
|
214
242
|
if (getter === null || shadowed(getter)) return null
|
|
215
243
|
if (ctx.nullSeededSignals?.has(getter)) return 'null'
|
|
@@ -224,6 +252,25 @@ function arithmeticOperandSeed(
|
|
|
224
252
|
return parsed === undefined ? null : walk(parsed, isShadowed)
|
|
225
253
|
}
|
|
226
254
|
|
|
255
|
+
/**
|
|
256
|
+
* A read of a prop declared as a nullable number. Its SSR value may be
|
|
257
|
+
* `null` or absent; it is coerced like a `null` operand. JS reads an absent
|
|
258
|
+
* prop as `undefined` (`NaN` in arithmetic), which a template engine cannot
|
|
259
|
+
* tell from `null` (the `omitted-optional-number-arithmetic` limitation).
|
|
260
|
+
*/
|
|
261
|
+
function nullableNumberPropRead(
|
|
262
|
+
expr: ParsedExpr,
|
|
263
|
+
ctx: NullishAttrContext,
|
|
264
|
+
shadowed: (name: string) => boolean,
|
|
265
|
+
): boolean {
|
|
266
|
+
if (!ctx.nullableNumberProps?.size) return false
|
|
267
|
+
if (expr.kind === 'identifier') return !shadowed(expr.name) && ctx.nullableNumberProps.has(expr.name)
|
|
268
|
+
if (expr.kind === 'member' && !expr.computed && expr.object.kind === 'identifier' && !shadowed(expr.object.name)) {
|
|
269
|
+
return ctx.nullableNumberProps.has(`${expr.object.name}.${expr.property}`)
|
|
270
|
+
}
|
|
271
|
+
return false
|
|
272
|
+
}
|
|
273
|
+
|
|
227
274
|
/**
|
|
228
275
|
* Arithmetic operators JS applies `ToNumber` to, so a `null` operand reads
|
|
229
276
|
* as `0` (`null * 2` is `0`). A template engine's native operator rejects a
|
|
@@ -232,3 +279,34 @@ function arithmeticOperandSeed(
|
|
|
232
279
|
* concatenates when either side is a string.
|
|
233
280
|
*/
|
|
234
281
|
export const NULL_COERCING_ARITHMETIC_OPS: ReadonlySet<string> = new Set(['-', '*', '/', '%', '**'])
|
|
282
|
+
|
|
283
|
+
/** How a DSL adapter coerces an arithmetic operand (#3474): `'null'` reads a
|
|
284
|
+
* nil as `0` (JS `ToNumber(null)`), `'undefined'` reads it as `NaN`
|
|
285
|
+
* (`ToNumber(undefined)`), `null` leaves the operand as is. */
|
|
286
|
+
export type ArithmeticCoercion = 'null' | 'undefined' | null
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* The one decision behind every DSL adapter's arithmetic over a nullish
|
|
290
|
+
* operand (#3474). An operand known to be `null` / `undefined`
|
|
291
|
+
* (`arithmeticOperandIsNull` / `arithmeticOperandIsUndefined`, or a nullable
|
|
292
|
+
* number prop) is coerced under the operators JS applies `ToNumber` to
|
|
293
|
+
* ({@link NULL_COERCING_ARITHMETIC_OPS}), and under `+` when the other
|
|
294
|
+
* operand is a number literal, where `+` cannot concatenate. Each adapter
|
|
295
|
+
* only spells the coercion; one whose engine already reads nil as `0`
|
|
296
|
+
* spells `'null'` as the operand itself.
|
|
297
|
+
*/
|
|
298
|
+
export function arithmeticOperandCoercion(
|
|
299
|
+
op: string,
|
|
300
|
+
operand: ParsedExpr | undefined,
|
|
301
|
+
other: ParsedExpr | undefined,
|
|
302
|
+
ctx: NullishAttrContext,
|
|
303
|
+
isShadowed: (name: string) => boolean = () => false,
|
|
304
|
+
): ArithmeticCoercion {
|
|
305
|
+
const numeric = NULL_COERCING_ARITHMETIC_OPS.has(op) || (op === '+' && isNumberLiteral(other))
|
|
306
|
+
return numeric ? arithmeticOperandSeed(operand, ctx, isShadowed) : null
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function isNumberLiteral(expr: ParsedExpr | undefined): boolean {
|
|
310
|
+
if (expr?.kind === 'unary' && expr.op === '-') return isNumberLiteral(expr.argument)
|
|
311
|
+
return expr?.kind === 'literal' && expr.literalType === 'number'
|
|
312
|
+
}
|