@barefootjs/jsx 0.39.9 → 0.40.1
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/ambiguous-nil-arithmetic.d.ts +41 -0
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -0
- package/dist/adapters/attr-plan.d.ts +1 -20
- package/dist/adapters/attr-plan.d.ts.map +1 -1
- package/dist/adapters/boolean-carrier.d.ts +31 -0
- package/dist/adapters/boolean-carrier.d.ts.map +1 -0
- package/dist/adapters/component-props.d.ts +22 -7
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +17 -0
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +95 -2
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/loop-plan.d.ts +41 -1
- package/dist/adapters/loop-plan.d.ts.map +1 -1
- package/dist/adapters/number-value.d.ts +43 -0
- package/dist/adapters/number-value.d.ts.map +1 -0
- package/dist/analyzer-context.d.ts +7 -0
- package/dist/analyzer-context.d.ts.map +1 -1
- package/dist/analyzer.d.ts +34 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/css-layer-prefixer.d.ts.map +1 -1
- package/dist/debug.d.ts.map +1 -1
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/index.d.ts +10 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2102 -1344
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts +29 -0
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-lazy-row.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/build-lazy-row.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-reactive-effects.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/inner-loop.d.ts +5 -4
- package/dist/ir-to-client-js/control-flow/plan/inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +15 -0
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/reactive-effects.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/reactive-effects.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +20 -0
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/lazy-row.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts +3 -2
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +7 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
- package/dist/ir-to-client-js/safe-html.d.ts +9 -3
- package/dist/ir-to-client-js/safe-html.d.ts.map +1 -1
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/types.d.ts +30 -0
- package/dist/ir-to-client-js/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +9 -1
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/module-exports.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +29 -3
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/props-binding.d.ts +27 -3
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/scope/binding-scope.d.ts +24 -1
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/scope/root-prop-alias.d.ts +14 -1
- package/dist/scope/root-prop-alias.d.ts.map +1 -1
- package/dist/signal-prop-name-collision.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts +4 -2
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +14 -0
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/template-parts.d.ts +1 -1
- package/dist/template-parts.d.ts.map +1 -1
- package/dist/types.d.ts +35 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/attr-plan.test.ts +1 -14
- package/src/__tests__/binding-scope-ratchet.test.ts +12 -2
- package/src/__tests__/boolean-carrier.test.ts +74 -0
- package/src/__tests__/client-template-escape-soundness.test.ts +17 -4
- package/src/__tests__/client-template-static-delimiters.test.ts +57 -0
- package/src/__tests__/component-props.test.ts +14 -0
- package/src/__tests__/computed-const.test.ts +85 -0
- package/src/__tests__/dsl-template-adapter.test.ts +70 -0
- package/src/__tests__/expression-parser.test.ts +24 -0
- package/src/__tests__/extract-function-params.test.ts +37 -0
- package/src/__tests__/issue-2960-multi-root-cond-same-tag.test.ts +18 -0
- package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +142 -0
- package/src/__tests__/jsx-text-linebreak-whitespace.test.ts +11 -0
- package/src/__tests__/loop-plan.test.ts +66 -1
- package/src/__tests__/loop-row-prop-read.test.ts +111 -0
- package/src/__tests__/memo-body-expr.test.ts +137 -0
- package/src/__tests__/nullable-signal-attr.test.ts +97 -2
- package/src/__tests__/profile-binding-ids.test.ts +14 -0
- package/src/__tests__/root-prop-alias.test.ts +34 -1
- package/src/__tests__/signal-prop-name-collision.test.ts +102 -2
- package/src/__tests__/source-file-cache.test.ts +105 -0
- package/src/__tests__/ssr-seed-plan.test.ts +43 -0
- package/src/__tests__/template-parts-literal-boundary.test.ts +41 -0
- package/src/adapters/ambiguous-nil-arithmetic.ts +94 -0
- package/src/adapters/attr-plan.ts +8 -56
- package/src/adapters/boolean-carrier.ts +76 -0
- package/src/adapters/component-props.ts +28 -8
- package/src/adapters/computed-const.ts +206 -7
- package/src/adapters/dsl-template-adapter.ts +183 -2
- package/src/adapters/loop-plan.ts +67 -2
- package/src/adapters/number-value.ts +95 -0
- package/src/analyzer-context.ts +8 -0
- package/src/analyzer.ts +187 -66
- package/src/augment-inherited-props.ts +5 -9
- package/src/css-layer-prefixer.ts +4 -5
- package/src/debug.ts +1 -0
- package/src/expression-parser.ts +4 -1
- package/src/index.ts +23 -5
- package/src/ir-to-client-js/collect-elements.ts +104 -46
- package/src/ir-to-client-js/compute-inlinability.ts +2 -2
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +15 -1
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +79 -4
- package/src/ir-to-client-js/control-flow/plan/build-lazy-row.ts +3 -0
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/build-reactive-effects.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/inner-loop.ts +5 -4
- package/src/ir-to-client-js/control-flow/plan/loop-child-arm.ts +2 -0
- package/src/ir-to-client-js/control-flow/plan/loop.ts +12 -0
- package/src/ir-to-client-js/control-flow/plan/reactive-effects.ts +2 -0
- package/src/ir-to-client-js/control-flow/shared.ts +3 -2
- package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +27 -0
- package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +8 -2
- package/src/ir-to-client-js/control-flow/stringify/event-listener.ts +8 -1
- package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +8 -7
- package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +8 -7
- package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +4 -4
- package/src/ir-to-client-js/control-flow/stringify/loop.ts +3 -3
- package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +6 -6
- package/src/ir-to-client-js/emit-reactive.ts +1 -1
- package/src/ir-to-client-js/html-template.ts +122 -75
- package/src/ir-to-client-js/plan/build-static-array-child-init.ts +11 -1
- package/src/ir-to-client-js/plan/static-array-child-init.ts +7 -1
- package/src/ir-to-client-js/reactivity.ts +14 -0
- package/src/ir-to-client-js/safe-html.ts +11 -4
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +6 -4
- package/src/ir-to-client-js/types.ts +23 -0
- package/src/ir-to-client-js/utils.ts +16 -2
- package/src/jsx-to-ir.ts +112 -55
- package/src/loop-destructure.ts +4 -1
- package/src/module-exports.ts +13 -21
- package/src/nullable-signal-attr.ts +134 -13
- package/src/props-binding.ts +54 -3
- package/src/rich-type-refusal.ts +3 -1
- package/src/scope/binding-scope.ts +35 -2
- package/src/scope/root-prop-alias.ts +17 -1
- package/src/signal-prop-name-collision.ts +113 -22
- package/src/ssr-defaults.ts +10 -4
- package/src/ssr-seed-plan.ts +82 -17
- package/src/template-parts.ts +25 -3
- package/src/template-position-walk.ts +3 -1
- package/src/types.ts +32 -1
|
@@ -60,6 +60,13 @@ export interface ComponentPropsOptions {
|
|
|
60
60
|
propsObjectName: string | null
|
|
61
61
|
/** The analyzer-extracted props params, which `{...props}` enumerates. */
|
|
62
62
|
propsParams: ReadonlyArray<ComponentPropsParam>
|
|
63
|
+
/**
|
|
64
|
+
* Whether the backend's runtime records which keys each component's
|
|
65
|
+
* caller passed, so a template can ask it whether a param was passed
|
|
66
|
+
* rather than whether it is nil (the DSL runtimes' `passed(name,
|
|
67
|
+
* fallback)`). See {@link propsSpreadParam}.
|
|
68
|
+
*/
|
|
69
|
+
callerKeys?: boolean
|
|
63
70
|
}
|
|
64
71
|
|
|
65
72
|
/** What a call has set before a `{...props}` spread, as far as the compiler can see. */
|
|
@@ -82,14 +89,25 @@ export interface EarlierComponentProps {
|
|
|
82
89
|
* - `'present'`: carried only when the caller passed it, so an absent param
|
|
83
90
|
* leaves the earlier value alone.
|
|
84
91
|
*
|
|
85
|
-
* A template
|
|
86
|
-
* `null` one as the same nil, so "passed"
|
|
87
|
-
* caller that passes `
|
|
88
|
-
*
|
|
92
|
+
* A template engine reads an absent prop and an explicit `undefined` or
|
|
93
|
+
* `null` one as the same nil, so "passed" can't be spelled "not nil": a
|
|
94
|
+
* caller that passes `null` must still override the earlier value, as JS
|
|
95
|
+
* does. With `callerKeys`, the runtime answers "passed" from the keys the
|
|
96
|
+
* caller actually handed over, and every optional param is `'present'`:
|
|
97
|
+
* an `'entry'` would hand the child the key even when the caller left it
|
|
98
|
+
* out, and the child would then read it as passed (a nil overriding its
|
|
99
|
+
* own earlier prop in a further `{...props}`). Without `callerKeys` (Go,
|
|
100
|
+
* whose Input struct can't tell the two nils apart), "passed" is "not
|
|
101
|
+
* nil", and an explicit nil keeps the earlier value (limitation
|
|
89
102
|
* `props-spread-explicit-null-override`).
|
|
90
103
|
*/
|
|
91
|
-
export function propsSpreadParam(
|
|
104
|
+
export function propsSpreadParam(
|
|
105
|
+
param: ComponentPropsParam,
|
|
106
|
+
earlier: EarlierComponentProps,
|
|
107
|
+
callerKeys = false,
|
|
108
|
+
): 'entry' | 'present' {
|
|
92
109
|
if (param.optional === false) return 'entry'
|
|
110
|
+
if (callerKeys) return 'present'
|
|
93
111
|
return earlier.spread || earlier.keys.has(param.name) ? 'present' : 'entry'
|
|
94
112
|
}
|
|
95
113
|
|
|
@@ -130,7 +148,7 @@ export function planComponentProps(
|
|
|
130
148
|
// against the params this same spread has already pushed.
|
|
131
149
|
const before: EarlierComponentProps = { keys: new Set(earlier.keys), spread: earlier.spread }
|
|
132
150
|
for (const param of options.propsParams) {
|
|
133
|
-
if (propsSpreadParam(param, before) === 'entry') {
|
|
151
|
+
if (propsSpreadParam(param, before, options.callerKeys) === 'entry') {
|
|
134
152
|
push({ kind: 'props-param', name: param.name })
|
|
135
153
|
} else {
|
|
136
154
|
earlier.keys.add(param.name)
|
|
@@ -173,8 +191,10 @@ export interface ComponentPropSpelling {
|
|
|
173
191
|
spread(value: SpreadAttr): string
|
|
174
192
|
/**
|
|
175
193
|
* The expression a `present-param` segment folds in like a spread: a
|
|
176
|
-
* one-key hash `{ name: <param> }` when the
|
|
177
|
-
* hash when it is
|
|
194
|
+
* one-key hash `{ name: <param> }` when the caller passed the param, an
|
|
195
|
+
* empty hash when it did not. "Passed" is the runtime's
|
|
196
|
+
* `passed('<name>', <param is not nil>)`: the caller's keys when the
|
|
197
|
+
* runtime recorded them, the nil test otherwise.
|
|
178
198
|
*/
|
|
179
199
|
presentParam(name: string): string
|
|
180
200
|
}
|
|
@@ -19,6 +19,7 @@ import { collectContextConsumers, lookupLiteralConst } from '../augment-inherite
|
|
|
19
19
|
import { freeIdentifiers, inlineBinding, parseExpression, stringifyParsedExpr, type ParsedExpr } from '../expression-parser.ts'
|
|
20
20
|
import type { ConstantInfo, ExpressionAttr, IRMetadata } from '../types.ts'
|
|
21
21
|
import { componentBindings } from '../relocate.ts'
|
|
22
|
+
import { PROPS_OBJECT_IDENTIFIER } from '../scope/binding-scope.ts'
|
|
22
23
|
|
|
23
24
|
export interface ComputedConstContext {
|
|
24
25
|
constants: IRMetadata['localConstants'] | undefined
|
|
@@ -28,6 +29,23 @@ export interface ComputedConstContext {
|
|
|
28
29
|
contextConsumers: ReadonlySet<string>
|
|
29
30
|
/** Consts a props destructure binds, which the adapter binds as props params. */
|
|
30
31
|
propsBindings: ReadonlySet<string>
|
|
32
|
+
/**
|
|
33
|
+
* Component-scope names that hold a prop as passed, with the prop's
|
|
34
|
+
* caller-facing key: a destructured signature param (`{ children: kids }`
|
|
35
|
+
* → `kids` → `children`) or a props destructure const. Empty when the
|
|
36
|
+
* props object identifier names something else.
|
|
37
|
+
*/
|
|
38
|
+
propReads: ReadonlyMap<string, string>
|
|
39
|
+
/** Component-scope local functions, which shadow a module const of the same name. */
|
|
40
|
+
localFunctions: ReadonlySet<string>
|
|
41
|
+
/**
|
|
42
|
+
* Names the component scope lexically binds (the props object, destructured
|
|
43
|
+
* and rest props, its consts, local functions, signals, memos), which
|
|
44
|
+
* shadow a module binding of the same name. A module const's initializer
|
|
45
|
+
* never reads one of them. A prop reachable only through the props object
|
|
46
|
+
* (`function C(props: { label: string })`) binds no name.
|
|
47
|
+
*/
|
|
48
|
+
componentNames: ReadonlySet<string>
|
|
31
49
|
/** Whether a loop row or callback binds `name` where the expression is read. */
|
|
32
50
|
isShadowed(name: string): boolean
|
|
33
51
|
/** Whether the adapter binds the const `name` itself, so a read of it stays as written. */
|
|
@@ -52,6 +70,9 @@ export function computedConstContext(
|
|
|
52
70
|
getters: new Set([...metadata.signals.map(s => s.getter), ...metadata.memos.map(m => m.name)]),
|
|
53
71
|
contextConsumers: new Set(collectContextConsumers(metadata).map(c => c.localName)),
|
|
54
72
|
propsBindings: propsBindingNames(metadata),
|
|
73
|
+
propReads: propReadKeys(metadata, bindings),
|
|
74
|
+
localFunctions: new Set(metadata.localFunctions.filter(f => !f.isModule).map(f => f.name)),
|
|
75
|
+
componentNames: componentScopeNames(metadata),
|
|
55
76
|
isShadowed,
|
|
56
77
|
isAdapterBound,
|
|
57
78
|
isGlobal: name => !bindings.has(name) && name !== metadata.restPropsName && !isShadowed(name),
|
|
@@ -83,6 +104,54 @@ function propsBindingNames(metadata: IRMetadata): Set<string> {
|
|
|
83
104
|
return names
|
|
84
105
|
}
|
|
85
106
|
|
|
107
|
+
/**
|
|
108
|
+
* {@link ComputedConstContext.componentNames}. Not `componentBindings`, which
|
|
109
|
+
* also lists every typed prop of a props object as a `prop` binding.
|
|
110
|
+
*/
|
|
111
|
+
function componentScopeNames(metadata: IRMetadata): Set<string> {
|
|
112
|
+
const names = new Set<string>()
|
|
113
|
+
if (metadata.propsObjectName !== null) names.add(metadata.propsObjectName)
|
|
114
|
+
// With no props object the signature destructures, so every param is bound.
|
|
115
|
+
else for (const p of metadata.propsParams) names.add(p.name)
|
|
116
|
+
if (metadata.restPropsName) names.add(metadata.restPropsName)
|
|
117
|
+
for (const c of metadata.localConstants) if (!c.isModule) names.add(c.name)
|
|
118
|
+
for (const f of metadata.localFunctions) if (!f.isModule) names.add(f.name)
|
|
119
|
+
for (const s of metadata.signals) {
|
|
120
|
+
if (s.isModule) continue
|
|
121
|
+
names.add(s.getter)
|
|
122
|
+
if (s.setter) names.add(s.setter)
|
|
123
|
+
}
|
|
124
|
+
for (const m of metadata.memos) if (!m.isModule) names.add(m.name)
|
|
125
|
+
return names
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* {@link ComputedConstContext.propReads}: each component-scope name bound to
|
|
130
|
+
* a prop as passed, with the prop's key, so a read of it can be spelled
|
|
131
|
+
* `props.<key>` where a loop row or callback shadows the name. A template
|
|
132
|
+
* reads that as the prop's own variable, which holds a destructure default
|
|
133
|
+
* already. Only when the props object identifier `props` names the props
|
|
134
|
+
* object or nothing in the component, so the spelling cannot read another
|
|
135
|
+
* binding.
|
|
136
|
+
*/
|
|
137
|
+
function propReadKeys(metadata: IRMetadata, bindings: ReadonlyMap<string, unknown>): Map<string, string> {
|
|
138
|
+
const reads = new Map<string, string>()
|
|
139
|
+
const propsObject = metadata.propsObjectName
|
|
140
|
+
// Under another props object name (`function C(p)`) the adapters do not
|
|
141
|
+
// agree on what a read of `props` is, so no prop read is respelled.
|
|
142
|
+
if (propsObject === null ? bindings.has(PROPS_OBJECT_IDENTIFIER) : propsObject !== PROPS_OBJECT_IDENTIFIER) return reads
|
|
143
|
+
const destructures = propsBindingNames(metadata)
|
|
144
|
+
for (const param of metadata.propsParams) {
|
|
145
|
+
if (param.isRest) continue
|
|
146
|
+
// A signature that names the props object binds no param locally; only
|
|
147
|
+
// a props destructure const does.
|
|
148
|
+
if (propsObject !== null && !destructures.has(param.name)) continue
|
|
149
|
+
if (bindings.get(param.name) !== 'prop') continue
|
|
150
|
+
reads.set(param.name, param.sourceName ?? param.name)
|
|
151
|
+
}
|
|
152
|
+
return reads
|
|
153
|
+
}
|
|
154
|
+
|
|
86
155
|
/** Upper bound on const-of-const chains, a guard against a malformed cycle. */
|
|
87
156
|
const MAX_DEPTH = 16
|
|
88
157
|
|
|
@@ -108,11 +177,140 @@ function inlineOnce(expr: ParsedExpr, ctx: ComputedConstContext): ParsedExpr {
|
|
|
108
177
|
for (const name of free) {
|
|
109
178
|
const init = inlinableInitializer(name, ctx)
|
|
110
179
|
if (!init) continue
|
|
111
|
-
result = inlineBinding(result, name, init) ?? result
|
|
180
|
+
result = inlineBinding(result, name, init) ?? inlineUnderCallbacks(result, name, init, ctx) ?? result
|
|
112
181
|
}
|
|
113
182
|
return result
|
|
114
183
|
}
|
|
115
184
|
|
|
185
|
+
/**
|
|
186
|
+
* {@link inlineBinding} for an initializer a callback param inside `expr`
|
|
187
|
+
* would capture (`rows.filter(a => a !== b)` with `const b = a`): the
|
|
188
|
+
* initializer resolved in the component scope first
|
|
189
|
+
* ({@link resolveInComponentScope}), so none of its free names is one the
|
|
190
|
+
* expression's callbacks bind.
|
|
191
|
+
*/
|
|
192
|
+
function inlineUnderCallbacks(expr: ParsedExpr, name: string, init: ParsedExpr, ctx: ComputedConstContext): ParsedExpr | null {
|
|
193
|
+
const params = arrowParamNames(expr)
|
|
194
|
+
if (params.size === 0) return null
|
|
195
|
+
const resolved = resolveInComponentScope(init, ctx, n => params.has(n) || ctx.isShadowed(n))
|
|
196
|
+
return resolved ? inlineBinding(expr, name, resolved) : null
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Every param name an arrow anywhere in `expr` binds. */
|
|
200
|
+
function arrowParamNames(expr: ParsedExpr): Set<string> {
|
|
201
|
+
const names = new Set<string>()
|
|
202
|
+
const visit = (node: unknown): void => {
|
|
203
|
+
if (node === null || typeof node !== 'object') return
|
|
204
|
+
if (Array.isArray(node)) {
|
|
205
|
+
node.forEach(visit)
|
|
206
|
+
return
|
|
207
|
+
}
|
|
208
|
+
const e = node as ParsedExpr
|
|
209
|
+
if (e.kind === 'arrow') for (const p of e.params) names.add(p)
|
|
210
|
+
for (const value of Object.values(e)) visit(value)
|
|
211
|
+
}
|
|
212
|
+
visit(expr)
|
|
213
|
+
return names
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** The scope a const's initializer is written in, which its free names resolve against. */
|
|
217
|
+
type InitScope = 'component' | 'module'
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* `init`, an initializer written in `scope`, with each free name replaced
|
|
221
|
+
* where reading it at the use site would reach a different binding: one
|
|
222
|
+
* `captured` holds there (a loop row or callback binds it), or, for a module
|
|
223
|
+
* const's initializer, one the component scope binds. The name becomes what
|
|
224
|
+
* it holds in `scope`: a computed or literal const its own initializer,
|
|
225
|
+
* resolved the same way (`const a = children; const b = a` read in
|
|
226
|
+
* `rows.map(a => …{b}…)` reads `children`), and a component-scope prop
|
|
227
|
+
* binding `props.<key>`, which the adapters read as the prop wherever a row
|
|
228
|
+
* rebinds its bare name (#3696). `null` when such a name holds anything
|
|
229
|
+
* else, or when `props` itself is captured.
|
|
230
|
+
*/
|
|
231
|
+
function resolveInitializer(
|
|
232
|
+
init: ParsedExpr,
|
|
233
|
+
ctx: ComputedConstContext,
|
|
234
|
+
captured: (name: string) => boolean,
|
|
235
|
+
scope: InitScope,
|
|
236
|
+
depth = 0,
|
|
237
|
+
): ParsedExpr | null {
|
|
238
|
+
const free = freeIdentifiers(init)
|
|
239
|
+
if (!free || depth >= MAX_DEPTH) return null
|
|
240
|
+
let result = init
|
|
241
|
+
for (const name of free) {
|
|
242
|
+
if (!captured(name) && !(scope === 'module' && ctx.componentNames.has(name))) continue
|
|
243
|
+
const value = scope === 'module' ? moduleScopeValue(name, ctx, captured, depth) : componentScopeValue(name, ctx, captured, depth)
|
|
244
|
+
const next = value ? inlineBinding(result, name, value) : null
|
|
245
|
+
if (!next) return null
|
|
246
|
+
result = next
|
|
247
|
+
}
|
|
248
|
+
return result
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** {@link resolveInitializer} for an initializer written in the component scope. */
|
|
252
|
+
function resolveInComponentScope(init: ParsedExpr, ctx: ComputedConstContext, captured: (name: string) => boolean): ParsedExpr | null {
|
|
253
|
+
return resolveInitializer(init, ctx, captured, 'component')
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** What the component-scope name `name` holds, for {@link resolveInitializer}. */
|
|
257
|
+
function componentScopeValue(
|
|
258
|
+
name: string,
|
|
259
|
+
ctx: ComputedConstContext,
|
|
260
|
+
captured: (name: string) => boolean,
|
|
261
|
+
depth: number,
|
|
262
|
+
): ParsedExpr | null {
|
|
263
|
+
if (ctx.getters.has(name) || ctx.contextConsumers.has(name) || ctx.localFunctions.has(name)) return null
|
|
264
|
+
if (ctx.isAdapterBound(name)) return null
|
|
265
|
+
const key = ctx.propReads.get(name)
|
|
266
|
+
if (key !== undefined) {
|
|
267
|
+
if (captured(PROPS_OBJECT_IDENTIFIER)) return null
|
|
268
|
+
return { kind: 'member', object: { kind: 'identifier', name: PROPS_OBJECT_IDENTIFIER }, property: key, computed: false, optional: false }
|
|
269
|
+
}
|
|
270
|
+
// Any other component binding of the name is not a const this can read.
|
|
271
|
+
if (ctx.componentNames.has(name) && !componentConst(name, ctx)) return null
|
|
272
|
+
return constValue(componentConst(name, ctx) ?? moduleConst(name, ctx), ctx, captured, depth)
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* What the module-scope name `name` holds, for {@link resolveInitializer}:
|
|
277
|
+
* only a module const, whatever the component scope binds by that name.
|
|
278
|
+
* `isAdapterBound` names a component-scope binding, so it does not apply.
|
|
279
|
+
*/
|
|
280
|
+
function moduleScopeValue(
|
|
281
|
+
name: string,
|
|
282
|
+
ctx: ComputedConstContext,
|
|
283
|
+
captured: (name: string) => boolean,
|
|
284
|
+
depth: number,
|
|
285
|
+
): ParsedExpr | null {
|
|
286
|
+
return constValue(moduleConst(name, ctx), ctx, captured, depth)
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** A const's initializer resolved in the scope it is declared in, or `null` when it is not one a template can inline. */
|
|
290
|
+
function constValue(
|
|
291
|
+
constant: ConstantInfo | undefined,
|
|
292
|
+
ctx: ComputedConstContext,
|
|
293
|
+
captured: (name: string) => boolean,
|
|
294
|
+
depth: number,
|
|
295
|
+
): ParsedExpr | null {
|
|
296
|
+
const init = constant?.parsed
|
|
297
|
+
// A props destructure const is the component's; a module const of that name is not one.
|
|
298
|
+
if (!constant || !init || (!constant.isModule && ctx.propsBindings.has(constant.name))) return null
|
|
299
|
+
const literal = lookupLiteralConst(constant.name, [constant], () => false) !== null
|
|
300
|
+
if (!literal && !(isPlainComputedConst(constant, init) && callsOnlyGetters(init, constant.isModule ? new Set() : ctx.getters))) return null
|
|
301
|
+
return resolveInitializer(init, ctx, captured, constant.isModule ? 'module' : 'component', depth + 1)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** The component-scope const `name`. */
|
|
305
|
+
function componentConst(name: string, ctx: ComputedConstContext): ConstantInfo | undefined {
|
|
306
|
+
return ctx.constants?.find(c => c.name === name && !c.isModule)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** The module-scope const `name`. */
|
|
310
|
+
function moduleConst(name: string, ctx: ComputedConstContext): ConstantInfo | undefined {
|
|
311
|
+
return ctx.constants?.find(c => c.name === name && c.isModule)
|
|
312
|
+
}
|
|
313
|
+
|
|
116
314
|
/**
|
|
117
315
|
* An `{expr}` attribute value read through {@link inlineComputedConsts}, so
|
|
118
316
|
* the attribute's own plan (a nullable prop read drops the attribute) sees
|
|
@@ -131,17 +329,18 @@ export function inlineComputedConstAttr(value: ExpressionAttr, ctx: ComputedCons
|
|
|
131
329
|
|
|
132
330
|
/** The initializer of a const read `name` stands for, or `null` when the read stays as it is. */
|
|
133
331
|
function inlinableInitializer(name: string, ctx: ComputedConstContext): ParsedExpr | null {
|
|
134
|
-
|
|
332
|
+
// A component-scope binding of the name shadows a module const.
|
|
333
|
+
const constant = componentConst(name, ctx) ?? (ctx.componentNames.has(name) ? undefined : moduleConst(name, ctx))
|
|
135
334
|
const init = constant?.parsed
|
|
136
335
|
if (!constant || !init || !isPlainComputedConst(constant, init)) return null
|
|
137
336
|
if (ctx.isShadowed(name) || ctx.getters.has(name) || ctx.contextConsumers.has(name) || ctx.propsBindings.has(name)) return null
|
|
138
337
|
if (ctx.isAdapterBound(name)) return null
|
|
139
338
|
// An adapter already inlines a literal const in its own syntax.
|
|
140
|
-
if (lookupLiteralConst(name,
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
return
|
|
339
|
+
if (lookupLiteralConst(name, [constant], ctx.isShadowed) !== null) return null
|
|
340
|
+
if (!callsOnlyGetters(init, ctx.getters)) return null
|
|
341
|
+
// A free name the read site rebinds would be captured by the substitution:
|
|
342
|
+
// it is read as what it holds where the const is declared instead.
|
|
343
|
+
return resolveInitializer(init, ctx, n => ctx.isShadowed(n), constant.isModule ? 'module' : 'component')
|
|
145
344
|
}
|
|
146
345
|
|
|
147
346
|
/**
|
|
@@ -8,18 +8,35 @@
|
|
|
8
8
|
import { augmentInheritedPropAccesses, collectModuleStringConsts } from '../augment-inherited-props.ts'
|
|
9
9
|
import { buildImportAliasMap } from '../ir-to-client-js/component-scope.ts'
|
|
10
10
|
import { prepareLoweringMatchers, type LoweringMatcher } from '../lowering-registry.ts'
|
|
11
|
+
import type { ParsedExpr } from '../expression-parser.ts'
|
|
11
12
|
import {
|
|
12
13
|
collectNullableSignalGetters,
|
|
13
14
|
collectNullishAttrContext,
|
|
15
|
+
type ArithmeticCoercion,
|
|
14
16
|
type NullishAttrContext,
|
|
15
17
|
} from '../nullable-signal-attr.ts'
|
|
16
18
|
import { BindingScope } from '../scope/binding-scope.ts'
|
|
17
|
-
import { rootPropAliasNames } from '../scope/root-prop-alias.ts'
|
|
18
|
-
import type {
|
|
19
|
+
import { rootPropAliasesForLoop, rootPropAliasNames } from '../scope/root-prop-alias.ts'
|
|
20
|
+
import type {
|
|
21
|
+
CompilerError,
|
|
22
|
+
ComponentIR,
|
|
23
|
+
ConstantInfo,
|
|
24
|
+
IRIfStatement,
|
|
25
|
+
IRLoop,
|
|
26
|
+
IRMetadata,
|
|
27
|
+
PreambleValueDeclaration,
|
|
28
|
+
} from '../types.ts'
|
|
29
|
+
import {
|
|
30
|
+
arithmeticCoercionOrRefuse,
|
|
31
|
+
refuseAmbiguousNilArithmetic,
|
|
32
|
+
refuseAmbiguousNilMemoArithmetic,
|
|
33
|
+
type AmbiguousNilArithmeticSink,
|
|
34
|
+
} from './ambiguous-nil-arithmetic.ts'
|
|
19
35
|
import { collectImportedLoopChildComponentErrors } from './component-tree.ts'
|
|
20
36
|
import { computedConstContext, type ComputedConstContext } from './computed-const.ts'
|
|
21
37
|
import { searchParamsLocalNames } from './env-signal.ts'
|
|
22
38
|
import { BaseAdapter, type AdapterGenerateOptions, type AdapterOutput } from './interface.ts'
|
|
39
|
+
import { type LoopPlan, type LoopPlanContext, planLoop } from './loop-plan.ts'
|
|
23
40
|
import { collectBooleanTypedProps, collectNullableOptionalProps } from './prop-classes.ts'
|
|
24
41
|
import { templateNameCollisionErrors } from './template-name-collisions.ts'
|
|
25
42
|
|
|
@@ -34,6 +51,56 @@ export interface TemplateIdentifierSpelling {
|
|
|
34
51
|
label: string
|
|
35
52
|
}
|
|
36
53
|
|
|
54
|
+
/**
|
|
55
|
+
* An adapter's spelling of a loop row's window ({@link DslTemplateAdapter.renderLoopRow}).
|
|
56
|
+
* Each hook runs under the row's scope.
|
|
57
|
+
*/
|
|
58
|
+
export interface LoopRowSpelling {
|
|
59
|
+
/** Runs first, before anything in the row renders: the adapter's own per-row bookkeeping. */
|
|
60
|
+
enter?(row: BindingScope, outside: BindingScope): void
|
|
61
|
+
/** A `.map()` preamble declaration as a per-row local (#2447). */
|
|
62
|
+
preamble(declaration: PreambleValueDeclaration): string
|
|
63
|
+
/** The whole-item-conditional `<!--bf-loop-i:KEY-->` anchor for the key expression `key`. */
|
|
64
|
+
keyAnchor(key: string): string
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** A loop row rendered under its own scope. */
|
|
68
|
+
export interface LoopRow {
|
|
69
|
+
/** The scope outside the loop, current again once the row is rendered. */
|
|
70
|
+
outside: BindingScope
|
|
71
|
+
/** `outside.enterLoopRow(loop)`: the scope the row rendered under. */
|
|
72
|
+
row: BindingScope
|
|
73
|
+
/** The props the row's bindings newly shadow, each with its root alias (#3314). */
|
|
74
|
+
rootAliases: { name: string; alias: string }[]
|
|
75
|
+
/** One spelled local per preamble declaration, in source order. */
|
|
76
|
+
preamble: string[]
|
|
77
|
+
/** The children, after the key anchor of a whole-item-conditional body. */
|
|
78
|
+
body: string
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** The parts of a {@link LoopPlanContext} each adapter spells. */
|
|
82
|
+
export type LoopPlanSpelling<Spelled> = Pick<
|
|
83
|
+
LoopPlanContext<Spelled>,
|
|
84
|
+
'resolveLiteralConst' | 'callbackLowering' | 'spellStatic'
|
|
85
|
+
>
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* An adapter's wording of the refusals a {@link LoopPlan} decides
|
|
89
|
+
* ({@link DslTemplateAdapter.recordLoopPlanRefusals}).
|
|
90
|
+
*/
|
|
91
|
+
export interface LoopRefusalSpelling {
|
|
92
|
+
/** The BF104 message for a destructure pattern `param` that no template binds. */
|
|
93
|
+
destructureMessage(param: string): string
|
|
94
|
+
/** The BF104 suggestion. */
|
|
95
|
+
destructureSuggestion: string
|
|
96
|
+
/**
|
|
97
|
+
* BF101 for a source naming a const no template binds: the engine name
|
|
98
|
+
* the shared wording uses ("Jinja", "MiniJinja", …), or the adapter's own
|
|
99
|
+
* recording of it.
|
|
100
|
+
*/
|
|
101
|
+
unresolvableConst: string | ((constant: ConstantInfo) => void)
|
|
102
|
+
}
|
|
103
|
+
|
|
37
104
|
export abstract class DslTemplateAdapter extends BaseAdapter {
|
|
38
105
|
// ===========================================================================
|
|
39
106
|
// Per-compile state, set at `generate()` entry
|
|
@@ -127,6 +194,12 @@ export abstract class DslTemplateAdapter extends BaseAdapter {
|
|
|
127
194
|
protected importAliases: Map<string, string> = new Map()
|
|
128
195
|
/** Numbers the children captures of a child component that has no slot id. */
|
|
129
196
|
protected childrenCaptureCounter = 0
|
|
197
|
+
/**
|
|
198
|
+
* True while a loop row's children render ({@link renderLoopRow}): a
|
|
199
|
+
* component inside takes the loop-child naming, and a child prop that
|
|
200
|
+
* needs a per-instance value has none to read.
|
|
201
|
+
*/
|
|
202
|
+
protected inLoop = false
|
|
130
203
|
|
|
131
204
|
// ===========================================================================
|
|
132
205
|
// What each adapter supplies
|
|
@@ -158,6 +231,112 @@ export abstract class DslTemplateAdapter extends BaseAdapter {
|
|
|
158
231
|
this.scope = outer
|
|
159
232
|
}
|
|
160
233
|
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* The coercion an arithmetic operand's emitter spells
|
|
237
|
+
* (`arithmeticOperandCoercion`), refusing an operand whose nil the
|
|
238
|
+
* declared type cannot classify (`arithmeticCoercionOrRefuse`, #3700).
|
|
239
|
+
* `isShadowed` is the adapter's scope test at the emission site.
|
|
240
|
+
*/
|
|
241
|
+
protected arithmeticCoercion(
|
|
242
|
+
op: string,
|
|
243
|
+
operand: ParsedExpr,
|
|
244
|
+
other: ParsedExpr | undefined,
|
|
245
|
+
isShadowed: (name: string) => boolean,
|
|
246
|
+
): ArithmeticCoercion {
|
|
247
|
+
return arithmeticCoercionOrRefuse(op, operand, other, this.nullishAttrCtx, isShadowed, this.ambiguousNilSink())
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Refuse the ambiguous-nil arithmetic (#3700) in a whole expression the
|
|
252
|
+
* adapter lowers through its own renderer rather than per operator: a
|
|
253
|
+
* `.filter()` predicate, read in the current scope with the callback's
|
|
254
|
+
* own `params` (its parameter and block locals) entered as a callback
|
|
255
|
+
* frame.
|
|
256
|
+
*/
|
|
257
|
+
protected refuseAmbiguousNilArithmeticIn(expr: ParsedExpr, params: readonly string[]): void {
|
|
258
|
+
refuseAmbiguousNilArithmetic(expr, this.nullishAttrCtx, this.scope.enterCallback(params), this.ambiguousNilSink())
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
private ambiguousNilSink(): AmbiguousNilArithmeticSink {
|
|
262
|
+
return { errors: this.errors, componentName: this.componentName }
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** {@link planLoop} against the current scope and the component's constants. */
|
|
266
|
+
protected loopPlanFor<Spelled>(loop: IRLoop, spelling: LoopPlanSpelling<Spelled>): LoopPlan<Spelled> {
|
|
267
|
+
return planLoop(loop, { scope: this.scope, localConstants: this.localConstants ?? [], ...spelling })
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Record the refusals `plan` decided for `loop`, at the loop's location:
|
|
272
|
+
* BF104 for a destructure pattern no template binds, then BF101 for a
|
|
273
|
+
* source naming a computed const no template binds (#3473). The plan
|
|
274
|
+
* decides; the adapter only words them.
|
|
275
|
+
*/
|
|
276
|
+
protected recordLoopPlanRefusals(loop: IRLoop, plan: LoopPlan<unknown>, spelling: LoopRefusalSpelling): void {
|
|
277
|
+
const loc = loop.loc ?? { file: this.componentName + '.tsx', start: { line: 1, column: 0 }, end: { line: 1, column: 0 } }
|
|
278
|
+
if (plan.destructure === 'unlowerable') {
|
|
279
|
+
this.errors.push({
|
|
280
|
+
code: 'BF104',
|
|
281
|
+
severity: 'error',
|
|
282
|
+
message: spelling.destructureMessage(loop.param),
|
|
283
|
+
loc,
|
|
284
|
+
suggestion: { message: spelling.destructureSuggestion },
|
|
285
|
+
})
|
|
286
|
+
}
|
|
287
|
+
const constant = plan.unresolvableConst
|
|
288
|
+
if (!constant) return
|
|
289
|
+
if (typeof spelling.unresolvableConst === 'function') {
|
|
290
|
+
spelling.unresolvableConst(constant)
|
|
291
|
+
return
|
|
292
|
+
}
|
|
293
|
+
this.errors.push({
|
|
294
|
+
code: 'BF101',
|
|
295
|
+
severity: 'error',
|
|
296
|
+
message: `Loop array \`${constant.name}\` is a const (module- or function-scope) computed value (\`${constant.value}\`) that the ${spelling.unresolvableConst} adapter cannot bind as a template variable — only numeric/string-literal locals inline at their use site.`,
|
|
297
|
+
loc,
|
|
298
|
+
suggestion: {
|
|
299
|
+
message:
|
|
300
|
+
'Pre-compute the array server-side and pass it as a prop, or mark the loop position as @client-only so it runs in JS on the client.',
|
|
301
|
+
escape: [{ kind: 'prop-precompute' }, { kind: 'client-directive' }],
|
|
302
|
+
},
|
|
303
|
+
})
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* A loop row's window (#3473): the row's scope entered around everything
|
|
308
|
+
* that evaluates per row (the preamble locals' initializers, the children,
|
|
309
|
+
* the whole-item-conditional key anchor) and the outer scope restored
|
|
310
|
+
* after. The children render under `inLoop`; the key anchor, after them,
|
|
311
|
+
* does not. The preamble converts first, so a later initializer sees an
|
|
312
|
+
* earlier local, as in the source block.
|
|
313
|
+
*
|
|
314
|
+
* What sits outside the window stays the adapter's: the loop header, the
|
|
315
|
+
* sort, and the filter predicate, which a filter callback's own frame
|
|
316
|
+
* scopes rather than the `.map()` row.
|
|
317
|
+
*/
|
|
318
|
+
protected renderLoopRow(loop: IRLoop, spelling: LoopRowSpelling): LoopRow {
|
|
319
|
+
const outside = this.scope
|
|
320
|
+
const row = outside.enterLoopRow(loop)
|
|
321
|
+
return this.renderInScope(row, () => {
|
|
322
|
+
spelling.enter?.(row, outside)
|
|
323
|
+
const preamble = (loop.preamble?.declarations ?? []).map(d => spelling.preamble(d))
|
|
324
|
+
const children = this.renderInLoop(() => this.renderChildren(loop.children))
|
|
325
|
+
const body = loop.bodyIsItemConditional && loop.key ? `${spelling.keyAnchor(loop.key)}\n${children}` : children
|
|
326
|
+
return { outside, row, rootAliases: rootPropAliasesForLoop(outside, row, this.rootPropAliases), preamble, body }
|
|
327
|
+
})
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
private renderInLoop<T>(render: () => T): T {
|
|
331
|
+
const outer = this.inLoop
|
|
332
|
+
this.inLoop = true
|
|
333
|
+
try {
|
|
334
|
+
return render()
|
|
335
|
+
} finally {
|
|
336
|
+
this.inLoop = outer
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
161
340
|
/** The root `if` statement of a component with conditional returns. */
|
|
162
341
|
protected abstract renderIfStatement(ifStmt: IRIfStatement): string
|
|
163
342
|
/** The script registrations the template starts with. */
|
|
@@ -236,6 +415,7 @@ export abstract class DslTemplateAdapter extends BaseAdapter {
|
|
|
236
415
|
this.computedConsts = computedConstContext(ir.metadata, name => this.scope.isBound(name))
|
|
237
416
|
this.importAliases = buildImportAliasMap(ir.metadata.imports ?? [])
|
|
238
417
|
this.childrenCaptureCounter = 0
|
|
418
|
+
this.inLoop = false
|
|
239
419
|
this.errors = []
|
|
240
420
|
this.resetAdapterState(ir)
|
|
241
421
|
}
|
|
@@ -255,5 +435,6 @@ export abstract class DslTemplateAdapter extends BaseAdapter {
|
|
|
255
435
|
if (!options?.siblingTemplatesRegistered) {
|
|
256
436
|
this.errors.push(...collectImportedLoopChildComponentErrors(ir, this.componentName, this.loopChildDiagnosticName))
|
|
257
437
|
}
|
|
438
|
+
refuseAmbiguousNilMemoArithmetic(ir.metadata.memos ?? [], this.nullishAttrCtx, this.ambiguousNilSink())
|
|
258
439
|
}
|
|
259
440
|
}
|
|
@@ -8,14 +8,15 @@
|
|
|
8
8
|
* captures need an alias, how the filter predicate reads on the row, and
|
|
9
9
|
* which locals the row binds for its index and its destructure bindings.
|
|
10
10
|
* {@link planLoop} makes those decisions once, against the scope outside the
|
|
11
|
-
* loop
|
|
11
|
+
* loop; {@link savedLoopBindings} decides, against the row's scope, which
|
|
12
|
+
* row bindings a backend without loop-scoped `set` saves and restores. The adapter keeps the spelling:
|
|
12
13
|
* the loop header, the row bindings, the diagnostics' wording, and the order
|
|
13
14
|
* it emits them in.
|
|
14
15
|
*/
|
|
15
16
|
|
|
16
17
|
import { type ParsedExpr, parseExpression, renameFreeIdentifiers, type SortComparator } from '../expression-parser.ts'
|
|
17
18
|
import { isLowerableLoopDestructure } from '../loop-destructure.ts'
|
|
18
|
-
import type { BindingScope } from '../scope/binding-scope.ts'
|
|
19
|
+
import type { BindingScope, ScopeBindingSource } from '../scope/binding-scope.ts'
|
|
19
20
|
import { resolveStaticLoopSource } from '../static-literal.ts'
|
|
20
21
|
import type { ConstantInfo, IRLoop, LoopBindingPathSegment } from '../types.ts'
|
|
21
22
|
import {
|
|
@@ -141,6 +142,70 @@ export function spellSortedSource(sort: SortPlan | null, recv: string, spelling:
|
|
|
141
142
|
return recv
|
|
142
143
|
}
|
|
143
144
|
|
|
145
|
+
/**
|
|
146
|
+
* The names a loop's for-header binds: the key and the value of an
|
|
147
|
+
* `entries` object iteration, the param of a `keys` / `values` one, and the
|
|
148
|
+
* item var of an array iteration when it is the param itself. A backend
|
|
149
|
+
* whose for-header scopes its targets to the loop passes these to
|
|
150
|
+
* {@link savedLoopBindings} as names it restores itself.
|
|
151
|
+
*/
|
|
152
|
+
export function loopHeaderTargets(loop: IRLoop, plan: Pick<LoopPlan<unknown>, 'itemVar'>): string[] {
|
|
153
|
+
if (loop.objectIteration === 'entries') return [loop.index ?? loop.param, loop.param]
|
|
154
|
+
return loop.objectIteration || plan.itemVar === loop.param ? [loop.param] : []
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Where a loop row sits, for {@link savedLoopBindings}. */
|
|
158
|
+
export interface LoopRowScopes {
|
|
159
|
+
/** The scope outside the loop. */
|
|
160
|
+
outside: BindingScope
|
|
161
|
+
/** `outside.enterLoopRow(loop)`. */
|
|
162
|
+
row: BindingScope
|
|
163
|
+
/** The template-level names: signals, memos, props and constants. */
|
|
164
|
+
templateLevelNames: ReadonlySet<string>
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const SAVE_ORDER: readonly ScopeBindingSource[] = ['item', 'index', 'destructure', 'preamble']
|
|
168
|
+
|
|
169
|
+
/** A row binding a loop saves before its header and restores after its end. */
|
|
170
|
+
export interface SavedLoopBinding {
|
|
171
|
+
name: string
|
|
172
|
+
/** The template variable that holds the outer value across the loop. */
|
|
173
|
+
temporary: string
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The row bindings a backend whose `set` is not loop-scoped (Pebble, Twig,
|
|
178
|
+
* Blade) saves before the loop and restores after it (#3386, #3391): each
|
|
179
|
+
* name the row binds that already means something outside it, an enclosing
|
|
180
|
+
* row's binding or a template-level name. JS scopes the callback's bindings
|
|
181
|
+
* to the row; without the restore the last row's value leaks past the loop.
|
|
182
|
+
* They come in the order a row binds them in a template: the for-header's
|
|
183
|
+
* item, the index local, each destructure local, each preamble local.
|
|
184
|
+
*
|
|
185
|
+
* `skip` names the bindings the backend restores itself: those its
|
|
186
|
+
* for-header scopes to the loop ({@link loopHeaderTargets}), or a root prop
|
|
187
|
+
* it restores through its alias. `temporary` spells the holder of the
|
|
188
|
+
* `ordinal`-th saved name; a holder that collides with a row binding or a
|
|
189
|
+
* template-level name gets `_` appended until it does not.
|
|
190
|
+
*/
|
|
191
|
+
export function savedLoopBindings(
|
|
192
|
+
scopes: LoopRowScopes,
|
|
193
|
+
skip: Iterable<string>,
|
|
194
|
+
temporary: (name: string, ordinal: number) => string,
|
|
195
|
+
): SavedLoopBinding[] {
|
|
196
|
+
const skipped = new Set(skip)
|
|
197
|
+
const rank = (name: string) => SAVE_ORDER.indexOf(scopes.row.lookup(name)?.binding.source ?? 'param')
|
|
198
|
+
return scopes.row
|
|
199
|
+
.innermostNames()
|
|
200
|
+
.filter((name) => !skipped.has(name) && (scopes.templateLevelNames.has(name) || scopes.outside.isBound(name)))
|
|
201
|
+
.sort((a, b) => rank(a) - rank(b))
|
|
202
|
+
.map((name, ordinal) => {
|
|
203
|
+
let held = temporary(name, ordinal)
|
|
204
|
+
while (scopes.row.isBound(held) || scopes.templateLevelNames.has(held)) held += '_'
|
|
205
|
+
return { name, temporary: held }
|
|
206
|
+
})
|
|
207
|
+
}
|
|
208
|
+
|
|
144
209
|
/**
|
|
145
210
|
* The `.filter()` predicate a `.filter().map()` loop tests on each row: the
|
|
146
211
|
* predicate with the names it reads renamed for the row, or the refusal of a
|