@barefootjs/jsx 0.41.1 → 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/adapters/attr-plan.d.ts +8 -0
- package/dist/adapters/attr-plan.d.ts.map +1 -1
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +19 -0
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +2 -1
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/inherited-const-server-read.d.ts +37 -0
- package/dist/adapters/inherited-const-server-read.d.ts.map +1 -0
- package/dist/adapters/loop-row-type.d.ts +18 -0
- package/dist/adapters/loop-row-type.d.ts.map +1 -1
- package/dist/adapters/non-primitive-equality.d.ts +8 -2
- package/dist/adapters/non-primitive-equality.d.ts.map +1 -1
- package/dist/adapters/unapplied-prop-default.d.ts +34 -0
- package/dist/adapters/unapplied-prop-default.d.ts.map +1 -0
- package/dist/adapters/uninlinable-seed-call.d.ts +29 -0
- package/dist/adapters/uninlinable-seed-call.d.ts.map +1 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +4 -22
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/caller-value-reach.d.ts +57 -0
- package/dist/caller-value-reach.d.ts.map +1 -0
- package/dist/const-identity.d.ts +38 -0
- package/dist/const-identity.d.ts.map +1 -0
- package/dist/index.d.ts +10 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2685 -1381
- package/dist/ir-to-client-js/child-props-object.d.ts +39 -41
- package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -1
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/component-scope.d.ts +13 -2
- package/dist/ir-to-client-js/component-scope.d.ts.map +1 -1
- 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-loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/local-call-body.d.ts +112 -0
- package/dist/local-call-body.d.ts.map +1 -0
- package/dist/nullable-signal-attr.d.ts +10 -0
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/prop-default-resolution.d.ts +52 -0
- package/dist/prop-default-resolution.d.ts.map +1 -0
- package/dist/props-binding.d.ts +15 -0
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/server-read-walk.d.ts +66 -0
- package/dist/server-read-walk.d.ts.map +1 -0
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +24 -1
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/static-const-member.d.ts +92 -0
- package/dist/static-const-member.d.ts.map +1 -0
- package/dist/static-literal.d.ts +21 -1
- package/dist/static-literal.d.ts.map +1 -1
- package/dist/types.d.ts +63 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/computed-const.test.ts +7 -1
- package/src/__tests__/const-identity-child-prop.test.ts +127 -0
- package/src/__tests__/inherited-const-server-read.test.ts +116 -0
- package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +8 -3
- package/src/__tests__/issue-3714-open-rest-spread-child.test.ts +188 -0
- package/src/__tests__/local-call-body.test.ts +101 -0
- 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__/prop-default-resolution.test.ts +185 -0
- package/src/__tests__/props-spread-shadowed.test.ts +113 -0
- package/src/__tests__/server-read-walk.test.ts +125 -0
- package/src/__tests__/ssr-defaults.test.ts +67 -7
- package/src/__tests__/static-literal.test.ts +47 -2
- package/src/__tests__/static-record-literal-array.test.ts +82 -11
- package/src/adapters/attr-plan.ts +12 -1
- package/src/adapters/component-props.ts +2 -1
- package/src/adapters/computed-const.ts +108 -12
- package/src/adapters/dsl-template-adapter.ts +17 -4
- package/src/adapters/inherited-const-server-read.ts +174 -0
- package/src/adapters/loop-row-type.ts +69 -2
- package/src/adapters/non-primitive-equality.ts +17 -2
- package/src/adapters/unapplied-prop-default.ts +83 -0
- package/src/adapters/uninlinable-seed-call.ts +62 -0
- package/src/analyzer.ts +43 -3
- package/src/augment-inherited-props.ts +4 -49
- package/src/caller-value-reach.ts +436 -0
- package/src/compiler.ts +4 -4
- package/src/const-identity.ts +189 -0
- package/src/index.ts +10 -5
- package/src/ir-to-client-js/child-props-object.ts +94 -62
- package/src/ir-to-client-js/collect-elements.ts +53 -68
- package/src/ir-to-client-js/component-scope.ts +16 -2
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +1 -0
- package/src/ir-to-client-js/html-template.ts +24 -4
- package/src/ir-to-client-js/index.ts +2 -2
- package/src/ir-to-client-js/prop-handling.ts +4 -0
- package/src/jsx-to-ir.ts +40 -2
- package/src/local-call-body.ts +191 -0
- package/src/nullable-signal-attr.ts +39 -17
- package/src/prop-default-resolution.ts +195 -0
- package/src/props-binding.ts +16 -0
- package/src/server-read-walk.ts +174 -0
- package/src/ssr-defaults.ts +93 -10
- package/src/ssr-seed-plan.ts +104 -18
- package/src/static-const-member.ts +243 -0
- package/src/static-literal.ts +55 -1
- package/src/types.ts +63 -1
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether an expression can reach a value the component's caller supplied
|
|
3
|
+
* that may be, or may build, a live element.
|
|
4
|
+
*
|
|
5
|
+
* A caller can hand a component an element factory: a callback prop
|
|
6
|
+
* (`renderNode={(n) => <Pill id={n.id} />}`), or an object holding one. The
|
|
7
|
+
* element such a callback returns owns its own state, so a branch that may
|
|
8
|
+
* hold one must not be evaluated again while it is mounted
|
|
9
|
+
* (`IRExpression.readsCallerValue`). The compiler cannot see what a caller
|
|
10
|
+
* passes, so the answer is built from the safe side: a name is safe only when
|
|
11
|
+
* the caller's props cannot reach it, and everything else is not.
|
|
12
|
+
*
|
|
13
|
+
* A name read inside the component is safe when it is
|
|
14
|
+
*
|
|
15
|
+
* - declared outside the component (a module const or function, an
|
|
16
|
+
* import, a global): it can see a prop only through an argument, and
|
|
17
|
+
* the arguments are read by the same walk;
|
|
18
|
+
* - a prop whose declared type holds data only (`isDataOnlyType`): a
|
|
19
|
+
* string, number, boolean, `null` or `undefined`, an array or object of
|
|
20
|
+
* those, or a function returning one;
|
|
21
|
+
* - a component `const` or `function` declaration whose initializer or
|
|
22
|
+
* body reads only safe names, through any number of hops.
|
|
23
|
+
*
|
|
24
|
+
* It is not safe when it is the props object used whole, a rest bag, a prop
|
|
25
|
+
* with any other or no declared type (`children` included), a `let`, a const
|
|
26
|
+
* the component mutates, a binding of a nested destructuring pattern, or a
|
|
27
|
+
* component declaration that reads any of these. How the value is then used
|
|
28
|
+
* does not matter: `props.render(row)`, `props['render'](row)`,
|
|
29
|
+
* `props.render.call(null, row)`, `apply(props.render, row)`, a wrapper
|
|
30
|
+
* `const draw = row => props.render(row)`, an object `{ draw: props.render }`
|
|
31
|
+
* and a memo or signal seeded from one all read an unsafe name.
|
|
32
|
+
*
|
|
33
|
+
* The declarations are read from the component function's own AST rather than
|
|
34
|
+
* from the analyzer's `localConstants` / `localFunctions` records: those hold
|
|
35
|
+
* free identifier NAMES, which cannot tell `props.count` from `props.render`,
|
|
36
|
+
* and they have no entry for an array pattern or a nested object pattern, so
|
|
37
|
+
* a name bound by one would read as a global.
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
import ts from 'typescript'
|
|
41
|
+
import type { AnalyzerContext } from './analyzer-context.ts'
|
|
42
|
+
import { unwrapJsxTransparent } from './analyzer.ts'
|
|
43
|
+
import type { ParamInfo, TypeInfo } from './types.ts'
|
|
44
|
+
import { isValueReferenceIdentifier } from './value-references.ts'
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Whether the recorded property list of a type declaration is its whole
|
|
48
|
+
* member list. `PropertyInfo[]` keeps property signatures only: a method
|
|
49
|
+
* (`render(row): unknown`), an index signature (`[k: string]: Fn`) and a
|
|
50
|
+
* call signature are dropped, and an interface records its own members
|
|
51
|
+
* without those of an `extends` clause. A type with any of these, or with
|
|
52
|
+
* type parameters, cannot be proved data-only from the list, so it is not.
|
|
53
|
+
* `source` is the declaration text (`interface ...`, `type ... = {...}`),
|
|
54
|
+
* read with the TypeScript parser.
|
|
55
|
+
*/
|
|
56
|
+
function hasCompleteMembers(source: string): boolean {
|
|
57
|
+
const file = ts.createSourceFile('__members__.ts', source, ts.ScriptTarget.Latest, false)
|
|
58
|
+
const declaration = file.statements[0]
|
|
59
|
+
if (!declaration || file.statements.length !== 1) return false
|
|
60
|
+
let members: ts.NodeArray<ts.TypeElement>
|
|
61
|
+
if (ts.isInterfaceDeclaration(declaration)) {
|
|
62
|
+
if (declaration.heritageClauses?.length || declaration.typeParameters?.length) return false
|
|
63
|
+
members = declaration.members
|
|
64
|
+
} else if (ts.isTypeAliasDeclaration(declaration) && ts.isTypeLiteralNode(declaration.type)) {
|
|
65
|
+
if (declaration.typeParameters?.length) return false
|
|
66
|
+
members = declaration.type.members
|
|
67
|
+
} else {
|
|
68
|
+
return false
|
|
69
|
+
}
|
|
70
|
+
return members.every(ts.isPropertySignature)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Whether a value of `type` is data all the way down: it is not an element
|
|
75
|
+
* and calling anything it holds cannot produce one. A named type resolves
|
|
76
|
+
* through the file's own type definitions; one declared elsewhere is unknown.
|
|
77
|
+
*/
|
|
78
|
+
export function isDataOnlyType(
|
|
79
|
+
type: TypeInfo | undefined | null,
|
|
80
|
+
analyzer: Pick<AnalyzerContext, 'typeDefinitions'>,
|
|
81
|
+
seen: ReadonlySet<string> = new Set(),
|
|
82
|
+
): boolean {
|
|
83
|
+
if (!type) return false
|
|
84
|
+
switch (type.kind) {
|
|
85
|
+
case 'primitive':
|
|
86
|
+
return true
|
|
87
|
+
case 'union':
|
|
88
|
+
return (type.unionTypes ?? []).length > 0 && (type.unionTypes ?? []).every(t => isDataOnlyType(t, analyzer, seen))
|
|
89
|
+
case 'array':
|
|
90
|
+
return isDataOnlyType(type.elementType, analyzer, seen)
|
|
91
|
+
case 'function':
|
|
92
|
+
return isDataOnlyType(type.returnType, analyzer, seen)
|
|
93
|
+
case 'object':
|
|
94
|
+
return type.properties !== undefined
|
|
95
|
+
&& hasCompleteMembers(`type T = ${type.raw}`)
|
|
96
|
+
&& type.properties.every(p => isDataOnlyType(p.type, analyzer, seen))
|
|
97
|
+
case 'interface': {
|
|
98
|
+
// A recursive type (`type Tree = { kids: Tree[] }`) is data when the
|
|
99
|
+
// rest of it is.
|
|
100
|
+
if (seen.has(type.raw)) return true
|
|
101
|
+
// Two declarations of one name merge (`interface X` twice); the
|
|
102
|
+
// recorded list is then partial.
|
|
103
|
+
const named = analyzer.typeDefinitions.filter(d => d.name === type.raw)
|
|
104
|
+
const definition = named.length === 1 ? named[0] : undefined
|
|
105
|
+
if (!definition?.properties || !hasCompleteMembers(definition.definition)) return false
|
|
106
|
+
const next = new Set(seen).add(type.raw)
|
|
107
|
+
return definition.properties.every(p => isDataOnlyType(p.type, analyzer, next))
|
|
108
|
+
}
|
|
109
|
+
case 'unknown':
|
|
110
|
+
return false
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The component's names a caller's element can be reached through. */
|
|
115
|
+
interface CallerReach {
|
|
116
|
+
/** The props object and the consts that alias it (`const p = props`). */
|
|
117
|
+
readonly propsObjects: ReadonlySet<string>
|
|
118
|
+
/** Every other component-scope name that is not safe. */
|
|
119
|
+
readonly unsafe: ReadonlySet<string>
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** A component-scope declaration still to be judged by what it reads. */
|
|
123
|
+
interface PendingDeclaration {
|
|
124
|
+
readonly names: readonly string[]
|
|
125
|
+
/** The initializer, or the whole function declaration. */
|
|
126
|
+
readonly reads: ts.Node
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const reachCache = new WeakMap<AnalyzerContext, CallerReach | null>()
|
|
130
|
+
|
|
131
|
+
function collectBindingNames(name: ts.BindingName, out: string[]): void {
|
|
132
|
+
if (ts.isIdentifier(name)) {
|
|
133
|
+
out.push(name.text)
|
|
134
|
+
return
|
|
135
|
+
}
|
|
136
|
+
for (const element of name.elements) {
|
|
137
|
+
if (!ts.isOmittedExpression(element)) collectBindingNames(element.name, out)
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function isFunctionLike(node: ts.Node): node is ts.FunctionLikeDeclaration {
|
|
142
|
+
return ts.isArrowFunction(node) || ts.isFunctionExpression(node) || ts.isFunctionDeclaration(node)
|
|
143
|
+
|| ts.isMethodDeclaration(node) || ts.isGetAccessorDeclaration(node) || ts.isSetAccessorDeclaration(node)
|
|
144
|
+
|| ts.isConstructorDeclaration(node)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** The component function: the one whose body the analyzer recorded, or that returns the JSX. */
|
|
148
|
+
function componentFunction(analyzer: AnalyzerContext): ts.FunctionLikeDeclaration | null {
|
|
149
|
+
let node: ts.Node | undefined = analyzer.componentBodyBlock ?? analyzer.jsxReturn ?? undefined
|
|
150
|
+
while (node && !isFunctionLike(node)) node = node.parent
|
|
151
|
+
return node ?? null
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** The declared type of the prop `key` names: a destructured local name, or a member of the props object. */
|
|
155
|
+
function propTypeOf(key: string, byLocalName: boolean, analyzer: AnalyzerContext): TypeInfo | undefined {
|
|
156
|
+
const param: ParamInfo | undefined = byLocalName
|
|
157
|
+
? analyzer.propsParams.find(p => p.name === key && !p.isRest)
|
|
158
|
+
: analyzer.propsParams.find(p => (p.sourceName ?? p.name) === key && !p.isRest)
|
|
159
|
+
return param?.declaredType ?? param?.type
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The prop a use of the props object reads: `props.name`, `props['name']`,
|
|
164
|
+
* through parentheses, `!` and `as`. `null` for any other use, which hands
|
|
165
|
+
* the whole object on.
|
|
166
|
+
*/
|
|
167
|
+
function readPropName(propsIdentifier: ts.Identifier): string | null {
|
|
168
|
+
let inner: ts.Node = propsIdentifier
|
|
169
|
+
let parent = inner.parent
|
|
170
|
+
while (
|
|
171
|
+
parent &&
|
|
172
|
+
(ts.isParenthesizedExpression(parent) || ts.isNonNullExpression(parent) || ts.isAsExpression(parent) || ts.isSatisfiesExpression(parent))
|
|
173
|
+
) {
|
|
174
|
+
inner = parent
|
|
175
|
+
parent = parent.parent
|
|
176
|
+
}
|
|
177
|
+
if (!parent) return null
|
|
178
|
+
if (ts.isPropertyAccessExpression(parent) && parent.expression === inner) return parent.name.text
|
|
179
|
+
if (ts.isElementAccessExpression(parent) && parent.expression === inner && ts.isStringLiteralLike(parent.argumentExpression)) {
|
|
180
|
+
return parent.argumentExpression.text
|
|
181
|
+
}
|
|
182
|
+
return null
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Whether anything under `node` reads a name a caller's element can be
|
|
187
|
+
* reached through. `isSafeName` answers first for names the caller knows
|
|
188
|
+
* (a loop row's item); a name a nested function binds as a parameter is that
|
|
189
|
+
* function's own.
|
|
190
|
+
*/
|
|
191
|
+
function readsUnsafeName(
|
|
192
|
+
node: ts.Node,
|
|
193
|
+
reach: { propsObjects: ReadonlySet<string>; unsafe: ReadonlySet<string> },
|
|
194
|
+
analyzer: AnalyzerContext,
|
|
195
|
+
resolveOuter: (name: string) => boolean | undefined,
|
|
196
|
+
): boolean {
|
|
197
|
+
const visit = (n: ts.Node, bound: ReadonlySet<string>): boolean => {
|
|
198
|
+
if (ts.isTypeNode(n)) return false
|
|
199
|
+
if (ts.isIdentifier(n)) {
|
|
200
|
+
if (!isValueReferenceIdentifier(n) || bound.has(n.text)) return false
|
|
201
|
+
const outer = resolveOuter(n.text)
|
|
202
|
+
if (outer !== undefined) return outer
|
|
203
|
+
if (reach.propsObjects.has(n.text)) {
|
|
204
|
+
const prop = readPropName(n)
|
|
205
|
+
return prop === null || !isDataOnlyType(propTypeOf(prop, false, analyzer), analyzer)
|
|
206
|
+
}
|
|
207
|
+
return reach.unsafe.has(n.text)
|
|
208
|
+
}
|
|
209
|
+
let inner = bound
|
|
210
|
+
if (isFunctionLike(n) && n.parameters.length > 0) {
|
|
211
|
+
const params: string[] = []
|
|
212
|
+
for (const p of n.parameters) collectBindingNames(p.name, params)
|
|
213
|
+
inner = new Set([...bound, ...params])
|
|
214
|
+
}
|
|
215
|
+
return ts.forEachChild(n, child => visit(child, inner) || undefined) === true
|
|
216
|
+
}
|
|
217
|
+
return visit(node, new Set())
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Collect the declarations of the statements a component body runs, without entering nested functions. */
|
|
221
|
+
function collectStatementDeclarations(
|
|
222
|
+
statement: ts.Node,
|
|
223
|
+
declarations: ts.VariableDeclaration[],
|
|
224
|
+
functions: ts.FunctionDeclaration[],
|
|
225
|
+
otherNames: string[],
|
|
226
|
+
): void {
|
|
227
|
+
if (ts.isFunctionDeclaration(statement)) {
|
|
228
|
+
if (statement.name) functions.push(statement)
|
|
229
|
+
return
|
|
230
|
+
}
|
|
231
|
+
if (ts.isClassDeclaration(statement)) {
|
|
232
|
+
if (statement.name) otherNames.push(statement.name.text)
|
|
233
|
+
return
|
|
234
|
+
}
|
|
235
|
+
if (ts.isVariableDeclaration(statement)) {
|
|
236
|
+
declarations.push(statement)
|
|
237
|
+
return
|
|
238
|
+
}
|
|
239
|
+
if (ts.isExpression(statement) || isFunctionLike(statement)) return
|
|
240
|
+
ts.forEachChild(statement, child => collectStatementDeclarations(child, declarations, functions, otherNames))
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** Sort the props parameter's bindings into the props object and the unsafe names. */
|
|
244
|
+
function classifyParameters(
|
|
245
|
+
fn: ts.FunctionLikeDeclaration,
|
|
246
|
+
analyzer: AnalyzerContext,
|
|
247
|
+
propsObjects: Set<string>,
|
|
248
|
+
unsafe: Set<string>,
|
|
249
|
+
): void {
|
|
250
|
+
fn.parameters.forEach((param, index) => {
|
|
251
|
+
if (index === 0 && ts.isIdentifier(param.name)) {
|
|
252
|
+
propsObjects.add(param.name.text)
|
|
253
|
+
return
|
|
254
|
+
}
|
|
255
|
+
if (index === 0 && ts.isObjectBindingPattern(param.name)) {
|
|
256
|
+
classifyPropsPattern(param.name, true, analyzer, unsafe, [])
|
|
257
|
+
return
|
|
258
|
+
}
|
|
259
|
+
const names: string[] = []
|
|
260
|
+
collectBindingNames(param.name, names)
|
|
261
|
+
for (const name of names) unsafe.add(name)
|
|
262
|
+
})
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Judge each binding of an object pattern over the props object: a plain
|
|
267
|
+
* `name`, `key: name` or `name = default` binding of a data-only prop is
|
|
268
|
+
* safe (its default is judged later, with the other declarations); a rest
|
|
269
|
+
* element, a nested pattern or any other prop is not.
|
|
270
|
+
*/
|
|
271
|
+
function classifyPropsPattern(
|
|
272
|
+
pattern: ts.ObjectBindingPattern,
|
|
273
|
+
isParameter: boolean,
|
|
274
|
+
analyzer: AnalyzerContext,
|
|
275
|
+
unsafe: Set<string>,
|
|
276
|
+
pending: PendingDeclaration[],
|
|
277
|
+
): void {
|
|
278
|
+
for (const element of pattern.elements) {
|
|
279
|
+
const names: string[] = []
|
|
280
|
+
collectBindingNames(element.name, names)
|
|
281
|
+
const key = element.propertyName ?? element.name
|
|
282
|
+
const plain = !element.dotDotDotToken && ts.isIdentifier(element.name) && (ts.isIdentifier(key) || ts.isStringLiteralLike(key))
|
|
283
|
+
const type = plain
|
|
284
|
+
? isParameter
|
|
285
|
+
? propTypeOf((element.name as ts.Identifier).text, true, analyzer)
|
|
286
|
+
: propTypeOf((key as ts.Identifier | ts.StringLiteralLike).text, false, analyzer)
|
|
287
|
+
: undefined
|
|
288
|
+
if (!plain || !isDataOnlyType(type, analyzer)) {
|
|
289
|
+
for (const name of names) unsafe.add(name)
|
|
290
|
+
} else if (element.initializer) {
|
|
291
|
+
pending.push({ names, reads: element.initializer })
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** The names a variable declaration binds. */
|
|
297
|
+
function declaredNames(declaration: ts.VariableDeclaration): string[] {
|
|
298
|
+
const names: string[] = []
|
|
299
|
+
collectBindingNames(declaration.name, names)
|
|
300
|
+
return names
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Split the component's variable declarations into the `const`s that can be
|
|
305
|
+
* judged by their initializer and the names that cannot: a `let` or `var`,
|
|
306
|
+
* a declaration with no initializer, and a const the component mutates.
|
|
307
|
+
*/
|
|
308
|
+
function analysableConsts(
|
|
309
|
+
declarations: readonly ts.VariableDeclaration[],
|
|
310
|
+
analyzer: AnalyzerContext,
|
|
311
|
+
unsafe: Set<string>,
|
|
312
|
+
): ts.VariableDeclaration[] {
|
|
313
|
+
const mutated = new Set(analyzer.localConstants.filter(c => c.mutatedAfterDeclaration).map(c => c.name))
|
|
314
|
+
const consts: ts.VariableDeclaration[] = []
|
|
315
|
+
for (const declaration of declarations) {
|
|
316
|
+
const names = declaredNames(declaration)
|
|
317
|
+
const isConst = ts.isVariableDeclarationList(declaration.parent) && (declaration.parent.flags & ts.NodeFlags.Const) !== 0
|
|
318
|
+
if (isConst && declaration.initializer && !names.some(n => mutated.has(n))) {
|
|
319
|
+
consts.push(declaration)
|
|
320
|
+
} else {
|
|
321
|
+
for (const name of names) unsafe.add(name)
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return consts
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** Whether an initializer is the props object itself: `props`, or a name already known to alias it. */
|
|
328
|
+
function isPropsObjectValue(init: ts.Expression, propsObjects: ReadonlySet<string>): boolean {
|
|
329
|
+
const value = unwrapJsxTransparent(init)
|
|
330
|
+
return ts.isIdentifier(value) && propsObjects.has(value.text)
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Move every `const p = props` alias, through any number of hops and in any
|
|
335
|
+
* declaration order, into `propsObjects`; return the other consts.
|
|
336
|
+
*/
|
|
337
|
+
function takePropsAliases(consts: readonly ts.VariableDeclaration[], propsObjects: Set<string>): ts.VariableDeclaration[] {
|
|
338
|
+
let rest = [...consts]
|
|
339
|
+
for (let grew = true; grew; ) {
|
|
340
|
+
grew = false
|
|
341
|
+
const next: ts.VariableDeclaration[] = []
|
|
342
|
+
for (const declaration of rest) {
|
|
343
|
+
if (ts.isIdentifier(declaration.name) && isPropsObjectValue(declaration.initializer!, propsObjects)) {
|
|
344
|
+
propsObjects.add(declaration.name.text)
|
|
345
|
+
grew = true
|
|
346
|
+
} else {
|
|
347
|
+
next.push(declaration)
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
rest = next
|
|
351
|
+
}
|
|
352
|
+
return rest
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Mark a declaration unsafe as soon as it reads an unsafe name, and repeat
|
|
357
|
+
* until none changes, so a chain of hops ends up unsafe in any order.
|
|
358
|
+
*/
|
|
359
|
+
function settleDeclarations(
|
|
360
|
+
pending: readonly PendingDeclaration[],
|
|
361
|
+
reach: { propsObjects: ReadonlySet<string>; unsafe: Set<string> },
|
|
362
|
+
analyzer: AnalyzerContext,
|
|
363
|
+
): void {
|
|
364
|
+
let open = [...pending]
|
|
365
|
+
for (let changed = true; changed; ) {
|
|
366
|
+
changed = false
|
|
367
|
+
const still: PendingDeclaration[] = []
|
|
368
|
+
for (const declaration of open) {
|
|
369
|
+
if (readsUnsafeName(declaration.reads, reach, analyzer, () => undefined)) {
|
|
370
|
+
for (const name of declaration.names) reach.unsafe.add(name)
|
|
371
|
+
changed = true
|
|
372
|
+
} else {
|
|
373
|
+
still.push(declaration)
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
open = still
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function buildCallerReach(analyzer: AnalyzerContext): CallerReach | null {
|
|
381
|
+
const fn = componentFunction(analyzer)
|
|
382
|
+
if (!fn) return null
|
|
383
|
+
const propsObjects = new Set<string>()
|
|
384
|
+
const unsafe = new Set<string>()
|
|
385
|
+
const pending: PendingDeclaration[] = []
|
|
386
|
+
classifyParameters(fn, analyzer, propsObjects, unsafe)
|
|
387
|
+
|
|
388
|
+
const declarations: ts.VariableDeclaration[] = []
|
|
389
|
+
const functions: ts.FunctionDeclaration[] = []
|
|
390
|
+
const otherNames: string[] = []
|
|
391
|
+
if (fn.body && ts.isBlock(fn.body)) {
|
|
392
|
+
for (const statement of fn.body.statements) collectStatementDeclarations(statement, declarations, functions, otherNames)
|
|
393
|
+
}
|
|
394
|
+
for (const name of otherNames) unsafe.add(name)
|
|
395
|
+
for (const declaration of functions) pending.push({ names: [declaration.name!.text], reads: declaration })
|
|
396
|
+
|
|
397
|
+
const consts = takePropsAliases(analysableConsts(declarations, analyzer, unsafe), propsObjects)
|
|
398
|
+
for (const declaration of consts) {
|
|
399
|
+
const init = declaration.initializer!
|
|
400
|
+
if (ts.isObjectBindingPattern(declaration.name) && isPropsObjectValue(init, propsObjects)) {
|
|
401
|
+
classifyPropsPattern(declaration.name, false, analyzer, unsafe, pending)
|
|
402
|
+
} else {
|
|
403
|
+
pending.push({ names: declaredNames(declaration), reads: init })
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const reach = { propsObjects, unsafe }
|
|
408
|
+
settleDeclarations(pending, reach, analyzer)
|
|
409
|
+
return reach
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Whether `node`, an expression of the component's JSX, reads a value the
|
|
414
|
+
* caller supplied that may be or build a live element. `rowName` answers for
|
|
415
|
+
* the names the enclosing loop rows bind: `true` for a row's item, index or
|
|
416
|
+
* destructured name (the row's own data), `false` for any other name a row
|
|
417
|
+
* or callback binds (a `.map()` preamble local, whose initializer this walk
|
|
418
|
+
* does not see), `undefined` for a name they do not bind.
|
|
419
|
+
*/
|
|
420
|
+
export function readsCallerValue(
|
|
421
|
+
node: ts.Node,
|
|
422
|
+
analyzer: AnalyzerContext,
|
|
423
|
+
rowName: (name: string) => boolean | undefined,
|
|
424
|
+
): boolean {
|
|
425
|
+
let reach = reachCache.get(analyzer)
|
|
426
|
+
if (reach === undefined) {
|
|
427
|
+
reach = buildCallerReach(analyzer)
|
|
428
|
+
reachCache.set(analyzer, reach)
|
|
429
|
+
}
|
|
430
|
+
// No component function to read the declarations from: nothing is known safe.
|
|
431
|
+
if (reach === null) return true
|
|
432
|
+
return readsUnsafeName(node, reach, analyzer, name => {
|
|
433
|
+
const row = rowName(name)
|
|
434
|
+
return row === undefined ? undefined : !row
|
|
435
|
+
})
|
|
436
|
+
}
|
package/src/compiler.ts
CHANGED
|
@@ -410,7 +410,7 @@ function compileMultipleComponents(
|
|
|
410
410
|
nonExportedSiblings.add(componentIR.metadata.componentName)
|
|
411
411
|
}
|
|
412
412
|
}
|
|
413
|
-
setActiveComponentScope({ fileScope, nonExportedSiblings })
|
|
413
|
+
const outerScope = setActiveComponentScope({ fileScope, nonExportedSiblings })
|
|
414
414
|
// Same adapter capabilities for every component compiled in this file
|
|
415
415
|
// (#1187 phase 3).
|
|
416
416
|
const multiAdapterCaps = {
|
|
@@ -482,7 +482,7 @@ function compileMultipleComponents(
|
|
|
482
482
|
errors.push(...componentIR.errors)
|
|
483
483
|
}
|
|
484
484
|
} finally {
|
|
485
|
-
setActiveComponentScope(
|
|
485
|
+
setActiveComponentScope(outerScope)
|
|
486
486
|
}
|
|
487
487
|
|
|
488
488
|
if (allOutputs.length === 0) {
|
|
@@ -956,7 +956,7 @@ export function compileJSX(
|
|
|
956
956
|
? new Set<string>()
|
|
957
957
|
: new Set([componentIR.metadata.componentName]),
|
|
958
958
|
}
|
|
959
|
-
setActiveComponentScope(singleScope)
|
|
959
|
+
const outerScope = setActiveComponentScope(singleScope)
|
|
960
960
|
// Adapter capabilities thread through to relocate's inline-safety
|
|
961
961
|
// check so a registered template primitive escapes the bridged-arg /
|
|
962
962
|
// zero-arg rejection (#1187 phase 3).
|
|
@@ -1000,7 +1000,7 @@ export function compileJSX(
|
|
|
1000
1000
|
}
|
|
1001
1001
|
}
|
|
1002
1002
|
} finally {
|
|
1003
|
-
setActiveComponentScope(
|
|
1003
|
+
setActiveComponentScope(outerScope)
|
|
1004
1004
|
}
|
|
1005
1005
|
|
|
1006
1006
|
return { files, errors }
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which consts a read site must reference as the binding itself rather than
|
|
3
|
+
* re-evaluate from the initializer (#3868).
|
|
4
|
+
*
|
|
5
|
+
* The client emitter passes a child component's props as getters, and for a
|
|
6
|
+
* prop whose value is a bare const it used to splice the const's initializer
|
|
7
|
+
* text into the getter (`get token() { return { n: 1 } }`). Each read then
|
|
8
|
+
* evaluated the initializer again. For a value that is a primitive this is
|
|
9
|
+
* invisible, and for one that reads a signal, a memo or a prop it is what
|
|
10
|
+
* keeps the prop live. For an initializer that allocates (an object or array
|
|
11
|
+
* literal, `new X()`, a call, a function expression) and reads nothing
|
|
12
|
+
* reactive it only breaks identity: two reads of the prop see two objects,
|
|
13
|
+
* where the source and the SSR render pass the one object the declaration
|
|
14
|
+
* created.
|
|
15
|
+
*
|
|
16
|
+
* The decision is made here, in Phase 1, from the initializer's AST and the
|
|
17
|
+
* component's bindings, and carried as `ConstantInfo.identityBound`. The
|
|
18
|
+
* emitter only reads the flag.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import ts from 'typescript'
|
|
22
|
+
import type { AnalyzerContext } from './analyzer-context.ts'
|
|
23
|
+
import type { ConstantInfo } from './types.ts'
|
|
24
|
+
import { containsReactiveExpression } from './reactivity-checker.ts'
|
|
25
|
+
import { signalSecondBinding } from './signal-initializer.ts'
|
|
26
|
+
|
|
27
|
+
function unwrapTransparent(node: ts.Expression): ts.Expression {
|
|
28
|
+
let current = node
|
|
29
|
+
while (
|
|
30
|
+
ts.isParenthesizedExpression(current) ||
|
|
31
|
+
ts.isAsExpression(current) ||
|
|
32
|
+
ts.isSatisfiesExpression(current) ||
|
|
33
|
+
ts.isNonNullExpression(current) ||
|
|
34
|
+
ts.isTypeAssertionExpression(current)
|
|
35
|
+
) {
|
|
36
|
+
current = current.expression
|
|
37
|
+
}
|
|
38
|
+
return current
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function isAllocatingNode(node: ts.Expression): boolean {
|
|
42
|
+
return (
|
|
43
|
+
ts.isObjectLiteralExpression(node) ||
|
|
44
|
+
ts.isArrayLiteralExpression(node) ||
|
|
45
|
+
ts.isNewExpression(node) ||
|
|
46
|
+
ts.isCallExpression(node) ||
|
|
47
|
+
ts.isTaggedTemplateExpression(node) ||
|
|
48
|
+
ts.isArrowFunction(node) ||
|
|
49
|
+
ts.isFunctionExpression(node) ||
|
|
50
|
+
ts.isClassExpression(node) ||
|
|
51
|
+
ts.isRegularExpressionLiteral(node)
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const VALUE_SELECTING_OPERATORS: ReadonlySet<ts.SyntaxKind> = new Set([
|
|
56
|
+
ts.SyntaxKind.QuestionQuestionToken,
|
|
57
|
+
ts.SyntaxKind.BarBarToken,
|
|
58
|
+
ts.SyntaxKind.AmpersandAmpersandToken,
|
|
59
|
+
ts.SyntaxKind.CommaToken,
|
|
60
|
+
])
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Can evaluating `initializer` a second time yield a different object than
|
|
64
|
+
* the first evaluation did? True when the value it produces may be one it
|
|
65
|
+
* creates: an allocating expression itself, or a ternary / `??` / `||` /
|
|
66
|
+
* `&&` / comma whose result may be such an operand.
|
|
67
|
+
*/
|
|
68
|
+
export function initializerAllocates(initializer: ts.Expression): boolean {
|
|
69
|
+
const node = unwrapTransparent(initializer)
|
|
70
|
+
if (isAllocatingNode(node)) return true
|
|
71
|
+
if (ts.isConditionalExpression(node)) {
|
|
72
|
+
return initializerAllocates(node.whenTrue) || initializerAllocates(node.whenFalse)
|
|
73
|
+
}
|
|
74
|
+
if (ts.isBinaryExpression(node) && VALUE_SELECTING_OPERATORS.has(node.operatorToken.kind)) {
|
|
75
|
+
return initializerAllocates(node.left) || initializerAllocates(node.right)
|
|
76
|
+
}
|
|
77
|
+
return false
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Is `initializer` an arrow or function expression (evaluating it reads nothing)? */
|
|
81
|
+
function isFunctionExpressionNode(initializer: ts.Expression): boolean {
|
|
82
|
+
const node = unwrapTransparent(initializer)
|
|
83
|
+
return ts.isArrowFunction(node) || ts.isFunctionExpression(node)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface IdentityCandidate {
|
|
87
|
+
readsReactiveBrand: boolean
|
|
88
|
+
isFunctionExpression: boolean
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* The per-declaration half of the decision, recorded while the declaration's
|
|
93
|
+
* AST is at hand: only consts whose initializer allocates are candidates, and
|
|
94
|
+
* whether the initializer reads a `Reactive<T>`-branded value needs the type
|
|
95
|
+
* checker (a component compiled without one reads none). Kept off
|
|
96
|
+
* `ConstantInfo`: `markIdentityBoundConstants` turns it into
|
|
97
|
+
* `identityBound` once the whole body has been walked.
|
|
98
|
+
*/
|
|
99
|
+
const candidates: WeakMap<ConstantInfo, IdentityCandidate> = new WeakMap()
|
|
100
|
+
|
|
101
|
+
export function recordIdentityCandidate(
|
|
102
|
+
constant: ConstantInfo,
|
|
103
|
+
initializer: ts.Expression | undefined,
|
|
104
|
+
checker: ts.TypeChecker | null,
|
|
105
|
+
): void {
|
|
106
|
+
if (!initializer || !initializerAllocates(initializer)) return
|
|
107
|
+
if (isFunctionExpressionNode(initializer)) {
|
|
108
|
+
// Evaluating a function expression reads none of the names its body
|
|
109
|
+
// reads, so re-evaluating it never made the prop reactive.
|
|
110
|
+
candidates.set(constant, { readsReactiveBrand: false, isFunctionExpression: true })
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
const readsReactiveBrand = checker ? containsReactiveExpression(initializer, checker) : false
|
|
114
|
+
candidates.set(constant, { readsReactiveBrand, isFunctionExpression: false })
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The component's bindings whose read, re-evaluated at each prop read, can
|
|
119
|
+
* see a new value: signal accessors (and their setters / actions), memos,
|
|
120
|
+
* props, the body-destructure aliases of props (rewritten to live prop
|
|
121
|
+
* reads), and component-scope functions, whose call may read any of those.
|
|
122
|
+
*/
|
|
123
|
+
function reactiveSourceNames(ctx: AnalyzerContext): Set<string> {
|
|
124
|
+
const names = new Set<string>()
|
|
125
|
+
for (const s of ctx.signals) {
|
|
126
|
+
names.add(s.getter)
|
|
127
|
+
const second = signalSecondBinding(s)
|
|
128
|
+
if (second) names.add(second)
|
|
129
|
+
}
|
|
130
|
+
for (const m of ctx.memos) names.add(m.name)
|
|
131
|
+
for (const p of ctx.propsParams) names.add(p.name)
|
|
132
|
+
if (ctx.propsObjectName) names.add(ctx.propsObjectName)
|
|
133
|
+
if (ctx.restPropsName) names.add(ctx.restPropsName)
|
|
134
|
+
for (const f of ctx.localFunctions) {
|
|
135
|
+
if (!f.isModule) names.add(f.name)
|
|
136
|
+
}
|
|
137
|
+
for (const c of ctx.localConstants) {
|
|
138
|
+
if (c.isModule) continue
|
|
139
|
+
if (c.propsDestructure || c.isFunctionValue) names.add(c.name)
|
|
140
|
+
}
|
|
141
|
+
addReactiveAliases(ctx, names)
|
|
142
|
+
return names
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* A component const whose initializer reads a reactive source is one too:
|
|
147
|
+
* `const p = props` or `const get = count` aliases it, and
|
|
148
|
+
* `const half = count() / 2` derives from it. The client inlines such a
|
|
149
|
+
* const at each read, so a const that reads IT stays live as well. Runs
|
|
150
|
+
* to a fixpoint, since an alias may be declared through another alias.
|
|
151
|
+
*/
|
|
152
|
+
function addReactiveAliases(ctx: AnalyzerContext, names: Set<string>): void {
|
|
153
|
+
const consts = ctx.localConstants.filter(c => !c.isModule)
|
|
154
|
+
let grew = true
|
|
155
|
+
while (grew) {
|
|
156
|
+
grew = false
|
|
157
|
+
for (const c of consts) {
|
|
158
|
+
if (names.has(c.name) || !readsAny(c.freeIdentifiers, names)) continue
|
|
159
|
+
names.add(c.name)
|
|
160
|
+
grew = true
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Set `identityBound` on every const whose initializer allocates and reads
|
|
167
|
+
* nothing reactive (see the module comment). A const written after its
|
|
168
|
+
* declaration is left alone: `mutatedAfterDeclaration` already keeps it a
|
|
169
|
+
* reference.
|
|
170
|
+
*/
|
|
171
|
+
export function markIdentityBoundConstants(ctx: AnalyzerContext): void {
|
|
172
|
+
const pending = ctx.localConstants.filter(c => candidates.has(c) && !c.isJsx && !c.isJsxFunction)
|
|
173
|
+
if (pending.length === 0) return
|
|
174
|
+
const reactive = reactiveSourceNames(ctx)
|
|
175
|
+
for (const constant of pending) {
|
|
176
|
+
const candidate = candidates.get(constant)!
|
|
177
|
+
if (candidate.readsReactiveBrand) continue
|
|
178
|
+
if (!candidate.isFunctionExpression && readsAny(constant.freeIdentifiers, reactive)) continue
|
|
179
|
+
constant.identityBound = true
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function readsAny(names: ReadonlySet<string> | undefined, of: ReadonlySet<string>): boolean {
|
|
184
|
+
if (!names) return false
|
|
185
|
+
for (const name of names) {
|
|
186
|
+
if (of.has(name)) return true
|
|
187
|
+
}
|
|
188
|
+
return false
|
|
189
|
+
}
|