@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,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A component- or module-scope `const` computed from props, signals or other
|
|
3
|
+
* consts (`const cls = 'btn ' + variant`, `const twice = props.n * 2`) has no
|
|
4
|
+
* binding in a template: the backend receives the props, not the
|
|
5
|
+
* component's locals. Every template adapter read such a const as a template
|
|
6
|
+
* variable nothing defines, so it rendered empty, a prop of the same name, or
|
|
7
|
+
* failed the render (#3494).
|
|
8
|
+
*
|
|
9
|
+
* `inlineComputedConsts` substitutes each such read with the const's
|
|
10
|
+
* initializer tree, exactly as if the source had written the expression in
|
|
11
|
+
* place; the adapter then lowers it like any other expression, refusing what
|
|
12
|
+
* it cannot lower (`Object.keys(labels)` has no template form, BF101) instead
|
|
13
|
+
* of reading an unbound name. The substitution is structural
|
|
14
|
+
* (`inlineBinding`), so precedence, string literals and callback params that
|
|
15
|
+
* shadow the name are respected.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { collectContextConsumers, lookupLiteralConst } from '../augment-inherited-props.ts'
|
|
19
|
+
import { freeIdentifiers, inlineBinding, parseExpression, stringifyParsedExpr, type ParsedExpr } from '../expression-parser.ts'
|
|
20
|
+
import type { ConstantInfo, ExpressionAttr, IRMetadata } from '../types.ts'
|
|
21
|
+
import { componentBindings } from '../relocate.ts'
|
|
22
|
+
|
|
23
|
+
export interface ComputedConstContext {
|
|
24
|
+
constants: IRMetadata['localConstants'] | undefined
|
|
25
|
+
/** Signal getters and memo names: the zero-arg calls a template reads as values. */
|
|
26
|
+
getters: ReadonlySet<string>
|
|
27
|
+
/** Context consumers (`const theme = useContext(ThemeContext)`), which the adapter seeds itself. */
|
|
28
|
+
contextConsumers: ReadonlySet<string>
|
|
29
|
+
/** Consts a props destructure binds, which the adapter binds as props params. */
|
|
30
|
+
propsBindings: ReadonlySet<string>
|
|
31
|
+
/** Whether a loop row or callback binds `name` where the expression is read. */
|
|
32
|
+
isShadowed(name: string): boolean
|
|
33
|
+
/** Whether the adapter binds the const `name` itself, so a read of it stays as written. */
|
|
34
|
+
isAdapterBound(name: string): boolean
|
|
35
|
+
/**
|
|
36
|
+
* Whether `name` read where the expression is read is a JavaScript global:
|
|
37
|
+
* bound by neither the component nor its module (`componentBindings`), nor
|
|
38
|
+
* the rest props, nor an enclosing loop or callback.
|
|
39
|
+
*/
|
|
40
|
+
isGlobal(name: string): boolean
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The {@link ComputedConstContext} fields that come from the component's metadata. */
|
|
44
|
+
export function computedConstContext(
|
|
45
|
+
metadata: IRMetadata,
|
|
46
|
+
isShadowed: (name: string) => boolean,
|
|
47
|
+
isAdapterBound: (name: string) => boolean = () => false,
|
|
48
|
+
): ComputedConstContext {
|
|
49
|
+
const bindings = componentBindings(metadata)
|
|
50
|
+
return {
|
|
51
|
+
constants: metadata.localConstants,
|
|
52
|
+
getters: new Set([...metadata.signals.map(s => s.getter), ...metadata.memos.map(m => m.name)]),
|
|
53
|
+
contextConsumers: new Set(collectContextConsumers(metadata).map(c => c.localName)),
|
|
54
|
+
propsBindings: propsBindingNames(metadata),
|
|
55
|
+
isShadowed,
|
|
56
|
+
isAdapterBound,
|
|
57
|
+
isGlobal: name => !bindings.has(name) && name !== metadata.restPropsName && !isShadowed(name),
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Consts a props destructure binds (`const { label: text = 'none' } = props`).
|
|
63
|
+
* The analyzer records each as a props param, renamed and defaulted, and the
|
|
64
|
+
* adapters bind it as one: its default applies to `undefined` alone, which
|
|
65
|
+
* the `??` of its recorded value would not keep on a backend whose `??`
|
|
66
|
+
* also tests `''`. A const merely named like a prop
|
|
67
|
+
* (`const label = props.label ?? 'Default'`, the prop itself carrying no
|
|
68
|
+
* default) is not one.
|
|
69
|
+
*/
|
|
70
|
+
function propsBindingNames(metadata: IRMetadata): Set<string> {
|
|
71
|
+
const names = new Set<string>()
|
|
72
|
+
const props = metadata.propsObjectName
|
|
73
|
+
if (!props) return names
|
|
74
|
+
for (const constant of metadata.localConstants ?? []) {
|
|
75
|
+
const param = metadata.propsParams.find(p => p.name === constant.name)
|
|
76
|
+
const value = constant.value?.trim()
|
|
77
|
+
if (!param || value === undefined) continue
|
|
78
|
+
const read = `${props}.${param.sourceName ?? param.name}`
|
|
79
|
+
if (value === read || (param.defaultValue !== undefined && value === `${read} ?? ${param.defaultValue}`)) {
|
|
80
|
+
names.add(constant.name)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return names
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Upper bound on const-of-const chains, a guard against a malformed cycle. */
|
|
87
|
+
const MAX_DEPTH = 16
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* `expr` with each read of a computed const replaced by its initializer,
|
|
91
|
+
* repeated so a const read inside an initializer is replaced too.
|
|
92
|
+
*/
|
|
93
|
+
export function inlineComputedConsts(expr: ParsedExpr, ctx: ComputedConstContext | null): ParsedExpr {
|
|
94
|
+
if (!ctx?.constants?.length) return expr
|
|
95
|
+
let current = expr
|
|
96
|
+
for (let depth = 0; depth < MAX_DEPTH; depth++) {
|
|
97
|
+
const next = inlineOnce(current, ctx)
|
|
98
|
+
if (next === current) return current
|
|
99
|
+
current = next
|
|
100
|
+
}
|
|
101
|
+
return current
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function inlineOnce(expr: ParsedExpr, ctx: ComputedConstContext): ParsedExpr {
|
|
105
|
+
const free = freeIdentifiers(expr)
|
|
106
|
+
if (!free) return expr
|
|
107
|
+
let result = expr
|
|
108
|
+
for (const name of free) {
|
|
109
|
+
const init = inlinableInitializer(name, ctx)
|
|
110
|
+
if (!init) continue
|
|
111
|
+
result = inlineBinding(result, name, init) ?? result
|
|
112
|
+
}
|
|
113
|
+
return result
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* An `{expr}` attribute value read through {@link inlineComputedConsts}, so
|
|
118
|
+
* the attribute's own plan (a nullable prop read drops the attribute) sees
|
|
119
|
+
* the const's initializer, not its name: `title={label}` with
|
|
120
|
+
* `const label = props.label ?? 'Default'` is not a bare read of `label`.
|
|
121
|
+
*/
|
|
122
|
+
export function inlineComputedConstAttr(value: ExpressionAttr, ctx: ComputedConstContext | null): ExpressionAttr {
|
|
123
|
+
if (!ctx) return value
|
|
124
|
+
const parsed = value.parsed ?? parseExpression(value.expr.trim())
|
|
125
|
+
const inlined = inlineComputedConsts(parsed, ctx)
|
|
126
|
+
if (inlined === parsed) return value
|
|
127
|
+
// `templateExpr` and `parts` are derived from the original `expr`; dropped
|
|
128
|
+
// so no consumer reads the const's name through them.
|
|
129
|
+
return { ...value, expr: stringifyParsedExpr(inlined), parsed: inlined, templateExpr: undefined, parts: undefined }
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The initializer of a const read `name` stands for, or `null` when the read stays as it is. */
|
|
133
|
+
function inlinableInitializer(name: string, ctx: ComputedConstContext): ParsedExpr | null {
|
|
134
|
+
const constant = ctx.constants?.find(c => c.name === name)
|
|
135
|
+
const init = constant?.parsed
|
|
136
|
+
if (!constant || !init || !isPlainComputedConst(constant, init)) return null
|
|
137
|
+
if (ctx.isShadowed(name) || ctx.getters.has(name) || ctx.contextConsumers.has(name) || ctx.propsBindings.has(name)) return null
|
|
138
|
+
if (ctx.isAdapterBound(name)) return null
|
|
139
|
+
// An adapter already inlines a literal const in its own syntax.
|
|
140
|
+
if (lookupLiteralConst(name, ctx.constants, ctx.isShadowed) !== null) return null
|
|
141
|
+
// A free name the read site rebinds would be captured by the substitution.
|
|
142
|
+
const free = freeIdentifiers(init)
|
|
143
|
+
if (!free || [...free].some(n => ctx.isShadowed(n))) return null
|
|
144
|
+
return callsOnlyGetters(init, ctx.getters) ? init : null
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* A `const` holding a value computed by an expression, as opposed to a
|
|
149
|
+
* function, JSX, a static object or array the adapters resolve through their
|
|
150
|
+
* own lookups, or a runtime construct. A `let` no statement reassigns after
|
|
151
|
+
* its declaration holds one value too: its render-time reassignments were
|
|
152
|
+
* folded into the initializer (`fold-render-reassignments.ts`, #3501).
|
|
153
|
+
*/
|
|
154
|
+
function isPlainComputedConst(constant: ConstantInfo, init: ParsedExpr): boolean {
|
|
155
|
+
if (constant.mutatedAfterDeclaration) return false
|
|
156
|
+
if (constant.isJsx || constant.isJsxFunction || constant.containsArrow || constant.isFunctionValue) return false
|
|
157
|
+
if (constant.systemConstructKind) return false
|
|
158
|
+
if (init.kind === 'call' && init.callee.kind === 'identifier' && init.callee.name === 'useContext') return false
|
|
159
|
+
return init.kind !== 'object-literal' && init.kind !== 'array-literal' && init.kind !== 'unsupported'
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Whether every zero-arg identifier call in `init` is a signal or memo read.
|
|
164
|
+
* Any other one (`const label = makeLabel()`) is an opaque accessor: inlined,
|
|
165
|
+
* it would read as a getter of the helper's name, so the const keeps its own
|
|
166
|
+
* refusal path instead.
|
|
167
|
+
*/
|
|
168
|
+
function callsOnlyGetters(init: ParsedExpr, getters: ReadonlySet<string>): boolean {
|
|
169
|
+
let ok = true
|
|
170
|
+
const visit = (node: unknown): void => {
|
|
171
|
+
if (!ok || node === null || typeof node !== 'object') return
|
|
172
|
+
if (Array.isArray(node)) {
|
|
173
|
+
node.forEach(visit)
|
|
174
|
+
return
|
|
175
|
+
}
|
|
176
|
+
const e = node as ParsedExpr
|
|
177
|
+
if (e.kind === 'call' && e.callee.kind === 'identifier' && e.args.length === 0 && !getters.has(e.callee.name)) {
|
|
178
|
+
ok = false
|
|
179
|
+
return
|
|
180
|
+
}
|
|
181
|
+
for (const value of Object.values(e)) visit(value)
|
|
182
|
+
}
|
|
183
|
+
visit(init)
|
|
184
|
+
return ok
|
|
185
|
+
}
|
|
@@ -243,3 +243,36 @@ export function dangerousInnerHtmlDiagnostic(
|
|
|
243
243
|
},
|
|
244
244
|
}
|
|
245
245
|
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* The children `element`'s `dangerouslySetInnerHTML` renders in place of
|
|
249
|
+
* its own, or `null` when it has none. A refusal is pushed onto `errors`
|
|
250
|
+
* and renders as `''`. `rawSink` writes a dynamic value through the
|
|
251
|
+
* adapter's raw-output sink; the runtime evaluates the expression, so no
|
|
252
|
+
* template-metachar guard applies, and the element already carries its
|
|
253
|
+
* hydration slot marker (#2319). A static literal is spliced as trusted
|
|
254
|
+
* template text after the `adapterId` metachar guard.
|
|
255
|
+
*/
|
|
256
|
+
export function renderDangerousInnerHtml(
|
|
257
|
+
element: IRElement,
|
|
258
|
+
errors: CompilerError[],
|
|
259
|
+
adapterId: string,
|
|
260
|
+
rawSink: (valueExpr: string, valueParsed: ParsedExpr, loc: SourceLocation) => string,
|
|
261
|
+
): string | null {
|
|
262
|
+
const resolution = resolveDangerousInnerHtml(element)
|
|
263
|
+
if (!resolution) return null
|
|
264
|
+
if (resolution.kind === 'unlowerable') {
|
|
265
|
+
errors.push(dangerousInnerHtmlDiagnostic(resolution.expr, resolution.loc))
|
|
266
|
+
return ''
|
|
267
|
+
}
|
|
268
|
+
if (resolution.kind === 'dynamic') {
|
|
269
|
+
return rawSink(resolution.valueExpr, resolution.valueParsed, resolution.loc)
|
|
270
|
+
}
|
|
271
|
+
const violation = dangerousInnerHtmlMetacharViolation(resolution.html, adapterId)
|
|
272
|
+
if (violation) {
|
|
273
|
+
const attr = element.attrs.find(isDangerousInnerHtmlAttr)!
|
|
274
|
+
errors.push(dangerousInnerHtmlDiagnostic(`{ __html: ${JSON.stringify(resolution.html)} }`, attr.loc, violation))
|
|
275
|
+
return ''
|
|
276
|
+
}
|
|
277
|
+
return resolution.html
|
|
278
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The HTML element every template adapter writes the same way (#3475): its
|
|
3
|
+
* attributes, its hydration markers, its children, and the void-element
|
|
4
|
+
* form. The adapter supplies how each value is spelled in its template
|
|
5
|
+
* language; the order and the conditions live here once.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { BF_PORTAL_OWNER, BF_REGION } from '@barefootjs/shared'
|
|
9
|
+
import type { IRElement, IRFragment, IRNode } from '../types.ts'
|
|
10
|
+
import { emitAttrValue, type AttrValueEmitter } from './attr-value-emitter.ts'
|
|
11
|
+
import { isDangerousInnerHtmlAttr } from './dangerous-inner-html.ts'
|
|
12
|
+
|
|
13
|
+
const VOID_ELEMENTS = [
|
|
14
|
+
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
|
|
15
|
+
'link', 'meta', 'param', 'source', 'track', 'wbr',
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The element's own attributes, each lowered through `emitter`, as a
|
|
20
|
+
* leading-space list (`''` when none renders).
|
|
21
|
+
*/
|
|
22
|
+
export function renderElementAttributes(element: IRElement, emitter: AttrValueEmitter): string {
|
|
23
|
+
const parts: string[] = []
|
|
24
|
+
|
|
25
|
+
for (const attr of element.attrs) {
|
|
26
|
+
// `/* @client */` attribute bindings are deferred to hydrate: the
|
|
27
|
+
// client runtime sets/patches the attribute in a mount effect (the
|
|
28
|
+
// CSR template omits it; ir-to-client-js emits the setAttribute
|
|
29
|
+
// effect). Skip SSR emission so the server omits the attribute and
|
|
30
|
+
// the unsupported-expression lowering is never reached for a deferred
|
|
31
|
+
// predicate (no BF101 / BF102). #1966
|
|
32
|
+
if (attr.clientOnly) continue
|
|
33
|
+
// `dangerouslySetInnerHTML` never renders as an HTML attribute — it's
|
|
34
|
+
// handled by `renderDangerousInnerHtml` instead, which replaces the
|
|
35
|
+
// element's children. Skip it here so its `{ __html: ... }` object
|
|
36
|
+
// literal never reaches the generic object-literal BF101 refusal
|
|
37
|
+
// (which would double-report alongside the purpose-built one).
|
|
38
|
+
if (isDangerousInnerHtmlAttr(attr)) continue
|
|
39
|
+
// `key` never renders as a literal HTML attribute — #2753 resolves it
|
|
40
|
+
// onto `element.keyAttr` (name AND value) at IR-build time, emitted
|
|
41
|
+
// separately by `renderElementShell`. A stray `key` that jsx-to-ir.ts did
|
|
42
|
+
// NOT recognize as a loop row root (so `keyAttr` was never set) has no
|
|
43
|
+
// defined rendering outside a `.map()` anyway; drop it rather than
|
|
44
|
+
// leak an invalid `key="..."` HTML attribute.
|
|
45
|
+
if (attr.name === 'key') continue
|
|
46
|
+
// Rewrite JSX special-prop names to their HTML-attribute counterparts (#1475).
|
|
47
|
+
const attrName = attr.name === 'className' ? 'class' : attr.name
|
|
48
|
+
const lowered = emitAttrValue(attr.value, emitter, attrName)
|
|
49
|
+
if (lowered) parts.push(lowered)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return parts.length > 0 ? ' ' + parts.join(' ') : ''
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** How one adapter spells the parts of an element {@link renderElementShell} writes. */
|
|
56
|
+
export interface ElementShellSpelling {
|
|
57
|
+
/** Lowers each attribute value, the row key included. */
|
|
58
|
+
attrEmitter: AttrValueEmitter
|
|
59
|
+
/** The element's `dangerouslySetInnerHTML` content, or `null` when it has none. */
|
|
60
|
+
renderDangerousInnerHtml(element: IRElement): string | null
|
|
61
|
+
renderChildren(children: IRNode[]): string
|
|
62
|
+
renderScopeMarker(instanceIdExpr: string): string
|
|
63
|
+
renderSlotMarker(slotId: string): string
|
|
64
|
+
/**
|
|
65
|
+
* The raw print of the runtime's `data-key` attribute, for a render root
|
|
66
|
+
* that carries whatever key its own caller supplies (the `bf` instance's
|
|
67
|
+
* `data_key` field).
|
|
68
|
+
*/
|
|
69
|
+
dataKeyRelay: string
|
|
70
|
+
/** The escaped print of this instance's scope id, the `bf-po` value. */
|
|
71
|
+
scopeIdValue: string
|
|
72
|
+
/** Sends an SSR-portal element to the `bf.portals()` outlet instead of placing it inline. */
|
|
73
|
+
wrapSsrPortalElement(rendered: string): string
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The element, from its open tag to its close tag. */
|
|
77
|
+
export function renderElementShell(element: IRElement, spelling: ElementShellSpelling): string {
|
|
78
|
+
const tag = element.tag
|
|
79
|
+
const attrs = renderElementAttributes(element, spelling.attrEmitter)
|
|
80
|
+
const dangerousHtml = spelling.renderDangerousInnerHtml(element)
|
|
81
|
+
const children = dangerousHtml !== null ? dangerousHtml : spelling.renderChildren(element.children)
|
|
82
|
+
|
|
83
|
+
let hydrationAttrs = ''
|
|
84
|
+
if (element.needsScope) {
|
|
85
|
+
hydrationAttrs += ` ${spelling.renderScopeMarker('')}`
|
|
86
|
+
}
|
|
87
|
+
// #2753: `element.keyAttr` is the ONE IR-resolved decision for this
|
|
88
|
+
// element's row-key attribute — replacing both the `carriesDataKey`
|
|
89
|
+
// boolean and the adapters' own `rootScopeNodes`/`needsScope` check
|
|
90
|
+
// (mechanism 2: a render-root relay, including the #2732 fragment-root
|
|
91
|
+
// case) and the separate `currentLoopKeyDepth`-driven rewrite of a
|
|
92
|
+
// literal `key` attribute the adapters used to do in `renderAttributes`
|
|
93
|
+
// (mechanism 1: a `.map()` row root compiled inline here).
|
|
94
|
+
if (element.keyAttr) {
|
|
95
|
+
if (element.keyAttr.value !== undefined) {
|
|
96
|
+
const lowered = emitAttrValue(
|
|
97
|
+
{ kind: 'expression', expr: element.keyAttr.value },
|
|
98
|
+
spelling.attrEmitter,
|
|
99
|
+
element.keyAttr.name,
|
|
100
|
+
)
|
|
101
|
+
if (lowered) hydrationAttrs += ` ${lowered}`
|
|
102
|
+
} else {
|
|
103
|
+
// Relay: this element is one of THIS component's own render roots,
|
|
104
|
+
// carrying whatever key its OWN caller supplies at runtime when this
|
|
105
|
+
// component is itself invoked as a caller's keyed loop row. Mirrors
|
|
106
|
+
// Hono stamping data-key on each loop item's root, including
|
|
107
|
+
// early-return (if-statement) roots (#1297), and #2732's fragment-root
|
|
108
|
+
// case.
|
|
109
|
+
hydrationAttrs += ` ${spelling.dataKeyRelay}`
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (element.slotId) {
|
|
113
|
+
hydrationAttrs += ` ${spelling.renderSlotMarker(element.slotId)}`
|
|
114
|
+
}
|
|
115
|
+
// Page-lifecycle boundary lowered from `<Region>` (spec/router.md). The id
|
|
116
|
+
// is a deterministic static string (`<file scope>:<index>`), so it emits as
|
|
117
|
+
// a plain literal attribute — no template tag.
|
|
118
|
+
if (element.regionId) {
|
|
119
|
+
hydrationAttrs += ` ${BF_REGION}="${element.regionId}"`
|
|
120
|
+
}
|
|
121
|
+
// #3119: the `ref`-callback SSR-portal pattern (`ssrPortalOwnerScope`,
|
|
122
|
+
// `isSsrPortalRefCallback` in `@barefootjs/jsx`) — the overlay/content
|
|
123
|
+
// pattern the dialog-style primitives use. Stamp `bf-po` directly on
|
|
124
|
+
// this element's own tag, matching exactly what the client
|
|
125
|
+
// `createPortal(el, document.body, { ownerScope })` stamps onto the
|
|
126
|
+
// SAME element at hydrate time (`packages/client/src/runtime/
|
|
127
|
+
// portal.ts`), mirroring the Hono reference adapter's identical
|
|
128
|
+
// `BF_PORTAL_OWNER` stamp. The element itself is then routed through
|
|
129
|
+
// `wrapSsrPortalElement` below instead of returned for inline
|
|
130
|
+
// placement, so it renders at the portal outlet the app's own layout
|
|
131
|
+
// places near `</body>`.
|
|
132
|
+
if (element.ssrPortalOwnerScope) {
|
|
133
|
+
hydrationAttrs += ` ${BF_PORTAL_OWNER}="${spelling.scopeIdValue}"`
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const rendered = VOID_ELEMENTS.includes(tag.toLowerCase())
|
|
137
|
+
? `<${tag}${attrs}${hydrationAttrs}>`
|
|
138
|
+
: `<${tag}${attrs}${hydrationAttrs}>${children}</${tag}>`
|
|
139
|
+
|
|
140
|
+
if (element.ssrPortalOwnerScope) {
|
|
141
|
+
return spelling.wrapSsrPortalElement(rendered)
|
|
142
|
+
}
|
|
143
|
+
return rendered
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* A fragment's children. A component-root fragment has no element of its
|
|
148
|
+
* own to carry the scope, so `wrapScopeComment` brackets the children with
|
|
149
|
+
* the comment-based scope marker pair; its end marker bounds the scope's
|
|
150
|
+
* sibling range, or queries from the fragment scope leak onto later
|
|
151
|
+
* siblings owned by the parent (#2289).
|
|
152
|
+
*/
|
|
153
|
+
export function renderFragmentScope(
|
|
154
|
+
fragment: IRFragment,
|
|
155
|
+
renderChildren: (children: IRNode[]) => string,
|
|
156
|
+
wrapScopeComment: (rendered: string) => string,
|
|
157
|
+
): string {
|
|
158
|
+
const children = renderChildren(fragment.children)
|
|
159
|
+
return fragment.needsScopeComment ? wrapScopeComment(children) : children
|
|
160
|
+
}
|
|
@@ -388,6 +388,14 @@ export abstract class BaseAdapter implements TemplateAdapter {
|
|
|
388
388
|
return children.map((child) => this.renderNode(child)).join('')
|
|
389
389
|
}
|
|
390
390
|
|
|
391
|
+
/** `null` for a `null` / `undefined` expression, which renders nothing; otherwise the node. */
|
|
392
|
+
protected renderNodeOrNull(node: IRNode): string | null {
|
|
393
|
+
if (node.type === 'expression' && (node.expr === 'null' || node.expr === 'undefined')) {
|
|
394
|
+
return null
|
|
395
|
+
}
|
|
396
|
+
return this.renderNode(node)
|
|
397
|
+
}
|
|
398
|
+
|
|
391
399
|
/** Default: render fallback + children inline (no streaming). Override for streaming support. */
|
|
392
400
|
renderAsync(node: IRAsync): string {
|
|
393
401
|
return this.renderNode(node.fallback) + this.renderChildren(node.children)
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prop classification shared by the DSL template adapters (#3475).
|
|
3
|
+
*
|
|
4
|
+
* Pure functions over `ir.metadata` that derive per-compile prop sets an
|
|
5
|
+
* adapter consults during lowering; no adapter instance state.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import type { ComponentIR } from '../types.ts'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Props whose declared TS type is boolean. A bare binding of one
|
|
12
|
+
* (`data-active={props.isActive}`) must stringify as JS `String(boolean)`
|
|
13
|
+
* ("true"/"false"), not the host language's own boolean text (Python's
|
|
14
|
+
* `True`, Perl's `1` / `''`) (#1897, pagination's data-active).
|
|
15
|
+
*/
|
|
16
|
+
export function collectBooleanTypedProps(ir: ComponentIR): Set<string> {
|
|
17
|
+
return new Set(
|
|
18
|
+
ir.metadata.propsParams
|
|
19
|
+
.filter(prop => prop.type?.primitive === 'boolean' || prop.type?.raw === 'boolean')
|
|
20
|
+
.map(prop => prop.name),
|
|
21
|
+
)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Props whose presence is uncertain: no default, and non-primitive typed OR
|
|
26
|
+
* declared optional (#2259, e.g. textarea's `rows`). A bare reference to one
|
|
27
|
+
* is absent (`None` / `nil` / `null` / `undef`) when the caller omits it, so
|
|
28
|
+
* each adapter guards it with its own presence test before rendering.
|
|
29
|
+
*/
|
|
30
|
+
export function collectNullableOptionalProps(ir: ComponentIR): Set<string> {
|
|
31
|
+
return new Set([
|
|
32
|
+
...ir.metadata.propsParams
|
|
33
|
+
.filter(
|
|
34
|
+
p =>
|
|
35
|
+
p.defaultValue === undefined &&
|
|
36
|
+
!p.isRest &&
|
|
37
|
+
(p.type?.kind !== 'primitive' || p.optional),
|
|
38
|
+
)
|
|
39
|
+
.map(p => p.name),
|
|
40
|
+
// A closed-type `{...rest}` key (#3057) is never destructured with its
|
|
41
|
+
// own default the way a normal prop can be, so it is always presence-
|
|
42
|
+
// uncertain the same way a no-default optional prop is — guard it the
|
|
43
|
+
// same way so `<span {...rest}>` omits a caller-omitted key instead of
|
|
44
|
+
// rendering `key=""`. Harmless for a required (non-optional) rest key
|
|
45
|
+
// too: the guard simply always passes for it.
|
|
46
|
+
...ir.metadata.restPropsExpandedKeys,
|
|
47
|
+
])
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface BooleanTypedPropContext {
|
|
51
|
+
/** The SolidJS-style props identifier (`function C(props: P)`), if any. */
|
|
52
|
+
propsObjectName: string | null
|
|
53
|
+
/** Props typed `boolean` (`collectBooleanTypedProps`). */
|
|
54
|
+
booleanTypedProps: ReadonlySet<string>
|
|
55
|
+
/** A binding at the read site shadows `name`. */
|
|
56
|
+
isBound(name: string): boolean
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Whether `expr` is a bare read of a boolean-typed prop (`open`,
|
|
61
|
+
* `props.open`), which renders through `bool_str`. Inside a `.map()`
|
|
62
|
+
* callback, a param that shares the prop's name is the row binding, not the
|
|
63
|
+
* prop (#2488).
|
|
64
|
+
*/
|
|
65
|
+
export function isBooleanTypedPropRef(expr: string, ctx: BooleanTypedPropContext): boolean {
|
|
66
|
+
let bare = expr.trim()
|
|
67
|
+
if (ctx.propsObjectName && bare.startsWith(`${ctx.propsObjectName}.`)) {
|
|
68
|
+
bare = bare.slice(ctx.propsObjectName.length + 1)
|
|
69
|
+
}
|
|
70
|
+
if (!/^[A-Za-z_$][\w$]*$/.test(bare)) return false
|
|
71
|
+
if (ctx.isBound(bare)) return false
|
|
72
|
+
return ctx.booleanTypedProps.has(bare)
|
|
73
|
+
}
|