@barefootjs/jsx 0.39.8 → 0.39.9
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/callback-method.d.ts +19 -7
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +2 -0
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/loop-plan.d.ts +133 -9
- package/dist/adapters/loop-plan.d.ts.map +1 -1
- package/dist/adapters/parsed-expr-emitter.d.ts +20 -3
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/assignment-target.d.ts +26 -0
- package/dist/assignment-target.d.ts.map +1 -0
- package/dist/errors.d.ts +1 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1913 -1457
- package/dist/ir-to-client-js/compute-inlinability.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/loop.d.ts +24 -1
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts +16 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts +6 -1
- package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/csr-substitute.d.ts +13 -2
- 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.map +1 -1
- package/dist/ir-to-client-js/phases/props-extraction.d.ts +5 -3
- package/dist/ir-to-client-js/phases/props-extraction.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +8 -0
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/rewrite-props-object.d.ts.map +1 -1
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +1 -0
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -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/prop-rewrite.d.ts +17 -8
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-binding.d.ts +57 -0
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/scope/binding-scope.d.ts +30 -2
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/scope/lexical-scope-walk.d.ts +26 -0
- package/dist/scope/lexical-scope-walk.d.ts.map +1 -0
- package/dist/types.d.ts +25 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/arithmetic-lowering.test.ts +5 -1
- package/src/__tests__/binding-scope.test.ts +29 -0
- package/src/__tests__/create-query.test.ts +1 -1
- package/src/__tests__/emit-to-string-call.test.ts +58 -0
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +90 -31
- package/src/__tests__/loop-plan.test.ts +131 -1
- package/src/__tests__/loop-row-child-children-handler.test.ts +129 -0
- package/src/__tests__/loop-row-child-children-inner-loop-shadow.test.ts +74 -0
- package/src/__tests__/loop-row-child-children-ref.test.ts +117 -0
- package/src/__tests__/props-alias-const.test.ts +60 -1
- package/src/__tests__/props-named-callback-param.test.ts +256 -0
- package/src/__tests__/renamed-body-destructure-template.test.ts +86 -0
- package/src/__tests__/rewrite-props-object.test.ts +90 -0
- package/src/__tests__/written-binding-substitution.test.ts +430 -0
- package/src/adapters/callback-method.ts +25 -10
- package/src/adapters/dsl-template-adapter.ts +11 -0
- package/src/adapters/loop-plan.ts +201 -13
- package/src/adapters/parsed-expr-emitter.ts +37 -4
- package/src/analyzer.ts +283 -2
- package/src/assignment-target.ts +79 -0
- package/src/errors.ts +11 -0
- package/src/index.ts +13 -2
- package/src/ir-to-client-js/compute-inlinability.ts +6 -3
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +21 -5
- package/src/ir-to-client-js/control-flow/plan/loop.ts +25 -1
- package/src/ir-to-client-js/control-flow/shared.ts +41 -3
- package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +23 -3
- package/src/ir-to-client-js/csr-substitute.ts +70 -22
- package/src/ir-to-client-js/emit-reactive.ts +2 -2
- package/src/ir-to-client-js/emit-registration.ts +6 -1
- package/src/ir-to-client-js/html-template.ts +18 -7
- package/src/ir-to-client-js/phases/props-extraction.ts +26 -5
- package/src/ir-to-client-js/plan/build-static-array-child-init.ts +7 -0
- package/src/ir-to-client-js/plan/static-array-child-init.ts +8 -0
- package/src/ir-to-client-js/rewrite-destructured-props.ts +2 -2
- package/src/ir-to-client-js/rewrite-props-object.ts +14 -8
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +4 -1
- package/src/ir-to-client-js/utils.ts +40 -96
- package/src/jsx-to-ir.ts +31 -12
- package/src/prop-rewrite.ts +31 -72
- package/src/props-binding.ts +73 -1
- package/src/scope/binding-scope.ts +34 -3
- package/src/scope/lexical-scope-walk.ts +167 -0
- package/src/types.ts +25 -1
package/src/props-binding.ts
CHANGED
|
@@ -96,6 +96,77 @@ export function isPropsAliasConst(
|
|
|
96
96
|
return read !== null && read.key === c.name && (!read.hasDefault || c.mutatedAfterDeclaration === true)
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
+
/**
|
|
100
|
+
* Whether a component-scope binding keeps the value its declaration gives
|
|
101
|
+
* it, so it may be read as the prop it was destructured from (a live
|
|
102
|
+
* `_p.<key> ?? <default>` read).
|
|
103
|
+
*
|
|
104
|
+
* - A body binding (`ConstantInfo`) must be a `const` nothing mutates
|
|
105
|
+
* after the declaration. A `let` may be reassigned (`local =
|
|
106
|
+
* ['changed']`, or a folded `if (!local) local = 'y'`) and a mutated
|
|
107
|
+
* value (`items.push(x)`, `markMutatedConstants`) has changed.
|
|
108
|
+
* - A destructured prop parameter (`ParamInfo`, no declaration kind)
|
|
109
|
+
* keeps the value the caller passed unless the component writes it.
|
|
110
|
+
*
|
|
111
|
+
* Either way a binding the component writes anywhere (`reassigned`, set on
|
|
112
|
+
* both by `markWrittenBindings`, analyzer.ts) does not: a write needs a
|
|
113
|
+
* local, so the init body declares one (`emitPropsExtraction`) and keeps
|
|
114
|
+
* every reference to it. The one rule for every site that turns a props
|
|
115
|
+
* binding into a live prop read: the init body's alias rewrite
|
|
116
|
+
* (`resolveBodyPropAliases`) and parameter rewrite
|
|
117
|
+
* (`rewriteDestructuredPropReads`), the reactive attribute rewrite
|
|
118
|
+
* (`rewriteDestructuredPropsInExpr`), and the template's prop shadow guard
|
|
119
|
+
* (`propShadowingNames`, `jsx-to-ir.ts`, #3579).
|
|
120
|
+
*/
|
|
121
|
+
export function keepsDeclaredValue(
|
|
122
|
+
b: { declarationKind?: ConstantInfo['declarationKind']; mutatedAfterDeclaration?: boolean; reassigned?: boolean },
|
|
123
|
+
): boolean {
|
|
124
|
+
if (b.reassigned === true || b.mutatedAfterDeclaration === true) return false
|
|
125
|
+
return b.declarationKind === undefined || b.declarationKind === 'const'
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Whether the component writes any of its bindings (`reassigned`, set by
|
|
130
|
+
* `markWrittenBindings`): a prop parameter or a component-scope `let`. Such
|
|
131
|
+
* a component's client template has to check each expression for a write
|
|
132
|
+
* (`csrSubstitute`'s `writes`), which only the CSR template emitter does, so
|
|
133
|
+
* it never takes the static template fast path (`emit-registration.ts`),
|
|
134
|
+
* and a child whose prop writes a binding is deferred to init
|
|
135
|
+
* (`computeDeferredChildSlots`).
|
|
136
|
+
*/
|
|
137
|
+
export function writesAnyBinding(b: {
|
|
138
|
+
propsParams: readonly Pick<ParamInfo, 'reassigned'>[]
|
|
139
|
+
localConstants: readonly Pick<ConstantInfo, 'reassigned'>[]
|
|
140
|
+
}): boolean {
|
|
141
|
+
return b.propsParams.some(p => p.reassigned === true) || b.localConstants.some(c => c.reassigned === true)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Whether a renamed binding of a body destructure (`const { b: local = d } =
|
|
146
|
+
* props`) is a prop under its local name: the analyzer gives such a binding
|
|
147
|
+
* with a default its own `ParamInfo` (`name: local`, `sourceName: b`, the
|
|
148
|
+
* default's fields), the same entry the parameter form `function C({ b:
|
|
149
|
+
* local = d })` has. A template then reads the prop through that entry
|
|
150
|
+
* (`livePropReadExpr`), default included, instead of inlining the const.
|
|
151
|
+
*
|
|
152
|
+
* Only a binding that keeps its declared value (`keepsDeclaredValue`): a
|
|
153
|
+
* reassigned or mutated one stays a local and keeps the unsafe-local path
|
|
154
|
+
* (an empty slot, a deferred child).
|
|
155
|
+
*
|
|
156
|
+
* Kept apart from `isPropsAliasConst`: that one also decides relocate's
|
|
157
|
+
* binding kind, whose `prop` lift reads `_p.<key>` without a default, so a
|
|
158
|
+
* renamed binding classified there would lose its default.
|
|
159
|
+
*/
|
|
160
|
+
export function isRenamedBodyPropParam(
|
|
161
|
+
c: Pick<ConstantInfo, 'name' | 'propsDestructure' | 'declarationKind' | 'mutatedAfterDeclaration' | 'reassigned'>,
|
|
162
|
+
propsParams: readonly ParamInfo[],
|
|
163
|
+
): boolean {
|
|
164
|
+
const key = c.propsDestructure?.sourceKey
|
|
165
|
+
if (key === undefined || key === c.name) return false
|
|
166
|
+
if (!keepsDeclaredValue(c)) return false
|
|
167
|
+
return propsParams.some(p => !p.isRest && p.name === c.name && p.sourceName === key)
|
|
168
|
+
}
|
|
169
|
+
|
|
99
170
|
/**
|
|
100
171
|
* How a parsed signal initializer reads a prop: a member chain rooted at the
|
|
101
172
|
* props object (`props.<name>`, `props.row.count`, `props.items[0]`) or a
|
|
@@ -276,7 +347,7 @@ export function resolveBodyPropAliases(
|
|
|
276
347
|
if (propsObjectName === null) return aliases
|
|
277
348
|
for (const c of localConstants) {
|
|
278
349
|
if (c.isModule || !c.value) continue
|
|
279
|
-
if (requireLiveRewriteSafe && (c
|
|
350
|
+
if (requireLiveRewriteSafe && !keepsDeclaredValue(c)) continue
|
|
280
351
|
// Re-parse `value` (rather than walking `c.parsed`) so the default's
|
|
281
352
|
// SOURCE TEXT is available verbatim — `ParsedExpr` structures the
|
|
282
353
|
// default but doesn't retain its original formatting, and this reuses
|
|
@@ -326,6 +397,7 @@ export function boundPropLocalNames(b: PropsParamBindings): ReadonlySet<string>
|
|
|
326
397
|
|
|
327
398
|
const EMPTY_SET: ReadonlySet<string> = new Set()
|
|
328
399
|
|
|
400
|
+
|
|
329
401
|
/**
|
|
330
402
|
* The subset of `ParamInfo` a live prop read actually needs. A body-level
|
|
331
403
|
* pure-alias local (`resolveBodyPropAliases`) has no real `ParamInfo` — it
|
|
@@ -40,6 +40,14 @@ export type ScopeBindingSource = 'item' | 'index' | 'destructure' | 'preamble' |
|
|
|
40
40
|
export interface ScopeBinding {
|
|
41
41
|
readonly source: ScopeBindingSource
|
|
42
42
|
readonly stringValue?: boolean
|
|
43
|
+
/**
|
|
44
|
+
* The row a `'item'` / `'destructure'` binding reads: the loop's item type
|
|
45
|
+
* and the accessor path from the item to the binding (empty for the plain
|
|
46
|
+
* item). Absent when the loop has no item type or the binding is a rest
|
|
47
|
+
* element. Lets a typed backend resolve the type of a row read
|
|
48
|
+
* (`row.id`, a destructured `id`) at the read.
|
|
49
|
+
*/
|
|
50
|
+
readonly row?: { readonly itemType: LoopValueType; readonly segments: readonly RowPathSegment[] }
|
|
43
51
|
}
|
|
44
52
|
|
|
45
53
|
export interface ScopeFrame {
|
|
@@ -56,16 +64,32 @@ export interface ScopeFrame {
|
|
|
56
64
|
export interface LoopBindingSource {
|
|
57
65
|
readonly param: string
|
|
58
66
|
readonly itemType?: LoopValueType | null
|
|
67
|
+
/**
|
|
68
|
+
* The item type a binding's `row` carries, when the caller knows more than
|
|
69
|
+
* `itemType` (which is scalar evidence only): a backend that resolves an
|
|
70
|
+
* object row's type from the loop's array source. Defaults to `itemType`.
|
|
71
|
+
*/
|
|
72
|
+
readonly rowType?: LoopValueType | null
|
|
59
73
|
readonly index?: string | null
|
|
60
|
-
readonly paramBindings?: readonly {
|
|
74
|
+
readonly paramBindings?: readonly {
|
|
75
|
+
readonly name: string
|
|
76
|
+
readonly segments?: readonly RowPathSegment[]
|
|
77
|
+
readonly rest?: unknown
|
|
78
|
+
}[]
|
|
61
79
|
readonly preamble?: { readonly declaredNames: readonly string[] } | null
|
|
62
80
|
}
|
|
63
81
|
|
|
64
|
-
|
|
82
|
+
/** Structural pick of `TypeInfo` (`packages/jsx/src/types.ts`). */
|
|
83
|
+
export interface LoopValueType {
|
|
65
84
|
readonly primitive?: string
|
|
66
85
|
readonly unionTypes?: readonly LoopValueType[]
|
|
67
86
|
}
|
|
68
87
|
|
|
88
|
+
/** Structural copy of `LoopBindingPathSegment` (`packages/jsx/src/types.ts`). */
|
|
89
|
+
export type RowPathSegment =
|
|
90
|
+
| { readonly kind: 'field'; readonly key: string; readonly isIdent: boolean }
|
|
91
|
+
| { readonly kind: 'index'; readonly index: number }
|
|
92
|
+
|
|
69
93
|
function isStringLoopValue(type: LoopValueType): boolean {
|
|
70
94
|
return type.primitive === 'string' ||
|
|
71
95
|
(type.unionTypes !== undefined && type.unionTypes.length > 0 && type.unionTypes.every(isStringLoopValue))
|
|
@@ -121,12 +145,19 @@ export class BindingScope {
|
|
|
121
145
|
*/
|
|
122
146
|
enterLoopRow(loop: LoopBindingSource): BindingScope {
|
|
123
147
|
const bindings = new Map<string, ScopeBinding>()
|
|
148
|
+
const rowType = loop.rowType ?? loop.itemType
|
|
124
149
|
if (loop.paramBindings && loop.paramBindings.length > 0) {
|
|
125
|
-
for (const b of loop.paramBindings)
|
|
150
|
+
for (const b of loop.paramBindings) {
|
|
151
|
+
bindings.set(b.name, {
|
|
152
|
+
source: 'destructure',
|
|
153
|
+
...(rowType && b.segments && b.rest === undefined ? { row: { itemType: rowType, segments: b.segments } } : {}),
|
|
154
|
+
})
|
|
155
|
+
}
|
|
126
156
|
} else {
|
|
127
157
|
bindings.set(loop.param, {
|
|
128
158
|
source: 'item',
|
|
129
159
|
...(loop.itemType ? { stringValue: isStringLoopValue(loop.itemType) } : {}),
|
|
160
|
+
...(rowType ? { row: { itemType: rowType, segments: [] } } : {}),
|
|
130
161
|
})
|
|
131
162
|
}
|
|
132
163
|
if (loop.index != null) bindings.set(loop.index, { source: 'index' })
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lexical binding frames for TS-AST walks over emitted or source JS text.
|
|
3
|
+
*
|
|
4
|
+
* Scope model: a name bound INSIDE the walked node — a nested arrow /
|
|
5
|
+
* function parameter, a block-scoped declaration, a `for` head's `let` /
|
|
6
|
+
* `const`, a catch variable — refers to that binding, not to an outer
|
|
7
|
+
* name of the same spelling, for the whole binding's scope. Names bound
|
|
8
|
+
* OUTSIDE the walked text (a loop callback that encloses a template
|
|
9
|
+
* fragment) are the caller's job — that is what `BindingScope`
|
|
10
|
+
* (`./binding-scope.ts`) answers; this module answers the complementary
|
|
11
|
+
* question for binders that live inside the text itself.
|
|
12
|
+
*
|
|
13
|
+
* Shared by the prop-reference rewrites in `prop-rewrite.ts`, the
|
|
14
|
+
* props-object rename in `ir-to-client-js/rewrite-props-object.ts` and the
|
|
15
|
+
* loop-param accessor rewrite in `ir-to-client-js/utils.ts`
|
|
16
|
+
* (`rewriteIdentifierReferences`, #3595), so all of them agree on which
|
|
17
|
+
* identifiers an inner binding shadows.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import ts from 'typescript'
|
|
21
|
+
|
|
22
|
+
/** Collect every name introduced by a binding name (identifier or pattern). */
|
|
23
|
+
function collectBindingNames(name: ts.BindingName, out: Set<string>): void {
|
|
24
|
+
if (ts.isIdentifier(name)) {
|
|
25
|
+
out.add(name.text)
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
for (const el of name.elements) {
|
|
29
|
+
if (ts.isBindingElement(el)) collectBindingNames(el.name, out)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Names bound by `n` for its subtree, or null when `n` introduces no
|
|
35
|
+
* scope. The frames follow JS's two scoping rules:
|
|
36
|
+
*
|
|
37
|
+
* - Function scope: a function-like binds its parameters and a function
|
|
38
|
+
* expression's own name; this frame covers the parameter list, so a
|
|
39
|
+
* default initializer sees the parameters but none of the body's
|
|
40
|
+
* declarations. A method's or accessor's computed name (`[k](k) { … }`)
|
|
41
|
+
* sits outside this frame: it is evaluated in the enclosing scope. The
|
|
42
|
+
* function's body block binds, on top of its own
|
|
43
|
+
* `let` / `const`, every `var` declared anywhere in the body — inside
|
|
44
|
+
* nested blocks and loop heads too (`if (c) { var x }` and `for (var x …)`
|
|
45
|
+
* are visible to the whole body), but not inside a nested function, which
|
|
46
|
+
* owns its own `var`s. A concise arrow body declares nothing. A class
|
|
47
|
+
* `static { … }` block is a var scope of its own in the same way. A named
|
|
48
|
+
* class expression binds its own name inside the class, as a named
|
|
49
|
+
* function expression does.
|
|
50
|
+
* - Block scope: a block binds its statement-level `let` / `const`, class
|
|
51
|
+
* and function declarations (the emitted client JS is a module, so it is
|
|
52
|
+
* strict code: a function declared in a block is scoped to that block);
|
|
53
|
+
* a `for` head binds its `let` / `const`; a catch clause binds its
|
|
54
|
+
* variable.
|
|
55
|
+
*
|
|
56
|
+
* A `var` with no enclosing function in the walked text binds at its top
|
|
57
|
+
* level, where no frame is pushed — the same as any other top-level
|
|
58
|
+
* declaration.
|
|
59
|
+
*/
|
|
60
|
+
function scopeFrameOf(n: ts.Node, parent: ts.Node | undefined): Set<string> | null {
|
|
61
|
+
const frame = new Set<string>()
|
|
62
|
+
if (ts.isFunctionLike(n)) collectFunctionBindings(n, frame)
|
|
63
|
+
else if (ts.isBlock(n)) {
|
|
64
|
+
if (parent && isFunctionBody(n, parent)) collectVarBindings(n, frame)
|
|
65
|
+
collectBlockBindings(n, frame)
|
|
66
|
+
}
|
|
67
|
+
else if (ts.isForStatement(n) || ts.isForOfStatement(n) || ts.isForInStatement(n)) collectForHeadBindings(n, frame)
|
|
68
|
+
else if (ts.isCatchClause(n) && n.variableDeclaration) collectBindingNames(n.variableDeclaration.name, frame)
|
|
69
|
+
else if (ts.isClassExpression(n) && n.name) frame.add(n.name.text)
|
|
70
|
+
return frame.size > 0 ? frame : null
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function collectFunctionBindings(n: ts.SignatureDeclaration, frame: Set<string>): void {
|
|
74
|
+
for (const p of n.parameters) collectBindingNames(p.name, frame)
|
|
75
|
+
if ((ts.isFunctionExpression(n) || ts.isFunctionDeclaration(n)) && n.name) frame.add(n.name.text)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A var-scope boundary: a function-like (function declaration / expression,
|
|
80
|
+
* arrow, method, accessor, constructor) or a class `static { … }` block. Each
|
|
81
|
+
* owns the `var`s declared in it. A class field initializer is an
|
|
82
|
+
* expression, so a `var` can only sit inside one of these boundaries there.
|
|
83
|
+
*/
|
|
84
|
+
function isVarScopeBoundary(n: ts.Node): n is ts.SignatureDeclaration | ts.ClassStaticBlockDeclaration {
|
|
85
|
+
return ts.isFunctionLike(n) || ts.isClassStaticBlockDeclaration(n)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** True when `block` is the body of the var-scope boundary `parent`. */
|
|
89
|
+
function isFunctionBody(block: ts.Block, parent: ts.Node): boolean {
|
|
90
|
+
return isVarScopeBoundary(parent) && (parent as { body?: ts.Node }).body === block
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** True for a `let` / `const` (and `using`) declaration list; false for `var`. */
|
|
94
|
+
function isBlockScopedList(list: ts.VariableDeclarationList): boolean {
|
|
95
|
+
return (list.flags & ts.NodeFlags.BlockScoped) !== 0
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Every `var`-declared name under `n`, through blocks and loop heads but not
|
|
100
|
+
* into a nested var-scope boundary (each binds its own `var`s).
|
|
101
|
+
*/
|
|
102
|
+
function collectVarBindings(n: ts.Node, frame: Set<string>): void {
|
|
103
|
+
if (ts.isVariableDeclarationList(n) && !isBlockScopedList(n)) {
|
|
104
|
+
for (const d of n.declarations) collectBindingNames(d.name, frame)
|
|
105
|
+
}
|
|
106
|
+
ts.forEachChild(n, child => {
|
|
107
|
+
if (!isVarScopeBoundary(child)) collectVarBindings(child, frame)
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function collectBlockBindings(n: ts.Block, frame: Set<string>): void {
|
|
112
|
+
for (const st of n.statements) {
|
|
113
|
+
if (ts.isVariableStatement(st)) {
|
|
114
|
+
if (!isBlockScopedList(st.declarationList)) continue
|
|
115
|
+
for (const d of st.declarationList.declarations) collectBindingNames(d.name, frame)
|
|
116
|
+
} else if ((ts.isFunctionDeclaration(st) || ts.isClassDeclaration(st)) && st.name) {
|
|
117
|
+
frame.add(st.name.text)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* A `for` head's `let` / `const` binds for the loop (`for (let i = 0; …;
|
|
124
|
+
* i++)`, `for (const x of xs)`): its condition, incrementor and body read
|
|
125
|
+
* the loop variable, not an outer prop of the same name.
|
|
126
|
+
*/
|
|
127
|
+
function collectForHeadBindings(n: ts.ForStatement | ts.ForOfStatement | ts.ForInStatement, frame: Set<string>): void {
|
|
128
|
+
if (!n.initializer || !ts.isVariableDeclarationList(n.initializer)) return
|
|
129
|
+
// A `for (var …)` binds in the enclosing function (`collectVarBindings`).
|
|
130
|
+
if (!isBlockScopedList(n.initializer)) return
|
|
131
|
+
for (const d of n.initializer.declarations) collectBindingNames(d.name, frame)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** The computed property name of a function-like (`[k](k) { … }`), if any. */
|
|
135
|
+
function computedNameOf(n: ts.Node): ts.ComputedPropertyName | undefined {
|
|
136
|
+
if (!ts.isFunctionLike(n)) return undefined
|
|
137
|
+
const name = (n as { name?: ts.Node }).name
|
|
138
|
+
return name && ts.isComputedPropertyName(name) ? name : undefined
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Depth-first walk that maintains the binding stack described above and
|
|
143
|
+
* reports every identifier along with whether a binding within `root`
|
|
144
|
+
* currently shadows it.
|
|
145
|
+
*/
|
|
146
|
+
export function walkWithScope(
|
|
147
|
+
root: ts.Node,
|
|
148
|
+
visit: (ident: ts.Identifier, parent: ts.Node | undefined, shadowed: boolean) => void,
|
|
149
|
+
): void {
|
|
150
|
+
const scopeStack: Set<string>[] = []
|
|
151
|
+
const isShadowed = (name: string) => scopeStack.some(frame => frame.has(name))
|
|
152
|
+
function rec(n: ts.Node, parent?: ts.Node) {
|
|
153
|
+
const frame = scopeFrameOf(n, parent)
|
|
154
|
+
// A method's / accessor's computed name (`{ [k](k) { … } }`) is evaluated
|
|
155
|
+
// in the enclosing scope, before the parameters exist: walk it outside
|
|
156
|
+
// the function's frame.
|
|
157
|
+
const outerName = frame ? computedNameOf(n) : undefined
|
|
158
|
+
if (outerName) rec(outerName, n)
|
|
159
|
+
if (frame) scopeStack.push(frame)
|
|
160
|
+
if (ts.isIdentifier(n)) visit(n, parent, isShadowed(n.text))
|
|
161
|
+
ts.forEachChild(n, child => {
|
|
162
|
+
if (child !== outerName) rec(child, n)
|
|
163
|
+
})
|
|
164
|
+
if (frame) scopeStack.pop()
|
|
165
|
+
}
|
|
166
|
+
rec(root)
|
|
167
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -128,6 +128,18 @@ export interface ParamInfo {
|
|
|
128
128
|
defaultParts?: PropDefaultPart[]
|
|
129
129
|
/** When true, the parameter is a rest spread (`...args`) — emit must prepend `...`. */
|
|
130
130
|
isRest?: boolean
|
|
131
|
+
/**
|
|
132
|
+
* Set when the component writes the destructured binding anywhere in its
|
|
133
|
+
* body (`++start`, `start = clamp(start)`, `[a, b] = [b, a]`,
|
|
134
|
+
* `for (start of …)`), event handlers and other callbacks included. The
|
|
135
|
+
* binding is then a local the component owns, as in JS, not the prop: the
|
|
136
|
+
* client init body declares it once (`let start = _p.start ?? 0`) and reads
|
|
137
|
+
* and writes that local instead of the live prop read `_p.start ?? 0`,
|
|
138
|
+
* which no write can take (`++(_p.start ?? 0)` is a SyntaxError). Set by
|
|
139
|
+
* `markWrittenBindings` (analyzer.ts); read through `keepsDeclaredValue`
|
|
140
|
+
* (props-binding.ts).
|
|
141
|
+
*/
|
|
142
|
+
reassigned?: boolean
|
|
131
143
|
/**
|
|
132
144
|
* Source property name for an aliased destructured prop (`{ createdAt: c }`
|
|
133
145
|
* → name: 'c', sourceName: 'createdAt'). Set ONLY when the binding renames —
|
|
@@ -2270,7 +2282,10 @@ export interface ConstantInfo {
|
|
|
2270
2282
|
* (the declaration's initializer text) no longer reflects what the
|
|
2271
2283
|
* binding actually holds by the time the rest of the component runs
|
|
2272
2284
|
* (#2910). Set by `markMutatedConstants` (analyzer.ts) from
|
|
2273
|
-
* `InitStatementInfo.mutatedIdentifiers`/`assignedIdentifiers
|
|
2285
|
+
* `InitStatementInfo.mutatedIdentifiers`/`assignedIdentifiers`, and for
|
|
2286
|
+
* a binding a markup expression writes during render (`{++nextId}`, a
|
|
2287
|
+
* `.map()` callback that increments a counter) — not an event handler,
|
|
2288
|
+
* which runs after render.
|
|
2274
2289
|
*
|
|
2275
2290
|
* Every consumer that would otherwise re-inline `value` verbatim in
|
|
2276
2291
|
* place of a bare reference to `name` — `expandDynamicPropValue`,
|
|
@@ -2282,6 +2297,15 @@ export interface ConstantInfo {
|
|
|
2282
2297
|
* was supposed to do.
|
|
2283
2298
|
*/
|
|
2284
2299
|
mutatedAfterDeclaration?: boolean
|
|
2300
|
+
/**
|
|
2301
|
+
* Set when the component writes the binding anywhere in its body, event
|
|
2302
|
+
* handlers and other callbacks included — the same fact
|
|
2303
|
+
* `ParamInfo.reassigned` records for a destructured prop, from the same
|
|
2304
|
+
* pass (`markWrittenBindings`, analyzer.ts). A written binding fails
|
|
2305
|
+
* `keepsDeclaredValue` (props-binding.ts), the one rule for whether a
|
|
2306
|
+
* binding may be read as the prop it was destructured from.
|
|
2307
|
+
*/
|
|
2308
|
+
reassigned?: boolean
|
|
2285
2309
|
/** The kind of system construct, if the initializer is createContext() or new WeakMap(). */
|
|
2286
2310
|
systemConstructKind?: 'createContext' | 'weakMap'
|
|
2287
2311
|
/** Value with destructured prop refs rewritten to _p.propName, for template inlining. */
|