@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
@@ -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,121 @@ 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
+ * Where an evaluator body is read, for its nil-reading rewrite
109
+ * (`rewriteNilReadingsDeep`): the enclosing scope's operand typing.
110
+ */
111
+ export interface NilReadingSite {
112
+ readonly typing: ScopedOperandTyping
113
+ }
114
+
115
+ /**
116
+ * Serialize a callback body for the runtime evaluator, with the captures its
117
+ * env needs; `null` when the body is outside the evaluator surface. `bound`
118
+ * are the callback's own params. `propsObject` is the component's props
119
+ * object identifier (`BindingScope.propsObject` / `propsObjectIdentifier`):
120
+ * `props`, or the props parameter's own name (`function C(p)`).
121
+ *
122
+ * A template has no props object: the props are its top-level variables,
123
+ * and the adapters spell `props.X` as the variable `X`. So each `props.X` the
124
+ * body reads, where no param binds `props`, becomes a fresh env name bound to
125
+ * the read itself, which the adapter spells as it spells `props.X` anywhere
126
+ * else. Capturing `props` whole would bind it to an undefined template
127
+ * variable, and the predicate would read every `props.X` as `undefined`.
128
+ */
129
+ export function evalBody(body: ParsedExpr, bound: ReadonlySet<string>, propsObject: string, site?: NilReadingSite): EvalBody | null {
130
+ // With the enclosing scope's typing, an operand whose nil Phase 1 reads is
131
+ // rewritten first (#3824 step 4a), for a caller that reaches here without
132
+ // `emitParsedExpr`, as `emitParsedExpr` would.
133
+ const read = site ? rewriteNilReadingsDeep(body, site.typing.enterCallback([...bound])) : body
134
+ const lifted = liftPropsReads(read, bound, propsObject)
135
+ const json = serializeParsedExpr(lifted.body)
136
+ if (json === null) return null
137
+ const captured = freeVarsInBody(lifted.body, bound).map((name) => ({
138
+ name,
139
+ value: lifted.values.get(name) ?? { kind: 'identifier' as const, name },
140
+ }))
141
+ return { json, captured }
142
+ }
143
+
144
+ /**
145
+ * `body` with each `<propsObject>.X` read that no arrow param shadows
146
+ * replaced by an identifier no other name in `body` uses, and the read each
147
+ * identifier stands for. A bare props object (`props[key]`) is left as it is.
148
+ */
149
+ function liftPropsReads(
150
+ body: ParsedExpr,
151
+ bound: ReadonlySet<string>,
152
+ propsObject: string,
153
+ ): { body: ParsedExpr; values: Map<string, ParsedExpr> } {
154
+ const values = new Map<string, ParsedExpr>()
155
+ if (bound.has(propsObject)) return { body, values }
156
+ const taken = new Set<string>(bound)
157
+ collectNames(body, taken)
158
+ const aliases = new Map<string, string>()
159
+ const aliasFor = (read: Extract<ParsedExpr, { kind: 'member' }>): string => {
160
+ const existing = aliases.get(read.property)
161
+ if (existing !== undefined) return existing
162
+ let alias = `__bf_props_${read.property.replace(/[^A-Za-z0-9_]/g, '_')}`
163
+ while (taken.has(alias)) alias += '_'
164
+ taken.add(alias)
165
+ aliases.set(read.property, alias)
166
+ values.set(alias, read)
167
+ return alias
168
+ }
169
+ const walk = (value: unknown): unknown => {
170
+ if (Array.isArray(value)) return value.map(walk)
171
+ if (value === null || typeof value !== 'object') return value
172
+ const node = value as { kind?: unknown } & Record<string, unknown>
173
+ if (isPropsRead(node, propsObject)) return { kind: 'identifier', name: aliasFor(node) }
174
+ // An arrow that binds the props object name makes it a different value in its body.
175
+ if (node.kind === 'arrow' && (node.params as string[]).includes(propsObject)) return node
176
+ const out: Record<string, unknown> = {}
177
+ for (const [k, v] of Object.entries(node)) out[k] = walk(v)
178
+ return out
179
+ }
180
+ return { body: walk(body) as ParsedExpr, values }
181
+ }
182
+
183
+ function isPropsRead(node: { kind?: unknown }, propsObject: string): node is Extract<ParsedExpr, { kind: 'member' }> {
184
+ const read = node as Partial<Extract<ParsedExpr, { kind: 'member' }>>
185
+ return (
186
+ read.kind === 'member' &&
187
+ read.object?.kind === 'identifier' &&
188
+ read.object.name === propsObject
189
+ )
190
+ }
191
+
192
+ /** Add every identifier and arrow param in `value` to `into`. */
193
+ function collectNames(value: unknown, into: Set<string>): void {
194
+ if (Array.isArray(value)) {
195
+ for (const v of value) collectNames(v, into)
196
+ return
197
+ }
198
+ if (value === null || typeof value !== 'object') return
199
+ const node = value as { kind?: unknown } & Record<string, unknown>
200
+ if (node.kind === 'identifier' && typeof node.name === 'string') into.add(node.name)
201
+ if (node.kind === 'arrow' && Array.isArray(node.params)) for (const p of node.params as string[]) into.add(p)
202
+ for (const v of Object.values(node)) collectNames(v, into)
87
203
  }
88
204
 
89
205
  /**
@@ -117,20 +233,24 @@ export interface CallbackLoweringOptions {
117
233
  nativePredicate?: boolean
118
234
  }
119
235
 
120
- /** Lower `<recv>.<method>(arrow, ...restArgs)`. */
236
+ /**
237
+ * Lower `<recv>.<method>(arrow, ...restArgs)`. `propsObject`: the
238
+ * component's props object identifier (see {@link evalBody}).
239
+ */
121
240
  export function lowerCallbackMethod(
122
241
  method: string,
123
242
  arrow: Arrow,
124
243
  restArgs: readonly ParsedExpr[],
125
244
  options: CallbackLoweringOptions,
245
+ propsObject: string,
126
246
  ): CallbackPlan {
127
247
  const { adapterName } = options
128
248
  const { body, params } = arrow
129
- if (isPredicateMethod(method)) return lowerPredicate(method, arrow, options)
130
- if (method === 'sort' || method === 'toSorted') return lowerSortComparator(method, arrow, adapterName)
249
+ if (isPredicateMethod(method)) return lowerPredicate(method, arrow, options, propsObject)
250
+ if (method === 'sort' || method === 'toSorted') return lowerSortComparator(method, arrow, adapterName, propsObject)
131
251
  if (method === 'reduce' || method === 'reduceRight') {
132
252
  const init = restArgs[0]
133
- const plan = init ? reduceEvalPlan(body, params, init, method === 'reduceRight' ? 'right' : 'left') : null
253
+ const plan = init ? reduceEvalPlan(body, params, init, method === 'reduceRight' ? 'right' : 'left', propsObject) : null
134
254
  return (
135
255
  plan ?? {
136
256
  kind: 'refuse',
@@ -140,7 +260,7 @@ export function lowerCallbackMethod(
140
260
  )
141
261
  }
142
262
  if (method === 'flatMap' || method === 'map') {
143
- const plan = projectionEvalPlan(method === 'map' ? 'map_eval' : 'flat_map_eval', body, params)
263
+ const plan = projectionEvalPlan(method === 'map' ? 'map_eval' : 'flat_map_eval', body, params, propsObject)
144
264
  return (
145
265
  plan ?? {
146
266
  kind: 'refuse',
@@ -152,7 +272,12 @@ export function lowerCallbackMethod(
152
272
  return { kind: 'receiver' }
153
273
  }
154
274
 
155
- function lowerPredicate(method: PredicateMethod, arrow: Arrow, options: CallbackLoweringOptions): CallbackPlan {
275
+ function lowerPredicate(
276
+ method: PredicateMethod,
277
+ arrow: Arrow,
278
+ options: CallbackLoweringOptions,
279
+ propsObject: string,
280
+ ): CallbackPlan {
156
281
  const param = String(arrow.params[0])
157
282
  const native: CallbackPlan = { kind: 'native-predicate', method, param, predicate: arrow.body }
158
283
  // A native form lowers an opaque call to a read of its callee, silently, and
@@ -162,15 +287,15 @@ function lowerPredicate(method: PredicateMethod, arrow: Arrow, options: Callback
162
287
  options.nativePredicate === true && !hasOpaqueCall(arrow.body) && !readsNestedCallbackIndex(arrow.body)
163
288
  const identity = method === 'filter' && arrow.body.kind === 'identifier' && arrow.body.name === param
164
289
  if (nativeOk && identity) return native
165
- const json = serializeParsedExpr(arrow.body)
166
- if (json !== null) {
290
+ const evaluable = evalBody(arrow.body, new Set(arrow.params.slice(0, 1)), propsObject)
291
+ if (evaluable !== null) {
167
292
  const { helper, forward } = PREDICATE_EVAL[method]
168
293
  const operands: CallbackEvalOperand[] = [
169
- { kind: 'string', value: json },
294
+ { kind: 'string', value: evaluable.json },
170
295
  { kind: 'string', value: param },
171
296
  ]
172
297
  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))) }
298
+ return { kind: 'eval', helper, operands, captured: evaluable.captured }
174
299
  }
175
300
  if (nativeOk) return native
176
301
  return {
@@ -185,8 +310,13 @@ function lowerPredicate(method: PredicateMethod, arrow: Arrow, options: Callback
185
310
  * structured `{keys: …}` descriptor, otherwise BF101. A loop source's sort
186
311
  * (`planLoop`) and `.sort(cmp)` at an expression position decide the same.
187
312
  */
188
- export function lowerSortComparator(method: 'sort' | 'toSorted', arrow: Arrow, adapterName: string): SortPlan {
189
- const plan = sortEvalPlan(arrow.body, arrow.params)
313
+ export function lowerSortComparator(
314
+ method: 'sort' | 'toSorted',
315
+ arrow: Arrow,
316
+ adapterName: string,
317
+ propsObject: string,
318
+ ): SortPlan {
319
+ const plan = sortEvalPlan(arrow.body, arrow.params, propsObject)
190
320
  if (plan) return plan
191
321
  const comparator = sortComparatorFromArrow(arrow)
192
322
  if (comparator) return { kind: 'structured-sort', comparator }
@@ -199,18 +329,18 @@ export function lowerSortComparator(method: 'sort' | 'toSorted', arrow: Arrow, a
199
329
  * has fewer than the two params a comparator needs. The loop-source sort
200
330
  * shares this with `.sort(cmp)` at an expression position.
201
331
  */
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
332
+ export function sortEvalPlan(body: ParsedExpr, params: readonly string[], propsObject: string): CallbackEvalPlan | null {
333
+ const evaluable = evalBody(body, new Set(params), propsObject)
334
+ if (evaluable === null || params.length < 2) return null
205
335
  return {
206
336
  kind: 'eval',
207
337
  helper: 'sort_eval',
208
338
  operands: [
209
- { kind: 'string', value: json },
339
+ { kind: 'string', value: evaluable.json },
210
340
  { kind: 'string', value: params[0]! },
211
341
  { kind: 'string', value: params[1]! },
212
342
  ],
213
- captured: freeVarsInBody(body, new Set(params)),
343
+ captured: evaluable.captured,
214
344
  }
215
345
  }
216
346
 
@@ -233,9 +363,10 @@ function reduceEvalPlan(
233
363
  params: readonly string[],
234
364
  init: ParsedExpr,
235
365
  direction: 'left' | 'right',
366
+ propsObject: string,
236
367
  ): CallbackEvalPlan | null {
237
- const json = serializeParsedExpr(body)
238
- if (json === null || init.kind !== 'literal' || params.length < 2) return null
368
+ const evaluable = evalBody(body, new Set(params), propsObject)
369
+ if (evaluable === null || init.kind !== 'literal' || params.length < 2) return null
239
370
  let seed: CallbackEvalOperand
240
371
  if (init.literalType === 'string') seed = { kind: 'string', value: String(init.value) }
241
372
  else if (init.literalType === 'number') seed = { kind: 'number', text: String(init.value) }
@@ -244,13 +375,13 @@ function reduceEvalPlan(
244
375
  kind: 'eval',
245
376
  helper: 'reduce_eval',
246
377
  operands: [
247
- { kind: 'string', value: json },
378
+ { kind: 'string', value: evaluable.json },
248
379
  { kind: 'string', value: params[0]! },
249
380
  { kind: 'string', value: params[1]! },
250
381
  seed,
251
382
  { kind: 'string', value: direction },
252
383
  ],
253
- captured: freeVarsInBody(body, new Set(params)),
384
+ captured: evaluable.captured,
254
385
  }
255
386
  }
256
387
 
@@ -258,17 +389,18 @@ function projectionEvalPlan(
258
389
  helper: 'map_eval' | 'flat_map_eval',
259
390
  body: ParsedExpr,
260
391
  params: readonly string[],
392
+ propsObject: string,
261
393
  ): CallbackEvalPlan | null {
262
- const json = serializeParsedExpr(body)
263
- if (json === null) return null
394
+ const evaluable = evalBody(body, new Set(params.slice(0, 1)), propsObject)
395
+ if (evaluable === null) return null
264
396
  return {
265
397
  kind: 'eval',
266
398
  helper,
267
399
  operands: [
268
- { kind: 'string', value: json },
400
+ { kind: 'string', value: evaluable.json },
269
401
  { kind: 'string', value: String(params[0]) },
270
402
  ],
271
- captured: freeVarsInBody(body, new Set(params.slice(0, 1))),
403
+ captured: evaluable.captured,
272
404
  }
273
405
  }
274
406
 
@@ -300,7 +432,7 @@ export function emitCallbackEval(
300
432
  else if (operand.kind === 'number') operands.push(operand.text)
301
433
  else operands.push(spelling.boolean(operand.value))
302
434
  }
303
- const env = plan.captured.map((name) => [name, emit({ kind: 'identifier', name })] as const)
435
+ const env = plan.captured.map(({ name, value }) => [name, emit(value)] as const)
304
436
  operands.push(spelling.env(env))
305
437
  return spelling.call(plan.helper, operands)
306
438
  }
@@ -96,10 +96,9 @@ export interface EarlierComponentProps {
96
96
  * caller actually handed over, and every optional param is `'present'`:
97
97
  * an `'entry'` would hand the child the key even when the caller left it
98
98
  * out, and the child would then read it as passed (a nil overriding its
99
- * own earlier prop in a further `{...props}`). Without `callerKeys` (Go,
100
- * whose Input struct can't tell the two nils apart), "passed" is "not
101
- * nil", and an explicit nil keeps the earlier value (limitation
102
- * `props-spread-explicit-null-override`).
99
+ * own earlier prop in a further `{...props}`). Without `callerKeys`,
100
+ * "passed" is "not nil"; Go answers it from its own `bf.Opt[T]` Input
101
+ * field (`Passed()`) instead.
103
102
  */
104
103
  export function propsSpreadParam(
105
104
  param: ComponentPropsParam,