@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
@@ -0,0 +1,295 @@
1
+ /**
2
+ * The nil-reading rewrite (`spec/js-primitive-semantics.md`, "Nil reading is
3
+ * a tree rewrite"; #3824 step 4a in `spec/js-undefined-value.md`).
4
+ *
5
+ * Every runtime carries JS `null` and `undefined` as two values
6
+ * (`spec/js-undefined-value.md`), and its primitive helpers (`to_number`,
7
+ * `to_string`, `plus`, the relational ones) read each exactly. Where
8
+ * Phase-1 types say which value a nil stands for, and the consumer's
9
+ * coercion is known statically, the operand is rewritten before planning so
10
+ * that it is never nil:
11
+ *
12
+ * | Consumer | nil reads as `null` | nil reads as `undefined` |
13
+ * | --- | --- | --- |
14
+ * | numeric (`- * / %`, unary `-` / `+`, relational, numeric `+`) | `x ?? 0` | `x ?? NaN` |
15
+ * | string (`+` concatenation, template-literal part) | `x ?? 'null'` | `x ?? 'undefined'` |
16
+ *
17
+ * After the rewrite the operand is non-nil, so the plan can name the native
18
+ * operator (`(n ?? NaN) * 2` stays native).
19
+ *
20
+ * A `+` whose present branch concatenates and whose nil branch adds
21
+ * (`o.id + 1` with `id?: string`) is split on nil by the numeric row:
22
+ * `(o.id ?? NaN) + 1` is `NaN` for an omitted field and `'o1'` for a present
23
+ * one, because the operand is then a string or a number and the plan takes
24
+ * the runtime's `plus`, which decides per value. With a `null` reading the
25
+ * nil branch is `0 + 1`, as JS's `null + 1`.
26
+ *
27
+ * What the rewrite leaves alone, to the runtime's helpers, which read the
28
+ * value, `null` or `UNDEF`, exactly:
29
+ *
30
+ * - An operand whose nil may be either (`n?: number | null`,
31
+ * `c?: string | null`).
32
+ * - A `+` whose other operand is neither a proven number nor a proven
33
+ * string: numeric addition or concatenation is decided at run time
34
+ * (the runtime's `plus`).
35
+ * - A nil Phase 1 cannot type (an index read, an untyped value): a missing
36
+ * key or an out-of-range index is `UNDEF`, data `null` is `null`.
37
+ */
38
+
39
+ import type { ParsedExpr, TemplatePart } from '../expression-parser.ts'
40
+ import { operandFacts } from './primitive-plan.ts'
41
+ import type { NilReading, OperandFacts, ScopedOperandTyping } from './operand-typing.ts'
42
+
43
+ const NUMERIC_BINARY_OPS: ReadonlySet<string> = new Set(['-', '*', '/', '%', '**', '<', '<=', '>', '>='])
44
+
45
+ /** The literal `NaN`, which every emitter spells through its runtime's `number('NaN')`. */
46
+ export const NAN_LITERAL: ParsedExpr = { kind: 'literal', value: Number.NaN, literalType: 'number', raw: 'NaN' }
47
+
48
+ const ZERO_LITERAL: ParsedExpr = { kind: 'literal', value: 0, literalType: 'number', raw: '0' }
49
+
50
+ function stringLiteral(value: string): ParsedExpr {
51
+ return { kind: 'literal', value, literalType: 'string' }
52
+ }
53
+
54
+ /** The reading of `facts`' nil, or null when the value is never nil or Phase 1 proves nothing about it. */
55
+ function readingOf(facts: OperandFacts): NilReading | null {
56
+ if (facts.types.has('unknown') || !facts.types.has('nil')) return null
57
+ return facts.nil ?? null
58
+ }
59
+
60
+ /** The types `facts` allow besides nil. */
61
+ function presentTypes(facts: OperandFacts): Set<string> {
62
+ const types = new Set<string>(facts.types)
63
+ types.delete('nil')
64
+ return types
65
+ }
66
+
67
+ /** The `??` nodes the rewrite builds, so a target can check their left operand can hold nil. */
68
+ const NIL_READINGS = new WeakSet<ParsedExpr>()
69
+
70
+ /**
71
+ * Whether `l ?? r` was built by the rewrite (its right operand is the
72
+ * rewrite's fallback). A target whose host types an absent value as a
73
+ * concrete zero (Go) must give `l` a nillable representation, or refuse:
74
+ * the nil test would see a present `''` / `0` / `false` as absent.
75
+ */
76
+ export function isNilReadingFallback(right: ParsedExpr): boolean {
77
+ return NIL_READINGS.has(right)
78
+ }
79
+
80
+ /** `operand ?? fallback`; the fallback alone when the operand can only be nil. */
81
+ function coalesce(operand: ParsedExpr, facts: OperandFacts, fallback: ParsedExpr): ParsedExpr {
82
+ if (presentTypes(facts).size === 0) return fallback
83
+ const right: ParsedExpr = { ...fallback }
84
+ NIL_READINGS.add(right)
85
+ return { kind: 'logical', op: '??', left: operand, right }
86
+ }
87
+
88
+ /** An operand of a numeric consumer: its nil as `0` (`null`) or `NaN` (`undefined`). */
89
+ function numericOperand(operand: ParsedExpr, facts: OperandFacts): ParsedExpr {
90
+ const reading = readingOf(facts)
91
+ if (reading === 'null') return coalesce(operand, facts, ZERO_LITERAL)
92
+ if (reading === 'undefined') return coalesce(operand, facts, NAN_LITERAL)
93
+ return operand
94
+ }
95
+
96
+ /**
97
+ * An operand of a string consumer: its nil as `'null'` / `'undefined'`; an
98
+ * `either` operand is left to the runtime's helper.
99
+ */
100
+ function stringOperand(operand: ParsedExpr, facts: OperandFacts): ParsedExpr {
101
+ const reading = readingOf(facts)
102
+ if (reading === null || reading === 'either') return operand
103
+ return coalesce(operand, facts, stringLiteral(reading))
104
+ }
105
+
106
+ const PRIMITIVE_TYPES: ReadonlySet<string> = new Set(['number', 'string', 'boolean'])
107
+
108
+ /** Whether `facts` prove exactly the one type `type`, never nil. */
109
+ function isExactly(facts: OperandFacts, type: string): boolean {
110
+ return facts.types.size === 1 && facts.types.has(type as never)
111
+ }
112
+
113
+ /**
114
+ * One operand of a `+` whose other operand has `other` facts: a string
115
+ * consumer when the other is a proven string; a numeric consumer (the nil
116
+ * split, for a string operand) when the other is a proven number and this
117
+ * one is a primitive; left alone otherwise.
118
+ */
119
+ function plusOperand(operand: ParsedExpr, facts: OperandFacts, other: OperandFacts): ParsedExpr {
120
+ if (isExactly(other, 'string')) return stringOperand(operand, facts)
121
+ if (!isExactly(other, 'number')) return operand
122
+ const present = presentTypes(facts)
123
+ return [...present].every(t => PRIMITIVE_TYPES.has(t)) ? numericOperand(operand, facts) : operand
124
+ }
125
+
126
+ function rewriteBinary(expr: Extract<ParsedExpr, { kind: 'binary' }>, typing: ScopedOperandTyping): ParsedExpr {
127
+ const lf = operandFacts(expr.left, typing)
128
+ const rf = operandFacts(expr.right, typing)
129
+ let left = expr.left
130
+ let right = expr.right
131
+ if (NUMERIC_BINARY_OPS.has(expr.op)) {
132
+ left = numericOperand(expr.left, lf)
133
+ right = numericOperand(expr.right, rf)
134
+ } else if (expr.op === '+') {
135
+ left = plusOperand(expr.left, lf, rf)
136
+ right = plusOperand(expr.right, rf, lf)
137
+ }
138
+ return left === expr.left && right === expr.right ? expr : { ...expr, left, right }
139
+ }
140
+
141
+ function rewriteTemplateLiteral(
142
+ expr: Extract<ParsedExpr, { kind: 'template-literal' }>,
143
+ typing: ScopedOperandTyping,
144
+ ): ParsedExpr {
145
+ let changed = false
146
+ const parts: TemplatePart[] = expr.parts.map(part => {
147
+ if (part.type !== 'expression') return part
148
+ const rewritten = stringOperand(part.expr, operandFacts(part.expr, typing))
149
+ if (rewritten === part.expr) return part
150
+ changed = true
151
+ return { ...part, expr: rewritten }
152
+ })
153
+ return changed ? { ...expr, parts } : expr
154
+ }
155
+
156
+ /**
157
+ * `expr` with the operands of its own coercion rewritten by their nil
158
+ * reading in `typing`'s scope; `expr` itself when nothing changes. Shallow:
159
+ * only `expr`'s direct operands are rewritten, as `emitParsedExpr` calls it
160
+ * at every node it dispatches. Idempotent: a rewritten operand is never nil.
161
+ */
162
+ export function rewriteNilReadings(expr: ParsedExpr, typing: ScopedOperandTyping): ParsedExpr {
163
+ switch (expr.kind) {
164
+ case 'binary':
165
+ return rewriteBinary(expr, typing)
166
+ case 'unary': {
167
+ if (expr.op !== '-' && expr.op !== '+') return expr
168
+ const argument = numericOperand(expr.argument, operandFacts(expr.argument, typing))
169
+ return argument === expr.argument ? expr : { ...expr, argument }
170
+ }
171
+ case 'template-literal':
172
+ return rewriteTemplateLiteral(expr, typing)
173
+ default:
174
+ return expr
175
+ }
176
+ }
177
+
178
+ /**
179
+ * {@link rewriteNilReadings} at every node of `expr`, children first, for a
180
+ * tree a template adapter hands to the runtime evaluator or to its own
181
+ * walker instead of `emitParsedExpr` (a callback body, a memo body). An
182
+ * arrow's params shadow outer names within its body.
183
+ */
184
+ export function rewriteNilReadingsDeep(expr: ParsedExpr, typing: ScopedOperandTyping): ParsedExpr {
185
+ const walk = (e: ParsedExpr, at: ScopedOperandTyping): ParsedExpr => {
186
+ if (e.kind === 'arrow') {
187
+ const body = walk(e.body, at.enterCallback(e.params))
188
+ return body === e.body ? e : { ...e, body }
189
+ }
190
+ return rewriteNilReadings(mapChildren(e, child => walk(child, at)), at)
191
+ }
192
+ return walk(expr, typing)
193
+ }
194
+
195
+ /** `items` mapped by `f`, or `items` itself when no item changes. */
196
+ function mapList(items: readonly ParsedExpr[], f: (child: ParsedExpr) => ParsedExpr): ParsedExpr[] | null {
197
+ let changed = false
198
+ const out = items.map(item => {
199
+ const next = f(item)
200
+ if (next !== item) changed = true
201
+ return next
202
+ })
203
+ return changed ? out : null
204
+ }
205
+
206
+ function mapTemplateParts(parts: readonly TemplatePart[], f: (child: ParsedExpr) => ParsedExpr): TemplatePart[] | null {
207
+ let changed = false
208
+ const out = parts.map(p => {
209
+ if (p.type !== 'expression') return p
210
+ const expr = f(p.expr)
211
+ if (expr === p.expr) return p
212
+ changed = true
213
+ return { ...p, expr }
214
+ })
215
+ return changed ? out : null
216
+ }
217
+
218
+ type ObjectLiteral = Extract<ParsedExpr, { kind: 'object-literal' }>
219
+
220
+ function mapProperties(properties: ObjectLiteral['properties'], f: (child: ParsedExpr) => ParsedExpr): ObjectLiteral['properties'] | null {
221
+ let changed = false
222
+ const out = properties.map(p => {
223
+ const value = p.kind === 'spread' ? p.expr : p.value
224
+ const next = f(value)
225
+ if (next === value) return p
226
+ changed = true
227
+ return p.kind === 'spread' ? { ...p, expr: next } : { ...p, value: next }
228
+ })
229
+ return changed ? out : null
230
+ }
231
+
232
+ /** `e` with each operand child replaced by `f(child)`; `e` itself when none changes. */
233
+ function mapOperands(e: ParsedExpr, f: (child: ParsedExpr) => ParsedExpr): ParsedExpr | undefined {
234
+ switch (e.kind) {
235
+ case 'binary':
236
+ case 'logical': {
237
+ const left = f(e.left)
238
+ const right = f(e.right)
239
+ return left === e.left && right === e.right ? e : ({ ...e, left, right } as ParsedExpr)
240
+ }
241
+ case 'unary': {
242
+ const argument = f(e.argument)
243
+ return argument === e.argument ? e : { ...e, argument }
244
+ }
245
+ case 'conditional': {
246
+ const test = f(e.test)
247
+ const consequent = f(e.consequent)
248
+ const alternate = f(e.alternate)
249
+ return test === e.test && consequent === e.consequent && alternate === e.alternate ? e : { ...e, test, consequent, alternate }
250
+ }
251
+ default:
252
+ return undefined
253
+ }
254
+ }
255
+
256
+ /** `e` with each value-position child replaced by `f(child)`; `e` itself when none changes. */
257
+ function mapChildren(e: ParsedExpr, f: (child: ParsedExpr) => ParsedExpr): ParsedExpr {
258
+ const operands = mapOperands(e, f)
259
+ if (operands !== undefined) return operands
260
+ switch (e.kind) {
261
+ case 'call': {
262
+ const callee = f(e.callee)
263
+ const args = mapList(e.args, f)
264
+ return callee === e.callee && !args ? e : { ...e, callee, args: args ?? e.args }
265
+ }
266
+ case 'member': {
267
+ const object = f(e.object)
268
+ return object === e.object ? e : { ...e, object }
269
+ }
270
+ case 'index-access': {
271
+ const object = f(e.object)
272
+ const index = f(e.index)
273
+ return object === e.object && index === e.index ? e : { ...e, object, index }
274
+ }
275
+ case 'template-literal': {
276
+ const parts = mapTemplateParts(e.parts, f)
277
+ return parts ? { ...e, parts } : e
278
+ }
279
+ case 'array-literal': {
280
+ const elements = mapList(e.elements, f)
281
+ return elements ? { ...e, elements } : e
282
+ }
283
+ case 'array-method': {
284
+ const object = f(e.object)
285
+ const args = mapList(e.args, f)
286
+ return object === e.object && !args ? e : ({ ...e, object, args: args ?? e.args } as ParsedExpr)
287
+ }
288
+ case 'object-literal': {
289
+ const properties = mapProperties(e.properties, f)
290
+ return properties ? { ...e, properties } : e
291
+ }
292
+ default:
293
+ return e
294
+ }
295
+ }
@@ -0,0 +1,92 @@
1
+ /**
2
+ * The refusal of `===` / `!==` over an operand that may be an object or an
3
+ * array (#3774; `spec/js-primitive-semantics.md`, "Decisions" (4), option
4
+ * (b)).
5
+ *
6
+ * JS compares objects and arrays by reference. A template runtime holds a
7
+ * decoded prop as a value (PHP copy-on-write arrays, Go structs, Ruby and
8
+ * Python containers compared by contents), so no runtime can tell two
9
+ * distinct structures with equal contents apart. Every template adapter
10
+ * therefore refuses the comparison with BF101, as the callback evaluator
11
+ * contract already does (`spec/compiler.md`, "ParsedExpr Evaluator
12
+ * Semantics"). A nullish test (`x === null`, `x === undefined`) is not a
13
+ * strict equality: it plans as the runtime's nil test.
14
+ *
15
+ * The primitive plan names the refusal per node (`mode: 'refuse'`), and
16
+ * every emitter spells the runtime's `strict_eq` for it; the error itself
17
+ * is recorded here, once per component, over every template position, each
18
+ * read in its own scope, and over the memo bodies the adapters seed at SSR.
19
+ */
20
+
21
+ import { exprToString, type ParsedExpr } from '../expression-parser.ts'
22
+ import { parsedExprChildren } from '../action-accessor.ts'
23
+ import { BindingScope } from '../scope/binding-scope.ts'
24
+ import { walkTemplatePositions } from '../template-position-walk.ts'
25
+ import type { ComponentIR, CompilerError, SourceLocation } from '../types.ts'
26
+ import type { OperandTyping, ScopedOperandTyping } from './operand-typing.ts'
27
+ import { isNullishTest, mayHoldObject } from './primitive-plan.ts'
28
+
29
+ /** Where a template adapter records the refusal. */
30
+ export interface NonPrimitiveEqualitySink {
31
+ readonly errors: CompilerError[]
32
+ readonly componentName: string
33
+ }
34
+
35
+ /** Each `===` / `!==` in `expr` with an operand that may be an object, read in `typing`'s scope. */
36
+ function refusedComparisons(expr: ParsedExpr, typing: ScopedOperandTyping, isObjectTypeName: (name: string) => boolean): ParsedExpr[] {
37
+ const found: ParsedExpr[] = []
38
+ const visit = (e: ParsedExpr, at: ScopedOperandTyping): void => {
39
+ if (
40
+ e.kind === 'binary' &&
41
+ (e.op === '===' || e.op === '!==') &&
42
+ !isNullishTest(e.op, e.left, e.right, at) &&
43
+ (mayHoldObject(e.left, at, isObjectTypeName) || mayHoldObject(e.right, at, isObjectTypeName))
44
+ ) {
45
+ found.push(e)
46
+ }
47
+ for (const child of parsedExprChildren(e)) visit(child.expr, child.params ? at.enterCallback(child.params) : at)
48
+ }
49
+ visit(expr, typing)
50
+ return found
51
+ }
52
+
53
+ /**
54
+ * Record BF101 for every `===` / `!==` over an operand that may be an
55
+ * object in `ir`'s template positions and memo bodies. One error per
56
+ * comparison text per compile.
57
+ */
58
+ export function refuseNonPrimitiveStrictEquality(ir: ComponentIR, typing: OperandTyping, sink: NonPrimitiveEqualitySink): void {
59
+ const component = BindingScope.forComponent(ir.metadata.propsObjectName ?? null)
60
+ // A named type is an object when the component defines it with fields
61
+ // (`interface Row { … }`, `type Pt = { … }`); a literal union alias is not.
62
+ const objectTypes = new Set(
63
+ (ir.metadata.typeDefinitions ?? []).filter(td => td.properties !== undefined && !td.stringLiteralUnion).map(td => td.name),
64
+ )
65
+ const isObjectTypeName = (name: string): boolean => objectTypes.has(name)
66
+ const check = (expr: ParsedExpr, scope: BindingScope, loc: SourceLocation): void => {
67
+ for (const comparison of refusedComparisons(expr, typing.at(scope), isObjectTypeName)) record(exprToString(comparison), loc, sink)
68
+ }
69
+ walkTemplatePositions(ir.root, ({ expr, loc, scope }) => check(expr, scope ?? component, loc), component)
70
+ for (const memo of ir.metadata.memos ?? []) {
71
+ if (memo.parsed) check(memo.parsed, component, ir.root.loc)
72
+ }
73
+ }
74
+
75
+ function record(comparison: string, loc: SourceLocation, sink: NonPrimitiveEqualitySink): void {
76
+ const message =
77
+ `\`${comparison}\` compares an object or array with \`===\` / \`!==\`. JS compares objects and arrays by reference, ` +
78
+ 'but a template engine holds a decoded prop as a value and cannot tell two distinct structures with equal contents apart, ' +
79
+ 'so the server render cannot give the JS answer.'
80
+ if (sink.errors.some(e => e.code === 'BF101' && e.message === message)) return
81
+ sink.errors.push({
82
+ code: 'BF101',
83
+ severity: 'error',
84
+ message,
85
+ loc,
86
+ suggestion: {
87
+ message:
88
+ 'Compare a primitive field instead (`a.id === b.id`), or defer the comparison to the client with /* @client */.',
89
+ escape: [{ kind: 'rewrite' }, { kind: 'client-directive' }],
90
+ },
91
+ })
92
+ }
@@ -6,9 +6,11 @@
6
6
  */
7
7
 
8
8
  import type { ParsedExpr } from '../expression-parser.ts'
9
+ import type { StringNameTest } from './parsed-expr-emitter.ts'
9
10
  import type { SsrSeedPlan } from '../ssr-seed-plan.ts'
10
11
  import type { ComponentIR, TypeInfo } from '../types.ts'
11
12
  import { isNumericArithmeticResult } from './parsed-expr-emitter.ts'
13
+ import { isRuntimeNumberLiteral } from './primitive-plan.ts'
12
14
 
13
15
  /**
14
16
  * Number-typed signals, memos, props and same-file local consts: a strict
@@ -36,16 +38,25 @@ export function collectNumberValueNames(ir: Pick<ComponentIR, 'metadata'>): { na
36
38
  if (!ir.metadata.propsObjectName) names.add(p.name)
37
39
  }
38
40
  for (const c of ir.metadata.localConstants) {
39
- if (isNumber(c.type)) names.add(c.name)
41
+ // The analyzer types an exponent literal (`const TINY = 1e-7`) `unknown`;
42
+ // its parsed initializer still says it is a number.
43
+ if (isNumber(c.type) || isNumberLiteralInitializer(c.parsed)) names.add(c.name)
40
44
  }
41
45
  return { names, props }
42
46
  }
43
47
 
48
+ /** A number literal, written negated or not (`1e-7`, `-0`). */
49
+ function isNumberLiteralInitializer(expr: ParsedExpr | undefined): boolean {
50
+ const literal = expr?.kind === 'unary' && expr.op === '-' ? expr.argument : expr
51
+ return literal?.kind === 'literal' && literal.literalType === 'number'
52
+ }
53
+
44
54
  /**
45
55
  * Whether an expression yields a JS NUMBER: arithmetic
46
56
  * ({@link isNumericArithmeticResult}), a read of a name `isNumberName`
47
57
  * accepts — a zero-argument getter call, a `props.<key>` member, or a bare
48
- * identifier — or a ternary with such a branch. Backends whose own number
58
+ * identifier — a number literal that is not a safe integer, or a ternary
59
+ * with such a branch. Backends whose own number
49
60
  * stringification differs from JS (Perl spells the infinities `Inf` and
50
61
  * prints `%.15g`) route such a value through their `Number::toString`
51
62
  * helper wherever JS stringifies it: an output position, or an operand of
@@ -54,11 +65,13 @@ export function collectNumberValueNames(ir: Pick<ComponentIR, 'metadata'>): { na
54
65
  */
55
66
  export function isNumberValuedExpr(
56
67
  expr: ParsedExpr | null | undefined,
57
- isStringName: (n: string, componentProperty?: boolean) => boolean,
68
+ isStringName: StringNameTest,
58
69
  isNumberName: (n: string, componentProperty?: boolean) => boolean,
59
70
  ): boolean {
60
71
  if (!expr) return false
61
72
  if (isNumericArithmeticResult(expr, isStringName)) return true
73
+ // A literal the plan spells through the runtime's `number` (#3824 step 6).
74
+ if (isRuntimeNumberLiteral(expr)) return true
62
75
  // Either branch may be the value printed; the helper passes a string
63
76
  // branch through unchanged.
64
77
  if (expr.kind === 'conditional') {
@@ -81,7 +94,7 @@ export function isNumberValuedExpr(
81
94
  */
82
95
  export function numberValuedSeedNames(
83
96
  plan: SsrSeedPlan,
84
- isStringName: (n: string, componentProperty?: boolean) => boolean,
97
+ isStringName: StringNameTest,
85
98
  isNumberName: (n: string, componentProperty?: boolean) => boolean,
86
99
  ): Set<string> {
87
100
  const names = new Set<string>()