@barefootjs/jsx 0.39.5 → 0.39.7
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.
- package/dist/adapters/array-method.d.ts +93 -0
- package/dist/adapters/array-method.d.ts.map +1 -0
- package/dist/adapters/attr-plan.d.ts +142 -0
- package/dist/adapters/attr-plan.d.ts.map +1 -0
- package/dist/adapters/boolean-result.d.ts +84 -9
- package/dist/adapters/boolean-result.d.ts.map +1 -1
- package/dist/adapters/callback-method.d.ts +153 -0
- package/dist/adapters/callback-method.d.ts.map +1 -0
- package/dist/adapters/component-props.d.ts +99 -0
- package/dist/adapters/component-props.d.ts.map +1 -0
- package/dist/adapters/component-tree.d.ts +25 -0
- package/dist/adapters/component-tree.d.ts.map +1 -0
- package/dist/adapters/computed-const.d.ts +52 -0
- package/dist/adapters/computed-const.d.ts.map +1 -0
- package/dist/adapters/dangerous-inner-html.d.ts +10 -0
- package/dist/adapters/dangerous-inner-html.d.ts.map +1 -1
- package/dist/adapters/element-shell.d.ts +44 -0
- package/dist/adapters/element-shell.d.ts.map +1 -0
- package/dist/adapters/interface.d.ts +2 -0
- package/dist/adapters/interface.d.ts.map +1 -1
- package/dist/adapters/literal-value.d.ts +22 -0
- package/dist/adapters/literal-value.d.ts.map +1 -0
- package/dist/adapters/loop-plan.d.ts +53 -0
- package/dist/adapters/loop-plan.d.ts.map +1 -0
- package/dist/adapters/prop-classes.d.ts +37 -0
- package/dist/adapters/prop-classes.d.ts.map +1 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +7 -0
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/expression-parser.d.ts +38 -0
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/fold-render-reassignments.d.ts +43 -0
- package/dist/fold-render-reassignments.d.ts.map +1 -0
- package/dist/free-refs.d.ts +9 -0
- package/dist/free-refs.d.ts.map +1 -1
- package/dist/index.d.ts +23 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3954 -2401
- package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
- package/dist/ir-to-client-js/csr-substitute.d.ts +0 -13
- package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts +9 -10
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/index.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/declaration-emit.d.ts +5 -0
- package/dist/ir-to-client-js/plan/declaration-emit.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts +11 -12
- package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +13 -0
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts +465 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +22 -2
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/props-binding.d.ts +34 -5
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/relocate.d.ts +16 -0
- package/dist/relocate.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +0 -6
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/types.d.ts +54 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +1 -1
- package/src/__tests__/action-accessor.test.ts +6 -24
- package/src/__tests__/array-method-catalogue.test.ts +99 -0
- package/src/__tests__/attr-plan.test.ts +121 -0
- package/src/__tests__/binding-scope-ratchet.test.ts +12 -12
- package/src/__tests__/block-memo-type.test.ts +51 -0
- package/src/__tests__/boolean-result-expr.test.ts +106 -0
- package/src/__tests__/boolean-result.test.ts +34 -1
- package/src/__tests__/callback-method.test.ts +264 -0
- package/src/__tests__/client-only-date-lowering.test.ts +2 -2
- package/src/__tests__/component-props.test.ts +148 -0
- package/src/__tests__/computed-const.test.ts +101 -0
- package/src/__tests__/delegated-handler-preamble.test.ts +4 -4
- package/src/__tests__/dsl-shared-helpers.test.ts +166 -0
- package/src/__tests__/element-shell.test.ts +78 -0
- package/src/__tests__/fold-render-reassignments.test.ts +96 -0
- package/src/__tests__/inlinable-local-method-call.test.ts +81 -0
- package/src/__tests__/issue-3240-nested-static-map-event-delegation.test.ts +1 -1
- package/src/__tests__/loop-plan.test.ts +104 -0
- package/src/__tests__/map-call-decisions.test.ts +521 -0
- package/src/__tests__/memo-body-expr.test.ts +120 -0
- package/src/__tests__/nullable-signal-attr.test.ts +45 -0
- package/src/__tests__/prop-handling.test.ts +35 -16
- package/src/__tests__/props-alias-const.test.ts +65 -0
- package/src/__tests__/signal-prop-seed.test.ts +90 -0
- package/src/__tests__/ssr-seed-plan.test.ts +54 -1
- package/src/__tests__/static-template-reachability.test.ts +137 -0
- package/src/adapters/array-method.ts +193 -0
- package/src/adapters/attr-plan.ts +228 -0
- package/src/adapters/boolean-result.ts +208 -12
- package/src/adapters/callback-method.ts +363 -0
- package/src/adapters/component-props.ts +144 -0
- package/src/adapters/component-tree.ts +118 -0
- package/src/adapters/computed-const.ts +185 -0
- package/src/adapters/dangerous-inner-html.ts +33 -0
- package/src/adapters/element-shell.ts +160 -0
- package/src/adapters/interface.ts +8 -0
- package/src/adapters/literal-value.ts +36 -0
- package/src/adapters/loop-plan.ts +88 -0
- package/src/adapters/prop-classes.ts +73 -0
- package/src/analyzer.ts +271 -82
- package/src/augment-inherited-props.ts +12 -0
- package/src/expression-parser.ts +537 -374
- package/src/fold-render-reassignments.ts +314 -0
- package/src/free-refs.ts +49 -0
- package/src/index.ts +61 -3
- package/src/ir-to-client-js/compute-inlinability.ts +93 -17
- package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +9 -7
- package/src/ir-to-client-js/csr-substitute.ts +13 -35
- package/src/ir-to-client-js/emit-registration.ts +3 -2
- package/src/ir-to-client-js/html-template.ts +26 -70
- package/src/ir-to-client-js/index.ts +3 -2
- package/src/ir-to-client-js/init-declarations.ts +1 -1
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +25 -51
- package/src/ir-to-client-js/plan/declaration-emit.ts +5 -0
- package/src/ir-to-client-js/prop-handling.ts +13 -54
- package/src/ir-to-client-js/rewrite-destructured-props.ts +6 -5
- package/src/ir-to-client-js/stringify/declaration-emit.ts +1 -1
- package/src/ir-to-client-js/utils.ts +11 -0
- package/src/jsx-to-ir.ts +1346 -1022
- package/src/nullable-signal-attr.ts +85 -4
- package/src/props-binding.ts +100 -5
- package/src/relocate.ts +40 -8
- package/src/ssr-seed-plan.ts +18 -6
- package/src/types.ts +50 -2
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The higher-order callback chain shared by the DSL adapters (#3477).
|
|
3
|
+
*
|
|
4
|
+
* `xs.filter(x => …)`, `xs.toSorted((a, b) => …)`, `xs.reduce((acc, x) => …,
|
|
5
|
+
* init)` and the rest of {@link CALLBACK_METHODS} lower the same way on every
|
|
6
|
+
* DSL backend: the runtime evaluator first (the callback body travels as
|
|
7
|
+
* serialized `ParsedExpr` JSON, see `serializeParsedExpr`), then a structured
|
|
8
|
+
* fallback (`.sort` only, the `{keys: …}` descriptor), then the adapter's own
|
|
9
|
+
* native predicate form where it has one (Kolon lambdas, Ruby blocks, Perl
|
|
10
|
+
* `grep`), and otherwise BF101. {@link lowerCallbackMethod} makes those
|
|
11
|
+
* decisions once and returns them as a plan; an adapter only spells the
|
|
12
|
+
* result through {@link CallbackEvalSpelling}.
|
|
13
|
+
*
|
|
14
|
+
* A callback that reads its index or array parameter never gets here: the
|
|
15
|
+
* support gate refuses it (`unboundCallbackParamRead`), because no DSL
|
|
16
|
+
* runtime binds those.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
freeVarsInBody,
|
|
21
|
+
hasOpaqueCall,
|
|
22
|
+
readsNestedCallbackIndex,
|
|
23
|
+
serializeParsedExpr,
|
|
24
|
+
sortComparatorFromArrow,
|
|
25
|
+
stringifyParsedExpr,
|
|
26
|
+
type ParsedExpr,
|
|
27
|
+
type SortComparator,
|
|
28
|
+
} from '../expression-parser.ts'
|
|
29
|
+
|
|
30
|
+
type Arrow = Extract<ParsedExpr, { kind: 'arrow' }>
|
|
31
|
+
|
|
32
|
+
/** The predicate members of `CALLBACK_METHODS`. */
|
|
33
|
+
export type PredicateMethod =
|
|
34
|
+
| 'filter'
|
|
35
|
+
| 'every'
|
|
36
|
+
| 'some'
|
|
37
|
+
| 'find'
|
|
38
|
+
| 'findIndex'
|
|
39
|
+
| 'findLast'
|
|
40
|
+
| 'findLastIndex'
|
|
41
|
+
|
|
42
|
+
const PREDICATE_EVAL: Readonly<Record<PredicateMethod, { helper: CallbackEvalHelper; forward?: boolean }>> = {
|
|
43
|
+
filter: { helper: 'filter_eval' },
|
|
44
|
+
every: { helper: 'every_eval' },
|
|
45
|
+
some: { helper: 'some_eval' },
|
|
46
|
+
find: { helper: 'find_eval', forward: true },
|
|
47
|
+
findLast: { helper: 'find_eval', forward: false },
|
|
48
|
+
findIndex: { helper: 'find_index_eval', forward: true },
|
|
49
|
+
findLastIndex: { helper: 'find_index_eval', forward: false },
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Whether `method` is one of the {@link PredicateMethod}s. */
|
|
53
|
+
export function isPredicateMethod(method: string): method is PredicateMethod {
|
|
54
|
+
return Object.hasOwn(PREDICATE_EVAL, method)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The runtime evaluator helpers, by id. Every DSL runtime spells them the same. */
|
|
58
|
+
export type CallbackEvalHelper =
|
|
59
|
+
| 'sort_eval'
|
|
60
|
+
| 'reduce_eval'
|
|
61
|
+
| 'filter_eval'
|
|
62
|
+
| 'every_eval'
|
|
63
|
+
| 'some_eval'
|
|
64
|
+
| 'find_eval'
|
|
65
|
+
| 'find_index_eval'
|
|
66
|
+
| 'flat_map_eval'
|
|
67
|
+
| 'map_eval'
|
|
68
|
+
|
|
69
|
+
/** An evaluator-call operand after the receiver and before the env. */
|
|
70
|
+
export type CallbackEvalOperand =
|
|
71
|
+
| { kind: 'string'; value: string }
|
|
72
|
+
/** A number literal's source text. */
|
|
73
|
+
| { kind: 'number'; text: string }
|
|
74
|
+
| { kind: 'boolean'; value: boolean }
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* An evaluator call: `helper(recv, ...operands, env)`. The operands carry the
|
|
78
|
+
* serialized body, the callback's param names and the per-helper extras (a
|
|
79
|
+
* reduce's initial value and direction, a find's direction). `captured` are
|
|
80
|
+
* the body's free names; the env maps each to its SSR value.
|
|
81
|
+
*/
|
|
82
|
+
export interface CallbackEvalPlan {
|
|
83
|
+
kind: 'eval'
|
|
84
|
+
helper: CallbackEvalHelper
|
|
85
|
+
operands: CallbackEvalOperand[]
|
|
86
|
+
captured: string[]
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** A lowered callback method call, as data. */
|
|
90
|
+
export type CallbackPlan =
|
|
91
|
+
| CallbackEvalPlan
|
|
92
|
+
/** A comparator the evaluator cannot model but the `{keys: …}` descriptor can. */
|
|
93
|
+
| { kind: 'structured-sort'; comparator: SortComparator }
|
|
94
|
+
/** A predicate for the adapter's own native form (see `nativePredicate`). */
|
|
95
|
+
| { kind: 'native-predicate'; method: PredicateMethod; param: string; predicate: ParsedExpr }
|
|
96
|
+
| { kind: 'refuse'; message: string; suggestion: string }
|
|
97
|
+
/** A method outside `CALLBACK_METHODS`; unreachable, as the parser recognises only those. */
|
|
98
|
+
| { kind: 'receiver' }
|
|
99
|
+
|
|
100
|
+
export interface CallbackLoweringOptions {
|
|
101
|
+
/** The display name BF101 uses: "the Jinja adapter". */
|
|
102
|
+
adapterName: string
|
|
103
|
+
/**
|
|
104
|
+
* The adapter lowers a predicate the evaluator cannot model to a native
|
|
105
|
+
* form (Xslate, ERB, Mojolicious). Such an adapter also takes the
|
|
106
|
+
* identity filter (`xs.filter(x => x)`) natively without trying the
|
|
107
|
+
* evaluator.
|
|
108
|
+
*/
|
|
109
|
+
nativePredicate?: boolean
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Lower `<recv>.<method>(arrow, ...restArgs)`. */
|
|
113
|
+
export function lowerCallbackMethod(
|
|
114
|
+
method: string,
|
|
115
|
+
arrow: Arrow,
|
|
116
|
+
restArgs: readonly ParsedExpr[],
|
|
117
|
+
options: CallbackLoweringOptions,
|
|
118
|
+
): CallbackPlan {
|
|
119
|
+
const { adapterName } = options
|
|
120
|
+
const { body, params } = arrow
|
|
121
|
+
if (isPredicateMethod(method)) return lowerPredicate(method, arrow, options)
|
|
122
|
+
if (method === 'sort' || method === 'toSorted') {
|
|
123
|
+
const plan = sortEvalPlan(body, params)
|
|
124
|
+
if (plan) return plan
|
|
125
|
+
const comparator = sortComparatorFromArrow(arrow)
|
|
126
|
+
if (comparator) return { kind: 'structured-sort', comparator }
|
|
127
|
+
return sortRefusal(method, adapterName)
|
|
128
|
+
}
|
|
129
|
+
if (method === 'reduce' || method === 'reduceRight') {
|
|
130
|
+
const init = restArgs[0]
|
|
131
|
+
const plan = init ? reduceEvalPlan(body, params, init, method === 'reduceRight' ? 'right' : 'left') : null
|
|
132
|
+
return (
|
|
133
|
+
plan ?? {
|
|
134
|
+
kind: 'refuse',
|
|
135
|
+
message: `'.${method}(...)' is outside the ${adapterName} adapter's evaluable surface (needs a literal initial value and an evaluable reducer body)`,
|
|
136
|
+
suggestion: `Pre-compute the reduced value, or move this position to a '/* @client */' boundary.`,
|
|
137
|
+
}
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
if (method === 'flatMap' || method === 'map') {
|
|
141
|
+
const plan = projectionEvalPlan(method === 'map' ? 'map_eval' : 'flat_map_eval', body, params)
|
|
142
|
+
return (
|
|
143
|
+
plan ?? {
|
|
144
|
+
kind: 'refuse',
|
|
145
|
+
message: `'.${method}(...)' projection is outside the ${adapterName} adapter's evaluable surface`,
|
|
146
|
+
suggestion: `Pre-compute the projected array, or move this position to a '/* @client */' boundary.`,
|
|
147
|
+
}
|
|
148
|
+
)
|
|
149
|
+
}
|
|
150
|
+
return { kind: 'receiver' }
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function lowerPredicate(method: PredicateMethod, arrow: Arrow, options: CallbackLoweringOptions): CallbackPlan {
|
|
154
|
+
const param = String(arrow.params[0])
|
|
155
|
+
const native: CallbackPlan = { kind: 'native-predicate', method, param, predicate: arrow.body }
|
|
156
|
+
// A native form lowers an opaque call to a read of its callee, silently, and
|
|
157
|
+
// binds no nested callback's index, so such a predicate takes the
|
|
158
|
+
// evaluator or BF101 on every adapter alike.
|
|
159
|
+
const nativeOk =
|
|
160
|
+
options.nativePredicate === true && !hasOpaqueCall(arrow.body) && !readsNestedCallbackIndex(arrow.body)
|
|
161
|
+
const identity = method === 'filter' && arrow.body.kind === 'identifier' && arrow.body.name === param
|
|
162
|
+
if (nativeOk && identity) return native
|
|
163
|
+
const json = serializeParsedExpr(arrow.body)
|
|
164
|
+
if (json !== null) {
|
|
165
|
+
const { helper, forward } = PREDICATE_EVAL[method]
|
|
166
|
+
const operands: CallbackEvalOperand[] = [
|
|
167
|
+
{ kind: 'string', value: json },
|
|
168
|
+
{ kind: 'string', value: param },
|
|
169
|
+
]
|
|
170
|
+
if (forward !== undefined) operands.push({ kind: 'boolean', value: forward })
|
|
171
|
+
return { kind: 'eval', helper, operands, captured: freeVarsInBody(arrow.body, new Set(arrow.params.slice(0, 1))) }
|
|
172
|
+
}
|
|
173
|
+
if (nativeOk) return native
|
|
174
|
+
return {
|
|
175
|
+
kind: 'refuse',
|
|
176
|
+
message: `'.${method}(...)' predicate is outside the ${options.adapterName} adapter's evaluable surface`,
|
|
177
|
+
suggestion: `Rewrite the predicate as a pure expression the evaluator can serialize (no nested method-call callbacks), or move this position to a '/* @client */' boundary.`,
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* The evaluator plan for a sort comparator, or `null` when the body is
|
|
183
|
+
* outside the evaluator surface (a `localeCompare` comparator) or the arrow
|
|
184
|
+
* has fewer than the two params a comparator needs. The loop-source sort
|
|
185
|
+
* shares this with `.sort(cmp)` at an expression position.
|
|
186
|
+
*/
|
|
187
|
+
export function sortEvalPlan(body: ParsedExpr, params: readonly string[]): CallbackEvalPlan | null {
|
|
188
|
+
const json = serializeParsedExpr(body)
|
|
189
|
+
if (json === null || params.length < 2) return null
|
|
190
|
+
return {
|
|
191
|
+
kind: 'eval',
|
|
192
|
+
helper: 'sort_eval',
|
|
193
|
+
operands: [
|
|
194
|
+
{ kind: 'string', value: json },
|
|
195
|
+
{ kind: 'string', value: params[0]! },
|
|
196
|
+
{ kind: 'string', value: params[1]! },
|
|
197
|
+
],
|
|
198
|
+
captured: freeVarsInBody(body, new Set(params)),
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** The BF101 for a comparator neither the evaluator nor the descriptor can model. */
|
|
203
|
+
export function sortRefusal(method: string, adapterName: string): { kind: 'refuse'; message: string; suggestion: string } {
|
|
204
|
+
return {
|
|
205
|
+
kind: 'refuse',
|
|
206
|
+
message: `'.${method}(...)' comparator is outside the ${adapterName} adapter's evaluable / structured surface`,
|
|
207
|
+
suggestion: `Pre-compute the sorted array, or move this position to a '/* @client */' boundary.`,
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* The evaluator plan for a reducer. Only a string or number literal seeds
|
|
213
|
+
* the fold; anything else (and a body outside the evaluator surface, or a
|
|
214
|
+
* reducer without both `acc` and item params) returns `null`.
|
|
215
|
+
*/
|
|
216
|
+
function reduceEvalPlan(
|
|
217
|
+
body: ParsedExpr,
|
|
218
|
+
params: readonly string[],
|
|
219
|
+
init: ParsedExpr,
|
|
220
|
+
direction: 'left' | 'right',
|
|
221
|
+
): CallbackEvalPlan | null {
|
|
222
|
+
const json = serializeParsedExpr(body)
|
|
223
|
+
if (json === null || init.kind !== 'literal' || params.length < 2) return null
|
|
224
|
+
let seed: CallbackEvalOperand
|
|
225
|
+
if (init.literalType === 'string') seed = { kind: 'string', value: String(init.value) }
|
|
226
|
+
else if (init.literalType === 'number') seed = { kind: 'number', text: String(init.value) }
|
|
227
|
+
else return null
|
|
228
|
+
return {
|
|
229
|
+
kind: 'eval',
|
|
230
|
+
helper: 'reduce_eval',
|
|
231
|
+
operands: [
|
|
232
|
+
{ kind: 'string', value: json },
|
|
233
|
+
{ kind: 'string', value: params[0]! },
|
|
234
|
+
{ kind: 'string', value: params[1]! },
|
|
235
|
+
seed,
|
|
236
|
+
{ kind: 'string', value: direction },
|
|
237
|
+
],
|
|
238
|
+
captured: freeVarsInBody(body, new Set(params)),
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function projectionEvalPlan(
|
|
243
|
+
helper: 'map_eval' | 'flat_map_eval',
|
|
244
|
+
body: ParsedExpr,
|
|
245
|
+
params: readonly string[],
|
|
246
|
+
): CallbackEvalPlan | null {
|
|
247
|
+
const json = serializeParsedExpr(body)
|
|
248
|
+
if (json === null) return null
|
|
249
|
+
return {
|
|
250
|
+
kind: 'eval',
|
|
251
|
+
helper,
|
|
252
|
+
operands: [
|
|
253
|
+
{ kind: 'string', value: json },
|
|
254
|
+
{ kind: 'string', value: String(params[0]) },
|
|
255
|
+
],
|
|
256
|
+
captured: freeVarsInBody(body, new Set(params.slice(0, 1))),
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** How one DSL backend spells an evaluator call. */
|
|
261
|
+
export interface CallbackEvalSpelling {
|
|
262
|
+
/** `helper(operands)`: `bf.sort_eval(…)`, `$bf->sort_eval(…)`, `bf->sort_eval(…)`. */
|
|
263
|
+
call(helper: CallbackEvalHelper, operands: readonly string[]): string
|
|
264
|
+
/** A single-quoted string literal, escaped for the backend. */
|
|
265
|
+
string(value: string): string
|
|
266
|
+
/** `true` / `false`, or Perl's `1` / `0`. */
|
|
267
|
+
boolean(value: boolean): string
|
|
268
|
+
/** The env literal mapping each captured name to its SSR value, `{}` when empty. */
|
|
269
|
+
env(entries: ReadonlyArray<readonly [name: string, value: string]>): string
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Emit an evaluator plan. `recv` is the already-emitted receiver; `emit`
|
|
274
|
+
* renders each captured name's SSR value, after the receiver.
|
|
275
|
+
*/
|
|
276
|
+
export function emitCallbackEval(
|
|
277
|
+
plan: CallbackEvalPlan,
|
|
278
|
+
recv: string,
|
|
279
|
+
emit: (e: ParsedExpr) => string,
|
|
280
|
+
spelling: CallbackEvalSpelling,
|
|
281
|
+
): string {
|
|
282
|
+
const operands = [recv]
|
|
283
|
+
for (const operand of plan.operands) {
|
|
284
|
+
if (operand.kind === 'string') operands.push(spelling.string(operand.value))
|
|
285
|
+
else if (operand.kind === 'number') operands.push(operand.text)
|
|
286
|
+
else operands.push(spelling.boolean(operand.value))
|
|
287
|
+
}
|
|
288
|
+
const env = plan.captured.map((name) => [name, emit({ kind: 'identifier', name })] as const)
|
|
289
|
+
operands.push(spelling.env(env))
|
|
290
|
+
return spelling.call(plan.helper, operands)
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Whether a call that reached an adapter's generic fallback has no lowering
|
|
295
|
+
* (#3493). A zero-arg call reads a getter (`props.value()`, a getter passed
|
|
296
|
+
* as a prop, a context getter `ctx.open()`), unless its receiver is rooted at
|
|
297
|
+
* a JavaScript global (`Date.now()`, `window.performance.now()`): `isGlobal`
|
|
298
|
+
* answers that from the component's bindings and the live loop scope, and a
|
|
299
|
+
* global has no binding in template scope (#3505). A call to a
|
|
300
|
+
* function-valued prop (`props.renderNode(node)`, a render prop) names a
|
|
301
|
+
* value a backend caller never passes, so the guard around it is never taken
|
|
302
|
+
* there; it keeps the callee read. Any other call with arguments
|
|
303
|
+
* (`s().localeCompare('m')`) would lose the call and its arguments.
|
|
304
|
+
* `propsObjectName` is the undestructured props parameter (`props`), `null`
|
|
305
|
+
* when the props are destructured: a destructured render prop is a
|
|
306
|
+
* bare-name call, which the adapters refuse on their own.
|
|
307
|
+
*/
|
|
308
|
+
export function isUnloweredCall(
|
|
309
|
+
callee: ParsedExpr,
|
|
310
|
+
args: readonly ParsedExpr[],
|
|
311
|
+
propsObjectName: string | null,
|
|
312
|
+
isGlobal: (name: string) => boolean,
|
|
313
|
+
): boolean {
|
|
314
|
+
if (args.length === 0) {
|
|
315
|
+
const root = memberRoot(callee)
|
|
316
|
+
return root !== null && isGlobal(root)
|
|
317
|
+
}
|
|
318
|
+
return !(callee.kind === 'member' && callee.object.kind === 'identifier' && callee.object.name === propsObjectName)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* The identifier a member chain is rooted at, through literal and computed
|
|
323
|
+
* access alike (`window.performance.now`, `Date[key]`,
|
|
324
|
+
* `globalThis[name].now`); `null` for any other callee.
|
|
325
|
+
*/
|
|
326
|
+
function memberRoot(callee: ParsedExpr): string | null {
|
|
327
|
+
if (callee.kind !== 'member' && callee.kind !== 'index-access') return null
|
|
328
|
+
let object = callee.object
|
|
329
|
+
while (object.kind === 'member' || object.kind === 'index-access') object = object.object
|
|
330
|
+
return object.kind === 'identifier' ? object.name : null
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* The BF101 for a call no template lowers: a method outside the array-method
|
|
335
|
+
* catalogue (`s().localeCompare('m')`), a zero-arg call on a JavaScript
|
|
336
|
+
* global (`Date.now()`), or any other callee that is not a getter, a template
|
|
337
|
+
* primitive or a lowering plugin's call. Read as its callee, the call and its
|
|
338
|
+
* arguments would silently disappear (#3493, #3505).
|
|
339
|
+
*/
|
|
340
|
+
export function unloweredCallRefusal(
|
|
341
|
+
callee: ParsedExpr,
|
|
342
|
+
args: readonly ParsedExpr[],
|
|
343
|
+
adapterName: string,
|
|
344
|
+
): { message: string; suggestion: string } {
|
|
345
|
+
const name = stringifyParsedExpr(callee)
|
|
346
|
+
return {
|
|
347
|
+
message: args.length === 0
|
|
348
|
+
? `Call '${name}()' reads a JavaScript global, which has no ${adapterName} template binding; read as a value it would drop the call`
|
|
349
|
+
: `Call '${name}(...)' has no ${adapterName} template lowering; read as a value it would drop the call and its arguments`,
|
|
350
|
+
suggestion: 'Pre-compute the value in the backend and pass it as a prop, or add /* @client */ for client-only evaluation (no SSR).',
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* The BF101 for a callback method nested inside a filter predicate, which a
|
|
356
|
+
* predicate emitter has no scalar form for.
|
|
357
|
+
*/
|
|
358
|
+
export function nestedCallbackRefusal(method: string, adapterName: string): { message: string; suggestion: string } {
|
|
359
|
+
return {
|
|
360
|
+
message: `Filter predicate contains a nested '.${method}(...)' callback, which has no ${adapterName} scalar form`,
|
|
361
|
+
suggestion: `Rewrite the predicate without a nested callback method, or add /* @client */ for client-only evaluation (no SSR).`,
|
|
362
|
+
}
|
|
363
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The props a DSL adapter passes to a child component's `render_child`
|
|
3
|
+
* call (#3479), as data.
|
|
4
|
+
*
|
|
5
|
+
* Every DSL adapter assembles them the same way: callback props and `ref`
|
|
6
|
+
* are client-only and dropped; a named JSX-valued prop is captured as
|
|
7
|
+
* HTML; `{...props}` (the component's own props object, which no DSL scope
|
|
8
|
+
* holds as one value) enumerates the props params; any other spread is
|
|
9
|
+
* folded in at its source position, so a later entry wins on a key
|
|
10
|
+
* conflict, as in `{...a, ...b}`; then the compiler-controlled `_bf_slot`
|
|
11
|
+
* and `children` entries come last, so a spread can never override them.
|
|
12
|
+
* {@link planComponentProps} makes those decisions once. An adapter spells
|
|
13
|
+
* the entries and spreads through {@link spellComponentProps}, keeps its
|
|
14
|
+
* own capture mechanism (`{% set %}…{% endset %}`, `_erbout.slice!`,
|
|
15
|
+
* `begin … end`, a Kolon macro), and folds the spelled segments into its
|
|
16
|
+
* own hash literal.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { resolveJsxChildrenProp } from '@barefootjs/shared'
|
|
20
|
+
import type { AttrValue, IRComponent, IRNode, SpreadAttr } from '../types.ts'
|
|
21
|
+
import { derivesScopeFromSlot } from './child-scope.ts'
|
|
22
|
+
|
|
23
|
+
/** One entry of the props hash. */
|
|
24
|
+
export type ComponentPropEntry =
|
|
25
|
+
/** An ordinary prop: the adapter lowers `value` with its `AttrValueEmitter`. */
|
|
26
|
+
| { kind: 'value'; name: string; value: AttrValue }
|
|
27
|
+
/** A named JSX-valued prop (`header={<b/>}`): captured as HTML, passed by reference. */
|
|
28
|
+
| { kind: 'named-slot'; name: string; children: IRNode[] }
|
|
29
|
+
/** One param of a spread props object (`{...props}`), read from its own template variable. */
|
|
30
|
+
| { kind: 'props-param'; name: string }
|
|
31
|
+
/** The child's mount slot, for its scope id (`derivesScopeFromSlot`). */
|
|
32
|
+
| { kind: 'slot-id'; slotId: string }
|
|
33
|
+
/** The forwarded children (nested `<Box>…</Box>`, or `children={<jsx/>}`): captured as HTML. */
|
|
34
|
+
| { kind: 'children'; children: IRNode[] }
|
|
35
|
+
|
|
36
|
+
/** A run of entries, or a spread folded in at its source position. */
|
|
37
|
+
export type ComponentPropSegment =
|
|
38
|
+
| { kind: 'entries'; entries: ComponentPropEntry[] }
|
|
39
|
+
| { kind: 'spread'; value: SpreadAttr }
|
|
40
|
+
|
|
41
|
+
export interface ComponentPropsOptions {
|
|
42
|
+
/** The SolidJS-style props identifier (`function C(props: P)`), if any. */
|
|
43
|
+
propsObjectName: string | null
|
|
44
|
+
/** The analyzer-extracted props params, which `{...props}` enumerates. */
|
|
45
|
+
propsParams: ReadonlyArray<{ name: string }>
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The key an entry sets in the props hash. */
|
|
49
|
+
export function componentPropKey(entry: ComponentPropEntry): string {
|
|
50
|
+
if (entry.kind === 'slot-id') return '_bf_slot'
|
|
51
|
+
if (entry.kind === 'children') return 'children'
|
|
52
|
+
return entry.name
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The ordered props segments for `comp`. The first segment is always an
|
|
57
|
+
* (possibly empty) entries segment, and a spread is followed by a new one
|
|
58
|
+
* only when an entry comes after it.
|
|
59
|
+
*/
|
|
60
|
+
export function planComponentProps(comp: IRComponent, options: ComponentPropsOptions): ComponentPropSegment[] {
|
|
61
|
+
const segments: ComponentPropSegment[] = [{ kind: 'entries', entries: [] }]
|
|
62
|
+
const push = (entry: ComponentPropEntry) => {
|
|
63
|
+
const last = segments[segments.length - 1]!
|
|
64
|
+
if (last.kind !== 'entries') {
|
|
65
|
+
segments.push({ kind: 'entries', entries: [entry] })
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
// A repeated key keeps its first position and takes the later value, as
|
|
69
|
+
// in a JS object literal (`{...props, tone: 'x'}`), so no backend sees a
|
|
70
|
+
// duplicate key (Ruby warns on one in a hash literal).
|
|
71
|
+
const at = last.entries.findIndex((e) => componentPropKey(e) === componentPropKey(entry))
|
|
72
|
+
if (at === -1) last.entries.push(entry)
|
|
73
|
+
else last.entries[at] = entry
|
|
74
|
+
}
|
|
75
|
+
for (const p of comp.props) {
|
|
76
|
+
// Callback props and `ref` are client-only for SSR: Hono renders
|
|
77
|
+
// neither, and the client JS wires them at hydration.
|
|
78
|
+
if ((/^on[A-Z]/.test(p.name) || p.name === 'ref') && p.value.kind === 'expression') continue
|
|
79
|
+
if (p.value.kind === 'jsx-children') {
|
|
80
|
+
// `children={<jsx/>}` is forwarded as the `children` entry below.
|
|
81
|
+
if (p.name !== 'children') push({ kind: 'named-slot', name: p.name, children: p.value.children })
|
|
82
|
+
} else if (p.value.kind === 'spread') {
|
|
83
|
+
if (options.propsObjectName !== null && options.propsObjectName === p.value.expr.trim()) {
|
|
84
|
+
for (const param of options.propsParams) push({ kind: 'props-param', name: param.name })
|
|
85
|
+
} else {
|
|
86
|
+
segments.push({ kind: 'spread', value: p.value })
|
|
87
|
+
}
|
|
88
|
+
} else {
|
|
89
|
+
push({ kind: 'value', name: p.name, value: p.value })
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// A loop row root takes a fresh scope id instead (#2444).
|
|
93
|
+
if (derivesScopeFromSlot(comp)) push({ kind: 'slot-id', slotId: comp.slotId! })
|
|
94
|
+
const children = comp.children.length > 0 ? comp.children : resolveJsxChildrenProp(comp.props)
|
|
95
|
+
if (children.length > 0) push({ kind: 'children', children })
|
|
96
|
+
return segments
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** A spelled segment: the entries' hash-literal parts, or the spread's expression. */
|
|
100
|
+
export type SpelledComponentPropSegment = { kind: 'entries'; parts: string[] } | { kind: 'spread'; expr: string }
|
|
101
|
+
|
|
102
|
+
export interface ComponentPropSpelling {
|
|
103
|
+
/** The hash-literal part for one entry; `''` drops it. */
|
|
104
|
+
entry(entry: ComponentPropEntry): string
|
|
105
|
+
/** The expression a spread folds in. */
|
|
106
|
+
spread(value: SpreadAttr): string
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Spell a plan in source order, so the captures an entry's spelling emits
|
|
111
|
+
* (named slots, then the children) keep their order and counter values.
|
|
112
|
+
*/
|
|
113
|
+
export function spellComponentProps(
|
|
114
|
+
segments: readonly ComponentPropSegment[],
|
|
115
|
+
spelling: ComponentPropSpelling,
|
|
116
|
+
): SpelledComponentPropSegment[] {
|
|
117
|
+
return segments.map((segment) => {
|
|
118
|
+
if (segment.kind === 'spread') return { kind: 'spread', expr: spelling.spread(segment.value) }
|
|
119
|
+
const parts: string[] = []
|
|
120
|
+
for (const entry of segment.entries) {
|
|
121
|
+
const part = spelling.entry(entry)
|
|
122
|
+
if (part) parts.push(part)
|
|
123
|
+
}
|
|
124
|
+
return { kind: 'entries', parts }
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Whether a spelled plan passes no props at all. */
|
|
129
|
+
export function isEmptyComponentProps(segments: readonly SpelledComponentPropSegment[]): boolean {
|
|
130
|
+
return segments.every((segment) => segment.kind === 'entries' && segment.parts.length === 0)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* The template a child component renders from: the component's declared
|
|
135
|
+
* name, resolved through an import alias (`import { Foo as Bar }`, `<Bar/>`
|
|
136
|
+
* renders `foo`, #2822), in snake_case.
|
|
137
|
+
*/
|
|
138
|
+
export function childTemplateName(componentName: string, importAliases: ReadonlyMap<string, string>): string {
|
|
139
|
+
const declaredName = importAliases.get(componentName) ?? componentName
|
|
140
|
+
return declaredName
|
|
141
|
+
.replace(/([A-Z])/g, '_$1')
|
|
142
|
+
.toLowerCase()
|
|
143
|
+
.replace(/^_/, '')
|
|
144
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Component-tree analysis shared by the DSL template adapters (#3475).
|
|
3
|
+
*
|
|
4
|
+
* Pure functions over the IR; they read no adapter instance state.
|
|
5
|
+
* `collectImportedLoopChildComponentErrors` returns its diagnostics instead
|
|
6
|
+
* of pushing onto an adapter's error list, so the adapter stays the sole
|
|
7
|
+
* owner of `errors`.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type {
|
|
11
|
+
ComponentIR,
|
|
12
|
+
IRNode,
|
|
13
|
+
IRComponent,
|
|
14
|
+
IRElement,
|
|
15
|
+
IRFragment,
|
|
16
|
+
IRConditional,
|
|
17
|
+
IRLoop,
|
|
18
|
+
IRIfStatement,
|
|
19
|
+
IRProvider,
|
|
20
|
+
IRAsync,
|
|
21
|
+
CompilerError,
|
|
22
|
+
} from '../types.ts'
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Whether the component needs the client runtime — it owns reactive state
|
|
26
|
+
* (signals / effects / onMount) or the analyzer flagged it as needing init.
|
|
27
|
+
*/
|
|
28
|
+
export function hasClientInteractivity(ir: ComponentIR): boolean {
|
|
29
|
+
return (
|
|
30
|
+
ir.metadata.signals.length > 0 ||
|
|
31
|
+
ir.metadata.effects.length > 0 ||
|
|
32
|
+
ir.metadata.onMounts.length > 0 ||
|
|
33
|
+
(ir.metadata.clientAnalysis?.needsInit ?? false)
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Build a `BF103` diagnostic for every component reference inside a loop body
|
|
39
|
+
* whose name is imported from a relative-path module. A DSL template backend
|
|
40
|
+
* renders such a child as a cross-template call, so the child's compiled
|
|
41
|
+
* template must be registered with the backend's loader alongside the
|
|
42
|
+
* parent at render time. `adapterName` is the display name the message
|
|
43
|
+
* uses ("The Jinja adapter emits …", "the resulting Jinja template").
|
|
44
|
+
* Returns the diagnostics so the caller pushes them onto its own error list.
|
|
45
|
+
*/
|
|
46
|
+
export function collectImportedLoopChildComponentErrors(
|
|
47
|
+
ir: ComponentIR,
|
|
48
|
+
componentName: string,
|
|
49
|
+
adapterName: string,
|
|
50
|
+
): CompilerError[] {
|
|
51
|
+
const errors: CompilerError[] = []
|
|
52
|
+
const relativeImports = new Set<string>()
|
|
53
|
+
for (const imp of ir.metadata.templateImports ?? ir.metadata.imports ?? []) {
|
|
54
|
+
if (!imp.source.startsWith('./') && !imp.source.startsWith('../')) continue
|
|
55
|
+
if (imp.isTypeOnly) continue
|
|
56
|
+
for (const spec of imp.specifiers) {
|
|
57
|
+
relativeImports.add(spec.alias ?? spec.name)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (relativeImports.size === 0) return errors
|
|
61
|
+
|
|
62
|
+
const loc = { file: componentName + '.tsx', start: { line: 1, column: 0 }, end: { line: 1, column: 0 } }
|
|
63
|
+
const visit = (node: IRNode, inLoop: boolean): void => {
|
|
64
|
+
if (node.type === 'component' && inLoop && relativeImports.has(node.name)) {
|
|
65
|
+
errors.push(importedLoopChildError(node, loc, adapterName))
|
|
66
|
+
}
|
|
67
|
+
const childInLoop = inLoop || node.type === 'loop'
|
|
68
|
+
for (const child of childNodes(node)) visit(child, childInLoop)
|
|
69
|
+
}
|
|
70
|
+
visit(ir.root, false)
|
|
71
|
+
return errors
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function importedLoopChildError(
|
|
75
|
+
comp: IRComponent,
|
|
76
|
+
loc: CompilerError['loc'],
|
|
77
|
+
adapterName: string,
|
|
78
|
+
): CompilerError {
|
|
79
|
+
return {
|
|
80
|
+
code: 'BF103',
|
|
81
|
+
severity: 'error',
|
|
82
|
+
message: `Component <${comp.name}> is imported from a sibling module and used inside a loop. The ${adapterName} adapter emits a cross-template call; the child template must be registered alongside the parent at render time.`,
|
|
83
|
+
loc: comp.loc ?? loc,
|
|
84
|
+
suggestion: {
|
|
85
|
+
message:
|
|
86
|
+
`Options:\n` +
|
|
87
|
+
` 1. Compile '${comp.name}' (its source file) with the same adapter and register the resulting ${adapterName} template alongside the parent at render time.\n` +
|
|
88
|
+
` 2. Inline <${comp.name}> directly inside the loop body so no cross-file template lookup is needed.\n` +
|
|
89
|
+
` 3. Mark the loop position as @client-only so the template is materialised on the client instead of at SSR time.`,
|
|
90
|
+
},
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The child nodes the BF103 walk descends into, in render order. */
|
|
95
|
+
function childNodes(node: IRNode): IRNode[] {
|
|
96
|
+
switch (node.type) {
|
|
97
|
+
case 'component':
|
|
98
|
+
case 'element':
|
|
99
|
+
case 'fragment':
|
|
100
|
+
case 'loop':
|
|
101
|
+
case 'provider':
|
|
102
|
+
return (node as IRComponent | IRElement | IRFragment | IRLoop | IRProvider).children
|
|
103
|
+
case 'conditional': {
|
|
104
|
+
const cond = node as IRConditional
|
|
105
|
+
return cond.whenFalse ? [cond.whenTrue, cond.whenFalse] : [cond.whenTrue]
|
|
106
|
+
}
|
|
107
|
+
case 'if-statement': {
|
|
108
|
+
const stmt = node as IRIfStatement
|
|
109
|
+
return stmt.alternate ? [stmt.consequent, stmt.alternate] : [stmt.consequent]
|
|
110
|
+
}
|
|
111
|
+
case 'async': {
|
|
112
|
+
const a = node as IRAsync
|
|
113
|
+
return [a.fallback, ...a.children]
|
|
114
|
+
}
|
|
115
|
+
default:
|
|
116
|
+
return []
|
|
117
|
+
}
|
|
118
|
+
}
|