@barefootjs/jsx 0.40.1 → 0.41.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (211) hide show
  1. package/dist/adapters/boolean-result.d.ts +32 -10
  2. package/dist/adapters/boolean-result.d.ts.map +1 -1
  3. package/dist/adapters/brace-delimiter-text.d.ts +18 -0
  4. package/dist/adapters/brace-delimiter-text.d.ts.map +1 -0
  5. package/dist/adapters/callback-method.d.ts +45 -5
  6. package/dist/adapters/callback-method.d.ts.map +1 -1
  7. package/dist/adapters/component-props.d.ts +3 -4
  8. package/dist/adapters/component-props.d.ts.map +1 -1
  9. package/dist/adapters/computed-const.d.ts +11 -1
  10. package/dist/adapters/computed-const.d.ts.map +1 -1
  11. package/dist/adapters/dsl-template-adapter.d.ts +24 -12
  12. package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
  13. package/dist/adapters/loop-bound-names.d.ts +9 -1
  14. package/dist/adapters/loop-bound-names.d.ts.map +1 -1
  15. package/dist/adapters/loop-row-type.d.ts +59 -0
  16. package/dist/adapters/loop-row-type.d.ts.map +1 -0
  17. package/dist/adapters/nil-reading.d.ts +63 -0
  18. package/dist/adapters/nil-reading.d.ts.map +1 -0
  19. package/dist/adapters/non-primitive-equality.d.ts +33 -0
  20. package/dist/adapters/non-primitive-equality.d.ts.map +1 -0
  21. package/dist/adapters/number-value.d.ts +5 -3
  22. package/dist/adapters/number-value.d.ts.map +1 -1
  23. package/dist/adapters/operand-typing.d.ts +126 -0
  24. package/dist/adapters/operand-typing.d.ts.map +1 -0
  25. package/dist/adapters/parsed-expr-emitter.d.ts +80 -12
  26. package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
  27. package/dist/adapters/primitive-plan.d.ts +320 -0
  28. package/dist/adapters/primitive-plan.d.ts.map +1 -0
  29. package/dist/adapters/prop-classes.d.ts +5 -0
  30. package/dist/adapters/prop-classes.d.ts.map +1 -1
  31. package/dist/analyzer-context.d.ts +4 -0
  32. package/dist/analyzer-context.d.ts.map +1 -1
  33. package/dist/analyzer.d.ts.map +1 -1
  34. package/dist/augment-inherited-props.d.ts +63 -8
  35. package/dist/augment-inherited-props.d.ts.map +1 -1
  36. package/dist/compiler.d.ts.map +1 -1
  37. package/dist/event-handler-name.d.ts +12 -0
  38. package/dist/event-handler-name.d.ts.map +1 -1
  39. package/dist/expression-parser.d.ts +8 -0
  40. package/dist/expression-parser.d.ts.map +1 -1
  41. package/dist/index.d.ts +20 -11
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +4903 -2311
  44. package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
  45. package/dist/ir-to-client-js/child-props-object.d.ts +106 -0
  46. package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -0
  47. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  48. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
  49. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
  50. package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
  51. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +4 -2
  52. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
  53. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  54. package/dist/ir-to-client-js/imports.d.ts +2 -2
  55. package/dist/ir-to-client-js/imports.d.ts.map +1 -1
  56. package/dist/ir-to-client-js/index.d.ts +2 -6
  57. package/dist/ir-to-client-js/index.d.ts.map +1 -1
  58. package/dist/ir-to-client-js/phases/props-extraction.d.ts +3 -3
  59. package/dist/ir-to-client-js/plan/build-declaration-emit.d.ts.map +1 -1
  60. package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
  61. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +30 -18
  62. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
  63. package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
  64. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts +3 -1
  65. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
  66. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +3 -2
  67. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
  68. package/dist/jsx-to-ir.d.ts +1 -0
  69. package/dist/jsx-to-ir.d.ts.map +1 -1
  70. package/dist/nullable-signal-attr.d.ts +51 -34
  71. package/dist/nullable-signal-attr.d.ts.map +1 -1
  72. package/dist/prop-rewrite.d.ts +7 -0
  73. package/dist/prop-rewrite.d.ts.map +1 -1
  74. package/dist/props-binding.d.ts +58 -13
  75. package/dist/props-binding.d.ts.map +1 -1
  76. package/dist/props-object-whole-read.d.ts +15 -0
  77. package/dist/props-object-whole-read.d.ts.map +1 -0
  78. package/dist/rich-type-refusal.d.ts.map +1 -1
  79. package/dist/scope/binding-scope.d.ts +85 -10
  80. package/dist/scope/binding-scope.d.ts.map +1 -1
  81. package/dist/scope/lexical-scope-walk.d.ts +3 -2
  82. package/dist/scope/lexical-scope-walk.d.ts.map +1 -1
  83. package/dist/scope/root-prop-alias.d.ts +10 -7
  84. package/dist/scope/root-prop-alias.d.ts.map +1 -1
  85. package/dist/ssr-defaults.d.ts +39 -3
  86. package/dist/ssr-defaults.d.ts.map +1 -1
  87. package/dist/strip-types.d.ts +13 -0
  88. package/dist/strip-types.d.ts.map +1 -1
  89. package/dist/template-position-walk.d.ts +21 -3
  90. package/dist/template-position-walk.d.ts.map +1 -1
  91. package/dist/types.d.ts +90 -0
  92. package/dist/types.d.ts.map +1 -1
  93. package/package.json +2 -2
  94. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +9 -9
  95. package/src/__tests__/binding-scope.test.ts +89 -1
  96. package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -0
  97. package/src/__tests__/body-destructured-prop-default-arrow.test.ts +2 -2
  98. package/src/__tests__/body-destructured-props-live.test.ts +5 -5
  99. package/src/__tests__/boolean-result.test.ts +22 -5
  100. package/src/__tests__/brace-delimiter-text.test.ts +26 -0
  101. package/src/__tests__/callback-method.test.ts +62 -2
  102. package/src/__tests__/child-components-in-map.test.ts +1 -1
  103. package/src/__tests__/client-js-generation.test.ts +3 -3
  104. package/src/__tests__/computed-const.test.ts +103 -0
  105. package/src/__tests__/destructure-default-reads-earlier-param.test.ts +7 -7
  106. package/src/__tests__/destructured-props-live.test.ts +1 -1
  107. package/src/__tests__/emit-to-string-call.test.ts +2 -1
  108. package/src/__tests__/hydration-payload-analysis.test.ts +166 -0
  109. package/src/__tests__/issue-2724-prop-alias-loop-not-static.test.ts +7 -7
  110. package/src/__tests__/issue-2754-rest-spread-needs-slot.test.ts +5 -2
  111. package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +1 -1
  112. package/src/__tests__/issue-2835-props-type-member-name-collision.test.ts +4 -4
  113. package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +1 -1
  114. package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +2 -1
  115. package/src/__tests__/issue-3713-props-spread-loop-row.test.ts +65 -0
  116. package/src/__tests__/issue-3861-object-spread-child.test.ts +102 -0
  117. package/src/__tests__/lookup-literal-const.test.ts +9 -3
  118. package/src/__tests__/loop-index-reactivity.test.ts +2 -2
  119. package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +1 -1
  120. package/src/__tests__/loop-plan.test.ts +1 -1
  121. package/src/__tests__/loop-row-const-initializer-shadow.test.ts +146 -0
  122. package/src/__tests__/loop-row-type.test.ts +64 -0
  123. package/src/__tests__/lowering-value-position.test.ts +4 -0
  124. package/src/__tests__/memo-body-expr.test.ts +2 -2
  125. package/src/__tests__/module-const-destructure-default.test.ts +156 -0
  126. package/src/__tests__/nested-loop-index-param.test.ts +1 -1
  127. package/src/__tests__/nested-loop-reactive-attrs.test.ts +2 -2
  128. package/src/__tests__/nested-row-shadowing.test.ts +2 -2
  129. package/src/__tests__/nil-reading.test.ts +78 -0
  130. package/src/__tests__/nullable-signal-attr.test.ts +109 -48
  131. package/src/__tests__/primitive-plan.test.ts +494 -0
  132. package/src/__tests__/profile-loop-binding-ids.test.ts +2 -2
  133. package/src/__tests__/profile-nested-binding-ids.test.ts +2 -2
  134. package/src/__tests__/prop-default-undefined-only.test.ts +115 -0
  135. package/src/__tests__/prop-references.test.ts +2 -2
  136. package/src/__tests__/props-destructuring.test.ts +17 -0
  137. package/src/__tests__/props-object-whole-read.test.ts +40 -0
  138. package/src/__tests__/prune-unused-prop-extractions.test.ts +3 -3
  139. package/src/__tests__/reactive-attrs-in-map.test.ts +1 -1
  140. package/src/__tests__/reactive-factory-inlining.test.ts +4 -1
  141. package/src/__tests__/renamed-body-destructure-template.test.ts +7 -7
  142. package/src/__tests__/rest-binding-gaps.test.ts +192 -0
  143. package/src/__tests__/rewrite-destructured-props.test.ts +1 -1
  144. package/src/__tests__/root-prop-alias.test.ts +53 -8
  145. package/src/__tests__/ssr-defaults.test.ts +113 -25
  146. package/src/__tests__/static-loop-csr-materialize.test.ts +2 -2
  147. package/src/__tests__/static-record-literal-array.test.ts +171 -0
  148. package/src/__tests__/string-concat-identifier.test.ts +40 -1
  149. package/src/__tests__/written-binding-substitution.test.ts +8 -8
  150. package/src/adapters/boolean-result.ts +72 -66
  151. package/src/adapters/brace-delimiter-text.ts +25 -0
  152. package/src/adapters/callback-method.ts +159 -27
  153. package/src/adapters/component-props.ts +3 -4
  154. package/src/adapters/computed-const.ts +238 -4
  155. package/src/adapters/dsl-template-adapter.ts +62 -26
  156. package/src/adapters/loop-bound-names.ts +9 -1
  157. package/src/adapters/loop-plan.ts +1 -1
  158. package/src/adapters/loop-row-type.ts +149 -0
  159. package/src/adapters/nil-reading.ts +295 -0
  160. package/src/adapters/non-primitive-equality.ts +92 -0
  161. package/src/adapters/number-value.ts +17 -4
  162. package/src/adapters/operand-typing.ts +522 -0
  163. package/src/adapters/parsed-expr-emitter.ts +183 -19
  164. package/src/adapters/primitive-plan.ts +765 -0
  165. package/src/adapters/prop-classes.ts +16 -3
  166. package/src/analyzer-context.ts +6 -0
  167. package/src/analyzer.ts +532 -26
  168. package/src/augment-inherited-props.ts +108 -10
  169. package/src/compiler.ts +17 -0
  170. package/src/event-handler-name.ts +15 -0
  171. package/src/expression-parser.ts +70 -1
  172. package/src/index.ts +35 -16
  173. package/src/ir-to-client-js/build-references.ts +69 -0
  174. package/src/ir-to-client-js/child-props-object.ts +240 -0
  175. package/src/ir-to-client-js/collect-elements.ts +31 -63
  176. package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +8 -1
  177. package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +31 -28
  178. package/src/ir-to-client-js/control-flow/shared.ts +20 -22
  179. package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +5 -3
  180. package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +2 -2
  181. package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +1 -1
  182. package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +2 -2
  183. package/src/ir-to-client-js/control-flow/stringify/loop.ts +1 -1
  184. package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +3 -3
  185. package/src/ir-to-client-js/html-template.ts +43 -17
  186. package/src/ir-to-client-js/imports.ts +2 -2
  187. package/src/ir-to-client-js/index.ts +53 -11
  188. package/src/ir-to-client-js/phases/props-extraction.ts +11 -7
  189. package/src/ir-to-client-js/plan/build-declaration-emit.ts +40 -7
  190. package/src/ir-to-client-js/plan/build-static-array-child-init.ts +31 -14
  191. package/src/ir-to-client-js/plan/static-array-child-init.ts +27 -18
  192. package/src/ir-to-client-js/prop-handling.ts +22 -3
  193. package/src/ir-to-client-js/prune-unused-prop-extractions.ts +58 -5
  194. package/src/ir-to-client-js/rewrite-destructured-props.ts +31 -1
  195. package/src/ir-to-client-js/stringify/static-array-child-init.ts +26 -10
  196. package/src/jsx-to-ir.ts +459 -37
  197. package/src/nullable-signal-attr.ts +200 -110
  198. package/src/prop-rewrite.ts +30 -6
  199. package/src/props-binding.ts +84 -19
  200. package/src/props-object-whole-read.ts +87 -0
  201. package/src/rich-type-refusal.ts +2 -1
  202. package/src/scope/binding-scope.ts +182 -18
  203. package/src/scope/lexical-scope-walk.ts +4 -3
  204. package/src/scope/root-prop-alias.ts +15 -8
  205. package/src/ssr-defaults.ts +100 -11
  206. package/src/strip-types.ts +31 -3
  207. package/src/template-position-walk.ts +57 -28
  208. package/src/types.ts +87 -0
  209. package/dist/adapters/ambiguous-nil-arithmetic.d.ts +0 -41
  210. package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +0 -1
  211. package/src/adapters/ambiguous-nil-arithmetic.ts +0 -94
@@ -1,19 +1,18 @@
1
1
  import { describe, test, expect } from 'bun:test'
2
2
  import { analyzeComponent } from '../analyzer'
3
- import { exprToString, parseExpression } from '../expression-parser'
3
+ import { parseExpression } from '../expression-parser'
4
4
  import {
5
- ambiguousNilArithmeticOperands,
6
5
  arithmeticOperandCoercion,
7
6
  arithmeticOperandIsNull,
8
7
  arithmeticOperandIsUndefined,
9
- arithmeticOperandNilIsAmbiguous,
10
8
  attrValueMayBeNullish,
11
9
  collectAbsentPropSeedReads,
12
10
  collectNullableSignalGetters,
13
11
  collectNullishAttrContext,
12
+ isNeverNullishOperand,
13
+ nullablePropsYieldedBy,
14
14
  nullableSignalAttrGetter,
15
15
  } from '../nullable-signal-attr'
16
- import { BindingScope } from '../scope/binding-scope'
17
16
  import type { ComponentIR } from '../types'
18
17
 
19
18
  function nullableGetters(decls: string): Set<string> {
@@ -215,9 +214,9 @@ describe('arithmeticOperandCoercion (#3474)', () => {
215
214
  for (const op of ['-', '*', '/', '%', '**']) expect(coercion(ctx, op, 'props.b', 'x')).toBe('undefined')
216
215
  expect(coercion(ctx, '+', 'props.b', '-2')).toBe('undefined')
217
216
  expect(coercion(ctx, '*', 'props.u', '2')).toBe('undefined')
218
- // A type that admits both keeps the null spelling; the arithmetic over it
219
- // is refused (`arithmeticOperandNilIsAmbiguous`, #3700).
220
- expect(coercion(ctx, '*', 'props.both', '2')).toBe('null')
217
+ // A type that admits both is left to the runtime's helper, which reads
218
+ // `null` and `UNDEF` apart (spec/js-undefined-value.md).
219
+ expect(coercion(ctx, '*', 'props.both', '2')).toBeNull()
221
220
  })
222
221
 
223
222
  test('unary minus passes no other operand and coerces like binary minus (#3533)', () => {
@@ -228,6 +227,30 @@ describe('arithmeticOperandCoercion (#3474)', () => {
228
227
  expect(unary('props.c')).toBeNull()
229
228
  })
230
229
 
230
+ test('unary plus passes no other operand and coerces like unary minus (#3824 step 3)', () => {
231
+ const ctx = ctxOf('props: { a: number | null; b?: number; c: number }')
232
+ const unary = (operand: string) => arithmeticOperandCoercion('+', parseExpression(operand), undefined, ctx)
233
+ expect(unary('props.a')).toBe('null')
234
+ expect(unary('props.b')).toBe('undefined')
235
+ expect(unary('props.c')).toBeNull()
236
+ })
237
+
238
+ test('a string or boolean relational operand that may only be absent reads NaN and keeps a present value (#3824 step 3)', () => {
239
+ const ctx = ctxOf('props: { s?: string; b?: boolean; t: string | null; c: string; u: string | undefined }')
240
+ for (const op of ['<', '<=', '>', '>=']) {
241
+ expect(coercion(ctx, op, 'props.s', '1')).toBe('undefined-keep')
242
+ expect(coercion(ctx, op, 'props.s', "'a'")).toBe('undefined-keep')
243
+ expect(coercion(ctx, op, 'props.b', '1')).toBe('undefined-keep')
244
+ expect(coercion(ctx, op, 'props.u', '1')).toBe('undefined-keep')
245
+ // A `null` reading is the helper's own; a required prop has no nil.
246
+ expect(coercion(ctx, op, 'props.t', '1')).toBeNull()
247
+ expect(coercion(ctx, op, 'props.c', '1')).toBeNull()
248
+ }
249
+ // Arithmetic over a string prop is not a relational: left as it is.
250
+ expect(coercion(ctx, '*', 'props.s', '2')).toBeNull()
251
+ expect(arithmeticOperandCoercion('<', parseExpression('props.s'), parseExpression('1'), ctx, n => n === 'props')).toBeNull()
252
+ })
253
+
231
254
  test('an absent-only prop is not certainly undefined: arithmeticOperandIsUndefined stays false', () => {
232
255
  const ctx = ctxOf('props: { b?: number }')
233
256
  expect(arithmeticOperandIsUndefined(parseExpression('props.b'), ctx)).toBe(false)
@@ -252,7 +275,7 @@ describe('arithmeticOperandCoercion (#3474)', () => {
252
275
  })
253
276
  })
254
277
 
255
- describe('arithmeticOperandNilIsAmbiguous (#3700)', () => {
278
+ describe("an operand whose nil may be either is left to the runtime's helper", () => {
256
279
  function ctxOf(signature: string, decls = ''): ReturnType<typeof collectNullishAttrContext> {
257
280
  const source = `
258
281
  'use client'
@@ -264,65 +287,103 @@ describe('arithmeticOperandNilIsAmbiguous (#3700)', () => {
264
287
  `
265
288
  return collectNullishAttrContext({ metadata: analyzeComponent(source, 'C.tsx') } as unknown as ComponentIR)
266
289
  }
267
- const ambiguous = (ctx: ReturnType<typeof collectNullishAttrContext>, op: string, operand: string, other?: string) =>
268
- arithmeticOperandNilIsAmbiguous(op, parseExpression(operand), other === undefined ? undefined : parseExpression(other), ctx)
290
+ const coercion = (ctx: ReturnType<typeof collectNullishAttrContext>, op: string, operand: string, other?: string) =>
291
+ arithmeticOperandCoercion(op, parseExpression(operand), other === undefined ? undefined : parseExpression(other), ctx)
269
292
 
270
293
  test('a number prop that admits both null and absence, in either props form', () => {
271
294
  for (const signature of ['props: { n?: number | null }', 'props: { n: number | null | undefined }']) {
272
295
  const ctx = ctxOf(signature)
273
- for (const op of ['-', '*', '/', '%', '**']) expect(ambiguous(ctx, op, 'props.n', 'x')).toBe(true)
274
- expect(ambiguous(ctx, '+', 'props.n', '1')).toBe(true)
275
- expect(ambiguous(ctx, '-', 'props.n')).toBe(true)
276
- // `+` against a non-number may concatenate: no `ToNumber`, no ambiguity.
277
- expect(ambiguous(ctx, '+', 'props.n', "'px'")).toBe(false)
296
+ for (const op of ['-', '*', '/', '%', '**']) expect(coercion(ctx, op, 'props.n', 'x')).toBeNull()
297
+ expect(coercion(ctx, '+', 'props.n', '1')).toBeNull()
298
+ expect(coercion(ctx, '-', 'props.n')).toBeNull()
299
+ expect(coercion(ctx, '+', 'props.n')).toBeNull()
278
300
  }
279
301
  const destructured = ctxOf('{ n, m: alias }: { n?: number | null; m?: null | number }')
280
- expect(ambiguous(destructured, '*', 'n', '2')).toBe(true)
281
- expect(ambiguous(destructured, '*', 'alias', '2')).toBe(true)
302
+ expect(coercion(destructured, '*', 'n', '2')).toBeNull()
303
+ expect(coercion(destructured, '*', 'alias', '2')).toBeNull()
282
304
  })
283
305
 
284
- test('a prop with one nil reading, a defaulted one and a shadowed one are not ambiguous', () => {
285
- const ctx = ctxOf('props: { a: number | null; b?: number; c: number }')
286
- for (const operand of ['props.a', 'props.b', 'props.c']) expect(ambiguous(ctx, '*', operand, '2')).toBe(false)
287
- expect(ambiguous(ctxOf('{ n = 1 }: { n?: number | null }'), '*', 'n', '2')).toBe(false)
306
+ test('a relational operand: only the one nil reading is spelled (#3824 step 3)', () => {
307
+ const ctx = ctxOf('props: { n?: number | null; a: number | null; b?: number }')
308
+ for (const op of ['<', '<=', '>', '>=']) {
309
+ expect(coercion(ctx, op, 'props.n', '1')).toBeNull()
310
+ expect(coercion(ctx, op, 'props.a', '1')).toBeNull()
311
+ expect(coercion(ctx, op, 'props.b', '1')).toBe('undefined')
312
+ }
313
+ })
314
+
315
+ test('a string or boolean prop admitting both nils under a comparison (#3824 step 3)', () => {
316
+ const ctx = ctxOf('props: { s?: string | null; b?: boolean | null; t?: string; m: string | null }', 'const id = createMemo(() => props.s)')
317
+ for (const op of ['<', '<=', '>', '>=']) {
318
+ expect(coercion(ctx, op, 'props.s', '1')).toBeNull()
319
+ expect(coercion(ctx, op, 'props.b', '1')).toBeNull()
320
+ expect(coercion(ctx, op, 'id()', '1')).toBeNull()
321
+ expect(coercion(ctx, op, 'props.t', '1')).toBe('undefined-keep')
322
+ expect(coercion(ctx, op, 'props.m', '1')).toBeNull()
323
+ }
324
+ })
325
+
326
+ test('a defaulted one and a shadowed one keep their own coercion', () => {
327
+ expect(coercion(ctxOf('{ n = 1 }: { n?: number | null }'), '*', 'n', '2')).toBeNull()
288
328
  const destructured = ctxOf('{ n }: { n?: number | null }')
289
- expect(arithmeticOperandNilIsAmbiguous('*', parseExpression('n'), parseExpression('2'), destructured, name => name === 'n')).toBe(false)
329
+ expect(arithmeticOperandCoercion('*', parseExpression('n'), parseExpression('2'), destructured, name => name === 'n')).toBeNull()
290
330
  })
291
331
 
292
- test('an identity memo over the prop is ambiguous too; the null spelling is kept for it', () => {
332
+ test('an identity memo over the prop is left to the helper too', () => {
293
333
  const ctx = ctxOf('props: { n?: number | null }', 'const m = createMemo(() => props.n)')
294
- expect(ambiguous(ctx, '*', 'm()', '2')).toBe(true)
295
- expect(arithmeticOperandCoercion('*', parseExpression('m()'), parseExpression('2'), ctx)).toBe('null')
334
+ expect(coercion(ctx, '*', 'm()', '2')).toBeNull()
296
335
  expect(arithmeticOperandIsNull(parseExpression('m()'), ctx)).toBe(true)
297
336
  })
298
337
 
299
338
  test('a destructured union binding keeps its declared type for the classification', () => {
300
339
  const ctx = ctxOf('{ a, u }: { a: number | null; u?: number | undefined }')
301
- expect(arithmeticOperandCoercion('*', parseExpression('a'), parseExpression('2'), ctx)).toBe('null')
302
- expect(arithmeticOperandCoercion('*', parseExpression('u'), parseExpression('2'), ctx)).toBe('undefined')
340
+ expect(coercion(ctx, '*', 'a', '2')).toBe('null')
341
+ expect(coercion(ctx, '*', 'u', '2')).toBe('undefined')
303
342
  })
304
343
  })
305
344
 
306
- describe('ambiguousNilArithmeticOperands (#3700)', () => {
307
- const ctx = collectNullishAttrContext({
308
- metadata: analyzeComponent(
309
- `export function C({ n, items }: { n?: number | null; items: number[] }) { return <p>x</p> }`,
310
- 'C.tsx',
311
- ),
312
- } as unknown as ComponentIR)
313
- const found = (source: string, scope?: BindingScope) =>
314
- ambiguousNilArithmeticOperands(parseExpression(source), ctx, scope).map(exprToString)
315
-
316
- test('finds the operands in nested positions', () => {
317
- expect(found("items.filter(i => i > n * 2).length")).toEqual(['n'])
318
- expect(found("(-n > 1 ? `a${n + 1}` : 'b')")).toEqual(['n', 'n'])
319
- })
320
-
321
- test('an arrow param or an enclosing binding of the same name shadows the prop', () => {
322
- expect(found("items.filter(n => n * 2 > 1).length")).toEqual([])
323
- expect(found("items.map(i => items.filter(n => n > i).length * n)")).toEqual(['n'])
324
- expect(found('n * 2', BindingScope.EMPTY.enterCallback(['n']))).toEqual([])
325
- // A sibling arrow's param does not leak out of its own body.
326
- expect(found('items.filter(n => n > 1).length * n')).toEqual(['n'])
345
+ describe('a prop that can be absent, and an operand that is never nullish', () => {
346
+ function irOf(signature: string): ComponentIR {
347
+ const source = `
348
+ export function C(${signature}) {
349
+ return <p>x</p>
350
+ }
351
+ `
352
+ return { metadata: analyzeComponent(source, 'C.tsx') } as unknown as ComponentIR
353
+ }
354
+ const mayBeNullish = (ir: ComponentIR, src: string, shadowed: (n: string) => boolean = () => false) =>
355
+ attrValueMayBeNullish(parseExpression(src), collectNullishAttrContext(ir), shadowed)
356
+
357
+ test('an && / ternary over an absent-able prop may be nullish, in both props forms', () => {
358
+ const object = irOf('props: { on?: boolean; label: string | undefined; n: number; d?: string }')
359
+ expect(mayBeNullish(object, "props.on && 'y'")).toBe(true)
360
+ expect(mayBeNullish(object, "props.n > 0 ? props.label : 'x'")).toBe(true)
361
+ expect(mayBeNullish(object, "props.n && 'y'")).toBe(false)
362
+ expect(mayBeNullish(object, "props.on ?? 'y'")).toBe(false)
363
+ expect(mayBeNullish(object, "props.on && 'y'", n => n === 'props')).toBe(false)
364
+ const destructured = irOf("{ on, d = 'z' }: { on?: boolean; d?: string }")
365
+ expect(mayBeNullish(destructured, "on && 'y'")).toBe(true)
366
+ expect(mayBeNullish(destructured, "on && 'y'", n => n === 'on')).toBe(false)
367
+ expect(mayBeNullish(destructured, "d && 'y'")).toBe(false)
368
+ })
369
+
370
+ test('nullablePropsYieldedBy names the props a value can be read whole from', () => {
371
+ const ir = irOf('props: { on?: boolean; label?: string; n?: number }')
372
+ const nullable = collectNullishAttrContext(ir).nullableProps!
373
+ expect([...nullablePropsYieldedBy(parseExpression("props.on && (props.n > 0 ? props.label : 'x')"), nullable)].sort()).toEqual(['label', 'on'])
374
+ expect([...nullablePropsYieldedBy(parseExpression("props.on ?? props.label"), nullable)]).toEqual(['label'])
375
+ })
376
+
377
+ test('isNeverNullishOperand: a comparison and a required scalar prop, not an optional or shadowed one', () => {
378
+ const object = irOf("props: { flag: boolean; s: string; n: number; tone: 'a' | 'b'; opt?: boolean; u: string | undefined; list: string[] }").metadata
379
+ const never = (src: string, isBound: (n: string) => boolean = () => false) =>
380
+ isNeverNullishOperand(parseExpression(src)!, object, isBound)
381
+ for (const src of ['props.flag', 'props.s', 'props.n', 'props.tone', 'props.n > 1', '!props.opt']) expect(never(src)).toBe(true)
382
+ for (const src of ['props.opt', 'props.u', 'props.list', 'flag']) expect(never(src)).toBe(false)
383
+ expect(never('props.flag', n => n === 'props')).toBe(false)
384
+ const destructured = irOf('{ flag, opt }: { flag: boolean; opt?: boolean }').metadata
385
+ expect(isNeverNullishOperand(parseExpression('flag')!, destructured, () => false)).toBe(true)
386
+ expect(isNeverNullishOperand(parseExpression('flag')!, destructured, n => n === 'flag')).toBe(false)
387
+ expect(isNeverNullishOperand(parseExpression('opt')!, destructured, () => false)).toBe(false)
327
388
  })
328
389
  })