@barefootjs/jsx 0.40.1 → 0.40.2

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 (176) hide show
  1. package/dist/adapters/ambiguous-nil-arithmetic.d.ts +4 -3
  2. package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -1
  3. package/dist/adapters/boolean-result.d.ts +32 -10
  4. package/dist/adapters/boolean-result.d.ts.map +1 -1
  5. package/dist/adapters/brace-delimiter-text.d.ts +18 -0
  6. package/dist/adapters/brace-delimiter-text.d.ts.map +1 -0
  7. package/dist/adapters/callback-method.d.ts +38 -5
  8. package/dist/adapters/callback-method.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 +21 -0
  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 +52 -0
  16. package/dist/adapters/loop-row-type.d.ts.map +1 -0
  17. package/dist/adapters/nil-reading.d.ts +68 -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 +70 -12
  26. package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
  27. package/dist/adapters/primitive-plan.d.ts +291 -0
  28. package/dist/adapters/primitive-plan.d.ts.map +1 -0
  29. package/dist/analyzer-context.d.ts +4 -0
  30. package/dist/analyzer-context.d.ts.map +1 -1
  31. package/dist/analyzer.d.ts.map +1 -1
  32. package/dist/augment-inherited-props.d.ts +63 -8
  33. package/dist/augment-inherited-props.d.ts.map +1 -1
  34. package/dist/compiler.d.ts.map +1 -1
  35. package/dist/event-handler-name.d.ts +12 -0
  36. package/dist/event-handler-name.d.ts.map +1 -1
  37. package/dist/expression-parser.d.ts +8 -0
  38. package/dist/expression-parser.d.ts.map +1 -1
  39. package/dist/index.d.ts +19 -9
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +4628 -2198
  42. package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
  43. package/dist/ir-to-client-js/child-props-object.d.ts +64 -0
  44. package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -0
  45. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  46. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
  47. package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
  48. package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
  49. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +4 -2
  50. package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
  51. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  52. package/dist/ir-to-client-js/imports.d.ts +2 -2
  53. package/dist/ir-to-client-js/imports.d.ts.map +1 -1
  54. package/dist/ir-to-client-js/index.d.ts +2 -6
  55. package/dist/ir-to-client-js/index.d.ts.map +1 -1
  56. package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
  57. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +30 -18
  58. package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
  59. package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
  60. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +3 -2
  61. package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
  62. package/dist/jsx-to-ir.d.ts +1 -0
  63. package/dist/jsx-to-ir.d.ts.map +1 -1
  64. package/dist/nullable-signal-attr.d.ts +68 -9
  65. package/dist/nullable-signal-attr.d.ts.map +1 -1
  66. package/dist/prop-rewrite.d.ts +7 -0
  67. package/dist/prop-rewrite.d.ts.map +1 -1
  68. package/dist/props-object-whole-read.d.ts +15 -0
  69. package/dist/props-object-whole-read.d.ts.map +1 -0
  70. package/dist/rich-type-refusal.d.ts.map +1 -1
  71. package/dist/scope/binding-scope.d.ts +85 -10
  72. package/dist/scope/binding-scope.d.ts.map +1 -1
  73. package/dist/scope/lexical-scope-walk.d.ts +3 -2
  74. package/dist/scope/lexical-scope-walk.d.ts.map +1 -1
  75. package/dist/scope/root-prop-alias.d.ts +10 -7
  76. package/dist/scope/root-prop-alias.d.ts.map +1 -1
  77. package/dist/strip-types.d.ts +13 -0
  78. package/dist/strip-types.d.ts.map +1 -1
  79. package/dist/template-position-walk.d.ts +21 -3
  80. package/dist/template-position-walk.d.ts.map +1 -1
  81. package/dist/types.d.ts +90 -0
  82. package/dist/types.d.ts.map +1 -1
  83. package/package.json +2 -2
  84. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +3 -3
  85. package/src/__tests__/binding-scope.test.ts +89 -1
  86. package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -0
  87. package/src/__tests__/boolean-result.test.ts +22 -5
  88. package/src/__tests__/brace-delimiter-text.test.ts +26 -0
  89. package/src/__tests__/callback-method.test.ts +62 -2
  90. package/src/__tests__/child-components-in-map.test.ts +1 -1
  91. package/src/__tests__/computed-const.test.ts +103 -0
  92. package/src/__tests__/emit-to-string-call.test.ts +2 -1
  93. package/src/__tests__/hydration-payload-analysis.test.ts +166 -0
  94. package/src/__tests__/issue-2724-prop-alias-loop-not-static.test.ts +7 -7
  95. package/src/__tests__/issue-2754-rest-spread-needs-slot.test.ts +5 -2
  96. package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +1 -1
  97. package/src/__tests__/issue-2835-props-type-member-name-collision.test.ts +4 -4
  98. package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +1 -1
  99. package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +2 -1
  100. package/src/__tests__/issue-3713-props-spread-loop-row.test.ts +65 -0
  101. package/src/__tests__/lookup-literal-const.test.ts +9 -3
  102. package/src/__tests__/loop-index-reactivity.test.ts +2 -2
  103. package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +1 -1
  104. package/src/__tests__/loop-plan.test.ts +1 -1
  105. package/src/__tests__/loop-row-const-initializer-shadow.test.ts +146 -0
  106. package/src/__tests__/loop-row-type.test.ts +64 -0
  107. package/src/__tests__/lowering-value-position.test.ts +4 -0
  108. package/src/__tests__/module-const-destructure-default.test.ts +156 -0
  109. package/src/__tests__/nested-loop-index-param.test.ts +1 -1
  110. package/src/__tests__/nested-loop-reactive-attrs.test.ts +2 -2
  111. package/src/__tests__/nested-row-shadowing.test.ts +2 -2
  112. package/src/__tests__/nil-reading.test.ts +77 -0
  113. package/src/__tests__/nullable-signal-attr.test.ts +134 -0
  114. package/src/__tests__/primitive-plan.test.ts +416 -0
  115. package/src/__tests__/profile-loop-binding-ids.test.ts +2 -2
  116. package/src/__tests__/profile-nested-binding-ids.test.ts +2 -2
  117. package/src/__tests__/props-object-whole-read.test.ts +40 -0
  118. package/src/__tests__/reactive-attrs-in-map.test.ts +1 -1
  119. package/src/__tests__/reactive-factory-inlining.test.ts +4 -1
  120. package/src/__tests__/rest-binding-gaps.test.ts +192 -0
  121. package/src/__tests__/root-prop-alias.test.ts +53 -8
  122. package/src/__tests__/static-loop-csr-materialize.test.ts +2 -2
  123. package/src/__tests__/static-record-literal-array.test.ts +171 -0
  124. package/src/__tests__/string-concat-identifier.test.ts +40 -1
  125. package/src/adapters/ambiguous-nil-arithmetic.ts +22 -13
  126. package/src/adapters/boolean-result.ts +72 -66
  127. package/src/adapters/brace-delimiter-text.ts +25 -0
  128. package/src/adapters/callback-method.ts +151 -27
  129. package/src/adapters/computed-const.ts +238 -4
  130. package/src/adapters/dsl-template-adapter.ts +40 -2
  131. package/src/adapters/loop-bound-names.ts +9 -1
  132. package/src/adapters/loop-plan.ts +1 -1
  133. package/src/adapters/loop-row-type.ts +139 -0
  134. package/src/adapters/nil-reading.ts +297 -0
  135. package/src/adapters/non-primitive-equality.ts +92 -0
  136. package/src/adapters/number-value.ts +17 -4
  137. package/src/adapters/operand-typing.ts +522 -0
  138. package/src/adapters/parsed-expr-emitter.ts +154 -19
  139. package/src/adapters/primitive-plan.ts +696 -0
  140. package/src/analyzer-context.ts +6 -0
  141. package/src/analyzer.ts +505 -19
  142. package/src/augment-inherited-props.ts +108 -10
  143. package/src/compiler.ts +17 -0
  144. package/src/event-handler-name.ts +15 -0
  145. package/src/expression-parser.ts +70 -1
  146. package/src/index.ts +33 -7
  147. package/src/ir-to-client-js/build-references.ts +69 -0
  148. package/src/ir-to-client-js/child-props-object.ts +106 -0
  149. package/src/ir-to-client-js/collect-elements.ts +30 -55
  150. package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +6 -1
  151. package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +29 -28
  152. package/src/ir-to-client-js/control-flow/shared.ts +20 -22
  153. package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +5 -3
  154. package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +2 -2
  155. package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +1 -1
  156. package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +2 -2
  157. package/src/ir-to-client-js/control-flow/stringify/loop.ts +1 -1
  158. package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +3 -3
  159. package/src/ir-to-client-js/html-template.ts +25 -8
  160. package/src/ir-to-client-js/imports.ts +1 -1
  161. package/src/ir-to-client-js/index.ts +53 -11
  162. package/src/ir-to-client-js/plan/build-static-array-child-init.ts +31 -14
  163. package/src/ir-to-client-js/plan/static-array-child-init.ts +27 -18
  164. package/src/ir-to-client-js/prop-handling.ts +22 -3
  165. package/src/ir-to-client-js/stringify/static-array-child-init.ts +26 -10
  166. package/src/jsx-to-ir.ts +446 -37
  167. package/src/nullable-signal-attr.ts +246 -29
  168. package/src/prop-rewrite.ts +30 -6
  169. package/src/props-object-whole-read.ts +87 -0
  170. package/src/rich-type-refusal.ts +2 -1
  171. package/src/scope/binding-scope.ts +182 -18
  172. package/src/scope/lexical-scope-walk.ts +4 -3
  173. package/src/scope/root-prop-alias.ts +15 -8
  174. package/src/strip-types.ts +31 -3
  175. package/src/template-position-walk.ts +57 -28
  176. package/src/types.ts +87 -0
@@ -40,17 +40,8 @@
40
40
 
41
41
  import { parseExpression, type ParsedExpr } from '../expression-parser.ts'
42
42
  import type { IRMetadata, TypeInfo } from '../types.ts'
43
-
44
- const COMPARISON_OPS = new Set([
45
- '<',
46
- '>',
47
- '<=',
48
- '>=',
49
- '==',
50
- '===',
51
- '!=',
52
- '!==',
53
- ])
43
+ import { operandFacts, planTest, type NativeSafeTable, type PrimitivePlan } from './primitive-plan.ts'
44
+ import { isExactly, typingFromNameTest, type ScopedOperandTyping } from './operand-typing.ts'
54
45
 
55
46
  /**
56
47
  * Whether `node` evaluates to a JS boolean: structurally, or, when the
@@ -64,68 +55,66 @@ export function isBooleanResultParsed(
64
55
  node: ParsedExpr,
65
56
  isBooleanName?: (name: string, componentProperty?: boolean) => boolean,
66
57
  ): boolean {
67
- switch (node.kind) {
68
- case 'literal':
69
- return node.literalType === 'boolean'
70
- case 'binary':
71
- return COMPARISON_OPS.has(node.op)
72
- case 'unary':
73
- return node.op === '!'
74
- case 'logical':
75
- // `x > 0 && y < 10` is boolean; `x() || 'fallback'` is not.
76
- // Only both-sides-boolean qualifies.
77
- return (
78
- isBooleanResultParsed(node.left, isBooleanName) && isBooleanResultParsed(node.right, isBooleanName)
79
- )
80
- case 'conditional':
81
- // `cond ? bool : bool` is boolean; `cond ? 'a' : 'b'` is not.
82
- return (
83
- isBooleanResultParsed(node.consequent, isBooleanName) &&
84
- isBooleanResultParsed(node.alternate, isBooleanName)
85
- )
86
- case 'call':
87
- return !!isBooleanName && node.callee.kind === 'identifier' && node.args.length === 0 && isBooleanName(node.callee.name)
88
- case 'member':
89
- return !!isBooleanName && !node.computed && node.object.kind === 'identifier' && node.object.name === 'props' &&
90
- isBooleanName(node.property, true)
91
- case 'identifier':
92
- return !!isBooleanName && isBooleanName(node.name)
93
- default:
94
- return false
95
- }
58
+ // A thin wrapper over the primitive plan's single classifier (#3824 step 0):
59
+ // structurally boolean, or a leaf `isBooleanName` proves boolean, combined
60
+ // through `&&` / `||` / `??` / `?:` only when every value operand is.
61
+ return isExactly(operandFacts(node, typingFromNameTest('boolean', isBooleanName)), 'boolean')
96
62
  }
97
63
 
98
64
  /**
99
- * Rewrites the boolean-result branches of a ternary whose OTHER branch is
100
- * not boolean (`yes() ? false : s()`), so a taken boolean branch carries
101
- * JS `String(boolean)` instead of Perl's `0` / `''` / `1` (#3349). The whole
102
- * value isn't boolean-shaped, so `bool_str` can't wrap it; instead a literal
103
- * `true` / `false` branch becomes the string literal `'true'` / `'false'`,
104
- * and any other boolean-result branch `b` becomes `b ? 'true' : 'false'`.
105
- * The existing ternary lowering then renders the rewritten tree unchanged.
106
- * Nested ternary branches are rewritten recursively; anything that is not
107
- * a mixed ternary is returned as is.
65
+ * Rewrites the boolean-result values a mixed expression can evaluate to, so
66
+ * a taken boolean carries JS `String(boolean)` instead of Perl's `0` / `''`
67
+ * / `1`. The whole value isn't boolean-shaped, so `bool_str` can't wrap it.
68
+ *
69
+ * - A ternary branch (`yes() ? false : s()`, #3349): a literal `true` /
70
+ * `false` becomes the string literal `'true'` / `'false'`, any other
71
+ * boolean-result branch `b` becomes `b ? 'true' : 'false'`.
72
+ * - An operand JS `&&` / `||` / `??` returns (`n > 0 && 'y'`): the left
73
+ * operand is returned only when it decides the result, so its rewritten
74
+ * form `l'` (the operand stringified, a nested chain or a ternary
75
+ * rewritten likewise) is what the expression yields then, while the
76
+ * original operand stays the test: `l && r` becomes `l ? r : l'` and
77
+ * `l || r` becomes `l ? l' : r`. `l ?? r` becomes `l' ?? r`, since the
78
+ * rewrite keeps nullishness. A boolean-result `l` gives `b ? r :
79
+ * 'false'` and `b ? 'true' : r`. The right operand is rewritten as a
80
+ * branch.
81
+ *
82
+ * The existing lowering then renders the rewritten tree unchanged. Nested
83
+ * values are rewritten recursively; a tree with nothing to rewrite is
84
+ * returned as is (the same object). Only an output position may take the
85
+ * rewrite: a `'false'` string is truthy.
108
86
  */
109
- export function stringifyBooleanTernaryBranches(node: ParsedExpr): ParsedExpr {
110
- if (node.kind !== 'conditional' || isBooleanResultParsed(node)) return node
111
- return {
112
- ...node,
113
- consequent: stringifyBooleanBranch(node.consequent),
114
- alternate: stringifyBooleanBranch(node.alternate),
87
+ export function stringifyBooleanBranches(node: ParsedExpr): ParsedExpr {
88
+ if (isBooleanResultParsed(node)) return node
89
+ if (node.kind === 'conditional') {
90
+ const consequent = stringifyBooleanBranch(node.consequent)
91
+ const alternate = stringifyBooleanBranch(node.alternate)
92
+ return consequent === node.consequent && alternate === node.alternate ? node : { ...node, consequent, alternate }
115
93
  }
94
+ if (node.kind !== 'logical') return node
95
+ const right = stringifyBooleanBranch(node.right)
96
+ const left = stringifyBooleanBranch(node.left)
97
+ if (left === node.left) return right === node.right ? node : { ...node, right }
98
+ const wholly = isBooleanResultParsed(node.left)
99
+ // `??` tests nullishness only, which the rewrite keeps: a stringified
100
+ // boolean is as non-nullish as the boolean, and a boolean is never nullish.
101
+ if (node.op === '??') return wholly ? left : { ...node, left, right }
102
+ // `&&` / `||` test the left operand's truthiness, which a `'false'`
103
+ // string would flip, so the original operand stays the test and the
104
+ // rewritten one is what the expression yields when it returns it.
105
+ return node.op === '&&'
106
+ ? { kind: 'conditional', test: node.left, consequent: right, alternate: wholly ? stringLiteral('false') : left }
107
+ : { kind: 'conditional', test: node.left, consequent: wholly ? stringLiteral('true') : left, alternate: right }
108
+ }
109
+
110
+ function stringLiteral(value: string): ParsedExpr {
111
+ return { kind: 'literal', value, literalType: 'string' }
116
112
  }
117
113
 
118
114
  function stringifyBooleanBranch(branch: ParsedExpr): ParsedExpr {
119
- if (!isBooleanResultParsed(branch)) return stringifyBooleanTernaryBranches(branch)
120
- if (branch.kind === 'literal') {
121
- return { kind: 'literal', value: String(branch.value), literalType: 'string' }
122
- }
123
- return {
124
- kind: 'conditional',
125
- test: branch,
126
- consequent: { kind: 'literal', value: 'true', literalType: 'string' },
127
- alternate: { kind: 'literal', value: 'false', literalType: 'string' },
128
- }
115
+ if (!isBooleanResultParsed(branch)) return stringifyBooleanBranches(branch)
116
+ if (branch.kind === 'literal') return stringLiteral(String(branch.value))
117
+ return { kind: 'conditional', test: branch, consequent: stringLiteral('true'), alternate: stringLiteral('false') }
129
118
  }
130
119
 
131
120
 
@@ -211,11 +200,28 @@ export function collectBooleanValueNames(
211
200
  * unparseable expression is not boolean-result.
212
201
  */
213
202
  export function isBooleanResultExpr(expr: string): boolean {
214
- const parsed = parseExpression(expr.trim())
203
+ const parsed = testTree(expr)
215
204
  if (!parsed) return false
216
205
  return isBooleanResultParsed(parsed)
217
206
  }
218
207
 
208
+ /**
209
+ * The ToBoolean plan (`planTest`, #3824) of a test whose `ParsedExpr` the IR
210
+ * does not carry: a template literal's ternary part, which holds only its
211
+ * condition text. It reads the same tree {@link isBooleanResultExpr} always
212
+ * parsed for such a test. A test the IR carries a tree for is planned with
213
+ * `planTest` on that tree.
214
+ */
215
+ export function planTestOfSource(expr: string, typing: ScopedOperandTyping, target: NativeSafeTable): PrimitivePlan | null {
216
+ const parsed = testTree(expr)
217
+ return parsed ? planTest(parsed, typing, target) : null
218
+ }
219
+
220
+ /** The tree of a test held only as source text; null when unparseable. */
221
+ function testTree(expr: string): ParsedExpr | null {
222
+ return parseExpression(expr.trim()) ?? null
223
+ }
224
+
219
225
  /**
220
226
  * True when `expr`'s top-level shape is an explicit one-argument JS
221
227
  * `String(x)` call (the `EVAL_BUILTIN_IDENTS` builtin the compiler
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Static text in a template engine whose tags open with a brace — Jinja,
3
+ * MiniJinja, Twig and Pebble (`{{`, `{%`, `{#`), Go `html/template` (`{{`)
4
+ * — must never put a tag opener into the template: JSX text such as
5
+ * `&#123;&#123; 1 &#125;&#125;` decodes to `{{ 1 }}`, which Hono prints as
6
+ * written but such an engine compiles as a tag, and so prints `1` or fails
7
+ * to parse.
8
+ *
9
+ * Every `{` that is followed by one of `followers` (the characters that turn
10
+ * a `{` into a tag opener on this engine) is emitted through `literal`
11
+ * instead — an output expression for the brace itself (`{{ '{' }}`,
12
+ * `{{"{"}}`). So is a `{` that ends the text: the text cannot know what the
13
+ * adapter writes after it, and a following output tag (`{{ x }}`) would turn
14
+ * that brace into part of a longer opener. The expression renders the same
15
+ * character either way.
16
+ */
17
+ export function escapeBraceDelimiters(text: string, followers: string, literal: string): string {
18
+ let out = ''
19
+ for (let i = 0; i < text.length; i++) {
20
+ const ch = text[i]
21
+ const next = text[i + 1]
22
+ out += ch === '{' && (next === undefined || followers.includes(next)) ? literal : ch
23
+ }
24
+ return out
25
+ }
@@ -16,6 +16,8 @@
16
16
  * runtime binds those.
17
17
  */
18
18
 
19
+ import { rewriteNilReadingsDeep } from './nil-reading.ts'
20
+ import type { ScopedOperandTyping } from './operand-typing.ts'
19
21
  import {
20
22
  freeVarsInBody,
21
23
  hasOpaqueCall,
@@ -83,7 +85,113 @@ export interface CallbackEvalPlan {
83
85
  kind: 'eval'
84
86
  helper: CallbackEvalHelper
85
87
  operands: CallbackEvalOperand[]
86
- captured: string[]
88
+ captured: CallbackCapture[]
89
+ }
90
+
91
+ /**
92
+ * A free name of an evaluator body and the expression whose SSR value the
93
+ * env binds it to: the name itself, or, for a lifted `props.X` read (see
94
+ * {@link evalBody}), that read.
95
+ */
96
+ export interface CallbackCapture {
97
+ name: string
98
+ value: ParsedExpr
99
+ }
100
+
101
+ /** A callback body the runtime evaluator can run: its JSON and its env. */
102
+ export interface EvalBody {
103
+ json: string
104
+ captured: CallbackCapture[]
105
+ }
106
+
107
+ /**
108
+ * Serialize a callback body for the runtime evaluator, with the captures its
109
+ * env needs; `null` when the body is outside the evaluator surface. `bound`
110
+ * are the callback's own params. `propsObject` is the component's props
111
+ * object identifier (`BindingScope.propsObject` / `propsObjectIdentifier`):
112
+ * `props`, or the props parameter's own name (`function C(p)`).
113
+ *
114
+ * A template has no props object: the props are its top-level variables,
115
+ * and the adapters spell `props.X` as the variable `X`. So each `props.X` the
116
+ * body reads, where no param binds `props`, becomes a fresh env name bound to
117
+ * the read itself, which the adapter spells as it spells `props.X` anywhere
118
+ * else. Capturing `props` whole would bind it to an undefined template
119
+ * variable, and the predicate would read every `props.X` as `undefined`.
120
+ */
121
+ export function evalBody(body: ParsedExpr, bound: ReadonlySet<string>, propsObject: string, typing?: ScopedOperandTyping): EvalBody | null {
122
+ // The evaluator reads every nil as `null`: with the enclosing scope's
123
+ // typing, an operand whose nil Phase 1 reads is rewritten first (#3824
124
+ // step 4a), for a caller that reaches here without `emitParsedExpr`.
125
+ const read = typing ? rewriteNilReadingsDeep(body, typing.enterCallback([...bound])) : body
126
+ const lifted = liftPropsReads(read, bound, propsObject)
127
+ const json = serializeParsedExpr(lifted.body)
128
+ if (json === null) return null
129
+ const captured = freeVarsInBody(lifted.body, bound).map((name) => ({
130
+ name,
131
+ value: lifted.values.get(name) ?? { kind: 'identifier' as const, name },
132
+ }))
133
+ return { json, captured }
134
+ }
135
+
136
+ /**
137
+ * `body` with each `<propsObject>.X` read that no arrow param shadows
138
+ * replaced by an identifier no other name in `body` uses, and the read each
139
+ * identifier stands for. A bare props object (`props[key]`) is left as it is.
140
+ */
141
+ function liftPropsReads(
142
+ body: ParsedExpr,
143
+ bound: ReadonlySet<string>,
144
+ propsObject: string,
145
+ ): { body: ParsedExpr; values: Map<string, ParsedExpr> } {
146
+ const values = new Map<string, ParsedExpr>()
147
+ if (bound.has(propsObject)) return { body, values }
148
+ const taken = new Set<string>(bound)
149
+ collectNames(body, taken)
150
+ const aliases = new Map<string, string>()
151
+ const aliasFor = (read: Extract<ParsedExpr, { kind: 'member' }>): string => {
152
+ const existing = aliases.get(read.property)
153
+ if (existing !== undefined) return existing
154
+ let alias = `__bf_props_${read.property.replace(/[^A-Za-z0-9_]/g, '_')}`
155
+ while (taken.has(alias)) alias += '_'
156
+ taken.add(alias)
157
+ aliases.set(read.property, alias)
158
+ values.set(alias, read)
159
+ return alias
160
+ }
161
+ const walk = (value: unknown): unknown => {
162
+ if (Array.isArray(value)) return value.map(walk)
163
+ if (value === null || typeof value !== 'object') return value
164
+ const node = value as { kind?: unknown } & Record<string, unknown>
165
+ if (isPropsRead(node, propsObject)) return { kind: 'identifier', name: aliasFor(node) }
166
+ // An arrow that binds the props object name makes it a different value in its body.
167
+ if (node.kind === 'arrow' && (node.params as string[]).includes(propsObject)) return node
168
+ const out: Record<string, unknown> = {}
169
+ for (const [k, v] of Object.entries(node)) out[k] = walk(v)
170
+ return out
171
+ }
172
+ return { body: walk(body) as ParsedExpr, values }
173
+ }
174
+
175
+ function isPropsRead(node: { kind?: unknown }, propsObject: string): node is Extract<ParsedExpr, { kind: 'member' }> {
176
+ const read = node as Partial<Extract<ParsedExpr, { kind: 'member' }>>
177
+ return (
178
+ read.kind === 'member' &&
179
+ read.object?.kind === 'identifier' &&
180
+ read.object.name === propsObject
181
+ )
182
+ }
183
+
184
+ /** Add every identifier and arrow param in `value` to `into`. */
185
+ function collectNames(value: unknown, into: Set<string>): void {
186
+ if (Array.isArray(value)) {
187
+ for (const v of value) collectNames(v, into)
188
+ return
189
+ }
190
+ if (value === null || typeof value !== 'object') return
191
+ const node = value as { kind?: unknown } & Record<string, unknown>
192
+ if (node.kind === 'identifier' && typeof node.name === 'string') into.add(node.name)
193
+ if (node.kind === 'arrow' && Array.isArray(node.params)) for (const p of node.params as string[]) into.add(p)
194
+ for (const v of Object.values(node)) collectNames(v, into)
87
195
  }
88
196
 
89
197
  /**
@@ -117,20 +225,24 @@ export interface CallbackLoweringOptions {
117
225
  nativePredicate?: boolean
118
226
  }
119
227
 
120
- /** Lower `<recv>.<method>(arrow, ...restArgs)`. */
228
+ /**
229
+ * Lower `<recv>.<method>(arrow, ...restArgs)`. `propsObject`: the
230
+ * component's props object identifier (see {@link evalBody}).
231
+ */
121
232
  export function lowerCallbackMethod(
122
233
  method: string,
123
234
  arrow: Arrow,
124
235
  restArgs: readonly ParsedExpr[],
125
236
  options: CallbackLoweringOptions,
237
+ propsObject: string,
126
238
  ): CallbackPlan {
127
239
  const { adapterName } = options
128
240
  const { body, params } = arrow
129
- if (isPredicateMethod(method)) return lowerPredicate(method, arrow, options)
130
- if (method === 'sort' || method === 'toSorted') return lowerSortComparator(method, arrow, adapterName)
241
+ if (isPredicateMethod(method)) return lowerPredicate(method, arrow, options, propsObject)
242
+ if (method === 'sort' || method === 'toSorted') return lowerSortComparator(method, arrow, adapterName, propsObject)
131
243
  if (method === 'reduce' || method === 'reduceRight') {
132
244
  const init = restArgs[0]
133
- const plan = init ? reduceEvalPlan(body, params, init, method === 'reduceRight' ? 'right' : 'left') : null
245
+ const plan = init ? reduceEvalPlan(body, params, init, method === 'reduceRight' ? 'right' : 'left', propsObject) : null
134
246
  return (
135
247
  plan ?? {
136
248
  kind: 'refuse',
@@ -140,7 +252,7 @@ export function lowerCallbackMethod(
140
252
  )
141
253
  }
142
254
  if (method === 'flatMap' || method === 'map') {
143
- const plan = projectionEvalPlan(method === 'map' ? 'map_eval' : 'flat_map_eval', body, params)
255
+ const plan = projectionEvalPlan(method === 'map' ? 'map_eval' : 'flat_map_eval', body, params, propsObject)
144
256
  return (
145
257
  plan ?? {
146
258
  kind: 'refuse',
@@ -152,7 +264,12 @@ export function lowerCallbackMethod(
152
264
  return { kind: 'receiver' }
153
265
  }
154
266
 
155
- function lowerPredicate(method: PredicateMethod, arrow: Arrow, options: CallbackLoweringOptions): CallbackPlan {
267
+ function lowerPredicate(
268
+ method: PredicateMethod,
269
+ arrow: Arrow,
270
+ options: CallbackLoweringOptions,
271
+ propsObject: string,
272
+ ): CallbackPlan {
156
273
  const param = String(arrow.params[0])
157
274
  const native: CallbackPlan = { kind: 'native-predicate', method, param, predicate: arrow.body }
158
275
  // A native form lowers an opaque call to a read of its callee, silently, and
@@ -162,15 +279,15 @@ function lowerPredicate(method: PredicateMethod, arrow: Arrow, options: Callback
162
279
  options.nativePredicate === true && !hasOpaqueCall(arrow.body) && !readsNestedCallbackIndex(arrow.body)
163
280
  const identity = method === 'filter' && arrow.body.kind === 'identifier' && arrow.body.name === param
164
281
  if (nativeOk && identity) return native
165
- const json = serializeParsedExpr(arrow.body)
166
- if (json !== null) {
282
+ const evaluable = evalBody(arrow.body, new Set(arrow.params.slice(0, 1)), propsObject)
283
+ if (evaluable !== null) {
167
284
  const { helper, forward } = PREDICATE_EVAL[method]
168
285
  const operands: CallbackEvalOperand[] = [
169
- { kind: 'string', value: json },
286
+ { kind: 'string', value: evaluable.json },
170
287
  { kind: 'string', value: param },
171
288
  ]
172
289
  if (forward !== undefined) operands.push({ kind: 'boolean', value: forward })
173
- return { kind: 'eval', helper, operands, captured: freeVarsInBody(arrow.body, new Set(arrow.params.slice(0, 1))) }
290
+ return { kind: 'eval', helper, operands, captured: evaluable.captured }
174
291
  }
175
292
  if (nativeOk) return native
176
293
  return {
@@ -185,8 +302,13 @@ function lowerPredicate(method: PredicateMethod, arrow: Arrow, options: Callback
185
302
  * structured `{keys: …}` descriptor, otherwise BF101. A loop source's sort
186
303
  * (`planLoop`) and `.sort(cmp)` at an expression position decide the same.
187
304
  */
188
- export function lowerSortComparator(method: 'sort' | 'toSorted', arrow: Arrow, adapterName: string): SortPlan {
189
- const plan = sortEvalPlan(arrow.body, arrow.params)
305
+ export function lowerSortComparator(
306
+ method: 'sort' | 'toSorted',
307
+ arrow: Arrow,
308
+ adapterName: string,
309
+ propsObject: string,
310
+ ): SortPlan {
311
+ const plan = sortEvalPlan(arrow.body, arrow.params, propsObject)
190
312
  if (plan) return plan
191
313
  const comparator = sortComparatorFromArrow(arrow)
192
314
  if (comparator) return { kind: 'structured-sort', comparator }
@@ -199,18 +321,18 @@ export function lowerSortComparator(method: 'sort' | 'toSorted', arrow: Arrow, a
199
321
  * has fewer than the two params a comparator needs. The loop-source sort
200
322
  * shares this with `.sort(cmp)` at an expression position.
201
323
  */
202
- export function sortEvalPlan(body: ParsedExpr, params: readonly string[]): CallbackEvalPlan | null {
203
- const json = serializeParsedExpr(body)
204
- if (json === null || params.length < 2) return null
324
+ export function sortEvalPlan(body: ParsedExpr, params: readonly string[], propsObject: string): CallbackEvalPlan | null {
325
+ const evaluable = evalBody(body, new Set(params), propsObject)
326
+ if (evaluable === null || params.length < 2) return null
205
327
  return {
206
328
  kind: 'eval',
207
329
  helper: 'sort_eval',
208
330
  operands: [
209
- { kind: 'string', value: json },
331
+ { kind: 'string', value: evaluable.json },
210
332
  { kind: 'string', value: params[0]! },
211
333
  { kind: 'string', value: params[1]! },
212
334
  ],
213
- captured: freeVarsInBody(body, new Set(params)),
335
+ captured: evaluable.captured,
214
336
  }
215
337
  }
216
338
 
@@ -233,9 +355,10 @@ function reduceEvalPlan(
233
355
  params: readonly string[],
234
356
  init: ParsedExpr,
235
357
  direction: 'left' | 'right',
358
+ propsObject: string,
236
359
  ): CallbackEvalPlan | null {
237
- const json = serializeParsedExpr(body)
238
- if (json === null || init.kind !== 'literal' || params.length < 2) return null
360
+ const evaluable = evalBody(body, new Set(params), propsObject)
361
+ if (evaluable === null || init.kind !== 'literal' || params.length < 2) return null
239
362
  let seed: CallbackEvalOperand
240
363
  if (init.literalType === 'string') seed = { kind: 'string', value: String(init.value) }
241
364
  else if (init.literalType === 'number') seed = { kind: 'number', text: String(init.value) }
@@ -244,13 +367,13 @@ function reduceEvalPlan(
244
367
  kind: 'eval',
245
368
  helper: 'reduce_eval',
246
369
  operands: [
247
- { kind: 'string', value: json },
370
+ { kind: 'string', value: evaluable.json },
248
371
  { kind: 'string', value: params[0]! },
249
372
  { kind: 'string', value: params[1]! },
250
373
  seed,
251
374
  { kind: 'string', value: direction },
252
375
  ],
253
- captured: freeVarsInBody(body, new Set(params)),
376
+ captured: evaluable.captured,
254
377
  }
255
378
  }
256
379
 
@@ -258,17 +381,18 @@ function projectionEvalPlan(
258
381
  helper: 'map_eval' | 'flat_map_eval',
259
382
  body: ParsedExpr,
260
383
  params: readonly string[],
384
+ propsObject: string,
261
385
  ): CallbackEvalPlan | null {
262
- const json = serializeParsedExpr(body)
263
- if (json === null) return null
386
+ const evaluable = evalBody(body, new Set(params.slice(0, 1)), propsObject)
387
+ if (evaluable === null) return null
264
388
  return {
265
389
  kind: 'eval',
266
390
  helper,
267
391
  operands: [
268
- { kind: 'string', value: json },
392
+ { kind: 'string', value: evaluable.json },
269
393
  { kind: 'string', value: String(params[0]) },
270
394
  ],
271
- captured: freeVarsInBody(body, new Set(params.slice(0, 1))),
395
+ captured: evaluable.captured,
272
396
  }
273
397
  }
274
398
 
@@ -300,7 +424,7 @@ export function emitCallbackEval(
300
424
  else if (operand.kind === 'number') operands.push(operand.text)
301
425
  else operands.push(spelling.boolean(operand.value))
302
426
  }
303
- const env = plan.captured.map((name) => [name, emit({ kind: 'identifier', name })] as const)
427
+ const env = plan.captured.map(({ name, value }) => [name, emit(value)] as const)
304
428
  operands.push(spelling.env(env))
305
429
  return spelling.call(plan.helper, operands)
306
430
  }