prunelle 0.0.0-stage → 0.5.2

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.
Files changed (200) hide show
  1. package/llms.txt +152 -0
  2. package/package.json +59 -4
  3. package/src/a11y/declaration.ts +335 -0
  4. package/src/a11y/index.ts +446 -0
  5. package/src/a11y/rgaa-map.ts +184 -0
  6. package/src/ambient.d.ts +6 -0
  7. package/src/attrs.ts +43 -0
  8. package/src/build/bundle.ts +657 -0
  9. package/src/build/carry.ts +57 -0
  10. package/src/build/client-entries.ts +107 -0
  11. package/src/build/client-plugin.ts +173 -0
  12. package/src/build/client-prune.ts +168 -0
  13. package/src/build/compile.ts +129 -0
  14. package/src/build/ecoindex.ts +229 -0
  15. package/src/build/files.ts +156 -0
  16. package/src/build/first-load.ts +164 -0
  17. package/src/build/graph.ts +247 -0
  18. package/src/build/index.ts +641 -0
  19. package/src/build/is-inside.ts +12 -0
  20. package/src/build/islands.ts +641 -0
  21. package/src/build/manifest.ts +325 -0
  22. package/src/build/output.ts +84 -0
  23. package/src/build/plugin-host.ts +278 -0
  24. package/src/build/precompress.ts +139 -0
  25. package/src/build/prerender.ts +232 -0
  26. package/src/build/render-rules.ts +205 -0
  27. package/src/build/report.ts +268 -0
  28. package/src/build/resolve.ts +52 -0
  29. package/src/build/routes.ts +342 -0
  30. package/src/build/server-entry.ts +38 -0
  31. package/src/build/static.ts +207 -0
  32. package/src/build/status-pages.ts +127 -0
  33. package/src/build/style-bundles.ts +526 -0
  34. package/src/cli/check-forms.ts +654 -0
  35. package/src/cli/check-links.ts +619 -0
  36. package/src/cli/check.ts +381 -0
  37. package/src/cli/commands.ts +345 -0
  38. package/src/cli/docs.ts +402 -0
  39. package/src/cli/index.ts +70 -0
  40. package/src/cli/inspect.ts +345 -0
  41. package/src/cli/mcp-tools.ts +480 -0
  42. package/src/cli/mcp.ts +205 -0
  43. package/src/cli/new.ts +41 -0
  44. package/src/cli/render.ts +261 -0
  45. package/src/cli/upgrade-names.ts +209 -0
  46. package/src/cli/upgrade.ts +354 -0
  47. package/src/compiler/analyze.ts +632 -0
  48. package/src/compiler/ast.ts +82 -0
  49. package/src/compiler/browser-globals.ts +127 -0
  50. package/src/compiler/cacheability.ts +592 -0
  51. package/src/compiler/compliance-rgaa.ts +502 -0
  52. package/src/compiler/compliance.ts +497 -0
  53. package/src/compiler/data-keys.ts +295 -0
  54. package/src/compiler/dev-source.ts +87 -0
  55. package/src/compiler/dom-attrs.ts +452 -0
  56. package/src/compiler/dom.ts +567 -0
  57. package/src/compiler/emit.ts +131 -0
  58. package/src/compiler/expr-context.ts +518 -0
  59. package/src/compiler/index.ts +8 -0
  60. package/src/compiler/interactivity.ts +366 -0
  61. package/src/compiler/mistakes.ts +499 -0
  62. package/src/compiler/node-env.ts +50 -0
  63. package/src/compiler/patch.ts +97 -0
  64. package/src/compiler/props.ts +511 -0
  65. package/src/compiler/scope.ts +607 -0
  66. package/src/compiler/server-only.ts +326 -0
  67. package/src/compiler/shared.ts +369 -0
  68. package/src/compiler/signal-reads.ts +242 -0
  69. package/src/compiler/slots.ts +227 -0
  70. package/src/compiler/ssr-helpers.ts +555 -0
  71. package/src/compiler/ssr-island.ts +90 -0
  72. package/src/compiler/ssr-prunelle-tags.ts +69 -0
  73. package/src/compiler/ssr.ts +526 -0
  74. package/src/context.ts +165 -0
  75. package/src/dev/child.ts +30 -0
  76. package/src/dev/error-log.ts +151 -0
  77. package/src/dev/index.ts +258 -0
  78. package/src/dev/modules.ts +68 -0
  79. package/src/dev/open-editor.ts +204 -0
  80. package/src/dev/protocol.ts +22 -0
  81. package/src/dev/queue.ts +79 -0
  82. package/src/dev/reload.ts +159 -0
  83. package/src/dev/supervisor.ts +204 -0
  84. package/src/dev/watchers.ts +251 -0
  85. package/src/dom/action.ts +164 -0
  86. package/src/dom/attrs.ts +84 -0
  87. package/src/dom/claim.ts +120 -0
  88. package/src/dom/extras.ts +562 -0
  89. package/src/dom/index.ts +289 -0
  90. package/src/dom/island.ts +285 -0
  91. package/src/dom/loop.ts +450 -0
  92. package/src/dom/revive.ts +36 -0
  93. package/src/index.ts +22 -0
  94. package/src/jsx-runtime.ts +566 -0
  95. package/src/plugins/config.ts +86 -0
  96. package/src/plugins/context.ts +66 -0
  97. package/src/plugins/index.ts +14 -0
  98. package/src/plugins/load.ts +70 -0
  99. package/src/plugins/order.ts +111 -0
  100. package/src/plugins/public.ts +15 -0
  101. package/src/plugins/requires.ts +47 -0
  102. package/src/plugins/services.ts +97 -0
  103. package/src/plugins/types.ts +261 -0
  104. package/src/reactivity/graph.ts +249 -0
  105. package/src/reactivity/index.ts +533 -0
  106. package/src/reactivity/selector.ts +51 -0
  107. package/src/reactivity/store.ts +208 -0
  108. package/src/server/action-protect.ts +198 -0
  109. package/src/server/action-registry.ts +78 -0
  110. package/src/server/action.ts +530 -0
  111. package/src/server/app-entry.ts +23 -0
  112. package/src/server/assets.ts +168 -0
  113. package/src/server/compress.ts +642 -0
  114. package/src/server/consent.ts +253 -0
  115. package/src/server/cookies.ts +139 -0
  116. package/src/server/document.ts +271 -0
  117. package/src/server/env.ts +33 -0
  118. package/src/server/errors.ts +391 -0
  119. package/src/server/form-schema.ts +343 -0
  120. package/src/server/health.ts +244 -0
  121. package/src/server/http.ts +538 -0
  122. package/src/server/index.ts +586 -0
  123. package/src/server/lifecycle.ts +75 -0
  124. package/src/server/loader.ts +123 -0
  125. package/src/server/page.ts +107 -0
  126. package/src/server/param-matchers.ts +112 -0
  127. package/src/server/plugin-host.ts +265 -0
  128. package/src/server/prerendered.ts +71 -0
  129. package/src/server/public.ts +19 -0
  130. package/src/server/rate-limit.ts +156 -0
  131. package/src/server/region.ts +96 -0
  132. package/src/server/request-body.ts +27 -0
  133. package/src/server/request-path.ts +73 -0
  134. package/src/server/request-scope.ts +22 -0
  135. package/src/server/route-cache.ts +423 -0
  136. package/src/server/route-table.ts +410 -0
  137. package/src/server/router.ts +261 -0
  138. package/src/server/routing.ts +472 -0
  139. package/src/server/security.ts +499 -0
  140. package/src/server/serve-handlers.ts +83 -0
  141. package/src/server/served-file.ts +128 -0
  142. package/src/server/standard-schema.ts +87 -0
  143. package/src/server/start.ts +143 -0
  144. package/src/server/static-dir.ts +220 -0
  145. package/src/server/stream.ts +211 -0
  146. package/src/ssr/context.ts +383 -0
  147. package/src/ssr/index.ts +596 -0
  148. package/src/ssr/navigation.ts +96 -0
  149. package/src/ssr/region.ts +183 -0
  150. package/src/style.ts +33 -0
  151. package/src/test/browser.ts +192 -0
  152. package/src/test/index.ts +191 -0
  153. package/src/ts-plugin/diagnostics.ts +378 -0
  154. package/src/ts-plugin/fixes.ts +364 -0
  155. package/src/ts-plugin/index.ts +194 -0
  156. package/src/unrenderable.ts +35 -0
  157. package/templates/_shared/AGENTS.md +10 -0
  158. package/templates/_shared/CLAUDE.md +1 -0
  159. package/templates/_shared/gitignore +3 -0
  160. package/templates/_shared/mcp.json +11 -0
  161. package/templates/_shared/public/robots.txt +2 -0
  162. package/templates/_shared/src/routes/legal.tsx +13 -0
  163. package/templates/_shared/tsconfig.json +25 -0
  164. package/templates/app/.env.example +4 -0
  165. package/templates/app/package.json +21 -0
  166. package/templates/app/prunelle.config.ts +11 -0
  167. package/templates/app/src/auth.ts +14 -0
  168. package/templates/app/src/routes/_layout.tsx +32 -0
  169. package/templates/app/src/routes/dashboard/_middleware.ts +3 -0
  170. package/templates/app/src/routes/dashboard/index.tsx +13 -0
  171. package/templates/app/src/routes/index.tsx +15 -0
  172. package/templates/app/src/routes/login.tsx +34 -0
  173. package/templates/app/src/styles/app.css +7 -0
  174. package/templates/app/tests/app.test.ts +32 -0
  175. package/templates/blog/content/posts/welcome.md +9 -0
  176. package/templates/blog/package.json +22 -0
  177. package/templates/blog/prunelle.config.ts +23 -0
  178. package/templates/blog/src/posts.ts +26 -0
  179. package/templates/blog/src/routes/_layout.tsx +20 -0
  180. package/templates/blog/src/routes/blog/[slug].tsx +31 -0
  181. package/templates/blog/src/routes/index.tsx +33 -0
  182. package/templates/blog/src/styles/app.css +10 -0
  183. package/templates/fullstack/package.json +19 -0
  184. package/templates/fullstack/prunelle.config.ts +6 -0
  185. package/templates/fullstack/src/actions/greet.ts +19 -0
  186. package/templates/fullstack/src/components/Counter.tsx +10 -0
  187. package/templates/fullstack/src/components/GreetForm.tsx +27 -0
  188. package/templates/fullstack/src/routes/_layout.tsx +14 -0
  189. package/templates/fullstack/src/routes/api/health.ts +5 -0
  190. package/templates/fullstack/src/routes/index.tsx +13 -0
  191. package/templates/fullstack/src/styles/app.css +29 -0
  192. package/templates/fullstack/tests/app.test.ts +37 -0
  193. package/templates/minimal/package.json +19 -0
  194. package/templates/minimal/prunelle.config.ts +6 -0
  195. package/templates/minimal/src/components/Counter.tsx +10 -0
  196. package/templates/minimal/src/routes/_layout.tsx +14 -0
  197. package/templates/minimal/src/routes/index.tsx +11 -0
  198. package/templates/minimal/src/styles/app.css +22 -0
  199. package/ts-plugin.cjs +4277 -0
  200. package/README.md +0 -3
@@ -0,0 +1,607 @@
1
+ // Lexical-scope AST walking shared by the DOM compiler: shadow-aware visitor, free-identifier rewrites and reference checks.
2
+
3
+ import type { ArrowFunctionExpression, BlockStatement, Function as OxcFunction, JSXElement, JSXFragment, Node, Program } from 'oxc-parser'
4
+ import { visitorKeys } from 'oxc-parser'
5
+ import { walk } from './ast'
6
+ import { SourceEditor } from './patch'
7
+ import { collectPatternNames, containsJsx, isComponentTag, isJsxNode, jsxTagName, unwrapParens, unwrapTsExpr } from './shared'
8
+
9
+ // Names whose every binding in the file is a `const` holding an array literal: only those can be looped once without tracking.
10
+ export function literalArrayConsts(program: Program): Set<string> {
11
+ const literal = new Set<string>()
12
+ const other = new Set<string>()
13
+ const bindOther = (pattern: Node | null | undefined): void => {
14
+ if (pattern !== null && pattern !== undefined)
15
+ collectPatternNames(pattern, other)
16
+ }
17
+ walk(program, (node) => {
18
+ if (node.type === 'VariableDeclaration') {
19
+ for (const decl of node.declarations) {
20
+ const init = decl.init !== null ? unwrapTsExpr(decl.init) : null
21
+ if (node.kind === 'const' && decl.id.type === 'Identifier' && init?.type === 'ArrayExpression')
22
+ literal.add(decl.id.name)
23
+ else
24
+ bindOther(decl.id)
25
+ }
26
+ }
27
+ else if (node.type === 'ImportSpecifier' || node.type === 'ImportDefaultSpecifier' || node.type === 'ImportNamespaceSpecifier') {
28
+ bindOther(node.local)
29
+ }
30
+ else if (node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration') {
31
+ bindOther(node.id)
32
+ }
33
+ if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') {
34
+ for (const param of node.params)
35
+ bindOther(param)
36
+ }
37
+ else if (node.type === 'CatchClause') {
38
+ bindOther(node.param)
39
+ }
40
+ })
41
+ for (const name of other)
42
+ literal.delete(name)
43
+ return literal
44
+ }
45
+
46
+ export interface ScopedWalkCallbacks {
47
+ shouldStop?: (() => boolean) | undefined
48
+ onIdentifier?: ((node: Extract<Node, { type: 'Identifier' }>, shadowed: ReadonlySet<string>) => void) | undefined
49
+ onWrite?: ((name: string, shadowed: ReadonlySet<string>) => void) | undefined
50
+ onShorthandProperty?: ((node: Extract<Node, { type: 'Property' }>, shadowed: ReadonlySet<string>) => boolean) | undefined
51
+ onJsx?: ((node: JSXElement | JSXFragment, shadowed: ReadonlySet<string>) => void) | undefined
52
+ onFunction?: ((node: ArrowFunctionExpression | OxcFunction, shadowed: ReadonlySet<string>) => boolean) | undefined
53
+ }
54
+
55
+ /** Single lexical-scope AST visitor shared by `referencesAny`, `collectMutatedNames`, and `rewriteFreeIdentifiers` (respects `for...of`, `switch`, `catch (e)`, class members, labels, and TS nodes). */
56
+ export function walkScoped(rootNode: Node, callbacks: ScopedWalkCallbacks): void {
57
+ const { shouldStop, onIdentifier, onWrite, onShorthandProperty, onJsx, onFunction } = callbacks
58
+
59
+ const reportPatternWrites = (target: Node, shadowed: Set<string>): void => {
60
+ if (onWrite === undefined)
61
+ return
62
+ const written = new Set<string>()
63
+ collectPatternNames(target, written)
64
+ for (const name of written) {
65
+ if (!shadowed.has(name))
66
+ onWrite(name, shadowed)
67
+ }
68
+ }
69
+
70
+ function visitPatternDefaults(pattern: Node | null | undefined, shadowed: Set<string>): void {
71
+ if (pattern === null || pattern === undefined || shouldStop?.())
72
+ return
73
+ switch (pattern.type) {
74
+ case 'AssignmentPattern':
75
+ visitPatternDefaults(pattern.left, shadowed)
76
+ visit(pattern.right, shadowed)
77
+ return
78
+ case 'RestElement':
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
+ }
100
+ }
101
+
102
+ function visit(node: Node | null | undefined, shadowed: Set<string>): void {
103
+ if (node === null || node === undefined || shouldStop?.())
104
+ return
105
+
106
+ if (onJsx !== undefined && isJsxNode(node)) {
107
+ onJsx(node, shadowed)
108
+ return
109
+ }
110
+
111
+ switch (node.type) {
112
+ case 'Identifier': {
113
+ if (!shadowed.has(node.name))
114
+ onIdentifier?.(node, shadowed)
115
+ return
116
+ }
117
+ case 'MemberExpression': {
118
+ visit(node.object, shadowed)
119
+ if (node.computed)
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
+ }
202
+ }
203
+ else {
204
+ reportPatternWrites(node.left, next)
205
+ visit(node.left, next)
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
+ }
220
+ }
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
+ }
253
+ case 'BlockStatement': {
254
+ const nextShadow = new Set(shadowed)
255
+ for (const stmt of node.body) {
256
+ if (stmt.type === 'FunctionDeclaration' && stmt.id !== null)
257
+ nextShadow.add(stmt.id.name)
258
+ else if (stmt.type === 'ClassDeclaration' && stmt.id !== null)
259
+ nextShadow.add(stmt.id.name)
260
+ }
261
+ for (const stmt of node.body) {
262
+ if (stmt.type === 'VariableDeclaration') {
263
+ for (const d of stmt.declarations) {
264
+ visit(d.init, nextShadow)
265
+ visitPatternDefaults(d.id, nextShadow)
266
+ collectPatternNames(d.id, nextShadow)
267
+ }
268
+ }
269
+ else {
270
+ visit(stmt, nextShadow)
271
+ }
272
+ }
273
+ return
274
+ }
275
+ case 'VariableDeclaration': {
276
+ const nextShadow = new Set(shadowed)
277
+ for (const d of node.declarations) {
278
+ visit(d.init, nextShadow)
279
+ visitPatternDefaults(d.id, nextShadow)
280
+ collectPatternNames(d.id, nextShadow)
281
+ }
282
+ return
283
+ }
284
+ case 'VariableDeclarator': {
285
+ visit(node.init, shadowed)
286
+ visitPatternDefaults(node.id, shadowed)
287
+ return
288
+ }
289
+ case 'TSAsExpression':
290
+ case 'TSNonNullExpression':
291
+ case 'TSSatisfiesExpression':
292
+ case 'TSTypeAssertion':
293
+ case 'TSInstantiationExpression': {
294
+ visit(node.expression, shadowed)
295
+ return
296
+ }
297
+ default: {
298
+ if (node.type.startsWith('TS'))
299
+ return
300
+ const keys = visitorKeys[node.type]
301
+ if (keys !== undefined) {
302
+ const rec = node as unknown as Record<string, unknown>
303
+ for (let i = 0; i < keys.length; i++) {
304
+ const val = rec[keys[i]!]
305
+ if (Array.isArray(val)) {
306
+ for (let j = 0; j < val.length; j++) {
307
+ const item = val[j]
308
+ if (item !== null && typeof item === 'object' && 'type' in item)
309
+ visit(item as Node, shadowed)
310
+ }
311
+ }
312
+ else if (val !== null && typeof val === 'object' && 'type' in val) {
313
+ visit(val as Node, shadowed)
314
+ }
315
+ }
316
+ return
317
+ }
318
+ for (const [key, val] of Object.entries(node)) {
319
+ if (key === 'start' || key === 'end' || key === 'type')
320
+ continue
321
+ if (Array.isArray(val)) {
322
+ for (let i = 0; i < val.length; i++) {
323
+ const item = val[i]
324
+ if (item !== null && typeof item === 'object' && 'type' in item)
325
+ visit(item as Node, shadowed)
326
+ }
327
+ }
328
+ else if (val !== null && typeof val === 'object' && 'type' in val) {
329
+ visit(val as Node, shadowed)
330
+ }
331
+ }
332
+ }
333
+ }
334
+ }
335
+
336
+ visit(rootNode, new Set())
337
+ }
338
+
339
+ const LOOP_OR_FN_ANCESTORS = new Set([
340
+ 'ArrowFunctionExpression',
341
+ 'FunctionExpression',
342
+ 'FunctionDeclaration',
343
+ 'ForStatement',
344
+ 'ForInStatement',
345
+ 'ForOfStatement',
346
+ 'WhileStatement',
347
+ 'DoWhileStatement',
348
+ ])
349
+
350
+ function isLazySlotContext(node: Node, ancestors: readonly Node[]): boolean {
351
+ let slotIdx = -1
352
+ let child: Node = node
353
+ for (let i = ancestors.length - 1; i >= 0; i--) {
354
+ const p = ancestors[i]!
355
+ if (p.type === 'ParenthesizedExpression') {
356
+ child = p
357
+ continue
358
+ }
359
+ if (p.type === 'LogicalExpression') {
360
+ if (p.right !== child)
361
+ return false
362
+ child = p
363
+ continue
364
+ }
365
+ if (p.type === 'ConditionalExpression') {
366
+ if (p.test === child)
367
+ return false
368
+ child = p
369
+ continue
370
+ }
371
+ if (p.type === 'JSXExpressionContainer') {
372
+ const holder = ancestors[i - 1]
373
+ if (holder === undefined || (holder.type !== 'JSXElement' && holder.type !== 'JSXFragment'))
374
+ return false
375
+ if (holder.type === 'JSXElement' && isComponentTag(jsxTagName(holder.openingElement.name)))
376
+ return false
377
+ slotIdx = i - 1
378
+ break
379
+ }
380
+ return false
381
+ }
382
+ if (slotIdx < 0)
383
+ return false
384
+ for (let i = slotIdx - 1; i >= 0; i--) {
385
+ if (LOOP_OR_FN_ANCESTORS.has(ancestors[i]!.type))
386
+ return false
387
+ }
388
+ return true
389
+ }
390
+
391
+ /** 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. */
392
+ export function isSafeLazyJsxRef(name: string, stmts: readonly Node[]): boolean {
393
+ let safe = true
394
+ let uses = 0
395
+ const ancestors: Node[] = []
396
+ const visit = (node: Node | null | undefined, shadowed: Set<string>): void => {
397
+ if (!safe || node === null || node === undefined)
398
+ return
399
+ if (node.type === 'Identifier') {
400
+ if (node.name !== name || shadowed.has(name))
401
+ return
402
+ const parent = ancestors[ancestors.length - 1]
403
+ if (parent !== undefined && (
404
+ (parent.type === 'MemberExpression' && !parent.computed && parent.property === node)
405
+ || (parent.type === 'Property' && !parent.computed && parent.key === node && !parent.shorthand)
406
+ || (parent.type === 'LabeledStatement' && parent.label === node)
407
+ )) {
408
+ return
409
+ }
410
+ uses++
411
+ if (!isLazySlotContext(node, ancestors))
412
+ safe = false
413
+ return
414
+ }
415
+ if (node.type === 'BlockStatement' || node.type === 'ArrowFunctionExpression' || node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration') {
416
+ const next = new Set(shadowed)
417
+ if (node.type !== 'BlockStatement') {
418
+ if (node.type !== 'ArrowFunctionExpression' && node.id !== null)
419
+ next.add(node.id.name)
420
+ for (const p of node.params)
421
+ collectPatternNames(p, next)
422
+ ancestors.push(node)
423
+ visit(node.body, next)
424
+ ancestors.pop()
425
+ return
426
+ }
427
+ for (const s of node.body) {
428
+ if ((s.type === 'FunctionDeclaration' || s.type === 'ClassDeclaration') && s.id !== null)
429
+ next.add(s.id.name)
430
+ }
431
+ ancestors.push(node)
432
+ for (const s of node.body) {
433
+ if (s.type === 'VariableDeclaration') {
434
+ for (const d of s.declarations) {
435
+ visit(d.init, next)
436
+ collectPatternNames(d.id, next)
437
+ }
438
+ }
439
+ else {
440
+ visit(s, next)
441
+ }
442
+ }
443
+ ancestors.pop()
444
+ return
445
+ }
446
+ if (node.type.startsWith('TS') && node.type !== 'TSAsExpression' && node.type !== 'TSSatisfiesExpression' && node.type !== 'TSNonNullExpression' && node.type !== 'TSTypeAssertion')
447
+ return
448
+ ancestors.push(node)
449
+ const keys = visitorKeys[node.type]
450
+ if (keys !== undefined) {
451
+ const rec = node as unknown as Record<string, unknown>
452
+ for (let i = 0; i < keys.length; i++) {
453
+ const val = rec[keys[i]!]
454
+ if (Array.isArray(val)) {
455
+ for (let j = 0; j < val.length; j++) {
456
+ const item = val[j]
457
+ if (item !== null && typeof item === 'object' && 'type' in item)
458
+ visit(item as Node, shadowed)
459
+ }
460
+ }
461
+ else if (val !== null && typeof val === 'object' && 'type' in val) {
462
+ visit(val as Node, shadowed)
463
+ }
464
+ }
465
+ }
466
+ ancestors.pop()
467
+ }
468
+ const topShadow = new Set<string>()
469
+ for (const s of stmts) {
470
+ if (s.type === 'VariableDeclaration') {
471
+ for (const d of s.declarations) {
472
+ visit(d.init, topShadow)
473
+ collectPatternNames(d.id, topShadow)
474
+ }
475
+ }
476
+ else {
477
+ visit(s, topShadow)
478
+ }
479
+ }
480
+ return safe && uses === 1
481
+ }
482
+
483
+ /** Returns true if `rootNode` contains any free identifier reference to a name in `names`. */
484
+ export function referencesAny(rootNode: Node, names: ReadonlySet<string>): boolean {
485
+ if (names.size === 0)
486
+ return false
487
+ let found = false
488
+ walkScoped(rootNode, {
489
+ shouldStop: () => found,
490
+ onIdentifier(node) {
491
+ if (names.has(node.name))
492
+ found = true
493
+ },
494
+ })
495
+ return found
496
+ }
497
+
498
+ /** Returns the subset of `candidates` that are freely assigned, updated, or targeted by `for...in`/`for...of` inside `rootNode`. */
499
+ export function collectMutatedNames(rootNode: Node | null | undefined, candidates: ReadonlySet<string>): Set<string> {
500
+ const mutated = new Set<string>()
501
+ if (rootNode === null || rootNode === undefined || candidates.size === 0)
502
+ return mutated
503
+ walkScoped(rootNode, {
504
+ onWrite(name) {
505
+ if (candidates.has(name))
506
+ mutated.add(name)
507
+ },
508
+ })
509
+ return mutated
510
+ }
511
+
512
+ /** Rewrites free identifier reads inside an AST node while respecting lexical scopes, class/object keys, labels, and nested JSX/components. */
513
+ export function rewriteFreeIdentifiers(
514
+ source: string,
515
+ rootNode: Node,
516
+ replacements: ReadonlyMap<string, string>,
517
+ jsxNodes: WeakSet<Node>,
518
+ onJsx?: (node: JSXElement | JSXFragment, rewrites: ReadonlyMap<string, string>) => string,
519
+ onFunction?: (fn: ArrowFunctionExpression | OxcFunction, rewrites: ReadonlyMap<string, string>) => string | undefined,
520
+ ): string {
521
+ if (replacements.size === 0 && (onJsx === undefined || !containsJsx(rootNode, jsxNodes)))
522
+ return source.slice(rootNode.start, rootNode.end)
523
+
524
+ const offset = rootNode.start
525
+ const editor = new SourceEditor(source.slice(rootNode.start, rootNode.end))
526
+ const activeMap = (shadowed: ReadonlySet<string>): ReadonlyMap<string, string> =>
527
+ shadowed.size === 0 ? replacements : new Map([...replacements].filter(([k]) => !shadowed.has(k)))
528
+
529
+ walkScoped(rootNode, {
530
+ onJsx: onJsx === undefined
531
+ ? undefined
532
+ : (node, shadowed) => {
533
+ editor.replace(node.start - offset, node.end - offset, onJsx(node, activeMap(shadowed)))
534
+ },
535
+ onIdentifier(node) {
536
+ const rep = replacements.get(node.name)
537
+ if (rep !== undefined)
538
+ editor.replace(node.start - offset, node.end - offset, rep)
539
+ },
540
+ onShorthandProperty(node, shadowed) {
541
+ if (node.value.type !== 'Identifier')
542
+ return false
543
+ const name = node.value.name
544
+ if (!shadowed.has(name) && replacements.has(name)) {
545
+ editor.replace(node.start - offset, node.end - offset, `${name}: ${replacements.get(name)}`)
546
+ return true
547
+ }
548
+ return false
549
+ },
550
+ onFunction: onFunction === undefined
551
+ ? undefined
552
+ : (node, shadowed) => {
553
+ const compiled = onFunction(node, activeMap(shadowed))
554
+ if (compiled !== undefined) {
555
+ editor.replace(node.start - offset, node.end - offset, compiled)
556
+ return true
557
+ }
558
+ return false
559
+ },
560
+ })
561
+ return editor.toString()
562
+ }
563
+
564
+ export function extractKeyExpr(jsxEl: Node): Node | undefined {
565
+ if (jsxEl.type !== 'JSXElement')
566
+ return undefined
567
+ for (const attr of jsxEl.openingElement.attributes) {
568
+ if (attr.type === 'JSXAttribute' && attr.name.type === 'JSXIdentifier' && attr.name.name === 'key') {
569
+ if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression')
570
+ return attr.value.expression
571
+ if (attr.value?.type === 'Literal')
572
+ return attr.value
573
+ }
574
+ }
575
+ return undefined
576
+ }
577
+
578
+ export function extractBlockKeyExpr(block: BlockStatement): Node | undefined {
579
+ const blockLocals = new Set<string>()
580
+ for (const stmt of block.body) {
581
+ if (stmt.type === 'VariableDeclaration') {
582
+ for (const d of stmt.declarations)
583
+ collectPatternNames(d.id, blockLocals)
584
+ }
585
+ else if ((stmt.type === 'FunctionDeclaration' || stmt.type === 'ClassDeclaration') && stmt.id !== null) {
586
+ blockLocals.add(stmt.id.name)
587
+ }
588
+ }
589
+ let found: Node | undefined
590
+ walk(block, (child) => {
591
+ if (found !== undefined)
592
+ return false
593
+ if (child.type === 'ArrowFunctionExpression' || child.type === 'FunctionExpression' || child.type === 'FunctionDeclaration')
594
+ return false
595
+ if (child.type === 'ReturnStatement' && child.argument !== null) {
596
+ const arg = unwrapParens(child.argument)
597
+ const k = extractKeyExpr(arg)
598
+ // Only hoist key expressions that don't depend on block-local variables.
599
+ if (k !== undefined && !referencesAny(k, blockLocals)) {
600
+ found = k
601
+ return false
602
+ }
603
+ }
604
+ return undefined
605
+ })
606
+ return found
607
+ }