@barefootjs/jsx 0.40.2 → 0.41.1

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 (95) hide show
  1. package/dist/adapters/callback-method.d.ts +8 -1
  2. package/dist/adapters/callback-method.d.ts.map +1 -1
  3. package/dist/adapters/component-props.d.ts +14 -5
  4. package/dist/adapters/component-props.d.ts.map +1 -1
  5. package/dist/adapters/dsl-template-adapter.d.ts +3 -12
  6. package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
  7. package/dist/adapters/loop-row-type.d.ts +7 -0
  8. package/dist/adapters/loop-row-type.d.ts.map +1 -1
  9. package/dist/adapters/nil-reading.d.ts +15 -20
  10. package/dist/adapters/nil-reading.d.ts.map +1 -1
  11. package/dist/adapters/non-primitive-equality.d.ts +1 -1
  12. package/dist/adapters/number-value.d.ts +2 -1
  13. package/dist/adapters/number-value.d.ts.map +1 -1
  14. package/dist/adapters/parsed-expr-emitter.d.ts +11 -1
  15. package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
  16. package/dist/adapters/primitive-plan.d.ts +40 -11
  17. package/dist/adapters/primitive-plan.d.ts.map +1 -1
  18. package/dist/adapters/prop-classes.d.ts +5 -0
  19. package/dist/adapters/prop-classes.d.ts.map +1 -1
  20. package/dist/analyzer.d.ts.map +1 -1
  21. package/dist/index.d.ts +6 -7
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +566 -367
  24. package/dist/ir-to-client-js/child-props-object.d.ts +43 -1
  25. package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -1
  26. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  27. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
  28. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
  29. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  30. package/dist/ir-to-client-js/imports.d.ts +2 -2
  31. package/dist/ir-to-client-js/imports.d.ts.map +1 -1
  32. package/dist/ir-to-client-js/phases/props-extraction.d.ts +3 -3
  33. package/dist/ir-to-client-js/plan/build-declaration-emit.d.ts.map +1 -1
  34. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts +3 -1
  35. package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
  36. package/dist/jsx-to-ir.d.ts.map +1 -1
  37. package/dist/nullable-signal-attr.d.ts +4 -46
  38. package/dist/nullable-signal-attr.d.ts.map +1 -1
  39. package/dist/props-binding.d.ts +58 -13
  40. package/dist/props-binding.d.ts.map +1 -1
  41. package/dist/ssr-defaults.d.ts +39 -3
  42. package/dist/ssr-defaults.d.ts.map +1 -1
  43. package/dist/types.d.ts +14 -0
  44. package/dist/types.d.ts.map +1 -1
  45. package/package.json +2 -2
  46. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +6 -6
  47. package/src/__tests__/body-destructured-prop-default-arrow.test.ts +2 -2
  48. package/src/__tests__/body-destructured-props-live.test.ts +5 -5
  49. package/src/__tests__/client-js-generation.test.ts +3 -3
  50. package/src/__tests__/destructure-default-reads-earlier-param.test.ts +7 -7
  51. package/src/__tests__/destructured-props-live.test.ts +1 -1
  52. package/src/__tests__/issue-3861-object-spread-child.test.ts +102 -0
  53. package/src/__tests__/memo-body-expr.test.ts +2 -2
  54. package/src/__tests__/nil-reading.test.ts +3 -2
  55. package/src/__tests__/nullable-signal-attr.test.ts +30 -103
  56. package/src/__tests__/primitive-plan.test.ts +90 -12
  57. package/src/__tests__/prop-default-undefined-only.test.ts +115 -0
  58. package/src/__tests__/prop-references.test.ts +2 -2
  59. package/src/__tests__/props-destructuring.test.ts +17 -0
  60. package/src/__tests__/prune-unused-prop-extractions.test.ts +3 -3
  61. package/src/__tests__/renamed-body-destructure-template.test.ts +7 -7
  62. package/src/__tests__/rewrite-destructured-props.test.ts +1 -1
  63. package/src/__tests__/ssr-defaults.test.ts +113 -25
  64. package/src/__tests__/static-const-child-prop.test.ts +85 -0
  65. package/src/__tests__/written-binding-substitution.test.ts +8 -8
  66. package/src/adapters/callback-method.ts +13 -5
  67. package/src/adapters/component-props.ts +20 -6
  68. package/src/adapters/dsl-template-adapter.ts +26 -28
  69. package/src/adapters/loop-row-type.ts +10 -0
  70. package/src/adapters/nil-reading.ts +23 -25
  71. package/src/adapters/non-primitive-equality.ts +1 -1
  72. package/src/adapters/number-value.ts +26 -2
  73. package/src/adapters/parsed-expr-emitter.ts +31 -2
  74. package/src/adapters/primitive-plan.ts +84 -15
  75. package/src/adapters/prop-classes.ts +16 -3
  76. package/src/analyzer.ts +27 -7
  77. package/src/index.ts +5 -12
  78. package/src/ir-to-client-js/child-props-object.ts +140 -6
  79. package/src/ir-to-client-js/collect-elements.ts +7 -14
  80. package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +4 -2
  81. package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +4 -2
  82. package/src/ir-to-client-js/html-template.ts +18 -9
  83. package/src/ir-to-client-js/imports.ts +1 -1
  84. package/src/ir-to-client-js/phases/props-extraction.ts +11 -7
  85. package/src/ir-to-client-js/plan/build-declaration-emit.ts +40 -7
  86. package/src/ir-to-client-js/prune-unused-prop-extractions.ts +58 -5
  87. package/src/ir-to-client-js/rewrite-destructured-props.ts +31 -1
  88. package/src/jsx-to-ir.ts +113 -60
  89. package/src/nullable-signal-attr.ts +11 -138
  90. package/src/props-binding.ts +84 -19
  91. package/src/ssr-defaults.ts +100 -11
  92. package/src/types.ts +11 -0
  93. package/dist/adapters/ambiguous-nil-arithmetic.d.ts +0 -42
  94. package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +0 -1
  95. package/src/adapters/ambiguous-nil-arithmetic.ts +0 -103
@@ -47,10 +47,19 @@ export interface SsrDefault {
47
47
  /**
48
48
  * Static fallback value when neither the caller's props nor the
49
49
  * adapter's own derivation supplies one. JSON-encodable: number,
50
- * string, boolean, null, or a tree of plain objects / arrays. `null`
51
- * when the source expression isn't statically evaluable.
50
+ * string, boolean, null, or a tree of plain objects / arrays, in which
51
+ * an `undefined` array element or object member is the sentinel
52
+ * `{"$undef": true}` (`UNDEFINED_SENTINEL`) that every runtime's seed
53
+ * decodes to its undefined value, and an object key that begins with `$`
54
+ * carries one more `$` (`encodeUndefinedMembers`). `null` when the source expression
55
+ * isn't statically evaluable.
56
+ *
57
+ * Absent on a prop entry whose prop has no destructure default
58
+ * (`spec/js-undefined-value.md`, "Shared: the seed and the default"):
59
+ * there is no default to fall back to, and every reader seeds such an
60
+ * entry with its nil, as {@link deriveStashFromDefaults} does.
52
61
  */
53
- value: unknown
62
+ value?: unknown
54
63
  /**
55
64
  * When set, the manifest consumer prefers `props[propName]` over
56
65
  * `value`. Mirrors destructured-prop defaults
@@ -135,18 +144,28 @@ export function deriveStashFromDefaults(
135
144
  continue
136
145
  }
137
146
  if (d.isRestProps) {
138
- extra[name] = name in props ? props[name] : d.value
147
+ extra[name] = name in props ? props[name] : fallbackOf(d)
139
148
  continue
140
149
  }
141
150
  if (d.propName !== undefined && d.propName in props && props[d.propName] != null) {
142
151
  extra[name] = props[d.propName]
143
152
  } else {
144
- extra[name] = d.value
153
+ extra[name] = fallbackOf(d)
145
154
  }
146
155
  }
147
156
  return extra
148
157
  }
149
158
 
159
+ /**
160
+ * An entry's static fallback: its `value`, or `null` when it has none (a
161
+ * prop with no default), as every runtime port reads a missing `value`
162
+ * (Python `d.get("value")`, Perl `$d->{value}`, PHP `?? null`, Rust
163
+ * `unwrap_or(Null)`, Ruby `d[:value]`, Java `d.get("value")`).
164
+ */
165
+ function fallbackOf(d: SsrDefault): unknown {
166
+ return 'value' in d ? decodeUndefinedMembers(d.value) : null
167
+ }
168
+
150
169
  const UNRESOLVED = Symbol('unresolved')
151
170
  type EvalResult = unknown | typeof UNRESOLVED
152
171
 
@@ -199,8 +218,11 @@ function addPropSsrDefaults(
199
218
  // No destructure default — the template reads the prop as-is;
200
219
  // Perl's `//` operator decides what `undef` becomes. The entry
201
220
  // still matters so the template variable exists and consumers
202
- // can supply the propName even with no static fallback.
203
- out[p.name] = { propName: callerPropName, value: null }
221
+ // can supply the propName even with no static fallback. It has no
222
+ // `value` (`spec/js-undefined-value.md`, "Shared: the seed and the
223
+ // default"): every runtime's seed reads a missing `value` as its
224
+ // nil, as it read the `null` this used to write.
225
+ out[p.name] = { propName: callerPropName }
204
226
  }
205
227
  }
206
228
  }
@@ -456,7 +478,72 @@ export function extractSsrDefaults(metadata: IRMetadata): Record<string, SsrDefa
456
478
  }
457
479
  }
458
480
 
459
- return Object.keys(out).length === 0 ? undefined : out
481
+ return Object.keys(out).length === 0 ? undefined : encodeSeedValues(out)
482
+ }
483
+
484
+ /**
485
+ * Each entry's `value` through {@link encodeUndefinedMembers}, in place.
486
+ * A getter alias shares its origin's entry object, so each distinct entry
487
+ * is encoded exactly once: encoding twice would escape the first pass's
488
+ * sentinels and `$`-prefixed keys again.
489
+ */
490
+ function encodeSeedValues(out: Record<string, SsrDefault>): Record<string, SsrDefault> {
491
+ for (const d of new Set(Object.values(out))) {
492
+ if ('value' in d) d.value = encodeUndefinedMembers(d.value)
493
+ }
494
+ return out
495
+ }
496
+
497
+ /**
498
+ * The manifest's spelling of JS `undefined` inside a seed value: the
499
+ * one-key sentinel the golden vectors use (`spec/js-undefined-value.md`,
500
+ * "JSON encoding for vectors"). JSON has no `undefined`, and dropping it
501
+ * would turn an array element into `null` and a present member into a
502
+ * missing key.
503
+ */
504
+ export const UNDEFINED_SENTINEL: Readonly<{ $undef: true }> = Object.freeze({ $undef: true })
505
+
506
+ /**
507
+ * `v` with each `undefined` array element and object member replaced by
508
+ * {@link UNDEFINED_SENTINEL}, as a new tree; a top-level `undefined` stays
509
+ * itself (the entry then has no `value`, which every seed reads as its
510
+ * undefined value). So that seed data shaped like the sentinel stays data,
511
+ * every object key that begins with `$` gets one more `$` (`$undef` to
512
+ * `$$undef`, `$$x` to `$$$x`): the only object left with the single key
513
+ * `$undef` is the sentinel, and {@link decodeUndefinedMembers} inverts the
514
+ * encoding exactly.
515
+ */
516
+ export function encodeUndefinedMembers(v: unknown): unknown {
517
+ const member = (x: unknown): unknown => (x === undefined ? { ...UNDEFINED_SENTINEL } : encodeUndefinedMembers(x))
518
+ if (Array.isArray(v)) return v.map(member)
519
+ if (v !== null && typeof v === 'object') {
520
+ return Object.fromEntries(
521
+ Object.entries(v as Record<string, unknown>).map(([k, x]) => [k.startsWith('$') ? `$${k}` : k, member(x)]),
522
+ )
523
+ }
524
+ return v
525
+ }
526
+
527
+ /**
528
+ * {@link encodeUndefinedMembers} inverted: each sentinel (an object whose
529
+ * only key is `$undef`, holding `true`) becomes `undefined` again, and each
530
+ * key that begins with `$$` loses one leading `$`.
531
+ */
532
+ export function decodeUndefinedMembers(v: unknown): unknown {
533
+ if (isUndefinedSentinel(v)) return undefined
534
+ if (Array.isArray(v)) return v.map(decodeUndefinedMembers)
535
+ if (v !== null && typeof v === 'object') {
536
+ return Object.fromEntries(
537
+ Object.entries(v as Record<string, unknown>).map(([k, x]) => [k.startsWith('$$') ? k.slice(1) : k, decodeUndefinedMembers(x)]),
538
+ )
539
+ }
540
+ return v
541
+ }
542
+
543
+ function isUndefinedSentinel(v: unknown): boolean {
544
+ if (v === null || typeof v !== 'object' || Array.isArray(v)) return false
545
+ const keys = Object.keys(v)
546
+ return keys.length === 1 && keys[0] === '$undef' && (v as { $undef: unknown }).$undef === true
460
547
  }
461
548
 
462
549
  /**
@@ -736,7 +823,9 @@ function evalNode(node: ts.Expression, ctx: EvalContext): EvalResult {
736
823
  }
737
824
  const v = evalNode(prop.initializer, ctx)
738
825
  if (v === UNRESOLVED) return UNRESOLVED
739
- obj[key] = v === undefined ? null : v
826
+ // An `undefined` member is a present key holding `undefined`; the
827
+ // manifest carries it as `{"$undef": true}` (`encodeUndefinedMembers`).
828
+ obj[key] = v
740
829
  }
741
830
  return obj
742
831
  }
@@ -747,7 +836,7 @@ function evalNode(node: ts.Expression, ctx: EvalContext): EvalResult {
747
836
  if (ts.isOmittedExpression(elem)) return UNRESOLVED
748
837
  const v = evalNode(elem, ctx)
749
838
  if (v === UNRESOLVED) return UNRESOLVED
750
- arr.push(v === undefined ? null : v)
839
+ arr.push(v)
751
840
  }
752
841
  return arr
753
842
  }
@@ -830,7 +919,7 @@ function evalNode(node: ts.Expression, ctx: EvalContext): EvalResult {
830
919
  const localBindings: Record<string, EvalResult> = { ...ctx.bindings, [paramName]: item }
831
920
  const v = evalNode(arrow.body as ts.Expression, { ...ctx, bindings: localBindings })
832
921
  if (v === UNRESOLVED) return UNRESOLVED
833
- mapped.push(v === undefined ? null : v)
922
+ mapped.push(v)
834
923
  }
835
924
  return mapped
836
925
  }
package/src/types.ts CHANGED
@@ -1487,6 +1487,17 @@ export interface ExpressionAttr {
1487
1487
  * differ, and the adapter then falls back to `expr`.
1488
1488
  */
1489
1489
  typed?: TypedExprSource
1490
+ /**
1491
+ * Set on a component prop whose value is a bare read of a const holding a
1492
+ * fully static array or object literal (`<Kid xs={QS} />` beside
1493
+ * `const QS = ['a', 'b']`): that literal, as source text and parsed tree.
1494
+ * A template receives the props, not the module's or the component's
1495
+ * consts, so a template adapter passes the literal, as if the source had
1496
+ * written it in place (`templatePropValue`). JS-runtime consumers (Hono,
1497
+ * the client JS) keep `expr`, so the child receives the const's own value
1498
+ * and its identity.
1499
+ */
1500
+ staticConstLiteral?: { expr: JsSourceText; parsed: ParsedExpr }
1490
1501
  }
1491
1502
 
1492
1503
  /** See {@link ExpressionAttr.typed}. */
@@ -1,42 +0,0 @@
1
- import { type ParsedExpr } from '../expression-parser.ts';
2
- import { type ArithmeticCoercion, type NullishAttrContext } from '../nullable-signal-attr.ts';
3
- import { BindingScope } from '../scope/binding-scope.ts';
4
- import type { CompilerError, MemoInfo } from '../types.ts';
5
- /**
6
- * Where a template adapter records the refusal of an arithmetic operand
7
- * whose nil the declared type cannot classify (#3700).
8
- */
9
- export interface AmbiguousNilArithmeticSink {
10
- readonly errors: CompilerError[];
11
- readonly componentName: string;
12
- }
13
- /**
14
- * The shared arithmetic-operand decision for a template adapter's emission
15
- * site: the coercion the adapter spells (`arithmeticOperandCoercion`), after
16
- * refusing (BF101) an operand that reads a number prop declared both
17
- * optional and nullable (`n?: number | null`, `arithmeticOperandNilIsAmbiguous`).
18
- * JS reads that prop as `undefined` when omitted (`NaN` in arithmetic) and
19
- * as `0` when `null`; the engine holds both as one nil, so any spelling
20
- * renders one of them wrong. Under a comparison the same holds for a string
21
- * or boolean prop declared `?: string | null` / `?: boolean | null` (#3824
22
- * step 3). Asked at the emission site so the operand is classified in the
23
- * scope it is read in (`isShadowed`). One error per operand text per compile.
24
- */
25
- export declare function arithmeticCoercionOrRefuse(op: string, operand: ParsedExpr, other: ParsedExpr | undefined, ctx: NullishAttrContext, isShadowed: (name: string) => boolean, sink: AmbiguousNilArithmeticSink): ArithmeticCoercion;
26
- /**
27
- * Refuse (BF101) each arithmetic operand in a whole expression that a
28
- * template adapter lowers without asking {@link arithmeticCoercionOrRefuse}
29
- * per operator: a `.filter()` predicate, an expression the runtime evaluator
30
- * runs, a condition lowered by its own renderer (`ambiguousNilArithmeticOperands`).
31
- * `scope` is the scope the expression is read in.
32
- */
33
- export declare function refuseAmbiguousNilArithmetic(expr: ParsedExpr, ctx: NullishAttrContext, scope: BindingScope, sink: AmbiguousNilArithmeticSink): void;
34
- /**
35
- * Refuse the ambiguous arithmetic in the component's memo bodies. A template
36
- * adapter seeds every memo at SSR, and some seed a memo through a path that
37
- * never reaches its operator emitter (a static seed after a speculative
38
- * lowering, a constructor-time evaluation), so the bodies are checked as a
39
- * whole, in the component scope they are declared in.
40
- */
41
- export declare function refuseAmbiguousNilMemoArithmetic(memos: readonly MemoInfo[], ctx: NullishAttrContext, sink: AmbiguousNilArithmeticSink): void;
42
- //# sourceMappingURL=ambiguous-nil-arithmetic.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ambiguous-nil-arithmetic.d.ts","sourceRoot":"","sources":["../../src/adapters/ambiguous-nil-arithmetic.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,UAAU,EAAE,MAAM,yBAAyB,CAAA;AACvE,OAAO,EAKL,KAAK,kBAAkB,EACvB,KAAK,kBAAkB,EACxB,MAAM,4BAA4B,CAAA;AACnC,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAE1D;;;GAGG;AACH,MAAM,WAAW,0BAA0B;IACzC,QAAQ,CAAC,MAAM,EAAE,aAAa,EAAE,CAAA;IAChC,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAA;CAC/B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,0BAA0B,CACxC,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,UAAU,EACnB,KAAK,EAAE,UAAU,GAAG,SAAS,EAC7B,GAAG,EAAE,kBAAkB,EACvB,UAAU,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,EACrC,IAAI,EAAE,0BAA0B,GAC/B,kBAAkB,CAIpB;AAED;;;;;;GAMG;AACH,wBAAgB,4BAA4B,CAC1C,IAAI,EAAE,UAAU,EAChB,GAAG,EAAE,kBAAkB,EACvB,KAAK,EAAE,YAAY,EACnB,IAAI,EAAE,0BAA0B,GAC/B,IAAI,CAIN;AAED;;;;;;GAMG;AACH,wBAAgB,gCAAgC,CAC9C,KAAK,EAAE,SAAS,QAAQ,EAAE,EAC1B,GAAG,EAAE,kBAAkB,EACvB,IAAI,EAAE,0BAA0B,GAC/B,IAAI,CAIN"}
@@ -1,103 +0,0 @@
1
- import { exprToString, type ParsedExpr } from '../expression-parser.ts'
2
- import {
3
- ambiguousNilFamily,
4
- ambiguousNilOperands,
5
- arithmeticOperandCoercion,
6
- type AmbiguousNilFamily,
7
- type ArithmeticCoercion,
8
- type NullishAttrContext,
9
- } from '../nullable-signal-attr.ts'
10
- import { BindingScope } from '../scope/binding-scope.ts'
11
- import type { CompilerError, MemoInfo } from '../types.ts'
12
-
13
- /**
14
- * Where a template adapter records the refusal of an arithmetic operand
15
- * whose nil the declared type cannot classify (#3700).
16
- */
17
- export interface AmbiguousNilArithmeticSink {
18
- readonly errors: CompilerError[]
19
- readonly componentName: string
20
- }
21
-
22
- /**
23
- * The shared arithmetic-operand decision for a template adapter's emission
24
- * site: the coercion the adapter spells (`arithmeticOperandCoercion`), after
25
- * refusing (BF101) an operand that reads a number prop declared both
26
- * optional and nullable (`n?: number | null`, `arithmeticOperandNilIsAmbiguous`).
27
- * JS reads that prop as `undefined` when omitted (`NaN` in arithmetic) and
28
- * as `0` when `null`; the engine holds both as one nil, so any spelling
29
- * renders one of them wrong. Under a comparison the same holds for a string
30
- * or boolean prop declared `?: string | null` / `?: boolean | null` (#3824
31
- * step 3). Asked at the emission site so the operand is classified in the
32
- * scope it is read in (`isShadowed`). One error per operand text per compile.
33
- */
34
- export function arithmeticCoercionOrRefuse(
35
- op: string,
36
- operand: ParsedExpr,
37
- other: ParsedExpr | undefined,
38
- ctx: NullishAttrContext,
39
- isShadowed: (name: string) => boolean,
40
- sink: AmbiguousNilArithmeticSink,
41
- ): ArithmeticCoercion {
42
- const family = ambiguousNilFamily(op, operand, other, ctx, isShadowed)
43
- if (family !== null) recordAmbiguousNilArithmetic(exprToString(operand), family, sink)
44
- return arithmeticOperandCoercion(op, operand, other, ctx, isShadowed)
45
- }
46
-
47
- /**
48
- * Refuse (BF101) each arithmetic operand in a whole expression that a
49
- * template adapter lowers without asking {@link arithmeticCoercionOrRefuse}
50
- * per operator: a `.filter()` predicate, an expression the runtime evaluator
51
- * runs, a condition lowered by its own renderer (`ambiguousNilArithmeticOperands`).
52
- * `scope` is the scope the expression is read in.
53
- */
54
- export function refuseAmbiguousNilArithmetic(
55
- expr: ParsedExpr,
56
- ctx: NullishAttrContext,
57
- scope: BindingScope,
58
- sink: AmbiguousNilArithmeticSink,
59
- ): void {
60
- for (const { operand, family } of ambiguousNilOperands(expr, ctx, scope)) {
61
- recordAmbiguousNilArithmetic(exprToString(operand), family, sink)
62
- }
63
- }
64
-
65
- /**
66
- * Refuse the ambiguous arithmetic in the component's memo bodies. A template
67
- * adapter seeds every memo at SSR, and some seed a memo through a path that
68
- * never reaches its operator emitter (a static seed after a speculative
69
- * lowering, a constructor-time evaluation), so the bodies are checked as a
70
- * whole, in the component scope they are declared in.
71
- */
72
- export function refuseAmbiguousNilMemoArithmetic(
73
- memos: readonly MemoInfo[],
74
- ctx: NullishAttrContext,
75
- sink: AmbiguousNilArithmeticSink,
76
- ): void {
77
- for (const memo of memos) {
78
- if (memo.parsed) refuseAmbiguousNilArithmetic(memo.parsed, ctx, BindingScope.EMPTY, sink)
79
- }
80
- }
81
-
82
- function recordAmbiguousNilArithmetic(operandText: string, family: AmbiguousNilFamily, sink: AmbiguousNilArithmeticSink): void {
83
- const message = ambiguousNilMessage(operandText, family)
84
- if (sink.errors.some(e => e.code === 'BF101' && e.message === message)) return
85
- sink.errors.push({
86
- code: 'BF101',
87
- severity: 'error',
88
- message,
89
- loc: { file: `${sink.componentName}.tsx`, start: { line: 1, column: 0 }, end: { line: 1, column: 0 } },
90
- suggestion: {
91
- message: `Declare the prop as only optional (\`?: ${family}\`) or only nullable (\`: ${family} | null\`), or give it a default value.`,
92
- escape: [{ kind: 'rewrite' }],
93
- },
94
- })
95
- }
96
-
97
- /** The BF101 text for an operand {@link ambiguousNilFamily} matched; the numeric one predates #3824 step 3. */
98
- function ambiguousNilMessage(operandText: string, family: AmbiguousNilFamily): string {
99
- if (family === 'number') {
100
- return `Arithmetic over \`${operandText}\` reads a number prop declared both optional and nullable (\`?: number | null\`). JS reads it as \`undefined\` when the caller omits it (\`NaN\` in arithmetic) and as \`0\` when it is \`null\`, but a template engine holds both as the same nil, so the server render cannot tell which one it is.`
101
- }
102
- return `A comparison over \`${operandText}\` reads a ${family} prop declared both optional and nullable (\`?: ${family} | null\`). JS reads it as \`undefined\` when the caller omits it (\`NaN\`, so the comparison is false) and as \`null\` when it is \`null\` (\`0\`), but a template engine holds both as the same nil, so the server render cannot tell which one it is.`
103
- }