@barefootjs/jsx 0.41.2 → 0.41.3
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/caller-value-reach.d.ts +57 -0
- package/dist/caller-value-reach.d.ts.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +877 -561
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/static-literal.d.ts +21 -1
- package/dist/static-literal.d.ts.map +1 -1
- package/dist/types.d.ts +18 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/loop-branch-bare-expression-reactive-text.test.ts +212 -56
- package/src/__tests__/nested-loop-conditional.test.ts +16 -16
- package/src/__tests__/static-literal.test.ts +47 -2
- package/src/caller-value-reach.ts +436 -0
- package/src/index.ts +1 -1
- package/src/ir-to-client-js/collect-elements.ts +25 -46
- package/src/jsx-to-ir.ts +17 -0
- package/src/static-literal.ts +55 -1
- package/src/types.ts +18 -1
package/src/static-literal.ts
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
* (`packages/adapter-<name>/src/adapter/lib/static-value.ts`).
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
import type
|
|
18
|
+
import { freeIdentifiers, mapParsedExpr, type ParsedExpr } from './expression-parser.ts'
|
|
19
19
|
import type { ConstantInfo } from './types.ts'
|
|
20
20
|
|
|
21
21
|
/**
|
|
@@ -109,6 +109,60 @@ export function evaluateStaticLiteral(
|
|
|
109
109
|
}
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
+
/**
|
|
113
|
+
* The expression a compile-time-unrolled loop row lowers, with every part
|
|
114
|
+
* that reads the row replaced by its value (#3798). `bindings` maps the
|
|
115
|
+
* unrolled loop parameters (outer to inner) to the row values the caller
|
|
116
|
+
* already resolved. Each maximal subtree that reads a bound name and
|
|
117
|
+
* evaluates ({@link evaluateStaticLiteral}) to a scalar becomes a `literal`
|
|
118
|
+
* node: `q` becomes `'x'` and `item.label` the label. A template literal keeps
|
|
119
|
+
* its shape and folds the parts inside its `${}`.
|
|
120
|
+
* The rest of the tree is left as is, so a backend lowers it through its
|
|
121
|
+
* ordinary path: `q === 'x' ? props.label : '0'` becomes
|
|
122
|
+
* `'x' === 'x' ? props.label : '0'`, which reads the component's own
|
|
123
|
+
* scope as it does outside a loop.
|
|
124
|
+
*
|
|
125
|
+
* Returns `null` when a bound name remains in the result: a row value that
|
|
126
|
+
* is not a scalar (an object read by something other than a static member
|
|
127
|
+
* chain), or a bound name the evaluator cannot resolve. The caller refuses
|
|
128
|
+
* rather than lower a name that has no `{{range}}` behind it. An arrow
|
|
129
|
+
* parameter named like a bound name shadows it.
|
|
130
|
+
*/
|
|
131
|
+
export function foldStaticBindings(
|
|
132
|
+
expr: ParsedExpr,
|
|
133
|
+
bindings: ReadonlyMap<string, unknown>,
|
|
134
|
+
): ParsedExpr | null {
|
|
135
|
+
if (bindings.size === 0) return expr
|
|
136
|
+
const folded = mapParsedExpr(expr, (e, enclosing) => {
|
|
137
|
+
// A template literal keeps its node (only its `${}` parts fold): a
|
|
138
|
+
// backend lowers it to text interleaved with actions, not to one value.
|
|
139
|
+
if (e.kind === 'literal' || e.kind === 'unsupported' || e.kind === 'template-literal') return null
|
|
140
|
+
const visible = new Map([...bindings].filter(([name]) => !enclosing.has(name)))
|
|
141
|
+
const free = freeIdentifiers(e)
|
|
142
|
+
if (free === null || ![...free].some(name => visible.has(name))) return null
|
|
143
|
+
const resolved = evaluateStaticLiteral(e, visible)
|
|
144
|
+
return resolved === null ? null : scalarToLiteral(resolved.value)
|
|
145
|
+
})
|
|
146
|
+
const remaining = freeIdentifiers(folded)
|
|
147
|
+
if (remaining === null || [...remaining].some(name => bindings.has(name))) return null
|
|
148
|
+
return folded
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** A string, number, boolean or `null` value as a `literal` node, or `null` for anything else (an absent member reads `undefined`, which a backend lowers differently, so it is refused). */
|
|
152
|
+
function scalarToLiteral(value: unknown): ParsedExpr | null {
|
|
153
|
+
if (value === null) return { kind: 'literal', value: null, literalType: 'null' }
|
|
154
|
+
switch (typeof value) {
|
|
155
|
+
case 'string':
|
|
156
|
+
return { kind: 'literal', value, literalType: 'string' }
|
|
157
|
+
case 'boolean':
|
|
158
|
+
return { kind: 'literal', value, literalType: 'boolean' }
|
|
159
|
+
case 'number':
|
|
160
|
+
return Number.isFinite(value) ? { kind: 'literal', value, literalType: 'number' } : null
|
|
161
|
+
default:
|
|
162
|
+
return null
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
112
166
|
/** `true` iff {@link evaluateStaticLiteral} resolves `expr` with no bindings. */
|
|
113
167
|
export function isFullyStaticLiteral(expr: ParsedExpr): boolean {
|
|
114
168
|
return evaluateStaticLiteral(expr) !== null
|
package/src/types.ts
CHANGED
|
@@ -562,6 +562,23 @@ export interface IRExpression {
|
|
|
562
562
|
* is never flagged and stays escaped text.
|
|
563
563
|
*/
|
|
564
564
|
loopRowPropRead?: true
|
|
565
|
+
/**
|
|
566
|
+
* Set on a conditional branch that contains a call and reads a value the
|
|
567
|
+
* component's caller supplied that may be, or may build, a live element:
|
|
568
|
+
* anything reached from the props other than a prop whose declared type
|
|
569
|
+
* holds data only (`caller-value-reach.ts` has the full rule). The read
|
|
570
|
+
* can be the callee (`props.renderNode(n)`, `props['renderNode'](n)`), an
|
|
571
|
+
* argument (`apply(props.renderNode, n)`), or a component declaration
|
|
572
|
+
* that reads one (`const draw = n => props.renderNode(n)`). Decided once
|
|
573
|
+
* in Phase 1 from the component's own declarations and the props type.
|
|
574
|
+
*
|
|
575
|
+
* Such a branch may hold an element the caller built
|
|
576
|
+
* (`renderNode={(n) => <Pill id={n.id} />}`), which owns its own state.
|
|
577
|
+
* A loop row keeps the element it got when the branch was mounted rather
|
|
578
|
+
* than evaluating the branch again each time the row's item changes, so
|
|
579
|
+
* the client gives this branch no update effect.
|
|
580
|
+
*/
|
|
581
|
+
readsCallerValue?: true
|
|
565
582
|
/** When true, expression calls signal getters or memos (has reactive `foo()` pattern). */
|
|
566
583
|
callsReactiveGetters?: boolean
|
|
567
584
|
/** When true, expression contains function call(s) — any `identifier()` pattern (computed from AST). */
|
|
@@ -1390,7 +1407,7 @@ export interface IRIfStatement {
|
|
|
1390
1407
|
export type IRTemplatePart =
|
|
1391
1408
|
| { type: 'string'; value: string }
|
|
1392
1409
|
| { type: 'expression'; expr: JsSourceText; templateExpr?: JsSourceText; parsed: ParsedExpr }
|
|
1393
|
-
| { type: 'ternary'; condition: string; templateCondition?: string; whenTrue: string; whenFalse: string }
|
|
1410
|
+
| { type: 'ternary'; condition: string; templateCondition?: string; parsedCondition?: ParsedExpr; whenTrue: string; whenFalse: string }
|
|
1394
1411
|
/**
|
|
1395
1412
|
* `${MAP[KEY]}` indexed lookup against a `Record<T, string>` literal
|
|
1396
1413
|
* (e.g. `${variantClasses[variant]}` where `variantClasses` is a
|