prunelle 0.1.1 → 0.7.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 (177) hide show
  1. package/LICENSE +21 -0
  2. package/llms.txt +137 -40
  3. package/package.json +11 -2
  4. package/src/a11y/declaration.ts +373 -0
  5. package/src/a11y/index.ts +477 -0
  6. package/src/a11y/rgaa-map.ts +230 -0
  7. package/src/attrs.ts +11 -3
  8. package/src/build/bundle.ts +89 -41
  9. package/src/build/byte-size.ts +11 -0
  10. package/src/build/client-entries.ts +3 -2
  11. package/src/build/client-plugin.ts +11 -5
  12. package/src/build/client-prune.ts +56 -6
  13. package/src/build/compile.ts +13 -10
  14. package/src/build/ecoindex.ts +238 -0
  15. package/src/build/files.ts +15 -0
  16. package/src/build/first-load.ts +85 -14
  17. package/src/build/graph.ts +29 -39
  18. package/src/build/index.ts +225 -122
  19. package/src/build/island-promote.ts +311 -0
  20. package/src/build/islands.ts +14 -203
  21. package/src/build/manifest.ts +56 -116
  22. package/src/build/output.ts +3 -6
  23. package/src/build/plugin-host.ts +115 -41
  24. package/src/build/precompress.ts +16 -3
  25. package/src/build/prerender.ts +177 -39
  26. package/src/build/reach.ts +200 -0
  27. package/src/build/render-rules.ts +2 -2
  28. package/src/build/report.ts +48 -5
  29. package/src/build/resolve.ts +8 -6
  30. package/src/build/routes.ts +175 -10
  31. package/src/build/static-headers.ts +112 -0
  32. package/src/build/static.ts +139 -39
  33. package/src/build/status-pages.ts +127 -0
  34. package/src/build/style-bundles.ts +526 -0
  35. package/src/cli/check-forms.ts +636 -0
  36. package/src/cli/check-imports.ts +31 -0
  37. package/src/cli/check-links.ts +632 -0
  38. package/src/cli/check-security.ts +64 -0
  39. package/src/cli/check.ts +101 -26
  40. package/src/cli/commands.ts +120 -11
  41. package/src/cli/docs.ts +403 -0
  42. package/src/cli/inspect.ts +105 -15
  43. package/src/cli/mcp-tools.ts +492 -0
  44. package/src/cli/mcp.ts +205 -0
  45. package/src/cli/new.ts +16 -5
  46. package/src/cli/render.ts +416 -0
  47. package/src/cli/upgrade-names.ts +73 -13
  48. package/src/cli/upgrade.ts +156 -22
  49. package/src/compiler/analyze.ts +87 -54
  50. package/src/compiler/browser-globals.ts +140 -17
  51. package/src/compiler/cacheability-private.ts +566 -0
  52. package/src/compiler/cacheability.ts +335 -0
  53. package/src/compiler/compliance-eco.ts +71 -0
  54. package/src/compiler/compliance-rgaa.ts +614 -0
  55. package/src/compiler/compliance-urls.ts +148 -0
  56. package/src/compiler/compliance.ts +407 -352
  57. package/src/compiler/data-keys.ts +199 -192
  58. package/src/compiler/dev-source.ts +87 -0
  59. package/src/compiler/dom-attrs.ts +138 -65
  60. package/src/compiler/dom.ts +517 -442
  61. package/src/compiler/expr-context.ts +260 -81
  62. package/src/compiler/foreign-imports.ts +88 -0
  63. package/src/compiler/mistakes.ts +23 -2
  64. package/src/compiler/props-body.ts +60 -0
  65. package/src/compiler/props.ts +87 -36
  66. package/src/compiler/scope.ts +400 -333
  67. package/src/compiler/server-only.ts +190 -151
  68. package/src/compiler/shared.ts +42 -14
  69. package/src/compiler/signal-reads.ts +6 -2
  70. package/src/compiler/spread-handlers.ts +295 -0
  71. package/src/compiler/ssr-prunelle-tags.ts +27 -14
  72. package/src/compiler/ssr.ts +220 -89
  73. package/src/context.ts +34 -0
  74. package/src/dev/error-log.ts +177 -0
  75. package/src/dev/host-check.ts +32 -0
  76. package/src/dev/index.ts +94 -34
  77. package/src/dev/open-editor.ts +228 -0
  78. package/src/dev/protocol.ts +3 -0
  79. package/src/dev/reload.ts +5 -3
  80. package/src/dev/supervisor.ts +50 -25
  81. package/src/dev/watchers.ts +74 -13
  82. package/src/dom/action.ts +47 -21
  83. package/src/dom/attrs.ts +15 -3
  84. package/src/dom/extras.ts +57 -40
  85. package/src/dom/index.ts +54 -14
  86. package/src/dom/island.ts +102 -68
  87. package/src/dom/loop.ts +66 -32
  88. package/src/eco/declaration.ts +328 -0
  89. package/src/eco/index.ts +69 -0
  90. package/src/index.ts +2 -2
  91. package/src/jsx-runtime.ts +2 -2
  92. package/src/plugins/config.ts +18 -12
  93. package/src/plugins/context.ts +44 -1
  94. package/src/plugins/index.ts +2 -2
  95. package/src/plugins/load.ts +30 -10
  96. package/src/plugins/order.ts +38 -1
  97. package/src/plugins/public.ts +6 -3
  98. package/src/plugins/services.ts +84 -0
  99. package/src/plugins/types.ts +51 -2
  100. package/src/reactivity/graph.ts +10 -0
  101. package/src/reactivity/index.ts +24 -16
  102. package/src/reactivity/selector.ts +31 -4
  103. package/src/reactivity/store.ts +64 -20
  104. package/src/server/action-flash.ts +158 -0
  105. package/src/server/action-protect.ts +199 -0
  106. package/src/server/action-registry.ts +1 -1
  107. package/src/server/action.ts +188 -152
  108. package/src/server/app-secret.ts +63 -0
  109. package/src/server/assets.ts +34 -2
  110. package/src/server/compress.ts +96 -10
  111. package/src/server/consent.ts +59 -22
  112. package/src/server/cookies.ts +16 -2
  113. package/src/server/document.ts +25 -10
  114. package/src/server/env.ts +14 -0
  115. package/src/server/errors.ts +308 -65
  116. package/src/server/form-schema.ts +337 -0
  117. package/src/server/health.ts +275 -0
  118. package/src/server/http.ts +186 -39
  119. package/src/server/index.ts +272 -273
  120. package/src/server/loader.ts +15 -2
  121. package/src/server/page.ts +15 -0
  122. package/src/server/param-matchers.ts +112 -0
  123. package/src/server/plugin-host.ts +125 -88
  124. package/src/server/public.ts +6 -5
  125. package/src/server/rate-limit.ts +102 -36
  126. package/src/server/request-body.ts +1 -1
  127. package/src/server/route-cache.ts +541 -0
  128. package/src/server/route-table.ts +356 -11
  129. package/src/server/router.ts +125 -40
  130. package/src/server/routing.ts +112 -63
  131. package/src/server/sanitize-html.ts +309 -0
  132. package/src/server/security.ts +279 -52
  133. package/src/server/serve-handlers.ts +31 -7
  134. package/src/server/standard-schema.ts +17 -0
  135. package/src/server/start.ts +60 -16
  136. package/src/server/static-dir.ts +91 -12
  137. package/src/server/stream.ts +68 -27
  138. package/src/ssr/consent-banner.ts +58 -0
  139. package/src/ssr/context.ts +106 -8
  140. package/src/ssr/index.ts +151 -4
  141. package/src/ssr/navigation.ts +2 -2
  142. package/src/ssr/region.ts +40 -19
  143. package/src/style.ts +64 -1
  144. package/src/test/browser.ts +28 -13
  145. package/src/test/index.ts +97 -12
  146. package/src/ts-plugin/diagnostics.ts +505 -0
  147. package/src/ts-plugin/fixes.ts +364 -0
  148. package/src/ts-plugin/index.ts +194 -0
  149. package/templates/_shared/AGENTS.md +2 -2
  150. package/templates/_shared/gitignore +5 -0
  151. package/templates/_shared/mcp.json +11 -0
  152. package/templates/_shared/src/routes/legal.tsx +15 -1
  153. package/templates/_shared/tsconfig.json +5 -0
  154. package/templates/app/.env.example +4 -0
  155. package/templates/app/package.json +2 -1
  156. package/templates/app/prunelle.config.ts +2 -0
  157. package/templates/app/src/auth.ts +4 -6
  158. package/templates/app/src/mail.ts +4 -0
  159. package/templates/app/src/routes/_layout.tsx +9 -1
  160. package/templates/app/src/routes/dashboard/index.tsx +42 -8
  161. package/templates/app/src/routes/login.tsx +49 -30
  162. package/templates/app/src/routes/privacy.tsx +25 -0
  163. package/templates/app/src/styles/app.css +8 -1
  164. package/templates/app/tests/app.test.ts +44 -3
  165. package/templates/blog/package.json +1 -1
  166. package/templates/blog/prunelle.config.ts +1 -1
  167. package/templates/blog/src/routes/_layout.tsx +6 -1
  168. package/templates/blog/src/routes/index.tsx +5 -1
  169. package/templates/blog/src/styles/app.css +6 -1
  170. package/templates/fullstack/package.json +1 -1
  171. package/templates/fullstack/src/components/GreetForm.tsx +2 -1
  172. package/templates/fullstack/src/routes/_layout.tsx +4 -0
  173. package/templates/fullstack/src/styles/app.css +11 -1
  174. package/templates/minimal/package.json +1 -1
  175. package/templates/minimal/src/routes/_layout.tsx +4 -0
  176. package/templates/minimal/src/styles/app.css +11 -1
  177. package/ts-plugin.cjs +4277 -0
@@ -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
- /** 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
63
+ interface ScopedWalkCtx {
64
+ rootNode: Node
65
+ callbacks: ScopedWalkCallbacks
66
+ }
58
67
 
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
+ 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
- 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
- }
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
- function visit(node: Node | null | undefined, shadowed: Set<string>): void {
103
- if (node === null || node === undefined || shouldStop?.())
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
- if (onJsx !== undefined && isJsxNode(node)) {
107
- onJsx(node, shadowed)
99
+ case 'RestElement':
100
+ visitPatternDefaults(ctx, pattern.argument, shadowed)
108
101
  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
- }
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
- 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
- }
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
- 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
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
- case 'VariableDeclarator': {
285
- visit(node.init, shadowed)
286
- visitPatternDefaults(node.id, shadowed)
287
- return
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
- case 'TSAsExpression':
290
- case 'TSNonNullExpression':
291
- case 'TSSatisfiesExpression':
292
- case 'TSTypeAssertion':
293
- case 'TSInstantiationExpression': {
294
- visit(node.expression, shadowed)
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
- 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
- }
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
- visit(rootNode, new Set())
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 every free reference to `name` in `stmts` sits solely in a JSX slot (`{name}` or component prop `p={name}`), so deferring `const name = <jsx>` to `() => <jsx>` is observationally equivalent and preserves hydration order. */
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 (!isSlotContext())
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
- const next = new Set(shadowed)
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') && node.type !== 'TSAsExpression' && node.type !== 'TSSatisfiesExpression' && node.type !== 'TSNonNullExpression' && node.type !== 'TSTypeAssertion')
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
- const val = rec[keys[i]!]
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 > 0
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`. */