@barefootjs/jsx 0.39.5 → 0.39.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/array-method.d.ts +93 -0
- package/dist/adapters/array-method.d.ts.map +1 -0
- package/dist/adapters/attr-plan.d.ts +142 -0
- package/dist/adapters/attr-plan.d.ts.map +1 -0
- package/dist/adapters/boolean-result.d.ts +84 -9
- package/dist/adapters/boolean-result.d.ts.map +1 -1
- package/dist/adapters/callback-method.d.ts +153 -0
- package/dist/adapters/callback-method.d.ts.map +1 -0
- package/dist/adapters/component-props.d.ts +99 -0
- package/dist/adapters/component-props.d.ts.map +1 -0
- package/dist/adapters/component-tree.d.ts +25 -0
- package/dist/adapters/component-tree.d.ts.map +1 -0
- package/dist/adapters/computed-const.d.ts +52 -0
- package/dist/adapters/computed-const.d.ts.map +1 -0
- package/dist/adapters/dangerous-inner-html.d.ts +10 -0
- package/dist/adapters/dangerous-inner-html.d.ts.map +1 -1
- package/dist/adapters/element-shell.d.ts +44 -0
- package/dist/adapters/element-shell.d.ts.map +1 -0
- package/dist/adapters/interface.d.ts +2 -0
- package/dist/adapters/interface.d.ts.map +1 -1
- package/dist/adapters/literal-value.d.ts +22 -0
- package/dist/adapters/literal-value.d.ts.map +1 -0
- package/dist/adapters/loop-plan.d.ts +53 -0
- package/dist/adapters/loop-plan.d.ts.map +1 -0
- package/dist/adapters/prop-classes.d.ts +37 -0
- package/dist/adapters/prop-classes.d.ts.map +1 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +7 -0
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/expression-parser.d.ts +38 -0
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/fold-render-reassignments.d.ts +43 -0
- package/dist/fold-render-reassignments.d.ts.map +1 -0
- package/dist/free-refs.d.ts +9 -0
- package/dist/free-refs.d.ts.map +1 -1
- package/dist/index.d.ts +23 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3954 -2401
- package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
- package/dist/ir-to-client-js/csr-substitute.d.ts +0 -13
- package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts +9 -10
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/index.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/declaration-emit.d.ts +5 -0
- package/dist/ir-to-client-js/plan/declaration-emit.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts +11 -12
- package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +13 -0
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts +465 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +22 -2
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/props-binding.d.ts +34 -5
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/relocate.d.ts +16 -0
- package/dist/relocate.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +0 -6
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/types.d.ts +54 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +1 -1
- package/src/__tests__/action-accessor.test.ts +6 -24
- package/src/__tests__/array-method-catalogue.test.ts +99 -0
- package/src/__tests__/attr-plan.test.ts +121 -0
- package/src/__tests__/binding-scope-ratchet.test.ts +12 -12
- package/src/__tests__/block-memo-type.test.ts +51 -0
- package/src/__tests__/boolean-result-expr.test.ts +106 -0
- package/src/__tests__/boolean-result.test.ts +34 -1
- package/src/__tests__/callback-method.test.ts +264 -0
- package/src/__tests__/client-only-date-lowering.test.ts +2 -2
- package/src/__tests__/component-props.test.ts +148 -0
- package/src/__tests__/computed-const.test.ts +101 -0
- package/src/__tests__/delegated-handler-preamble.test.ts +4 -4
- package/src/__tests__/dsl-shared-helpers.test.ts +166 -0
- package/src/__tests__/element-shell.test.ts +78 -0
- package/src/__tests__/fold-render-reassignments.test.ts +96 -0
- package/src/__tests__/inlinable-local-method-call.test.ts +81 -0
- package/src/__tests__/issue-3240-nested-static-map-event-delegation.test.ts +1 -1
- package/src/__tests__/loop-plan.test.ts +104 -0
- package/src/__tests__/map-call-decisions.test.ts +521 -0
- package/src/__tests__/memo-body-expr.test.ts +120 -0
- package/src/__tests__/nullable-signal-attr.test.ts +45 -0
- package/src/__tests__/prop-handling.test.ts +35 -16
- package/src/__tests__/props-alias-const.test.ts +65 -0
- package/src/__tests__/signal-prop-seed.test.ts +90 -0
- package/src/__tests__/ssr-seed-plan.test.ts +54 -1
- package/src/__tests__/static-template-reachability.test.ts +137 -0
- package/src/adapters/array-method.ts +193 -0
- package/src/adapters/attr-plan.ts +228 -0
- package/src/adapters/boolean-result.ts +208 -12
- package/src/adapters/callback-method.ts +363 -0
- package/src/adapters/component-props.ts +144 -0
- package/src/adapters/component-tree.ts +118 -0
- package/src/adapters/computed-const.ts +185 -0
- package/src/adapters/dangerous-inner-html.ts +33 -0
- package/src/adapters/element-shell.ts +160 -0
- package/src/adapters/interface.ts +8 -0
- package/src/adapters/literal-value.ts +36 -0
- package/src/adapters/loop-plan.ts +88 -0
- package/src/adapters/prop-classes.ts +73 -0
- package/src/analyzer.ts +271 -82
- package/src/augment-inherited-props.ts +12 -0
- package/src/expression-parser.ts +537 -374
- package/src/fold-render-reassignments.ts +314 -0
- package/src/free-refs.ts +49 -0
- package/src/index.ts +61 -3
- package/src/ir-to-client-js/compute-inlinability.ts +93 -17
- package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +9 -7
- package/src/ir-to-client-js/csr-substitute.ts +13 -35
- package/src/ir-to-client-js/emit-registration.ts +3 -2
- package/src/ir-to-client-js/html-template.ts +26 -70
- package/src/ir-to-client-js/index.ts +3 -2
- package/src/ir-to-client-js/init-declarations.ts +1 -1
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +25 -51
- package/src/ir-to-client-js/plan/declaration-emit.ts +5 -0
- package/src/ir-to-client-js/prop-handling.ts +13 -54
- package/src/ir-to-client-js/rewrite-destructured-props.ts +6 -5
- package/src/ir-to-client-js/stringify/declaration-emit.ts +1 -1
- package/src/ir-to-client-js/utils.ts +11 -0
- package/src/jsx-to-ir.ts +1346 -1022
- package/src/nullable-signal-attr.ts +85 -4
- package/src/props-binding.ts +100 -5
- package/src/relocate.ts +40 -8
- package/src/ssr-seed-plan.ts +18 -6
- package/src/types.ts +50 -2
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fold render-time reassignments of a component-scope `let` into its
|
|
3
|
+
* initializer (#3501).
|
|
4
|
+
*
|
|
5
|
+
* A template evaluates a binding's value, never the statements that follow
|
|
6
|
+
* its declaration, so a `let` reassigned before the component returns had
|
|
7
|
+
* no value a template could read:
|
|
8
|
+
*
|
|
9
|
+
* let label = props.label ?? 'Default'
|
|
10
|
+
* if (!label) label = 'Fallback'
|
|
11
|
+
*
|
|
12
|
+
* This pre-pass rewrites that to the single value the binding holds at
|
|
13
|
+
* render time, binding each intermediate value once, and removes the
|
|
14
|
+
* reassigning statements:
|
|
15
|
+
*
|
|
16
|
+
* const label_1 = props.label ?? 'Default'; let label = ((!(label_1)) ? ('Fallback') : (label_1))
|
|
17
|
+
*
|
|
18
|
+
* Every later stage (the analyzer, the CSR template, the template adapters,
|
|
19
|
+
* the client init body) then sees ordinary bindings with one value each, so
|
|
20
|
+
* they agree by construction.
|
|
21
|
+
*
|
|
22
|
+
* The rewrite only applies where it cannot change behaviour:
|
|
23
|
+
* - the reassigning statements directly follow the declaration, and each
|
|
24
|
+
* is an assignment to the binding (`x = e`, `x += e`, `x ??= e`, …), an
|
|
25
|
+
* `if`/`else` whose branches are such assignments, or a block of them;
|
|
26
|
+
* - the initializer, every assigned value and every condition is free of
|
|
27
|
+
* side effects (no calls, no assignments, no functions);
|
|
28
|
+
* - every expression is evaluated exactly as often, and in the same order,
|
|
29
|
+
* as the original statements evaluate it, since a member read may run a
|
|
30
|
+
* getter. Each statement's incoming value is a binding (a read of it is
|
|
31
|
+
* unobservable) or a literal, a conditional evaluates only the branch it
|
|
32
|
+
* takes, and inside a statement a computed value may only flow into a
|
|
33
|
+
* compound assignment (see `foldStatement`).
|
|
34
|
+
* Anything else leaves the binding as it is (and `markMutatedConstants`
|
|
35
|
+
* flags it as before).
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
import ts from 'typescript'
|
|
39
|
+
|
|
40
|
+
/** Folded values longer than this are left unfolded. */
|
|
41
|
+
const MAX_FOLDED_LENGTH = 4000
|
|
42
|
+
|
|
43
|
+
interface Edit {
|
|
44
|
+
start: number
|
|
45
|
+
end: number
|
|
46
|
+
text: string
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The source with each foldable `let` folded, or `null` when nothing was
|
|
51
|
+
* folded. Line count is preserved where possible (removed statements are
|
|
52
|
+
* blanked rather than deleted), so later source locations stay valid.
|
|
53
|
+
*/
|
|
54
|
+
export function foldRenderReassignmentsInSource(source: string, filePath: string): string | null {
|
|
55
|
+
if (!source.includes('let ')) return null
|
|
56
|
+
const sf = ts.createSourceFile(filePath, source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX)
|
|
57
|
+
const edits: Edit[] = []
|
|
58
|
+
const names = new FreshNames(sf)
|
|
59
|
+
for (const body of topLevelFunctionBodies(sf)) foldBlock(body.statements, sf, names, edits)
|
|
60
|
+
if (edits.length === 0) return null
|
|
61
|
+
edits.sort((a, b) => b.start - a.start)
|
|
62
|
+
let out = source
|
|
63
|
+
for (const e of edits) out = out.slice(0, e.start) + e.text + out.slice(e.end)
|
|
64
|
+
return out
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function topLevelFunctionBodies(sf: ts.SourceFile): ts.Block[] {
|
|
68
|
+
const bodies: ts.Block[] = []
|
|
69
|
+
const add = (fn: ts.Node | undefined) => {
|
|
70
|
+
if (fn && (ts.isArrowFunction(fn) || ts.isFunctionExpression(fn)) && fn.body && ts.isBlock(fn.body)) bodies.push(fn.body)
|
|
71
|
+
}
|
|
72
|
+
for (const stmt of sf.statements) {
|
|
73
|
+
if (ts.isFunctionDeclaration(stmt) && stmt.body) bodies.push(stmt.body)
|
|
74
|
+
else if (ts.isVariableStatement(stmt)) for (const d of stmt.declarationList.declarations) add(d.initializer)
|
|
75
|
+
else if (ts.isExportAssignment(stmt)) add(stmt.expression)
|
|
76
|
+
}
|
|
77
|
+
return bodies
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Identifiers not yet used anywhere in the file, for the intermediate bindings. */
|
|
81
|
+
class FreshNames {
|
|
82
|
+
private readonly used = new Set<string>()
|
|
83
|
+
constructor(sf: ts.SourceFile) {
|
|
84
|
+
const visit = (node: ts.Node): void => {
|
|
85
|
+
if (ts.isIdentifier(node)) this.used.add(node.text)
|
|
86
|
+
ts.forEachChild(node, visit)
|
|
87
|
+
}
|
|
88
|
+
visit(sf)
|
|
89
|
+
}
|
|
90
|
+
next(base: string): string {
|
|
91
|
+
let k = 1
|
|
92
|
+
while (this.used.has(`${base}_${k}`)) k++
|
|
93
|
+
const name = `${base}_${k}`
|
|
94
|
+
this.used.add(name)
|
|
95
|
+
return name
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** A value: its source text, and whether it is a literal or a plain identifier, which may be repeated freely. */
|
|
100
|
+
interface Value {
|
|
101
|
+
text: string
|
|
102
|
+
trivial: boolean
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function foldBlock(statements: ts.NodeArray<ts.Statement>, sf: ts.SourceFile, names: FreshNames, edits: Edit[]): void {
|
|
106
|
+
for (let i = 0; i < statements.length; i++) {
|
|
107
|
+
const decl = foldableLet(statements[i]!)
|
|
108
|
+
if (!decl) continue
|
|
109
|
+
const name = decl.name.text
|
|
110
|
+
// `value` is the binding's value so far. Each statement reads it through
|
|
111
|
+
// a binding of its own (or directly, when it is a literal or identifier),
|
|
112
|
+
// so its expression is evaluated once, where the original evaluated it.
|
|
113
|
+
let value: Value = { text: decl.initializer.getText(sf), trivial: isTrivial(decl.initializer) }
|
|
114
|
+
const bindings: string[] = []
|
|
115
|
+
let last = i
|
|
116
|
+
for (let j = i + 1; j < statements.length; j++) {
|
|
117
|
+
const ref = value.trivial ? value.text : names.next(name)
|
|
118
|
+
const next = foldStatement(statements[j]!, name, { text: ref, trivial: true }, sf)
|
|
119
|
+
if (next === null || next.text.length > MAX_FOLDED_LENGTH) break
|
|
120
|
+
if (!value.trivial) bindings.push(`const ${ref} = ${value.text}; `)
|
|
121
|
+
value = next
|
|
122
|
+
last = j
|
|
123
|
+
}
|
|
124
|
+
if (last === i) continue
|
|
125
|
+
const stmt = statements[i]!
|
|
126
|
+
const initText = decl.initializer.getText(sf)
|
|
127
|
+
const prefix = bindings.join('')
|
|
128
|
+
edits.push({ start: stmt.getStart(sf), end: stmt.getStart(sf), text: prefix })
|
|
129
|
+
edits.push({ start: decl.initializer.getStart(sf), end: decl.initializer.getEnd(), text: value.text })
|
|
130
|
+
const surplus = countNewlines(prefix) + countNewlines(value.text) - countNewlines(initText)
|
|
131
|
+
edits.push(...blankStatements(statements.slice(i + 1, last + 1), sf, surplus))
|
|
132
|
+
i = last
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function isTrivial(expr: ts.Expression): boolean {
|
|
137
|
+
return PURE_LEAF_KINDS.has(skipParens(expr).kind)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** `let x = <pure initializer>` with a single identifier binding. */
|
|
141
|
+
function foldableLet(stmt: ts.Statement): (ts.VariableDeclaration & { name: ts.Identifier; initializer: ts.Expression }) | null {
|
|
142
|
+
if (!ts.isVariableStatement(stmt)) return null
|
|
143
|
+
const list = stmt.declarationList
|
|
144
|
+
if (!(list.flags & ts.NodeFlags.Let) || list.declarations.length !== 1) return null
|
|
145
|
+
const d = list.declarations[0]!
|
|
146
|
+
if (!ts.isIdentifier(d.name) || !d.initializer || !isPure(d.initializer)) return null
|
|
147
|
+
return d as ts.VariableDeclaration & { name: ts.Identifier; initializer: ts.Expression }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const COMPOUND_OPERATORS = new Map<ts.SyntaxKind, string>([
|
|
151
|
+
[ts.SyntaxKind.PlusEqualsToken, '+'],
|
|
152
|
+
[ts.SyntaxKind.MinusEqualsToken, '-'],
|
|
153
|
+
[ts.SyntaxKind.AsteriskEqualsToken, '*'],
|
|
154
|
+
[ts.SyntaxKind.SlashEqualsToken, '/'],
|
|
155
|
+
[ts.SyntaxKind.PercentEqualsToken, '%'],
|
|
156
|
+
[ts.SyntaxKind.QuestionQuestionEqualsToken, '??'],
|
|
157
|
+
[ts.SyntaxKind.BarBarEqualsToken, '||'],
|
|
158
|
+
[ts.SyntaxKind.AmpersandAmpersandEqualsToken, '&&'],
|
|
159
|
+
])
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* The binding's value after `stmt`, given its value `current` before; `null`
|
|
163
|
+
* when `stmt` is not foldable.
|
|
164
|
+
*
|
|
165
|
+
* A trivial `current` (a binding, a literal) may be read any number of times,
|
|
166
|
+
* or not at all. A computed one (left by an earlier assignment inside the
|
|
167
|
+
* same statement) must be evaluated exactly once and before anything else, as
|
|
168
|
+
* the original evaluated it, so only a compound assignment (`x += e`, whose
|
|
169
|
+
* `e` does not read `x`) or an empty statement may follow it: an overwrite
|
|
170
|
+
* would drop its evaluation, and any other read could repeat or reorder it.
|
|
171
|
+
*/
|
|
172
|
+
function foldStatement(stmt: ts.Statement, name: string, current: Value, sf: ts.SourceFile): Value | null {
|
|
173
|
+
if (ts.isEmptyStatement(stmt)) return current
|
|
174
|
+
if (ts.isBlock(stmt)) return foldSequence(stmt.statements, name, current, sf)
|
|
175
|
+
if (ts.isIfStatement(stmt)) return current.trivial ? foldIf(stmt, name, current, sf) : null
|
|
176
|
+
const assignment = reassignment(stmt, name)
|
|
177
|
+
if (!assignment) return null
|
|
178
|
+
const value = substitute(assignment.right, name, current.text, sf)
|
|
179
|
+
if (assignment.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
|
|
180
|
+
return current.trivial ? { text: value.text, trivial: value.reads === 0 && isTrivial(assignment.right) } : null
|
|
181
|
+
}
|
|
182
|
+
const op = COMPOUND_OPERATORS.get(assignment.operatorToken.kind)
|
|
183
|
+
if (op === undefined || (!current.trivial && value.reads > 0)) return null
|
|
184
|
+
return { text: `((${current.text}) ${op} (${value.text}))`, trivial: false }
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** `name = e` / `name op= e` with a side-effect-free `e`, as an expression statement. */
|
|
188
|
+
function reassignment(stmt: ts.Statement, name: string): ts.BinaryExpression | null {
|
|
189
|
+
if (!ts.isExpressionStatement(stmt)) return null
|
|
190
|
+
const expr = skipParens(stmt.expression)
|
|
191
|
+
if (!ts.isBinaryExpression(expr)) return null
|
|
192
|
+
const target = skipParens(expr.left)
|
|
193
|
+
return ts.isIdentifier(target) && target.text === name && isPure(expr.right) ? expr : null
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Statements in order: each reads the value the previous one left. */
|
|
197
|
+
function foldSequence(stmts: readonly ts.Statement[], name: string, current: Value, sf: ts.SourceFile): Value | null {
|
|
198
|
+
let value = current
|
|
199
|
+
for (const s of stmts) {
|
|
200
|
+
const next = foldStatement(s, name, value, sf)
|
|
201
|
+
if (next === null) return null
|
|
202
|
+
value = next
|
|
203
|
+
}
|
|
204
|
+
return value
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** A conditional evaluates its test, then only the branch it takes. `current` is trivial here. */
|
|
208
|
+
function foldIf(stmt: ts.IfStatement, name: string, current: Value, sf: ts.SourceFile): Value | null {
|
|
209
|
+
if (!isPure(stmt.expression)) return null
|
|
210
|
+
const whenTrue = foldStatement(stmt.thenStatement, name, current, sf)
|
|
211
|
+
if (whenTrue === null) return null
|
|
212
|
+
const whenFalse = stmt.elseStatement ? foldStatement(stmt.elseStatement, name, current, sf) : current
|
|
213
|
+
if (whenFalse === null) return null
|
|
214
|
+
const test = substitute(stmt.expression, name, current.text, sf)
|
|
215
|
+
return { text: `((${test.text}) ? (${whenTrue.text}) : (${whenFalse.text}))`, trivial: false }
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** `expr`'s source text with every read of `name` replaced by `(current)`, and how many reads there were. */
|
|
219
|
+
function substitute(expr: ts.Expression, name: string, current: string, sf: ts.SourceFile): { text: string; reads: number } {
|
|
220
|
+
const start = expr.getStart(sf)
|
|
221
|
+
const reads: ts.Identifier[] = []
|
|
222
|
+
const visit = (node: ts.Node): void => {
|
|
223
|
+
if (ts.isTypeNode(node)) return
|
|
224
|
+
if (ts.isIdentifier(node) && node.text === name && !isPropertyName(node)) reads.push(node)
|
|
225
|
+
ts.forEachChild(node, visit)
|
|
226
|
+
}
|
|
227
|
+
visit(expr)
|
|
228
|
+
let out = expr.getText(sf)
|
|
229
|
+
for (const id of [...reads].reverse()) {
|
|
230
|
+
const at = id.getStart(sf) - start
|
|
231
|
+
out = `${out.slice(0, at)}(${current})${out.slice(at + id.text.length)}`
|
|
232
|
+
}
|
|
233
|
+
return { text: out, reads: reads.length }
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function isPropertyName(id: ts.Identifier): boolean {
|
|
237
|
+
const p = id.parent
|
|
238
|
+
return ts.isPropertyAccessExpression(p) && p.name === id
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const PURE_LEAF_KINDS: ReadonlySet<ts.SyntaxKind> = new Set([
|
|
242
|
+
ts.SyntaxKind.Identifier,
|
|
243
|
+
ts.SyntaxKind.StringLiteral,
|
|
244
|
+
ts.SyntaxKind.NumericLiteral,
|
|
245
|
+
ts.SyntaxKind.BigIntLiteral,
|
|
246
|
+
ts.SyntaxKind.NoSubstitutionTemplateLiteral,
|
|
247
|
+
ts.SyntaxKind.TrueKeyword,
|
|
248
|
+
ts.SyntaxKind.FalseKeyword,
|
|
249
|
+
ts.SyntaxKind.NullKeyword,
|
|
250
|
+
])
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Whether evaluating `expr` has no side effects and reads nothing a
|
|
254
|
+
* statement between the declaration and the reassignment could change:
|
|
255
|
+
* identifiers, literals, member reads and operators only.
|
|
256
|
+
*/
|
|
257
|
+
function isPure(expr: ts.Expression): boolean {
|
|
258
|
+
const e = skipParens(expr)
|
|
259
|
+
if (PURE_LEAF_KINDS.has(e.kind)) return true
|
|
260
|
+
const operands = pureOperands(e)
|
|
261
|
+
return operands !== null && operands.every(isPure)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** The sub-expressions of a side-effect-free form, or `null` for any other form. */
|
|
265
|
+
function pureOperands(e: ts.Expression): readonly ts.Expression[] | null {
|
|
266
|
+
if (ts.isPropertyAccessExpression(e) || ts.isTypeOfExpression(e)) return [e.expression]
|
|
267
|
+
if (ts.isAsExpression(e) || ts.isNonNullExpression(e) || ts.isSatisfiesExpression(e)) return [e.expression]
|
|
268
|
+
if (ts.isElementAccessExpression(e)) return [e.expression, e.argumentExpression]
|
|
269
|
+
if (ts.isConditionalExpression(e)) return [e.condition, e.whenTrue, e.whenFalse]
|
|
270
|
+
if (ts.isTemplateExpression(e)) return e.templateSpans.map(s => s.expression)
|
|
271
|
+
if (ts.isPrefixUnaryExpression(e)) {
|
|
272
|
+
const sideEffect = e.operator === ts.SyntaxKind.PlusPlusToken || e.operator === ts.SyntaxKind.MinusMinusToken
|
|
273
|
+
return sideEffect ? null : [e.operand]
|
|
274
|
+
}
|
|
275
|
+
if (ts.isBinaryExpression(e)) return isPureOperator(e.operatorToken.kind) ? [e.left, e.right] : null
|
|
276
|
+
return null
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** A binary operator that neither assigns nor sequences. */
|
|
280
|
+
function isPureOperator(op: ts.SyntaxKind): boolean {
|
|
281
|
+
const isAssignment = op >= ts.SyntaxKind.FirstAssignment && op <= ts.SyntaxKind.LastAssignment
|
|
282
|
+
return !isAssignment && op !== ts.SyntaxKind.CommaToken
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function skipParens(expr: ts.Expression): ts.Expression {
|
|
286
|
+
let e = expr
|
|
287
|
+
while (ts.isParenthesizedExpression(e)) e = e.expression
|
|
288
|
+
return e
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function countNewlines(text: string): number {
|
|
292
|
+
let n = 0
|
|
293
|
+
for (const ch of text) if (ch === '\n') n++
|
|
294
|
+
return n
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Blank each removed statement to spaces, keeping its newlines so later
|
|
299
|
+
* lines keep their numbers. `surplus` newlines the folded initializer added
|
|
300
|
+
* are taken back out of the blanked text (or, when negative, added to it).
|
|
301
|
+
*/
|
|
302
|
+
function blankStatements(stmts: readonly ts.Statement[], sf: ts.SourceFile, surplus: number): Edit[] {
|
|
303
|
+
let pending = surplus
|
|
304
|
+
return stmts.map((s, k) => {
|
|
305
|
+
let text = ''
|
|
306
|
+
for (const ch of s.getText(sf)) {
|
|
307
|
+
if (ch !== '\n') text += ' '
|
|
308
|
+
else if (pending > 0) pending--
|
|
309
|
+
else text += '\n'
|
|
310
|
+
}
|
|
311
|
+
if (k === stmts.length - 1 && pending < 0) text += '\n'.repeat(-pending)
|
|
312
|
+
return { start: s.getStart(sf), end: s.getEnd(), text }
|
|
313
|
+
})
|
|
314
|
+
}
|
package/src/free-refs.ts
CHANGED
|
@@ -425,3 +425,52 @@ export function resolveFreeRefs(
|
|
|
425
425
|
): FreeReference[] {
|
|
426
426
|
return resolveFreeRefsInternal(node, env, new Set<string>())
|
|
427
427
|
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Replace the free references to a helper's parameters, inside the IR built
|
|
431
|
+
* from its inlined body, with the references of the call's arguments
|
|
432
|
+
* (#3509). `argRefs` maps each parameter name to its argument's resolved
|
|
433
|
+
* references. Rewrites `origin.freeRefs` (expressions, conditions, and any
|
|
434
|
+
* other node carrying an origin) and attribute `freeIdentifiers` in place,
|
|
435
|
+
* anywhere in the subtree.
|
|
436
|
+
*/
|
|
437
|
+
export function rebindHelperParamRefs(root: unknown, argRefs: ReadonlyMap<string, readonly FreeReference[]>): void {
|
|
438
|
+
if (argRefs.size === 0) return
|
|
439
|
+
const seen = new WeakSet<object>()
|
|
440
|
+
const visit = (value: unknown): void => {
|
|
441
|
+
if (typeof value !== 'object' || value === null || seen.has(value)) return
|
|
442
|
+
seen.add(value)
|
|
443
|
+
if (Array.isArray(value)) {
|
|
444
|
+
for (const item of value) visit(item)
|
|
445
|
+
return
|
|
446
|
+
}
|
|
447
|
+
if (value instanceof Set || value instanceof Map) return
|
|
448
|
+
const node = value as { origin?: { freeRefs?: FreeReference[] }; freeIdentifiers?: ReadonlySet<string> }
|
|
449
|
+
if (node.origin?.freeRefs) node.origin.freeRefs = rebindRefs(node.origin.freeRefs, argRefs)
|
|
450
|
+
if (node.freeIdentifiers instanceof Set) node.freeIdentifiers = rebindIds(node.freeIdentifiers, argRefs)
|
|
451
|
+
for (const child of Object.values(value)) visit(child)
|
|
452
|
+
}
|
|
453
|
+
visit(root)
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function rebindRefs(refs: FreeReference[], argRefs: ReadonlyMap<string, readonly FreeReference[]>): FreeReference[] {
|
|
457
|
+
if (!refs.some(r => argRefs.has(r.name))) return refs
|
|
458
|
+
const out: FreeReference[] = []
|
|
459
|
+
for (const ref of refs) {
|
|
460
|
+
const replacement = argRefs.get(ref.name)
|
|
461
|
+
if (!replacement) out.push(ref)
|
|
462
|
+
else for (const r of replacement) if (!out.some(o => o.name === r.name && o.kind === r.kind)) out.push(r)
|
|
463
|
+
}
|
|
464
|
+
return out
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
function rebindIds(ids: ReadonlySet<string>, argRefs: ReadonlyMap<string, readonly FreeReference[]>): ReadonlySet<string> {
|
|
468
|
+
if (![...ids].some(id => argRefs.has(id))) return ids
|
|
469
|
+
const out = new Set<string>()
|
|
470
|
+
for (const id of ids) {
|
|
471
|
+
const replacement = argRefs.get(id)
|
|
472
|
+
if (!replacement) out.add(id)
|
|
473
|
+
else for (const r of replacement) if (r.kind !== 'reactive-brand') out.add(r.name)
|
|
474
|
+
}
|
|
475
|
+
return out
|
|
476
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -122,12 +122,66 @@ export type { ParsedExprEmitter, LoweringEmitter, HigherOrderMethod, ArrayMethod
|
|
|
122
122
|
export { collectLoopBoundNames } from './adapters/loop-bound-names.ts'
|
|
123
123
|
export { filterCaptureCollisions } from './adapters/filter-capture-collisions.ts'
|
|
124
124
|
export { templateNameCollisionErrors } from './adapters/template-name-collisions.ts'
|
|
125
|
-
export {
|
|
125
|
+
export {
|
|
126
|
+
collectBooleanValueNames,
|
|
127
|
+
isAriaBooleanAttr,
|
|
128
|
+
isBooleanResultExpr,
|
|
129
|
+
isBooleanResultParsed,
|
|
130
|
+
isExplicitStringCall,
|
|
131
|
+
stringifyBooleanTernaryBranches,
|
|
132
|
+
} from './adapters/boolean-result.ts'
|
|
133
|
+
export { isBareStringLiteral, isLiteralIndex, isStringTypeInfo } from './adapters/literal-value.ts'
|
|
134
|
+
export { collectBooleanTypedProps, collectNullableOptionalProps, isBooleanTypedPropRef } from './adapters/prop-classes.ts'
|
|
135
|
+
export type { BooleanTypedPropContext } from './adapters/prop-classes.ts'
|
|
136
|
+
export { collectImportedLoopChildComponentErrors, hasClientInteractivity } from './adapters/component-tree.ts'
|
|
137
|
+
export {
|
|
138
|
+
ARRAY_METHOD_CATALOGUE,
|
|
139
|
+
emitArrayMethod,
|
|
140
|
+
lowerArrayMethod,
|
|
141
|
+
prefixedArrayMethodSpelling,
|
|
142
|
+
} from './adapters/array-method.ts'
|
|
143
|
+
export {
|
|
144
|
+
emitCallbackEval,
|
|
145
|
+
isPredicateMethod,
|
|
146
|
+
lowerCallbackMethod,
|
|
147
|
+
nestedCallbackRefusal,
|
|
148
|
+
isUnloweredCall,
|
|
149
|
+
unloweredCallRefusal,
|
|
150
|
+
sortEvalPlan,
|
|
151
|
+
sortRefusal,
|
|
152
|
+
} from './adapters/callback-method.ts'
|
|
153
|
+
export type {
|
|
154
|
+
CallbackEvalHelper,
|
|
155
|
+
CallbackEvalOperand,
|
|
156
|
+
CallbackEvalPlan,
|
|
157
|
+
CallbackEvalSpelling,
|
|
158
|
+
CallbackLoweringOptions,
|
|
159
|
+
CallbackPlan,
|
|
160
|
+
PredicateMethod,
|
|
161
|
+
} from './adapters/callback-method.ts'
|
|
162
|
+
export type {
|
|
163
|
+
ArrayMethodArg,
|
|
164
|
+
ArrayMethodDefault,
|
|
165
|
+
ArrayMethodEntry,
|
|
166
|
+
ArrayMethodHelper,
|
|
167
|
+
ArrayMethodParam,
|
|
168
|
+
ArrayMethodPlan,
|
|
169
|
+
ArrayMethodSpelling,
|
|
170
|
+
} from './adapters/array-method.ts'
|
|
126
171
|
export { derivesScopeFromSlot } from './adapters/child-scope.ts'
|
|
172
|
+
export { childTemplateName, componentPropKey, isEmptyComponentProps, planComponentProps, spellComponentProps } from './adapters/component-props.ts'
|
|
173
|
+
export type { ComponentPropEntry, ComponentPropSegment, ComponentPropSpelling, ComponentPropsOptions, SpelledComponentPropSegment } from './adapters/component-props.ts'
|
|
174
|
+
export { parseUndefinedAlternateTernary, planAttrExpression, planSpreadAttr, unsupportedAttrExpression, unsupportedAttrExpressionError } from './adapters/attr-plan.ts'
|
|
175
|
+
export type { AttrExpressionPlan, AttrPlanContext, SpreadAttrPlan, SpreadPlanContext, UndefinedAlternateTernary } from './adapters/attr-plan.ts'
|
|
176
|
+
export { planLoop } from './adapters/loop-plan.ts'
|
|
177
|
+
export { computedConstContext, inlineComputedConstAttr, inlineComputedConsts } from './adapters/computed-const.ts'
|
|
178
|
+
export type { ComputedConstContext } from './adapters/computed-const.ts'
|
|
179
|
+
export type { LoopPlan, LoopPlanContext } from './adapters/loop-plan.ts'
|
|
127
180
|
export { evaluateStaticLiteral, isFullyStaticLiteral, resolveStaticLoopSource } from './static-literal.ts'
|
|
128
181
|
export {
|
|
129
182
|
arithmeticOperandIsNull,
|
|
130
183
|
arithmeticOperandIsUndefined,
|
|
184
|
+
arithmeticOperandCoercion,
|
|
131
185
|
bareGetterCallName,
|
|
132
186
|
attrValueMayBeNullish,
|
|
133
187
|
collectNullableSignalGetters,
|
|
@@ -137,6 +191,7 @@ export {
|
|
|
137
191
|
NULL_COERCING_ARITHMETIC_OPS,
|
|
138
192
|
nullableSignalAttrGetter,
|
|
139
193
|
type NullishAttrContext,
|
|
194
|
+
type ArithmeticCoercion,
|
|
140
195
|
} from './nullable-signal-attr.ts'
|
|
141
196
|
export { importsSearchParams, searchParamsLocalNames, envSignalLocalNames, envSignalReaderFor, ENV_SIGNAL_READERS, queryHrefLocalNames, matchSearchParamsMethodCall } from './adapters/env-signal.ts'
|
|
142
197
|
export type { EnvSignalReader } from './adapters/env-signal.ts'
|
|
@@ -174,7 +229,10 @@ export {
|
|
|
174
229
|
resolveDangerousInnerHtml,
|
|
175
230
|
dangerousInnerHtmlMetacharViolation,
|
|
176
231
|
dangerousInnerHtmlDiagnostic,
|
|
232
|
+
renderDangerousInnerHtml,
|
|
177
233
|
} from './adapters/dangerous-inner-html.ts'
|
|
234
|
+
export { renderElementAttributes, renderElementShell, renderFragmentScope } from './adapters/element-shell.ts'
|
|
235
|
+
export type { ElementShellSpelling } from './adapters/element-shell.ts'
|
|
178
236
|
export type { DangerousInnerHtmlResolution } from './adapters/dangerous-inner-html.ts'
|
|
179
237
|
|
|
180
238
|
// Client JS Generator
|
|
@@ -253,7 +311,7 @@ export { ErrorCodes, createError, formatError, generateCodeFrame } from './error
|
|
|
253
311
|
export { isValueReferenceIdentifier, collectValueReferencedNames } from './value-references.ts'
|
|
254
312
|
|
|
255
313
|
// Expression Parser
|
|
256
|
-
export { parseExpression, tsNodeToParsedExpr, asCallbackMethodCall, CALLBACK_METHODS, sortComparatorFromArrow, serializeParsedExpr, freeVarsInBody, freeIdentifiers, renameFreeIdentifiers, materializeGetterCalls, isSupported, isSupportedValue, exprToString, stringifyParsedExpr, identifierPath, parseBlockBody, parseBlockBodyTolerant, foldBlockToExpr, predicateTernaryToLogical, containsHigherOrder, extractArrowBodyExpression, parseStyleObjectEntries, hasUnsafeStyleValue, parseProviderObjectLiteral, isFunctionShapedExpression, type ProviderObjectMember, type FoldBlockOptions } from './expression-parser.ts'
|
|
314
|
+
export { parseExpression, tsNodeToParsedExpr, asCallbackMethodCall, hasOpaqueCall, unboundCallbackParamRead, unboundCallbackParamReason, CALLBACK_METHODS, sortComparatorFromArrow, serializeParsedExpr, freeVarsInBody, freeIdentifiers, renameFreeIdentifiers, materializeGetterCalls, isSupported, isSupportedValue, exprToString, stringifyParsedExpr, identifierPath, parseBlockBody, parseBlockBodyTolerant, foldBlockToExpr, predicateTernaryToLogical, containsHigherOrder, extractArrowBodyExpression, parseStyleObjectEntries, hasUnsafeStyleValue, parseProviderObjectLiteral, isFunctionShapedExpression, type ProviderObjectMember, type FoldBlockOptions } from './expression-parser.ts'
|
|
257
315
|
export type { StyleObjectEntry } from './expression-parser.ts'
|
|
258
316
|
export { literalMemberIndex } from './literal-member-index.ts'
|
|
259
317
|
export { PARSED_EXPR_KINDS, ARRAY_METHOD_NAMES, SORT_KEY_TYPES, SORT_KEY_TARGETS, SORT_KEY_DIRECTIONS } from './expression-parser.ts'
|
|
@@ -380,7 +438,7 @@ export { BOOLEAN_ATTRS, isBooleanAttr } from './html-constants.ts'
|
|
|
380
438
|
export { USE_CLIENT_DIRECTIVE, CLIENT_EXPRESSION_DIRECTIVE } from './directives.ts'
|
|
381
439
|
|
|
382
440
|
// Shared props-object-pattern helpers for the Go / Mojo template adapters
|
|
383
|
-
export { augmentInheritedPropAccesses, parseRecordIndexAccess, evalStringArrayJoin, collectModuleStringConsts, lookupStaticRecordLiteral, lookupLiteralConst, collectContextConsumers } from './augment-inherited-props.ts'
|
|
441
|
+
export { augmentInheritedPropAccesses, parseRecordIndexAccess, evalStringArrayJoin, collectModuleStringConsts, lookupStaticRecordLiteral, lookupLiteralConst, collectContextConsumers, contextConsumerSeed } from './augment-inherited-props.ts'
|
|
384
442
|
export type { RecordIndexAccess, RecordIndexEntry, ContextConsumer } from './augment-inherited-props.ts'
|
|
385
443
|
|
|
386
444
|
// HTML element attribute types
|
|
@@ -296,19 +296,27 @@ export function computeInlinability(
|
|
|
296
296
|
// here rather than calling `computeDeclarationScopes` a second time.
|
|
297
297
|
|
|
298
298
|
const decisionsByName = new Map<string, RelocateDecision[]>()
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
299
|
+
// Constants are classified in declaration order, so a later const can see
|
|
300
|
+
// which earlier ones inline (#3513) and accept a method call on them.
|
|
301
|
+
const inlinableLocals = new Set<string>()
|
|
302
|
+
env.inlinableLocals = inlinableLocals
|
|
303
|
+
const classifyAll = (refused: ReadonlySet<string>) => {
|
|
304
|
+
inlinableLocals.clear()
|
|
305
|
+
for (const c of ctx.localConstants) {
|
|
306
|
+
const { status, decisions } = classifyConstantInitial(
|
|
307
|
+
c,
|
|
308
|
+
graph.declaredNames,
|
|
309
|
+
signalGetters,
|
|
310
|
+
signalSetters,
|
|
311
|
+
memoNames,
|
|
312
|
+
env,
|
|
313
|
+
getterAliases,
|
|
314
|
+
constantScope,
|
|
315
|
+
)
|
|
316
|
+
constants.set(c.name, status)
|
|
317
|
+
decisionsByName.set(c.name, decisions)
|
|
318
|
+
if (status.kind === 'inlinable' && !refused.has(c.name)) inlinableLocals.add(c.name)
|
|
319
|
+
}
|
|
312
320
|
}
|
|
313
321
|
|
|
314
322
|
const templateRiskyNames = collectTemplateRiskyNames(irRoot)
|
|
@@ -320,7 +328,21 @@ export function computeInlinability(
|
|
|
320
328
|
// transformation. The map lives on `ClientJsContext` — not on the
|
|
321
329
|
// cross-adapter `ConstantInfo` IR — so SSR adapters don't see CSR
|
|
322
330
|
// substitution semantics they have no use for.
|
|
323
|
-
|
|
331
|
+
//
|
|
332
|
+
// A const classified inlinable can still end with no CSR form here
|
|
333
|
+
// (`(choices).includes(name)` fails the post-substitution check). A
|
|
334
|
+
// method call on it was accepted on the provisional verdict, so it would
|
|
335
|
+
// read as `undefined.m()`: withdraw the receiver and classify again
|
|
336
|
+
// until no accepted receiver is refused. Each round removes at least one
|
|
337
|
+
// name, so this stops within `localConstants.length` rounds.
|
|
338
|
+
const refused = new Set<string>()
|
|
339
|
+
for (;;) {
|
|
340
|
+
classifyAll(refused)
|
|
341
|
+
populateCsrInlinable(ctx, env, getterAliases, constantScope)
|
|
342
|
+
const withdrawn = [...inlinableLocals].filter(name => ctx.csrInlinable.get(name) === null)
|
|
343
|
+
if (withdrawn.length === 0) break
|
|
344
|
+
for (const name of withdrawn) refused.add(name)
|
|
345
|
+
}
|
|
324
346
|
|
|
325
347
|
return { constants, functions, decisionsByName, templateRiskyNames }
|
|
326
348
|
}
|
|
@@ -381,6 +403,7 @@ function populateCsrInlinable(
|
|
|
381
403
|
substitutions: new Map([...baseEnv.substitutions, ...constSubs]),
|
|
382
404
|
propsObjectName: baseEnv.propsObjectName,
|
|
383
405
|
})
|
|
406
|
+
const receivers = inlinedReceiverCheck(relocateEnv)
|
|
384
407
|
|
|
385
408
|
// Pre-mark consts that are structurally ineligible — they record
|
|
386
409
|
// `null` in `ctx.csrInlinable`. Mirrors `classifyConstantInitial`
|
|
@@ -473,11 +496,10 @@ function populateCsrInlinable(
|
|
|
473
496
|
// `rewritePropsObjectRef` doesn't need to redo the AST work. The
|
|
474
497
|
// free identifiers are re-extracted from the bridged form so
|
|
475
498
|
// the post-substitution unsafe-name check stays exact.
|
|
476
|
-
const
|
|
477
|
-
if (
|
|
499
|
+
const bridgedRewritten = bridgedCsrForm(rewritten, source, env, relocateEnv, receivers)
|
|
500
|
+
if (bridgedRewritten === null) {
|
|
478
501
|
ctx.csrInlinable.set(c.name, null)
|
|
479
502
|
} else {
|
|
480
|
-
const bridgedRewritten = inlineResult.rewrittenValue
|
|
481
503
|
const bridgedFreeIdentifiers = recomputeFreeIdentifiers(bridgedRewritten, freeIdentifiers)
|
|
482
504
|
ctx.csrInlinable.set(c.name, { rewrittenValue: bridgedRewritten, freeIdentifiers: bridgedFreeIdentifiers })
|
|
483
505
|
constSubs.set(c.name, {
|
|
@@ -485,6 +507,7 @@ function populateCsrInlinable(
|
|
|
485
507
|
replacement: bridgedRewritten,
|
|
486
508
|
freeIdentifiers: bridgedFreeIdentifiers,
|
|
487
509
|
})
|
|
510
|
+
receivers.add(c.name, bridgedRewritten)
|
|
488
511
|
}
|
|
489
512
|
finalised.add(c.name)
|
|
490
513
|
progressed = true
|
|
@@ -500,6 +523,59 @@ function populateCsrInlinable(
|
|
|
500
523
|
}
|
|
501
524
|
}
|
|
502
525
|
|
|
526
|
+
/**
|
|
527
|
+
* The bridged CSR form of a const whose substituted text is `rewritten`, or
|
|
528
|
+
* null when it isn't safe in template scope.
|
|
529
|
+
*/
|
|
530
|
+
function bridgedCsrForm(
|
|
531
|
+
rewritten: string,
|
|
532
|
+
source: string,
|
|
533
|
+
env: CsrEnv,
|
|
534
|
+
relocateEnv: RelocateEnv,
|
|
535
|
+
receivers: ReturnType<typeof inlinedReceiverCheck>,
|
|
536
|
+
): string | null {
|
|
537
|
+
const inlineResult = isInlinableInTemplate(rewritten, relocateEnv)
|
|
538
|
+
return inlineResult.ok ? inlineResult.rewrittenValue : receivers.check(source, env)
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
/**
|
|
542
|
+
* The post-substitution check refuses a method call on an inlinable local
|
|
543
|
+
* (`text.toUpperCase()`, #3513): once `text` is substituted, the receiver
|
|
544
|
+
* is a parenthesized expression the adapter can't vouch for. `check`
|
|
545
|
+
* re-runs it with the receivers that already have a CSR form left as
|
|
546
|
+
* identifiers, so relocate's receiver rule decides the call and inlines
|
|
547
|
+
* each one as its `inline` form (an `inline` read inside a call argument
|
|
548
|
+
* still counts as bridged, #1138). A receiver whose own CSR form was
|
|
549
|
+
* refused is never added, so a call on it stays refused.
|
|
550
|
+
*/
|
|
551
|
+
function inlinedReceiverCheck(relocateEnv: RelocateEnv): {
|
|
552
|
+
add: (name: string, bridged: string) => void
|
|
553
|
+
check: (source: string, env: CsrEnv) => string | null
|
|
554
|
+
} {
|
|
555
|
+
const inlined = new Map<string, string>()
|
|
556
|
+
const vetted = new Set<string>()
|
|
557
|
+
const receiverEnv: RelocateEnv = { ...relocateEnv, inlinable: inlined, inlinableLocals: vetted }
|
|
558
|
+
return {
|
|
559
|
+
add(name, bridged) {
|
|
560
|
+
// Only an init-local reaches relocate's receiver rule; a module
|
|
561
|
+
// const keeps the post-substitution verdict (#2106).
|
|
562
|
+
const kind = relocateEnv.bindings.get(name)
|
|
563
|
+
if (kind !== 'init-local' && kind !== 'sub-init-local') return
|
|
564
|
+
if (relocateEnv.inlinableLocals?.has(name) !== true) return
|
|
565
|
+
vetted.add(name)
|
|
566
|
+
inlined.set(name, `(${bridged})`)
|
|
567
|
+
},
|
|
568
|
+
check(source, env) {
|
|
569
|
+
if (vetted.size === 0) return null
|
|
570
|
+
const substitutions = new Map(env.substitutions)
|
|
571
|
+
for (const name of vetted) substitutions.delete(name)
|
|
572
|
+
const partial = csrSubstitute(source, { substitutions, propsObjectName: env.propsObjectName }).rewritten
|
|
573
|
+
const result = isInlinableInTemplate(partial, receiverEnv)
|
|
574
|
+
return result.ok ? result.rewrittenValue : null
|
|
575
|
+
},
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
503
579
|
/**
|
|
504
580
|
* Recompute free identifiers after `relocate` has rewritten bare prop
|
|
505
581
|
* refs to `_p.X`. The pre-rewrite free-id set may still mention the
|