prunelle 0.1.1 → 0.5.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/llms.txt +23 -19
- package/package.json +8 -2
- package/src/a11y/declaration.ts +335 -0
- package/src/a11y/index.ts +446 -0
- package/src/a11y/rgaa-map.ts +184 -0
- package/src/attrs.ts +11 -3
- package/src/build/bundle.ts +80 -36
- package/src/build/client-entries.ts +3 -2
- package/src/build/client-plugin.ts +8 -4
- package/src/build/client-prune.ts +56 -6
- package/src/build/compile.ts +11 -9
- package/src/build/ecoindex.ts +229 -0
- package/src/build/index.ts +174 -108
- package/src/build/islands.ts +2 -2
- package/src/build/manifest.ts +66 -5
- package/src/build/output.ts +2 -2
- package/src/build/plugin-host.ts +91 -33
- package/src/build/precompress.ts +15 -2
- package/src/build/prerender.ts +128 -46
- package/src/build/report.ts +23 -2
- package/src/build/resolve.ts +8 -6
- package/src/build/routes.ts +175 -10
- package/src/build/static.ts +137 -39
- package/src/build/status-pages.ts +127 -0
- package/src/build/style-bundles.ts +526 -0
- package/src/cli/check-forms.ts +561 -0
- package/src/cli/check-links.ts +632 -0
- package/src/cli/check.ts +75 -20
- package/src/cli/commands.ts +85 -4
- package/src/cli/docs.ts +402 -0
- package/src/cli/inspect.ts +103 -14
- package/src/cli/mcp-tools.ts +490 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +2 -2
- package/src/cli/render.ts +421 -0
- package/src/cli/upgrade-names.ts +73 -13
- package/src/cli/upgrade.ts +137 -16
- package/src/compiler/analyze.ts +42 -9
- package/src/compiler/browser-globals.ts +140 -17
- package/src/compiler/cacheability.ts +636 -0
- package/src/compiler/compliance-rgaa.ts +508 -0
- package/src/compiler/compliance.ts +241 -289
- package/src/compiler/data-keys.ts +199 -192
- package/src/compiler/dev-source.ts +87 -0
- package/src/compiler/dom-attrs.ts +138 -65
- package/src/compiler/dom.ts +517 -442
- package/src/compiler/expr-context.ts +167 -57
- package/src/compiler/mistakes.ts +4 -1
- package/src/compiler/props.ts +87 -36
- package/src/compiler/scope.ts +400 -333
- package/src/compiler/server-only.ts +190 -151
- package/src/compiler/shared.ts +40 -12
- package/src/compiler/signal-reads.ts +6 -2
- package/src/compiler/ssr-prunelle-tags.ts +27 -14
- package/src/compiler/ssr.ts +218 -87
- package/src/dev/error-log.ts +151 -0
- package/src/dev/index.ts +84 -34
- package/src/dev/open-editor.ts +204 -0
- package/src/dev/protocol.ts +1 -0
- package/src/dev/reload.ts +5 -3
- package/src/dev/supervisor.ts +42 -25
- package/src/dev/watchers.ts +47 -11
- package/src/dom/action.ts +43 -20
- package/src/dom/attrs.ts +15 -3
- package/src/dom/extras.ts +28 -12
- package/src/dom/index.ts +26 -8
- package/src/dom/island.ts +35 -28
- package/src/dom/loop.ts +10 -9
- package/src/plugins/config.ts +8 -7
- package/src/plugins/index.ts +2 -2
- package/src/plugins/load.ts +26 -7
- package/src/plugins/order.ts +38 -1
- package/src/plugins/public.ts +5 -2
- package/src/plugins/services.ts +62 -0
- package/src/plugins/types.ts +32 -0
- package/src/reactivity/graph.ts +10 -0
- package/src/reactivity/index.ts +24 -16
- package/src/reactivity/selector.ts +31 -4
- package/src/reactivity/store.ts +45 -13
- package/src/server/action-protect.ts +198 -0
- package/src/server/action-registry.ts +1 -1
- package/src/server/action.ts +82 -27
- package/src/server/assets.ts +34 -2
- package/src/server/compress.ts +91 -9
- package/src/server/document.ts +25 -10
- package/src/server/errors.ts +303 -65
- package/src/server/form-schema.ts +343 -0
- package/src/server/health.ts +244 -0
- package/src/server/http.ts +128 -31
- package/src/server/index.ts +230 -260
- package/src/server/loader.ts +5 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +123 -88
- package/src/server/public.ts +3 -3
- package/src/server/rate-limit.ts +58 -35
- package/src/server/request-body.ts +1 -1
- package/src/server/route-cache.ts +423 -0
- package/src/server/route-table.ts +339 -8
- package/src/server/router.ts +125 -40
- package/src/server/routing.ts +196 -63
- package/src/server/security.ts +208 -32
- package/src/server/serve-handlers.ts +21 -7
- package/src/server/start.ts +60 -16
- package/src/server/static-dir.ts +61 -8
- package/src/server/stream.ts +41 -22
- package/src/ssr/context.ts +85 -3
- package/src/ssr/index.ts +130 -3
- package/src/ssr/region.ts +31 -19
- package/src/test/browser.ts +28 -13
- package/src/test/index.ts +97 -12
- package/src/ts-plugin/diagnostics.ts +505 -0
- package/src/ts-plugin/fixes.ts +364 -0
- package/src/ts-plugin/index.ts +194 -0
- package/templates/_shared/AGENTS.md +2 -2
- package/templates/_shared/mcp.json +11 -0
- package/templates/_shared/src/routes/legal.tsx +5 -0
- package/templates/_shared/tsconfig.json +5 -0
- package/templates/blog/prunelle.config.ts +1 -1
- package/templates/blog/src/routes/index.tsx +4 -0
- package/ts-plugin.cjs +4277 -0
package/src/compiler/scope.ts
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
// Lexical-scope AST walking shared by the DOM compiler: shadow-aware visitor, free-identifier rewrites and reference checks.
|
|
2
2
|
|
|
3
|
-
import type { ArrowFunctionExpression, BlockStatement, Function as OxcFunction, JSXElement, JSXFragment, Node, Program } from 'oxc-parser'
|
|
3
|
+
import type { ArrowFunctionExpression, BlockStatement, Function as OxcFunction, JSXElement, JSXFragment, Node, Program, Statement } from 'oxc-parser'
|
|
4
4
|
import { visitorKeys } from 'oxc-parser'
|
|
5
5
|
import { walk } from './ast'
|
|
6
6
|
import { SourceEditor } from './patch'
|
|
7
|
-
import { collectPatternNames, containsJsx, isJsxNode, unwrapParens, unwrapTsExpr } from './shared'
|
|
7
|
+
import { collectPatternNames, containsJsx, isComponentTag, isJsxNode, jsxTagName, unwrapParens, unwrapTsExpr } from './shared'
|
|
8
|
+
|
|
9
|
+
const TS_EXPR_WRAPPERS = new Set([
|
|
10
|
+
'TSAsExpression',
|
|
11
|
+
'TSNonNullExpression',
|
|
12
|
+
'TSSatisfiesExpression',
|
|
13
|
+
'TSTypeAssertion',
|
|
14
|
+
'TSInstantiationExpression',
|
|
15
|
+
])
|
|
8
16
|
|
|
9
17
|
// Names whose every binding in the file is a `const` holding an array literal: only those can be looped once without tracking.
|
|
10
18
|
export function literalArrayConsts(program: Program): Set<string> {
|
|
@@ -52,377 +60,436 @@ export interface ScopedWalkCallbacks {
|
|
|
52
60
|
onFunction?: ((node: ArrowFunctionExpression | OxcFunction, shadowed: ReadonlySet<string>) => boolean) | undefined
|
|
53
61
|
}
|
|
54
62
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
63
|
+
interface ScopedWalkCtx {
|
|
64
|
+
rootNode: Node
|
|
65
|
+
callbacks: ScopedWalkCallbacks
|
|
66
|
+
}
|
|
58
67
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
onWrite(name, shadowed)
|
|
67
|
-
}
|
|
68
|
+
function hasStatementBindings(stmts: readonly Statement[]): boolean {
|
|
69
|
+
for (let i = 0; i < stmts.length; i++) {
|
|
70
|
+
const stmt = stmts[i]!
|
|
71
|
+
if (stmt.type === 'VariableDeclaration')
|
|
72
|
+
return true
|
|
73
|
+
if ((stmt.type === 'FunctionDeclaration' || stmt.type === 'ClassDeclaration') && stmt.id !== null)
|
|
74
|
+
return true
|
|
68
75
|
}
|
|
76
|
+
return false
|
|
77
|
+
}
|
|
69
78
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
visitPatternDefaults(pattern.argument, shadowed)
|
|
80
|
-
return
|
|
81
|
-
case 'TSParameterProperty':
|
|
82
|
-
visitPatternDefaults(pattern.parameter, shadowed)
|
|
83
|
-
return
|
|
84
|
-
case 'ArrayPattern':
|
|
85
|
-
for (const el of pattern.elements)
|
|
86
|
-
visitPatternDefaults(el, shadowed)
|
|
87
|
-
return
|
|
88
|
-
case 'ObjectPattern':
|
|
89
|
-
for (const prop of pattern.properties) {
|
|
90
|
-
if (prop.type === 'RestElement') {
|
|
91
|
-
visitPatternDefaults(prop.argument, shadowed)
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
if (prop.computed)
|
|
95
|
-
visit(prop.key, shadowed)
|
|
96
|
-
visitPatternDefaults(prop.value, shadowed)
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
79
|
+
function reportPatternWrites(ctx: ScopedWalkCtx, target: Node, shadowed: Set<string>): void {
|
|
80
|
+
const onWrite = ctx.callbacks.onWrite
|
|
81
|
+
if (onWrite === undefined)
|
|
82
|
+
return
|
|
83
|
+
const written = new Set<string>()
|
|
84
|
+
collectPatternNames(target, written)
|
|
85
|
+
for (const name of written) {
|
|
86
|
+
if (!shadowed.has(name))
|
|
87
|
+
onWrite(name, shadowed)
|
|
100
88
|
}
|
|
89
|
+
}
|
|
101
90
|
|
|
102
|
-
|
|
103
|
-
|
|
91
|
+
function visitPatternDefaults(ctx: ScopedWalkCtx, pattern: Node | null | undefined, shadowed: Set<string>): void {
|
|
92
|
+
if (pattern === null || pattern === undefined || ctx.callbacks.shouldStop?.())
|
|
93
|
+
return
|
|
94
|
+
switch (pattern.type) {
|
|
95
|
+
case 'AssignmentPattern':
|
|
96
|
+
visitPatternDefaults(ctx, pattern.left, shadowed)
|
|
97
|
+
visitScopedNode(ctx, pattern.right, shadowed)
|
|
104
98
|
return
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
onJsx(node, shadowed)
|
|
99
|
+
case 'RestElement':
|
|
100
|
+
visitPatternDefaults(ctx, pattern.argument, shadowed)
|
|
108
101
|
return
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
visit(node.property, shadowed)
|
|
121
|
-
return
|
|
122
|
-
}
|
|
123
|
-
case 'MetaProperty':
|
|
124
|
-
case 'BreakStatement':
|
|
125
|
-
case 'ContinueStatement':
|
|
126
|
-
return
|
|
127
|
-
case 'LabeledStatement': {
|
|
128
|
-
visit(node.body, shadowed)
|
|
129
|
-
return
|
|
130
|
-
}
|
|
131
|
-
case 'Property': {
|
|
132
|
-
if (node.computed)
|
|
133
|
-
visit(node.key, shadowed)
|
|
134
|
-
if (node.shorthand && node.value.type === 'Identifier' && onShorthandProperty?.(node, shadowed) === true)
|
|
135
|
-
return
|
|
136
|
-
visit(node.value, shadowed)
|
|
137
|
-
return
|
|
138
|
-
}
|
|
139
|
-
case 'MethodDefinition':
|
|
140
|
-
case 'PropertyDefinition': {
|
|
141
|
-
if (node.computed)
|
|
142
|
-
visit(node.key, shadowed)
|
|
143
|
-
visit(node.value, shadowed)
|
|
144
|
-
return
|
|
145
|
-
}
|
|
146
|
-
case 'ClassDeclaration':
|
|
147
|
-
case 'ClassExpression': {
|
|
148
|
-
const next = new Set(shadowed)
|
|
149
|
-
if (node.id !== null)
|
|
150
|
-
next.add(node.id.name)
|
|
151
|
-
visit(node.superClass, shadowed)
|
|
152
|
-
visit(node.body, next)
|
|
153
|
-
return
|
|
154
|
-
}
|
|
155
|
-
case 'CatchClause': {
|
|
156
|
-
const next = new Set(shadowed)
|
|
157
|
-
visitPatternDefaults(node.param, next)
|
|
158
|
-
collectPatternNames(node.param, next)
|
|
159
|
-
visit(node.body, next)
|
|
160
|
-
return
|
|
161
|
-
}
|
|
162
|
-
case 'ForStatement': {
|
|
163
|
-
const next = new Set(shadowed)
|
|
164
|
-
if (node.init !== null) {
|
|
165
|
-
if (node.init.type === 'VariableDeclaration') {
|
|
166
|
-
for (const d of node.init.declarations) {
|
|
167
|
-
visit(d.init, next)
|
|
168
|
-
visitPatternDefaults(d.id, next)
|
|
169
|
-
collectPatternNames(d.id, next)
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
visit(node.init, next)
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
visit(node.test, next)
|
|
177
|
-
visit(node.update, next)
|
|
178
|
-
visit(node.body, next)
|
|
179
|
-
return
|
|
180
|
-
}
|
|
181
|
-
case 'AssignmentExpression': {
|
|
182
|
-
reportPatternWrites(node.left, shadowed)
|
|
183
|
-
visit(node.left, shadowed)
|
|
184
|
-
visit(node.right, shadowed)
|
|
185
|
-
return
|
|
186
|
-
}
|
|
187
|
-
case 'UpdateExpression': {
|
|
188
|
-
if (onWrite !== undefined && node.argument.type === 'Identifier' && !shadowed.has(node.argument.name))
|
|
189
|
-
onWrite(node.argument.name, shadowed)
|
|
190
|
-
visit(node.argument, shadowed)
|
|
191
|
-
return
|
|
192
|
-
}
|
|
193
|
-
case 'ForInStatement':
|
|
194
|
-
case 'ForOfStatement': {
|
|
195
|
-
visit(node.right, shadowed)
|
|
196
|
-
const next = new Set(shadowed)
|
|
197
|
-
if (node.left.type === 'VariableDeclaration') {
|
|
198
|
-
for (const d of node.left.declarations) {
|
|
199
|
-
visitPatternDefaults(d.id, next)
|
|
200
|
-
collectPatternNames(d.id, next)
|
|
201
|
-
}
|
|
102
|
+
case 'TSParameterProperty':
|
|
103
|
+
visitPatternDefaults(ctx, pattern.parameter, shadowed)
|
|
104
|
+
return
|
|
105
|
+
case 'ArrayPattern':
|
|
106
|
+
for (const el of pattern.elements)
|
|
107
|
+
visitPatternDefaults(ctx, el, shadowed)
|
|
108
|
+
return
|
|
109
|
+
case 'ObjectPattern':
|
|
110
|
+
for (const prop of pattern.properties) {
|
|
111
|
+
if (prop.type === 'RestElement') {
|
|
112
|
+
visitPatternDefaults(ctx, prop.argument, shadowed)
|
|
202
113
|
}
|
|
203
114
|
else {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
visit(node.body, next)
|
|
208
|
-
return
|
|
209
|
-
}
|
|
210
|
-
case 'SwitchStatement': {
|
|
211
|
-
visit(node.discriminant, shadowed)
|
|
212
|
-
const caseShadow = new Set(shadowed)
|
|
213
|
-
for (const c of node.cases) {
|
|
214
|
-
for (const stmt of c.consequent) {
|
|
215
|
-
if (stmt.type === 'FunctionDeclaration' && stmt.id !== null)
|
|
216
|
-
caseShadow.add(stmt.id.name)
|
|
217
|
-
else if (stmt.type === 'ClassDeclaration' && stmt.id !== null)
|
|
218
|
-
caseShadow.add(stmt.id.name)
|
|
219
|
-
}
|
|
115
|
+
if (prop.computed)
|
|
116
|
+
visitScopedNode(ctx, prop.key, shadowed)
|
|
117
|
+
visitPatternDefaults(ctx, prop.value, shadowed)
|
|
220
118
|
}
|
|
221
|
-
for (const c of node.cases) {
|
|
222
|
-
visit(c.test, caseShadow)
|
|
223
|
-
for (const stmt of c.consequent) {
|
|
224
|
-
if (stmt.type === 'VariableDeclaration') {
|
|
225
|
-
for (const d of stmt.declarations) {
|
|
226
|
-
visit(d.init, caseShadow)
|
|
227
|
-
visitPatternDefaults(d.id, caseShadow)
|
|
228
|
-
collectPatternNames(d.id, caseShadow)
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
else {
|
|
232
|
-
visit(stmt, caseShadow)
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
return
|
|
237
|
-
}
|
|
238
|
-
case 'ArrowFunctionExpression':
|
|
239
|
-
case 'FunctionExpression':
|
|
240
|
-
case 'FunctionDeclaration': {
|
|
241
|
-
if (node !== rootNode && onFunction?.(node, shadowed) === true)
|
|
242
|
-
return
|
|
243
|
-
const nextShadow = new Set(shadowed)
|
|
244
|
-
if (node.type !== 'ArrowFunctionExpression' && node.id !== null)
|
|
245
|
-
nextShadow.add(node.id.name)
|
|
246
|
-
for (const p of node.params) {
|
|
247
|
-
visitPatternDefaults(p, nextShadow)
|
|
248
|
-
collectPatternNames(p, nextShadow)
|
|
249
|
-
}
|
|
250
|
-
visit(node.body, nextShadow)
|
|
251
|
-
return
|
|
252
119
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function visitScopedVarDecls(ctx: ScopedWalkCtx, decl: Extract<Node, { type: 'VariableDeclaration' }>, shadowed: Set<string>): void {
|
|
124
|
+
for (const d of decl.declarations) {
|
|
125
|
+
visitScopedNode(ctx, d.init, shadowed)
|
|
126
|
+
visitPatternDefaults(ctx, d.id, shadowed)
|
|
127
|
+
collectPatternNames(d.id, shadowed)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function visitScopedStatements(ctx: ScopedWalkCtx, stmts: readonly Statement[], shadowed: Set<string>): void {
|
|
132
|
+
if (!hasStatementBindings(stmts)) {
|
|
133
|
+
for (let i = 0; i < stmts.length; i++)
|
|
134
|
+
visitScopedNode(ctx, stmts[i], shadowed)
|
|
135
|
+
return
|
|
136
|
+
}
|
|
137
|
+
const nextShadow = new Set(shadowed)
|
|
138
|
+
for (const stmt of stmts) {
|
|
139
|
+
if ((stmt.type === 'FunctionDeclaration' || stmt.type === 'ClassDeclaration') && stmt.id !== null)
|
|
140
|
+
nextShadow.add(stmt.id.name)
|
|
141
|
+
}
|
|
142
|
+
for (const stmt of stmts) {
|
|
143
|
+
if (stmt.type === 'VariableDeclaration')
|
|
144
|
+
visitScopedVarDecls(ctx, stmt, nextShadow)
|
|
145
|
+
else
|
|
146
|
+
visitScopedNode(ctx, stmt, nextShadow)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function visitScopedSwitch(ctx: ScopedWalkCtx, node: Extract<Node, { type: 'SwitchStatement' }>, shadowed: Set<string>): void {
|
|
151
|
+
visitScopedNode(ctx, node.discriminant, shadowed)
|
|
152
|
+
const hasBindings = node.cases.some(c => hasStatementBindings(c.consequent))
|
|
153
|
+
const caseShadow = hasBindings ? new Set(shadowed) : shadowed
|
|
154
|
+
if (hasBindings) {
|
|
155
|
+
for (const c of node.cases) {
|
|
156
|
+
for (const stmt of c.consequent) {
|
|
157
|
+
if ((stmt.type === 'FunctionDeclaration' || stmt.type === 'ClassDeclaration') && stmt.id !== null)
|
|
158
|
+
caseShadow.add(stmt.id.name)
|
|
283
159
|
}
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
for (const c of node.cases) {
|
|
163
|
+
visitScopedNode(ctx, c.test, caseShadow)
|
|
164
|
+
for (const stmt of c.consequent) {
|
|
165
|
+
if (stmt.type === 'VariableDeclaration')
|
|
166
|
+
visitScopedVarDecls(ctx, stmt, caseShadow)
|
|
167
|
+
else
|
|
168
|
+
visitScopedNode(ctx, stmt, caseShadow)
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function visitScopedLoopOrCatch(ctx: ScopedWalkCtx, node: Node, shadowed: Set<string>): boolean {
|
|
174
|
+
if (node.type === 'CatchClause') {
|
|
175
|
+
if (node.param === null) {
|
|
176
|
+
visitScopedNode(ctx, node.body, shadowed)
|
|
177
|
+
return true
|
|
178
|
+
}
|
|
179
|
+
const next = new Set(shadowed)
|
|
180
|
+
visitPatternDefaults(ctx, node.param, next)
|
|
181
|
+
collectPatternNames(node.param, next)
|
|
182
|
+
visitScopedNode(ctx, node.body, next)
|
|
183
|
+
return true
|
|
184
|
+
}
|
|
185
|
+
if (node.type === 'ForStatement') {
|
|
186
|
+
const hasVar = node.init?.type === 'VariableDeclaration'
|
|
187
|
+
const next = hasVar ? new Set(shadowed) : shadowed
|
|
188
|
+
if (node.init?.type === 'VariableDeclaration')
|
|
189
|
+
visitScopedVarDecls(ctx, node.init, next)
|
|
190
|
+
else
|
|
191
|
+
visitScopedNode(ctx, node.init, next)
|
|
192
|
+
visitScopedNode(ctx, node.test, next)
|
|
193
|
+
visitScopedNode(ctx, node.update, next)
|
|
194
|
+
visitScopedNode(ctx, node.body, next)
|
|
195
|
+
return true
|
|
196
|
+
}
|
|
197
|
+
if (node.type === 'ForInStatement' || node.type === 'ForOfStatement') {
|
|
198
|
+
visitScopedNode(ctx, node.right, shadowed)
|
|
199
|
+
const hasVar = node.left.type === 'VariableDeclaration'
|
|
200
|
+
const next = hasVar ? new Set(shadowed) : shadowed
|
|
201
|
+
if (node.left.type === 'VariableDeclaration') {
|
|
202
|
+
for (const d of node.left.declarations) {
|
|
203
|
+
visitPatternDefaults(ctx, d.id, next)
|
|
204
|
+
collectPatternNames(d.id, next)
|
|
288
205
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
reportPatternWrites(ctx, node.left, next)
|
|
209
|
+
visitScopedNode(ctx, node.left, next)
|
|
210
|
+
}
|
|
211
|
+
visitScopedNode(ctx, node.body, next)
|
|
212
|
+
return true
|
|
213
|
+
}
|
|
214
|
+
return false
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function visitChildValues(val: unknown, visitFn: (child: Node) => void): void {
|
|
218
|
+
if (Array.isArray(val)) {
|
|
219
|
+
for (let i = 0; i < val.length; i++) {
|
|
220
|
+
const item = val[i]
|
|
221
|
+
if (item !== null && typeof item === 'object' && 'type' in item)
|
|
222
|
+
visitFn(item as Node)
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
else if (val !== null && typeof val === 'object' && 'type' in val) {
|
|
226
|
+
visitFn(val as Node)
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function visitScopedChildren(ctx: ScopedWalkCtx, node: Node, shadowed: Set<string>): void {
|
|
231
|
+
const keys = visitorKeys[node.type]
|
|
232
|
+
const rec = node as unknown as Record<string, unknown>
|
|
233
|
+
const visitFn = (child: Node): void => visitScopedNode(ctx, child, shadowed)
|
|
234
|
+
if (keys !== undefined) {
|
|
235
|
+
for (let i = 0; i < keys.length; i++)
|
|
236
|
+
visitChildValues(rec[keys[i]!], visitFn)
|
|
237
|
+
return
|
|
238
|
+
}
|
|
239
|
+
for (const [key, val] of Object.entries(rec)) {
|
|
240
|
+
if (key !== 'start' && key !== 'end' && key !== 'type')
|
|
241
|
+
visitChildValues(val, visitFn)
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function visitScopedFunction(
|
|
246
|
+
ctx: ScopedWalkCtx,
|
|
247
|
+
node: ArrowFunctionExpression | OxcFunction,
|
|
248
|
+
shadowed: Set<string>,
|
|
249
|
+
): void {
|
|
250
|
+
if (node !== ctx.rootNode && ctx.callbacks.onFunction?.(node, shadowed) === true)
|
|
251
|
+
return
|
|
252
|
+
const hasName = node.type !== 'ArrowFunctionExpression' && node.id !== null
|
|
253
|
+
if (!hasName && node.params.length === 0) {
|
|
254
|
+
visitScopedNode(ctx, node.body, shadowed)
|
|
255
|
+
return
|
|
256
|
+
}
|
|
257
|
+
const nextShadow = new Set(shadowed)
|
|
258
|
+
if (hasName)
|
|
259
|
+
nextShadow.add(node.id!.name)
|
|
260
|
+
for (const p of node.params) {
|
|
261
|
+
visitPatternDefaults(ctx, p, nextShadow)
|
|
262
|
+
collectPatternNames(p, nextShadow)
|
|
263
|
+
}
|
|
264
|
+
visitScopedNode(ctx, node.body, nextShadow)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function visitScopedSpecialExpr(ctx: ScopedWalkCtx, node: Node, shadowed: Set<string>): boolean {
|
|
268
|
+
switch (node.type) {
|
|
269
|
+
case 'Identifier':
|
|
270
|
+
if (!shadowed.has(node.name))
|
|
271
|
+
ctx.callbacks.onIdentifier?.(node, shadowed)
|
|
272
|
+
return true
|
|
273
|
+
case 'MemberExpression':
|
|
274
|
+
visitScopedNode(ctx, node.object, shadowed)
|
|
275
|
+
if (node.computed)
|
|
276
|
+
visitScopedNode(ctx, node.property, shadowed)
|
|
277
|
+
return true
|
|
278
|
+
case 'MetaProperty':
|
|
279
|
+
case 'BreakStatement':
|
|
280
|
+
case 'ContinueStatement':
|
|
281
|
+
return true
|
|
282
|
+
case 'LabeledStatement':
|
|
283
|
+
visitScopedNode(ctx, node.body, shadowed)
|
|
284
|
+
return true
|
|
285
|
+
case 'Property':
|
|
286
|
+
if (node.computed)
|
|
287
|
+
visitScopedNode(ctx, node.key, shadowed)
|
|
288
|
+
if (node.shorthand && node.value.type === 'Identifier' && ctx.callbacks.onShorthandProperty?.(node, shadowed) === true)
|
|
289
|
+
return true
|
|
290
|
+
visitScopedNode(ctx, node.value, shadowed)
|
|
291
|
+
return true
|
|
292
|
+
case 'MethodDefinition':
|
|
293
|
+
case 'PropertyDefinition':
|
|
294
|
+
if (node.computed)
|
|
295
|
+
visitScopedNode(ctx, node.key, shadowed)
|
|
296
|
+
visitScopedNode(ctx, node.value, shadowed)
|
|
297
|
+
return true
|
|
298
|
+
case 'AssignmentExpression':
|
|
299
|
+
reportPatternWrites(ctx, node.left, shadowed)
|
|
300
|
+
visitScopedNode(ctx, node.left, shadowed)
|
|
301
|
+
visitScopedNode(ctx, node.right, shadowed)
|
|
302
|
+
return true
|
|
303
|
+
case 'UpdateExpression':
|
|
304
|
+
if (ctx.callbacks.onWrite !== undefined && node.argument.type === 'Identifier' && !shadowed.has(node.argument.name))
|
|
305
|
+
ctx.callbacks.onWrite(node.argument.name, shadowed)
|
|
306
|
+
visitScopedNode(ctx, node.argument, shadowed)
|
|
307
|
+
return true
|
|
308
|
+
default:
|
|
309
|
+
return false
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
function visitScopedNode(ctx: ScopedWalkCtx, node: Node | null | undefined, shadowed: Set<string>): void {
|
|
314
|
+
if (node === null || node === undefined || ctx.callbacks.shouldStop?.())
|
|
315
|
+
return
|
|
316
|
+
if (ctx.callbacks.onJsx !== undefined && isJsxNode(node)) {
|
|
317
|
+
ctx.callbacks.onJsx(node, shadowed)
|
|
318
|
+
return
|
|
319
|
+
}
|
|
320
|
+
if (visitScopedSpecialExpr(ctx, node, shadowed) || visitScopedLoopOrCatch(ctx, node, shadowed))
|
|
321
|
+
return
|
|
322
|
+
|
|
323
|
+
switch (node.type) {
|
|
324
|
+
case 'ClassDeclaration':
|
|
325
|
+
case 'ClassExpression': {
|
|
326
|
+
const next = node.id !== null ? new Set(shadowed).add(node.id.name) : shadowed
|
|
327
|
+
visitScopedNode(ctx, node.superClass, shadowed)
|
|
328
|
+
visitScopedNode(ctx, node.body, next)
|
|
329
|
+
return
|
|
330
|
+
}
|
|
331
|
+
case 'SwitchStatement':
|
|
332
|
+
visitScopedSwitch(ctx, node, shadowed)
|
|
333
|
+
return
|
|
334
|
+
case 'ArrowFunctionExpression':
|
|
335
|
+
case 'FunctionExpression':
|
|
336
|
+
case 'FunctionDeclaration':
|
|
337
|
+
visitScopedFunction(ctx, node, shadowed)
|
|
338
|
+
return
|
|
339
|
+
case 'BlockStatement':
|
|
340
|
+
visitScopedStatements(ctx, node.body, shadowed)
|
|
341
|
+
return
|
|
342
|
+
case 'VariableDeclaration':
|
|
343
|
+
visitScopedVarDecls(ctx, node, new Set(shadowed))
|
|
344
|
+
return
|
|
345
|
+
case 'VariableDeclarator':
|
|
346
|
+
visitScopedNode(ctx, node.init, shadowed)
|
|
347
|
+
visitPatternDefaults(ctx, node.id, shadowed)
|
|
348
|
+
return
|
|
349
|
+
default:
|
|
350
|
+
if (node.type.startsWith('TS')) {
|
|
351
|
+
if (TS_EXPR_WRAPPERS.has(node.type))
|
|
352
|
+
visitScopedNode(ctx, (node as { expression?: Node }).expression, shadowed)
|
|
295
353
|
return
|
|
296
354
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
}
|
|
332
|
-
}
|
|
355
|
+
visitScopedChildren(ctx, node, shadowed)
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** Single lexical-scope AST visitor shared by `referencesAny`, `collectMutatedNames`, and `rewriteFreeIdentifiers` (respects `for...of`, `switch`, `catch (e)`, class members, labels, and TS nodes). */
|
|
360
|
+
export function walkScoped(rootNode: Node, callbacks: ScopedWalkCallbacks): void {
|
|
361
|
+
visitScopedNode({ rootNode, callbacks }, rootNode, new Set())
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const LOOP_OR_FN_ANCESTORS = new Set([
|
|
365
|
+
'ArrowFunctionExpression',
|
|
366
|
+
'FunctionExpression',
|
|
367
|
+
'FunctionDeclaration',
|
|
368
|
+
'ForStatement',
|
|
369
|
+
'ForInStatement',
|
|
370
|
+
'ForOfStatement',
|
|
371
|
+
'WhileStatement',
|
|
372
|
+
'DoWhileStatement',
|
|
373
|
+
])
|
|
374
|
+
|
|
375
|
+
function isLazySlotContext(node: Node, ancestors: readonly Node[]): boolean {
|
|
376
|
+
let slotIdx = -1
|
|
377
|
+
let child: Node = node
|
|
378
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
379
|
+
const p = ancestors[i]!
|
|
380
|
+
if (p.type === 'ParenthesizedExpression') {
|
|
381
|
+
child = p
|
|
382
|
+
continue
|
|
383
|
+
}
|
|
384
|
+
if (p.type === 'LogicalExpression') {
|
|
385
|
+
if (p.right !== child)
|
|
386
|
+
return false
|
|
387
|
+
child = p
|
|
388
|
+
continue
|
|
333
389
|
}
|
|
390
|
+
if (p.type === 'ConditionalExpression') {
|
|
391
|
+
if (p.test === child)
|
|
392
|
+
return false
|
|
393
|
+
child = p
|
|
394
|
+
continue
|
|
395
|
+
}
|
|
396
|
+
if (p.type === 'JSXExpressionContainer') {
|
|
397
|
+
const holder = ancestors[i - 1]
|
|
398
|
+
if (holder === undefined || (holder.type !== 'JSXElement' && holder.type !== 'JSXFragment'))
|
|
399
|
+
return false
|
|
400
|
+
if (holder.type === 'JSXElement' && isComponentTag(jsxTagName(holder.openingElement.name)))
|
|
401
|
+
return false
|
|
402
|
+
slotIdx = i - 1
|
|
403
|
+
break
|
|
404
|
+
}
|
|
405
|
+
return false
|
|
334
406
|
}
|
|
407
|
+
if (slotIdx < 0)
|
|
408
|
+
return false
|
|
409
|
+
for (let i = slotIdx - 1; i >= 0; i--) {
|
|
410
|
+
if (LOOP_OR_FN_ANCESTORS.has(ancestors[i]!.type))
|
|
411
|
+
return false
|
|
412
|
+
}
|
|
413
|
+
return true
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function isNonRefIdentifier(node: Node, parent: Node | undefined): boolean {
|
|
417
|
+
if (parent === undefined)
|
|
418
|
+
return false
|
|
419
|
+
return (parent.type === 'MemberExpression' && !parent.computed && parent.property === node)
|
|
420
|
+
|| (parent.type === 'Property' && !parent.computed && parent.key === node && !parent.shorthand)
|
|
421
|
+
|| (parent.type === 'LabeledStatement' && parent.label === node)
|
|
422
|
+
}
|
|
335
423
|
|
|
336
|
-
|
|
424
|
+
function visitLazyScopeNode(
|
|
425
|
+
node: BlockStatement | ArrowFunctionExpression | OxcFunction,
|
|
426
|
+
shadowed: Set<string>,
|
|
427
|
+
ancestors: Node[],
|
|
428
|
+
visit: (n: Node | null | undefined, s: Set<string>) => void,
|
|
429
|
+
): void {
|
|
430
|
+
if (node.type !== 'BlockStatement') {
|
|
431
|
+
const fnName = node.type !== 'ArrowFunctionExpression' && node.id !== null ? node.id.name : undefined
|
|
432
|
+
const next = fnName !== undefined || node.params.length > 0 ? new Set(shadowed) : shadowed
|
|
433
|
+
if (fnName !== undefined)
|
|
434
|
+
next.add(fnName)
|
|
435
|
+
for (const p of node.params)
|
|
436
|
+
collectPatternNames(p, next)
|
|
437
|
+
ancestors.push(node)
|
|
438
|
+
visit(node.body, next)
|
|
439
|
+
ancestors.pop()
|
|
440
|
+
return
|
|
441
|
+
}
|
|
442
|
+
const hasBindings = hasStatementBindings(node.body)
|
|
443
|
+
const next = hasBindings ? new Set(shadowed) : shadowed
|
|
444
|
+
if (hasBindings) {
|
|
445
|
+
for (const s of node.body) {
|
|
446
|
+
if ((s.type === 'FunctionDeclaration' || s.type === 'ClassDeclaration') && s.id !== null)
|
|
447
|
+
next.add(s.id.name)
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
ancestors.push(node)
|
|
451
|
+
for (const s of node.body) {
|
|
452
|
+
if (s.type === 'VariableDeclaration') {
|
|
453
|
+
for (const d of s.declarations) {
|
|
454
|
+
visit(d.init, next)
|
|
455
|
+
collectPatternNames(d.id, next)
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
visit(s, next)
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
ancestors.pop()
|
|
337
463
|
}
|
|
338
464
|
|
|
339
|
-
/** Returns true if
|
|
465
|
+
/** Returns true if the sole free reference to `name` in `stmts` sits directly in an element/fragment JSX child slot (never a component prop, test position, or repeated use), so deferring `const name = <jsx>` to `() => <jsx>` is observationally equivalent and preserves hydration order. */
|
|
340
466
|
export function isSafeLazyJsxRef(name: string, stmts: readonly Node[]): boolean {
|
|
341
467
|
let safe = true
|
|
342
468
|
let uses = 0
|
|
343
469
|
const ancestors: Node[] = []
|
|
344
|
-
const isSlotContext = (): boolean => {
|
|
345
|
-
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
346
|
-
const p = ancestors[i]!
|
|
347
|
-
if (p.type === 'ParenthesizedExpression' || p.type === 'LogicalExpression' || p.type === 'ConditionalExpression')
|
|
348
|
-
continue
|
|
349
|
-
if (p.type === 'JSXExpressionContainer') {
|
|
350
|
-
const holder = ancestors[i - 1]
|
|
351
|
-
return holder !== undefined && (holder.type === 'JSXElement' || holder.type === 'JSXFragment' || holder.type === 'JSXAttribute')
|
|
352
|
-
}
|
|
353
|
-
return false
|
|
354
|
-
}
|
|
355
|
-
return false
|
|
356
|
-
}
|
|
357
470
|
const visit = (node: Node | null | undefined, shadowed: Set<string>): void => {
|
|
358
471
|
if (!safe || node === null || node === undefined)
|
|
359
472
|
return
|
|
360
473
|
if (node.type === 'Identifier') {
|
|
361
|
-
if (node.name !== name || shadowed.has(name))
|
|
362
|
-
return
|
|
363
|
-
const parent = ancestors[ancestors.length - 1]
|
|
364
|
-
if (parent !== undefined && (
|
|
365
|
-
(parent.type === 'MemberExpression' && !parent.computed && parent.property === node)
|
|
366
|
-
|| (parent.type === 'Property' && !parent.computed && parent.key === node && !parent.shorthand)
|
|
367
|
-
|| (parent.type === 'LabeledStatement' && parent.label === node)
|
|
368
|
-
)) {
|
|
474
|
+
if (node.name !== name || shadowed.has(name) || isNonRefIdentifier(node, ancestors[ancestors.length - 1]))
|
|
369
475
|
return
|
|
370
|
-
}
|
|
371
476
|
uses++
|
|
372
|
-
if (!
|
|
477
|
+
if (!isLazySlotContext(node, ancestors))
|
|
373
478
|
safe = false
|
|
374
479
|
return
|
|
375
480
|
}
|
|
376
481
|
if (node.type === 'BlockStatement' || node.type === 'ArrowFunctionExpression' || node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration') {
|
|
377
|
-
|
|
378
|
-
if (node.type !== 'BlockStatement') {
|
|
379
|
-
if (node.type !== 'ArrowFunctionExpression' && node.id !== null)
|
|
380
|
-
next.add(node.id.name)
|
|
381
|
-
for (const p of node.params)
|
|
382
|
-
collectPatternNames(p, next)
|
|
383
|
-
ancestors.push(node)
|
|
384
|
-
visit(node.body, next)
|
|
385
|
-
ancestors.pop()
|
|
386
|
-
return
|
|
387
|
-
}
|
|
388
|
-
for (const s of node.body) {
|
|
389
|
-
if ((s.type === 'FunctionDeclaration' || s.type === 'ClassDeclaration') && s.id !== null)
|
|
390
|
-
next.add(s.id.name)
|
|
391
|
-
}
|
|
392
|
-
ancestors.push(node)
|
|
393
|
-
for (const s of node.body) {
|
|
394
|
-
if (s.type === 'VariableDeclaration') {
|
|
395
|
-
for (const d of s.declarations) {
|
|
396
|
-
visit(d.init, next)
|
|
397
|
-
collectPatternNames(d.id, next)
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
else {
|
|
401
|
-
visit(s, next)
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
ancestors.pop()
|
|
482
|
+
visitLazyScopeNode(node, shadowed, ancestors, visit)
|
|
405
483
|
return
|
|
406
484
|
}
|
|
407
|
-
if (node.type.startsWith('TS') &&
|
|
485
|
+
if (node.type.startsWith('TS') && !TS_EXPR_WRAPPERS.has(node.type))
|
|
408
486
|
return
|
|
409
487
|
ancestors.push(node)
|
|
410
488
|
const keys = visitorKeys[node.type]
|
|
411
489
|
if (keys !== undefined) {
|
|
412
490
|
const rec = node as unknown as Record<string, unknown>
|
|
413
|
-
for (let i = 0; i < keys.length; i++)
|
|
414
|
-
|
|
415
|
-
if (Array.isArray(val)) {
|
|
416
|
-
for (let j = 0; j < val.length; j++) {
|
|
417
|
-
const item = val[j]
|
|
418
|
-
if (item !== null && typeof item === 'object' && 'type' in item)
|
|
419
|
-
visit(item as Node, shadowed)
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
else if (val !== null && typeof val === 'object' && 'type' in val) {
|
|
423
|
-
visit(val as Node, shadowed)
|
|
424
|
-
}
|
|
425
|
-
}
|
|
491
|
+
for (let i = 0; i < keys.length; i++)
|
|
492
|
+
visitChildValues(rec[keys[i]!], child => visit(child, shadowed))
|
|
426
493
|
}
|
|
427
494
|
ancestors.pop()
|
|
428
495
|
}
|
|
@@ -438,7 +505,7 @@ export function isSafeLazyJsxRef(name: string, stmts: readonly Node[]): boolean
|
|
|
438
505
|
visit(s, topShadow)
|
|
439
506
|
}
|
|
440
507
|
}
|
|
441
|
-
return safe && uses
|
|
508
|
+
return safe && uses === 1
|
|
442
509
|
}
|
|
443
510
|
|
|
444
511
|
/** Returns true if `rootNode` contains any free identifier reference to a name in `names`. */
|