@barefootjs/jsx 0.39.4 → 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.
Files changed (97) hide show
  1. package/dist/adapters/array-method.d.ts +93 -0
  2. package/dist/adapters/array-method.d.ts.map +1 -0
  3. package/dist/adapters/attr-plan.d.ts +135 -0
  4. package/dist/adapters/attr-plan.d.ts.map +1 -0
  5. package/dist/adapters/boolean-result.d.ts +84 -9
  6. package/dist/adapters/boolean-result.d.ts.map +1 -1
  7. package/dist/adapters/callback-method.d.ts +126 -0
  8. package/dist/adapters/callback-method.d.ts.map +1 -0
  9. package/dist/adapters/component-props.d.ts +99 -0
  10. package/dist/adapters/component-props.d.ts.map +1 -0
  11. package/dist/adapters/component-tree.d.ts +25 -0
  12. package/dist/adapters/component-tree.d.ts.map +1 -0
  13. package/dist/adapters/filter-capture-collisions.d.ts +17 -0
  14. package/dist/adapters/filter-capture-collisions.d.ts.map +1 -0
  15. package/dist/adapters/literal-value.d.ts +22 -0
  16. package/dist/adapters/literal-value.d.ts.map +1 -0
  17. package/dist/adapters/loop-plan.d.ts +53 -0
  18. package/dist/adapters/loop-plan.d.ts.map +1 -0
  19. package/dist/adapters/parsed-expr-emitter.d.ts +8 -0
  20. package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
  21. package/dist/adapters/prop-classes.d.ts +37 -0
  22. package/dist/adapters/prop-classes.d.ts.map +1 -0
  23. package/dist/adapters/template-name-collisions.d.ts +21 -0
  24. package/dist/adapters/template-name-collisions.d.ts.map +1 -0
  25. package/dist/analyzer-context.d.ts.map +1 -1
  26. package/dist/expression-parser.d.ts +48 -0
  27. package/dist/expression-parser.d.ts.map +1 -1
  28. package/dist/index.d.ts +20 -4
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +2077 -1000
  31. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  32. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts +8 -0
  33. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
  34. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts +3 -2
  35. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
  36. package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
  37. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  38. package/dist/ir-to-client-js/utils.d.ts +39 -1
  39. package/dist/ir-to-client-js/utils.d.ts.map +1 -1
  40. package/dist/jsx-to-ir.d.ts +465 -1
  41. package/dist/jsx-to-ir.d.ts.map +1 -1
  42. package/dist/nullable-signal-attr.d.ts +31 -0
  43. package/dist/nullable-signal-attr.d.ts.map +1 -1
  44. package/dist/scope/binding-scope.d.ts +10 -0
  45. package/dist/scope/binding-scope.d.ts.map +1 -1
  46. package/dist/types.d.ts +7 -0
  47. package/dist/types.d.ts.map +1 -1
  48. package/package.json +2 -2
  49. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +1 -1
  50. package/src/__tests__/action-accessor.test.ts +6 -24
  51. package/src/__tests__/array-method-catalogue.test.ts +99 -0
  52. package/src/__tests__/attr-plan.test.ts +121 -0
  53. package/src/__tests__/binding-scope-ratchet.test.ts +13 -13
  54. package/src/__tests__/binding-scope.test.ts +11 -0
  55. package/src/__tests__/boolean-result-expr.test.ts +106 -0
  56. package/src/__tests__/boolean-result.test.ts +34 -1
  57. package/src/__tests__/callback-method.test.ts +189 -0
  58. package/src/__tests__/compile-without-node-process.test.ts +78 -0
  59. package/src/__tests__/component-props.test.ts +148 -0
  60. package/src/__tests__/delegated-handler-preamble.test.ts +4 -4
  61. package/src/__tests__/doc-examples.test.ts +1 -0
  62. package/src/__tests__/dsl-shared-helpers.test.ts +166 -0
  63. package/src/__tests__/issue-3240-nested-static-map-event-delegation.test.ts +1 -1
  64. package/src/__tests__/loop-plan.test.ts +104 -0
  65. package/src/__tests__/map-call-decisions.test.ts +521 -0
  66. package/src/__tests__/nested-row-shadowing.test.ts +57 -0
  67. package/src/__tests__/nullable-signal-attr.test.ts +45 -0
  68. package/src/__tests__/rename-free-identifiers.test.ts +26 -0
  69. package/src/__tests__/template-name-collisions.test.ts +81 -0
  70. package/src/adapters/array-method.ts +193 -0
  71. package/src/adapters/attr-plan.ts +200 -0
  72. package/src/adapters/boolean-result.ts +208 -12
  73. package/src/adapters/callback-method.ts +301 -0
  74. package/src/adapters/component-props.ts +144 -0
  75. package/src/adapters/component-tree.ts +118 -0
  76. package/src/adapters/filter-capture-collisions.ts +29 -0
  77. package/src/adapters/literal-value.ts +36 -0
  78. package/src/adapters/loop-plan.ts +88 -0
  79. package/src/adapters/parsed-expr-emitter.ts +18 -0
  80. package/src/adapters/prop-classes.ts +73 -0
  81. package/src/adapters/template-name-collisions.ts +120 -0
  82. package/src/analyzer-context.ts +15 -1
  83. package/src/expression-parser.ts +569 -374
  84. package/src/index.ts +58 -3
  85. package/src/ir-to-client-js/collect-elements.ts +11 -7
  86. package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +31 -10
  87. package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +12 -5
  88. package/src/ir-to-client-js/control-flow/plan/build-reactive-effects.ts +2 -2
  89. package/src/ir-to-client-js/control-flow/shared.ts +4 -1
  90. package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +9 -7
  91. package/src/ir-to-client-js/html-template.ts +13 -7
  92. package/src/ir-to-client-js/index.ts +7 -2
  93. package/src/ir-to-client-js/utils.ts +81 -7
  94. package/src/jsx-to-ir.ts +1328 -991
  95. package/src/nullable-signal-attr.ts +116 -6
  96. package/src/scope/binding-scope.ts +19 -0
  97. package/src/types.ts +7 -0
@@ -84,7 +84,15 @@ export interface NullishAttrContext {
84
84
  /** Signals seeded with a literal `null`: their SSR value is `null`, not
85
85
  * `undefined` (`arithmeticOperandIsNull`). */
86
86
  readonly nullSeededSignals?: ReadonlySet<string>
87
+ /** Signals seeded with `undefined` (`createSignal(undefined)` or a
88
+ * zero-arg `createSignal()`): their SSR value is `undefined`, not `null`
89
+ * (`arithmeticOperandIsUndefined`). */
90
+ readonly undefinedSeededSignals?: ReadonlySet<string>
87
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>
88
96
  }
89
97
 
90
98
  export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
@@ -94,10 +102,35 @@ export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
94
102
  nullSeededSignals: new Set(
95
103
  signals.filter(s => s.parsed?.kind === 'literal' && s.parsed.literalType === 'null').map(s => s.getter),
96
104
  ),
105
+ undefinedSeededSignals: new Set(
106
+ signals.filter(s => s.parsed?.kind === 'identifier' && s.parsed.name === 'undefined').map(s => s.getter),
107
+ ),
97
108
  memos: new Map((ir.metadata.memos ?? []).map(m => [m.name, m])),
109
+ nullableNumberProps: collectNullableNumberProps(ir),
98
110
  }
99
111
  }
100
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
+
101
134
  /**
102
135
  * Whether an attribute value's SSR value can be `undefined` / `null`, so a
103
136
  * DSL adapter must guard the attribute on a nil check to omit it the way
@@ -176,20 +209,66 @@ export function arithmeticOperandIsNull(
176
209
  ctx: NullishAttrContext,
177
210
  isShadowed: (name: string) => boolean = () => false,
178
211
  ): boolean {
212
+ return arithmeticOperandSeed(parsed, ctx, isShadowed) === 'null'
213
+ }
214
+
215
+ /**
216
+ * Whether an arithmetic operand's SSR value is certainly `undefined` — the
217
+ * literal `undefined`, a read of a signal seeded with `undefined` (or a
218
+ * zero-arg `createSignal()`), or a memo whose body is such a read. JS
219
+ * `ToNumber(undefined)` is `NaN`, so adapters coerce such an operand with
220
+ * their JS `Number()` helper, which maps the engine's nil to `NaN` (#3390).
221
+ */
222
+ export function arithmeticOperandIsUndefined(
223
+ parsed: ParsedExpr | undefined,
224
+ ctx: NullishAttrContext,
225
+ isShadowed: (name: string) => boolean = () => false,
226
+ ): boolean {
227
+ return arithmeticOperandSeed(parsed, ctx, isShadowed) === 'undefined'
228
+ }
229
+
230
+ function arithmeticOperandSeed(
231
+ parsed: ParsedExpr | undefined,
232
+ ctx: NullishAttrContext,
233
+ isShadowed: (name: string) => boolean,
234
+ ): 'null' | 'undefined' | null {
179
235
  const visiting = new Set<string>()
180
- const walk = (expr: ParsedExpr, shadowed: (name: string) => boolean): boolean => {
181
- if (expr.kind === 'literal') return expr.literalType === 'null'
236
+ const walk = (expr: ParsedExpr, shadowed: (name: string) => boolean): 'null' | 'undefined' | null => {
237
+ if (expr.kind === 'literal') return expr.literalType === 'null' ? 'null' : null
238
+ if (expr.kind === 'identifier' && expr.name === 'undefined' && !shadowed('undefined')) return 'undefined'
239
+ const prop = nullableNumberPropRead(expr, ctx, shadowed)
240
+ if (prop) return 'null'
182
241
  const getter = expr.kind === 'identifier' ? expr.name : bareGetterCallName(expr)
183
- if (getter === null || shadowed(getter)) return false
184
- if (ctx.nullSeededSignals?.has(getter)) return true
242
+ if (getter === null || shadowed(getter)) return null
243
+ if (ctx.nullSeededSignals?.has(getter)) return 'null'
244
+ if (ctx.undefinedSeededSignals?.has(getter)) return 'undefined'
185
245
  const memo = ctx.memos.get(getter)
186
- if (!memo?.parsed || visiting.has(getter)) return false
246
+ if (!memo?.parsed || visiting.has(getter)) return null
187
247
  visiting.add(getter)
188
248
  const result = walk(memo.parsed, () => false)
189
249
  visiting.delete(getter)
190
250
  return result
191
251
  }
192
- return parsed !== undefined && walk(parsed, isShadowed)
252
+ return parsed === undefined ? null : walk(parsed, isShadowed)
253
+ }
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
193
272
  }
194
273
 
195
274
  /**
@@ -200,3 +279,34 @@ export function arithmeticOperandIsNull(
200
279
  * concatenates when either side is a string.
201
280
  */
202
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
+ }
@@ -148,6 +148,25 @@ export class BindingScope {
148
148
  return new BindingScope([frame, ...this.frames])
149
149
  }
150
150
 
151
+ /**
152
+ * Child scope with a `'callback'` frame binding each alias to the binding
153
+ * its original name resolves to here (same source and string typing). An
154
+ * adapter that rewrites a `.filter()` predicate's captured names to
155
+ * loop-scoped aliases enters this while emitting the predicate, so the
156
+ * aliases render and type exactly like the names they stand for (#3402).
157
+ * Aliases whose original is unbound are skipped; returns `this` when none
158
+ * bind. Parent is not mutated.
159
+ */
160
+ enterAliases(aliasToOriginal: ReadonlyMap<string, string>): BindingScope {
161
+ const bindings = new Map<string, ScopeBinding>()
162
+ for (const [alias, original] of aliasToOriginal) {
163
+ const hit = this.lookup(original)
164
+ if (hit) bindings.set(alias, hit.binding)
165
+ }
166
+ if (bindings.size === 0) return this
167
+ return new BindingScope([{ kind: 'callback', bindings }, ...this.frames])
168
+ }
169
+
151
170
  /** Innermost-first membership check across every frame in the stack. */
152
171
  isBound(name: string): boolean {
153
172
  for (const frame of this.frames) {
package/src/types.ts CHANGED
@@ -1130,6 +1130,13 @@ export interface LoopParamBinding {
1130
1130
  * reading it as "binding at the loop root".
1131
1131
  */
1132
1132
  segments?: readonly LoopBindingPathSegment[]
1133
+ /**
1134
+ * Client JS only: the row accessor call this binding reads through
1135
+ * (`__bfItem_l0()`), set on the bindings of a nested destructured `.map()`
1136
+ * row so its render callback does not shadow an enclosing row's
1137
+ * `__bfItem` (#3397). Absent for a top-level row, which reads `__bfItem()`.
1138
+ */
1139
+ accessor?: string
1133
1140
  }
1134
1141
 
1135
1142
  export interface IRComponent {