@barefootjs/jsx 0.39.5 → 0.39.6
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 +135 -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 +126 -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/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/expression-parser.d.ts +38 -0
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/index.d.ts +17 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1765 -914
- 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 +19 -0
- package/dist/nullable-signal-attr.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__/boolean-result-expr.test.ts +106 -0
- package/src/__tests__/boolean-result.test.ts +34 -1
- package/src/__tests__/callback-method.test.ts +189 -0
- package/src/__tests__/component-props.test.ts +148 -0
- package/src/__tests__/delegated-handler-preamble.test.ts +4 -4
- package/src/__tests__/dsl-shared-helpers.test.ts +166 -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__/nullable-signal-attr.test.ts +45 -0
- package/src/adapters/array-method.ts +193 -0
- package/src/adapters/attr-plan.ts +200 -0
- package/src/adapters/boolean-result.ts +208 -12
- package/src/adapters/callback-method.ts +301 -0
- package/src/adapters/component-props.ts +144 -0
- package/src/adapters/component-tree.ts +118 -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/expression-parser.ts +537 -374
- package/src/index.ts +53 -2
- package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +9 -7
- package/src/jsx-to-ir.ts +1328 -991
- package/src/nullable-signal-attr.ts +78 -0
|
@@ -0,0 +1,301 @@
|
|
|
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
|
+
type ParsedExpr,
|
|
26
|
+
type SortComparator,
|
|
27
|
+
} from '../expression-parser.ts'
|
|
28
|
+
|
|
29
|
+
type Arrow = Extract<ParsedExpr, { kind: 'arrow' }>
|
|
30
|
+
|
|
31
|
+
/** The predicate members of `CALLBACK_METHODS`. */
|
|
32
|
+
export type PredicateMethod =
|
|
33
|
+
| 'filter'
|
|
34
|
+
| 'every'
|
|
35
|
+
| 'some'
|
|
36
|
+
| 'find'
|
|
37
|
+
| 'findIndex'
|
|
38
|
+
| 'findLast'
|
|
39
|
+
| 'findLastIndex'
|
|
40
|
+
|
|
41
|
+
const PREDICATE_EVAL: Readonly<Record<PredicateMethod, { helper: CallbackEvalHelper; forward?: boolean }>> = {
|
|
42
|
+
filter: { helper: 'filter_eval' },
|
|
43
|
+
every: { helper: 'every_eval' },
|
|
44
|
+
some: { helper: 'some_eval' },
|
|
45
|
+
find: { helper: 'find_eval', forward: true },
|
|
46
|
+
findLast: { helper: 'find_eval', forward: false },
|
|
47
|
+
findIndex: { helper: 'find_index_eval', forward: true },
|
|
48
|
+
findLastIndex: { helper: 'find_index_eval', forward: false },
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Whether `method` is one of the {@link PredicateMethod}s. */
|
|
52
|
+
export function isPredicateMethod(method: string): method is PredicateMethod {
|
|
53
|
+
return Object.hasOwn(PREDICATE_EVAL, method)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The runtime evaluator helpers, by id. Every DSL runtime spells them the same. */
|
|
57
|
+
export type CallbackEvalHelper =
|
|
58
|
+
| 'sort_eval'
|
|
59
|
+
| 'reduce_eval'
|
|
60
|
+
| 'filter_eval'
|
|
61
|
+
| 'every_eval'
|
|
62
|
+
| 'some_eval'
|
|
63
|
+
| 'find_eval'
|
|
64
|
+
| 'find_index_eval'
|
|
65
|
+
| 'flat_map_eval'
|
|
66
|
+
| 'map_eval'
|
|
67
|
+
|
|
68
|
+
/** An evaluator-call operand after the receiver and before the env. */
|
|
69
|
+
export type CallbackEvalOperand =
|
|
70
|
+
| { kind: 'string'; value: string }
|
|
71
|
+
/** A number literal's source text. */
|
|
72
|
+
| { kind: 'number'; text: string }
|
|
73
|
+
| { kind: 'boolean'; value: boolean }
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* An evaluator call: `helper(recv, ...operands, env)`. The operands carry the
|
|
77
|
+
* serialized body, the callback's param names and the per-helper extras (a
|
|
78
|
+
* reduce's initial value and direction, a find's direction). `captured` are
|
|
79
|
+
* the body's free names; the env maps each to its SSR value.
|
|
80
|
+
*/
|
|
81
|
+
export interface CallbackEvalPlan {
|
|
82
|
+
kind: 'eval'
|
|
83
|
+
helper: CallbackEvalHelper
|
|
84
|
+
operands: CallbackEvalOperand[]
|
|
85
|
+
captured: string[]
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** A lowered callback method call, as data. */
|
|
89
|
+
export type CallbackPlan =
|
|
90
|
+
| CallbackEvalPlan
|
|
91
|
+
/** A comparator the evaluator cannot model but the `{keys: …}` descriptor can. */
|
|
92
|
+
| { kind: 'structured-sort'; comparator: SortComparator }
|
|
93
|
+
/** A predicate for the adapter's own native form (see `nativePredicate`). */
|
|
94
|
+
| { kind: 'native-predicate'; method: PredicateMethod; param: string; predicate: ParsedExpr }
|
|
95
|
+
| { kind: 'refuse'; message: string; suggestion: string }
|
|
96
|
+
/** A method outside `CALLBACK_METHODS`; unreachable, as the parser recognises only those. */
|
|
97
|
+
| { kind: 'receiver' }
|
|
98
|
+
|
|
99
|
+
export interface CallbackLoweringOptions {
|
|
100
|
+
/** The display name BF101 uses: "the Jinja adapter". */
|
|
101
|
+
adapterName: string
|
|
102
|
+
/**
|
|
103
|
+
* The adapter lowers a predicate the evaluator cannot model to a native
|
|
104
|
+
* form (Xslate, ERB, Mojolicious). Such an adapter also takes the
|
|
105
|
+
* identity filter (`xs.filter(x => x)`) natively without trying the
|
|
106
|
+
* evaluator.
|
|
107
|
+
*/
|
|
108
|
+
nativePredicate?: boolean
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Lower `<recv>.<method>(arrow, ...restArgs)`. */
|
|
112
|
+
export function lowerCallbackMethod(
|
|
113
|
+
method: string,
|
|
114
|
+
arrow: Arrow,
|
|
115
|
+
restArgs: readonly ParsedExpr[],
|
|
116
|
+
options: CallbackLoweringOptions,
|
|
117
|
+
): CallbackPlan {
|
|
118
|
+
const { adapterName } = options
|
|
119
|
+
const { body, params } = arrow
|
|
120
|
+
if (isPredicateMethod(method)) return lowerPredicate(method, arrow, options)
|
|
121
|
+
if (method === 'sort' || method === 'toSorted') {
|
|
122
|
+
const plan = sortEvalPlan(body, params)
|
|
123
|
+
if (plan) return plan
|
|
124
|
+
const comparator = sortComparatorFromArrow(arrow)
|
|
125
|
+
if (comparator) return { kind: 'structured-sort', comparator }
|
|
126
|
+
return sortRefusal(method, adapterName)
|
|
127
|
+
}
|
|
128
|
+
if (method === 'reduce' || method === 'reduceRight') {
|
|
129
|
+
const init = restArgs[0]
|
|
130
|
+
const plan = init ? reduceEvalPlan(body, params, init, method === 'reduceRight' ? 'right' : 'left') : null
|
|
131
|
+
return (
|
|
132
|
+
plan ?? {
|
|
133
|
+
kind: 'refuse',
|
|
134
|
+
message: `'.${method}(...)' is outside the ${adapterName} adapter's evaluable surface (needs a literal initial value and an evaluable reducer body)`,
|
|
135
|
+
suggestion: `Pre-compute the reduced value, or move this position to a '/* @client */' boundary.`,
|
|
136
|
+
}
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
if (method === 'flatMap' || method === 'map') {
|
|
140
|
+
const plan = projectionEvalPlan(method === 'map' ? 'map_eval' : 'flat_map_eval', body, params)
|
|
141
|
+
return (
|
|
142
|
+
plan ?? {
|
|
143
|
+
kind: 'refuse',
|
|
144
|
+
message: `'.${method}(...)' projection is outside the ${adapterName} adapter's evaluable surface`,
|
|
145
|
+
suggestion: `Pre-compute the projected array, or move this position to a '/* @client */' boundary.`,
|
|
146
|
+
}
|
|
147
|
+
)
|
|
148
|
+
}
|
|
149
|
+
return { kind: 'receiver' }
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function lowerPredicate(method: PredicateMethod, arrow: Arrow, options: CallbackLoweringOptions): CallbackPlan {
|
|
153
|
+
const param = String(arrow.params[0])
|
|
154
|
+
const native: CallbackPlan = { kind: 'native-predicate', method, param, predicate: arrow.body }
|
|
155
|
+
// A native form lowers an opaque call to a read of its callee, silently, and
|
|
156
|
+
// binds no nested callback's index, so such a predicate takes the
|
|
157
|
+
// evaluator or BF101 on every adapter alike.
|
|
158
|
+
const nativeOk =
|
|
159
|
+
options.nativePredicate === true && !hasOpaqueCall(arrow.body) && !readsNestedCallbackIndex(arrow.body)
|
|
160
|
+
const identity = method === 'filter' && arrow.body.kind === 'identifier' && arrow.body.name === param
|
|
161
|
+
if (nativeOk && identity) return native
|
|
162
|
+
const json = serializeParsedExpr(arrow.body)
|
|
163
|
+
if (json !== null) {
|
|
164
|
+
const { helper, forward } = PREDICATE_EVAL[method]
|
|
165
|
+
const operands: CallbackEvalOperand[] = [
|
|
166
|
+
{ kind: 'string', value: json },
|
|
167
|
+
{ kind: 'string', value: param },
|
|
168
|
+
]
|
|
169
|
+
if (forward !== undefined) operands.push({ kind: 'boolean', value: forward })
|
|
170
|
+
return { kind: 'eval', helper, operands, captured: freeVarsInBody(arrow.body, new Set(arrow.params.slice(0, 1))) }
|
|
171
|
+
}
|
|
172
|
+
if (nativeOk) return native
|
|
173
|
+
return {
|
|
174
|
+
kind: 'refuse',
|
|
175
|
+
message: `'.${method}(...)' predicate is outside the ${options.adapterName} adapter's evaluable surface`,
|
|
176
|
+
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.`,
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* The evaluator plan for a sort comparator, or `null` when the body is
|
|
182
|
+
* outside the evaluator surface (a `localeCompare` comparator) or the arrow
|
|
183
|
+
* has fewer than the two params a comparator needs. The loop-source sort
|
|
184
|
+
* shares this with `.sort(cmp)` at an expression position.
|
|
185
|
+
*/
|
|
186
|
+
export function sortEvalPlan(body: ParsedExpr, params: readonly string[]): CallbackEvalPlan | null {
|
|
187
|
+
const json = serializeParsedExpr(body)
|
|
188
|
+
if (json === null || params.length < 2) return null
|
|
189
|
+
return {
|
|
190
|
+
kind: 'eval',
|
|
191
|
+
helper: 'sort_eval',
|
|
192
|
+
operands: [
|
|
193
|
+
{ kind: 'string', value: json },
|
|
194
|
+
{ kind: 'string', value: params[0]! },
|
|
195
|
+
{ kind: 'string', value: params[1]! },
|
|
196
|
+
],
|
|
197
|
+
captured: freeVarsInBody(body, new Set(params)),
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** The BF101 for a comparator neither the evaluator nor the descriptor can model. */
|
|
202
|
+
export function sortRefusal(method: string, adapterName: string): { kind: 'refuse'; message: string; suggestion: string } {
|
|
203
|
+
return {
|
|
204
|
+
kind: 'refuse',
|
|
205
|
+
message: `'.${method}(...)' comparator is outside the ${adapterName} adapter's evaluable / structured surface`,
|
|
206
|
+
suggestion: `Pre-compute the sorted array, or move this position to a '/* @client */' boundary.`,
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* The evaluator plan for a reducer. Only a string or number literal seeds
|
|
212
|
+
* the fold; anything else (and a body outside the evaluator surface, or a
|
|
213
|
+
* reducer without both `acc` and item params) returns `null`.
|
|
214
|
+
*/
|
|
215
|
+
function reduceEvalPlan(
|
|
216
|
+
body: ParsedExpr,
|
|
217
|
+
params: readonly string[],
|
|
218
|
+
init: ParsedExpr,
|
|
219
|
+
direction: 'left' | 'right',
|
|
220
|
+
): CallbackEvalPlan | null {
|
|
221
|
+
const json = serializeParsedExpr(body)
|
|
222
|
+
if (json === null || init.kind !== 'literal' || params.length < 2) return null
|
|
223
|
+
let seed: CallbackEvalOperand
|
|
224
|
+
if (init.literalType === 'string') seed = { kind: 'string', value: String(init.value) }
|
|
225
|
+
else if (init.literalType === 'number') seed = { kind: 'number', text: String(init.value) }
|
|
226
|
+
else return null
|
|
227
|
+
return {
|
|
228
|
+
kind: 'eval',
|
|
229
|
+
helper: 'reduce_eval',
|
|
230
|
+
operands: [
|
|
231
|
+
{ kind: 'string', value: json },
|
|
232
|
+
{ kind: 'string', value: params[0]! },
|
|
233
|
+
{ kind: 'string', value: params[1]! },
|
|
234
|
+
seed,
|
|
235
|
+
{ kind: 'string', value: direction },
|
|
236
|
+
],
|
|
237
|
+
captured: freeVarsInBody(body, new Set(params)),
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function projectionEvalPlan(
|
|
242
|
+
helper: 'map_eval' | 'flat_map_eval',
|
|
243
|
+
body: ParsedExpr,
|
|
244
|
+
params: readonly string[],
|
|
245
|
+
): CallbackEvalPlan | null {
|
|
246
|
+
const json = serializeParsedExpr(body)
|
|
247
|
+
if (json === null) return null
|
|
248
|
+
return {
|
|
249
|
+
kind: 'eval',
|
|
250
|
+
helper,
|
|
251
|
+
operands: [
|
|
252
|
+
{ kind: 'string', value: json },
|
|
253
|
+
{ kind: 'string', value: String(params[0]) },
|
|
254
|
+
],
|
|
255
|
+
captured: freeVarsInBody(body, new Set(params.slice(0, 1))),
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** How one DSL backend spells an evaluator call. */
|
|
260
|
+
export interface CallbackEvalSpelling {
|
|
261
|
+
/** `helper(operands)`: `bf.sort_eval(…)`, `$bf->sort_eval(…)`, `bf->sort_eval(…)`. */
|
|
262
|
+
call(helper: CallbackEvalHelper, operands: readonly string[]): string
|
|
263
|
+
/** A single-quoted string literal, escaped for the backend. */
|
|
264
|
+
string(value: string): string
|
|
265
|
+
/** `true` / `false`, or Perl's `1` / `0`. */
|
|
266
|
+
boolean(value: boolean): string
|
|
267
|
+
/** The env literal mapping each captured name to its SSR value, `{}` when empty. */
|
|
268
|
+
env(entries: ReadonlyArray<readonly [name: string, value: string]>): string
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Emit an evaluator plan. `recv` is the already-emitted receiver; `emit`
|
|
273
|
+
* renders each captured name's SSR value, after the receiver.
|
|
274
|
+
*/
|
|
275
|
+
export function emitCallbackEval(
|
|
276
|
+
plan: CallbackEvalPlan,
|
|
277
|
+
recv: string,
|
|
278
|
+
emit: (e: ParsedExpr) => string,
|
|
279
|
+
spelling: CallbackEvalSpelling,
|
|
280
|
+
): string {
|
|
281
|
+
const operands = [recv]
|
|
282
|
+
for (const operand of plan.operands) {
|
|
283
|
+
if (operand.kind === 'string') operands.push(spelling.string(operand.value))
|
|
284
|
+
else if (operand.kind === 'number') operands.push(operand.text)
|
|
285
|
+
else operands.push(spelling.boolean(operand.value))
|
|
286
|
+
}
|
|
287
|
+
const env = plan.captured.map((name) => [name, emit({ kind: 'identifier', name })] as const)
|
|
288
|
+
operands.push(spelling.env(env))
|
|
289
|
+
return spelling.call(plan.helper, operands)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* The BF101 for a callback method nested inside a filter predicate, which a
|
|
294
|
+
* predicate emitter has no scalar form for.
|
|
295
|
+
*/
|
|
296
|
+
export function nestedCallbackRefusal(method: string, adapterName: string): { message: string; suggestion: string } {
|
|
297
|
+
return {
|
|
298
|
+
message: `Filter predicate contains a nested '.${method}(...)' callback, which has no ${adapterName} scalar form`,
|
|
299
|
+
suggestion: `Rewrite the predicate without a nested callback method, or add /* @client */ for client-only evaluation (no SSR).`,
|
|
300
|
+
}
|
|
301
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Literal-value predicates shared by the DSL template adapters (#3475).
|
|
3
|
+
*
|
|
4
|
+
* Pure functions over analyzer type info and parsed expressions. Every DSL
|
|
5
|
+
* adapter classifies string-typed signals, props and constants with
|
|
6
|
+
* {@link isStringTypeInfo} / {@link isBareStringLiteral}, and tells a
|
|
7
|
+
* literal index from a variable one with {@link isLiteralIndex}.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { ParsedExpr } from '../expression-parser.ts'
|
|
11
|
+
import type { TypeInfo } from '../types.ts'
|
|
12
|
+
|
|
13
|
+
/** True when `type` is the `string` primitive. */
|
|
14
|
+
export function isStringTypeInfo(type: TypeInfo | undefined): boolean {
|
|
15
|
+
return type?.kind === 'primitive' && type.primitive === 'string'
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** True when `initialValue` is a bare string-literal expression. */
|
|
19
|
+
export function isBareStringLiteral(initialValue: string | undefined): boolean {
|
|
20
|
+
if (!initialValue) return false
|
|
21
|
+
const v = initialValue.trim()
|
|
22
|
+
return (v.startsWith("'") && v.endsWith("'")) || (v.startsWith('"') && v.endsWith('"'))
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* True when a parsed index is a numeric or string literal (`arr[0]`,
|
|
27
|
+
* `m['k']`). The spread lowerings refuse (BF101) an indexed spread value
|
|
28
|
+
* they could not inline only when its index is NOT a literal: a
|
|
29
|
+
* variable-index record access (`sizeMap[size]`) has no static answer.
|
|
30
|
+
*/
|
|
31
|
+
export function isLiteralIndex(index: ParsedExpr): boolean {
|
|
32
|
+
return (
|
|
33
|
+
index.kind === 'literal' &&
|
|
34
|
+
(index.literalType === 'number' || index.literalType === 'string')
|
|
35
|
+
)
|
|
36
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The decisions behind a DSL adapter's `renderLoop` (#3473), as data.
|
|
3
|
+
*
|
|
4
|
+
* Every DSL adapter decides the same things about a `.map()` loop before it
|
|
5
|
+
* spells a line of template: whether its destructure pattern lowers (BF104),
|
|
6
|
+
* whether the source is a static literal, whether it names a computed const
|
|
7
|
+
* no template binds (BF101), how its `.sort` comparator lowers, and which
|
|
8
|
+
* filter captures need an alias. {@link planLoop} makes those decisions once,
|
|
9
|
+
* against the scope outside the loop. The adapter keeps the spelling: the
|
|
10
|
+
* loop header, the row bindings, the diagnostics' wording, and the order it
|
|
11
|
+
* emits them in.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { parseExpression } from '../expression-parser.ts'
|
|
15
|
+
import { isLowerableLoopDestructure } from '../loop-destructure.ts'
|
|
16
|
+
import type { BindingScope } from '../scope/binding-scope.ts'
|
|
17
|
+
import { resolveStaticLoopSource } from '../static-literal.ts'
|
|
18
|
+
import type { ConstantInfo, IRLoop } from '../types.ts'
|
|
19
|
+
import { type CallbackLoweringOptions, type CallbackPlan, lowerCallbackMethod } from './callback-method.ts'
|
|
20
|
+
import { filterCaptureCollisions } from './filter-capture-collisions.ts'
|
|
21
|
+
|
|
22
|
+
export interface LoopPlanContext<Spelled> {
|
|
23
|
+
/** The scope outside the loop. */
|
|
24
|
+
scope: BindingScope
|
|
25
|
+
localConstants: readonly ConstantInfo[]
|
|
26
|
+
/** The adapter's literal-const resolution: a literal const inlines at its use site. */
|
|
27
|
+
resolveLiteralConst(name: string): unknown
|
|
28
|
+
callbackLowering: CallbackLoweringOptions
|
|
29
|
+
/** The adapter's spelling of a static source's items, or `null` when the backend cannot represent one. */
|
|
30
|
+
spellStatic(items: unknown[]): Spelled | null
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface LoopPlan<Spelled> {
|
|
34
|
+
/**
|
|
35
|
+
* The loop param's destructure pattern: none, one the adapter lowers to
|
|
36
|
+
* per-binding accessors, or one no template binds (BF104).
|
|
37
|
+
*/
|
|
38
|
+
destructure: 'none' | 'lowerable' | 'unlowerable'
|
|
39
|
+
/**
|
|
40
|
+
* The source spelled as a template literal, when it is an inline literal or
|
|
41
|
+
* an unshadowed static const whose items the backend can represent.
|
|
42
|
+
*/
|
|
43
|
+
staticSource: Spelled | null
|
|
44
|
+
/**
|
|
45
|
+
* The const the source names when it is not spelled statically and no
|
|
46
|
+
* template binds it as a literal (BF101). A loop binding that shadows the
|
|
47
|
+
* name is the row's value, not the const (#3473).
|
|
48
|
+
*/
|
|
49
|
+
unresolvableConst: ConstantInfo | null
|
|
50
|
+
/** The `.sort` / `.toSorted` comparator's lowering, or `null` without one. */
|
|
51
|
+
sort: CallbackPlan | null
|
|
52
|
+
/** The filter predicate's captures that a row binding would shadow, each with its alias. */
|
|
53
|
+
captureAliases: Array<{ name: string; alias: string }>
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Plan a loop against the scope outside it. */
|
|
57
|
+
export function planLoop<Spelled>(loop: IRLoop, ctx: LoopPlanContext<Spelled>): LoopPlan<Spelled> {
|
|
58
|
+
const staticItems = resolveStaticLoopSource(loop.arrayParsed, ctx.localConstants, {
|
|
59
|
+
isNameShadowed: ctx.scope.asShadowPredicate(),
|
|
60
|
+
})
|
|
61
|
+
const staticSource = staticItems !== null ? ctx.spellStatic(staticItems) : null
|
|
62
|
+
return {
|
|
63
|
+
destructure: destructureKind(loop),
|
|
64
|
+
staticSource,
|
|
65
|
+
unresolvableConst: staticSource === null ? unresolvableConst(loop, ctx) : null,
|
|
66
|
+
sort: loop.sortComparator ? lowerCallbackMethod('sort', loop.sortComparator.arrow, [], ctx.callbackLowering) : null,
|
|
67
|
+
captureAliases: filterCaptureCollisions(loop).map((name) => ({ name, alias: `__bf_cap_${loop.markerId}_${name}` })),
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function destructureKind(loop: IRLoop): LoopPlan<unknown>['destructure'] {
|
|
72
|
+
if (!loop.paramBindings || loop.paramBindings.length === 0) return 'none'
|
|
73
|
+
return isLowerableLoopDestructure(loop) ? 'lowerable' : 'unlowerable'
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The source is read from its parsed tree, which unwraps parentheses: the
|
|
78
|
+
* raw text of `(entries).map(…)` is no bare identifier, yet it names the
|
|
79
|
+
* same const.
|
|
80
|
+
*/
|
|
81
|
+
function unresolvableConst(loop: IRLoop, ctx: LoopPlanContext<unknown>): ConstantInfo | null {
|
|
82
|
+
const source = loop.arrayParsed ?? parseExpression(loop.array.trim())
|
|
83
|
+
if (source?.kind !== 'identifier') return null
|
|
84
|
+
const name = source.name
|
|
85
|
+
if (ctx.scope.isBound(name)) return null
|
|
86
|
+
const constant = ctx.localConstants.find((c) => c.name === name)
|
|
87
|
+
return constant && ctx.resolveLiteralConst(name) === null ? constant : null
|
|
88
|
+
}
|