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,6 +1,7 @@
1
1
  // What must never reach a browser bundle, shared by `prunelle build` and `prunelle check`.
2
2
 
3
3
  import type { MemberExpression, Node, Program } from 'oxc-parser'
4
+ import { visitorKeys } from 'oxc-parser'
4
5
  import { loadDeclarations } from './analyze'
5
6
  import { unwrapTsExpr } from './shared'
6
7
 
@@ -108,6 +109,16 @@ export const SERVER_ONLY_FILTER = new RegExp(`^(?:node:.+|bun:.+|${[...SERVER_ON
108
109
 
109
110
  // The client build inlines PUBLIC_* (from its own environment) and NODE_ENV; any other read ships a server variable name and crashes in the browser.
110
111
  const CLIENT_ENV_KEY = /^(?:PUBLIC_\w*|NODE_ENV)$/
112
+ const IMPORT_META_SAFE_KEYS = new Set(['MODE', 'DEV', 'PROD'])
113
+ const TS_EXPR_WRAPPERS = new Set([
114
+ 'TSAsExpression',
115
+ 'TSSatisfiesExpression',
116
+ 'TSNonNullExpression',
117
+ 'TSTypeAssertion',
118
+ 'TSInstantiationExpression',
119
+ ])
120
+
121
+ type EnvRootKind = 'process' | 'bun' | 'import.meta'
111
122
 
112
123
  function propKeyName(member: MemberExpression): string | undefined {
113
124
  if (!member.computed && member.property.type === 'Identifier')
@@ -117,13 +128,13 @@ function propKeyName(member: MemberExpression): string | undefined {
117
128
  return undefined
118
129
  }
119
130
 
120
- function isEnvRoot(node: Node, shadowed: ReadonlySet<string>): 'process' | 'bun' | undefined {
131
+ function isEnvRoot(node: Node, shadowed: ReadonlySet<string>): EnvRootKind | undefined {
121
132
  const inner = unwrapTsExpr(node)
122
- if (inner.type !== 'MemberExpression')
123
- return undefined
124
- if (propKeyName(inner) !== 'env')
133
+ if (inner.type !== 'MemberExpression' || propKeyName(inner) !== 'env')
125
134
  return undefined
126
135
  const obj = unwrapTsExpr(inner.object)
136
+ if (obj.type === 'MetaProperty' && obj.meta.name === 'import' && obj.property.name === 'meta')
137
+ return 'import.meta'
127
138
  if (obj.type === 'Identifier') {
128
139
  if (obj.name === 'process' && !shadowed.has('process'))
129
140
  return 'process'
@@ -146,181 +157,209 @@ function isEnvRoot(node: Node, shadowed: ReadonlySet<string>): 'process' | 'bun'
146
157
  export interface PrivateEnvMatch {
147
158
  offset: number
148
159
  kind: 'process' | 'bun'
160
+ /** More specific source (`'import.meta'`) while preserving `'process' | 'bun'` on `kind`. */
161
+ source?: EnvRootKind | undefined
149
162
  }
150
163
 
151
- /** Offset and source (`process` or `bun`) of the first private `process.env` or `Bun.env` read outside `load()`, or `undefined`. */
152
- export function findPrivateEnv(program: Program, isInlined: (key: string) => boolean = key => CLIENT_ENV_KEY.test(key)): PrivateEnvMatch | undefined {
153
- let found: PrivateEnvMatch | undefined
154
- const loadCode = new Set<Node>(loadDeclarations(program))
155
-
156
- function visitPattern(pattern: Node | null | undefined, shadowed: Set<string>): void {
157
- if (found !== undefined || pattern === null || pattern === undefined)
158
- return
159
- switch (pattern.type) {
160
- case 'Identifier':
161
- shadowed.add(pattern.name)
162
- return
163
- case 'AssignmentPattern':
164
- visit(pattern.right, shadowed)
165
- visitPattern(pattern.left, shadowed)
166
- return
167
- case 'RestElement':
168
- visitPattern(pattern.argument, shadowed)
169
- return
170
- case 'TSParameterProperty':
171
- visitPattern(pattern.parameter, shadowed)
172
- return
173
- case 'ArrayPattern':
174
- for (const el of pattern.elements) {
175
- if (el !== null)
176
- visitPattern(el, shadowed)
177
- }
178
- return
179
- case 'ObjectPattern':
180
- for (const prop of pattern.properties) {
181
- if (prop.type === 'RestElement') {
182
- visitPattern(prop.argument, shadowed)
183
- }
184
- else {
185
- if (prop.computed)
186
- visit(prop.key, shadowed)
187
- visitPattern(prop.value, shadowed)
188
- }
189
- }
190
- }
191
- }
164
+ interface EnvScanState {
165
+ found: PrivateEnvMatch | undefined
166
+ loadCode: ReadonlySet<Node>
167
+ isInlined: (key: string) => boolean
168
+ acceptOffset: ((offset: number) => boolean) | undefined
169
+ }
192
170
 
193
- function visitStatements(body: readonly Node[], shadowed: Set<string>): void {
194
- for (const stmt of body) {
195
- const decl = (stmt.type === 'ExportNamedDeclaration' || stmt.type === 'ExportDefaultDeclaration') ? stmt.declaration : stmt
196
- if ((decl?.type === 'FunctionDeclaration' || decl?.type === 'ClassDeclaration') && decl.id !== null)
197
- shadowed.add(decl.id.name)
171
+ function checkEnvMember(n: MemberExpression, shadowed: ReadonlySet<string>, state: EnvScanState): boolean {
172
+ const rootKind = isEnvRoot(n.object, shadowed)
173
+ if (rootKind !== undefined) {
174
+ if (state.acceptOffset !== undefined && !state.acceptOffset(n.start))
175
+ return true
176
+ if (rootKind === 'bun') {
177
+ state.found = { offset: n.start, kind: 'bun', source: 'bun' }
178
+ return true
198
179
  }
199
- for (const stmt of body) {
200
- if (stmt.type === 'VariableDeclaration') {
201
- for (const d of stmt.declarations) {
202
- visit(d.init, shadowed)
203
- visitPattern(d.id, shadowed)
204
- }
205
- }
206
- else if (stmt.type === 'ExportNamedDeclaration' && stmt.declaration?.type === 'VariableDeclaration') {
207
- for (const d of stmt.declaration.declarations) {
208
- visit(d.init, shadowed)
209
- visitPattern(d.id, shadowed)
210
- }
211
- }
212
- else {
213
- visit(stmt, shadowed)
214
- }
180
+ const key = propKeyName(n)
181
+ if (rootKind === 'import.meta') {
182
+ const allowed = key !== undefined && (IMPORT_META_SAFE_KEYS.has(key) || (key.startsWith('PUBLIC_') && state.isInlined(key)))
183
+ if (!allowed)
184
+ state.found = { offset: n.start, kind: 'process', source: 'import.meta' }
185
+ return true
215
186
  }
187
+ if (key === undefined || !state.isInlined(key))
188
+ state.found = { offset: n.start, kind: 'process', source: 'process' }
189
+ return true
190
+ }
191
+ const directKind = isEnvRoot(n, shadowed)
192
+ if (directKind !== undefined) {
193
+ if (state.acceptOffset === undefined || state.acceptOffset(n.start))
194
+ state.found = { offset: n.start, kind: directKind === 'bun' ? 'bun' : 'process', source: directKind }
195
+ return true
216
196
  }
197
+ return false
198
+ }
217
199
 
218
- function visit(node: unknown, shadowed: Set<string>): void {
219
- if (found !== undefined || node === null || typeof node !== 'object')
220
- return
221
- if (Array.isArray(node)) {
222
- for (let i = 0; i < node.length; i++)
223
- visit(node[i], shadowed)
200
+ function visitEnvPattern(pattern: Node | null | undefined, shadowed: Set<string>, state: EnvScanState): void {
201
+ if (state.found !== undefined || pattern === null || pattern === undefined)
202
+ return
203
+ switch (pattern.type) {
204
+ case 'Identifier':
205
+ shadowed.add(pattern.name)
224
206
  return
225
- }
226
- const n = node as Node
227
- if (typeof n.type !== 'string' || loadCode.has(n))
228
- return
229
-
230
- if (n.type === 'MemberExpression') {
231
- const rootKind = isEnvRoot(n.object, shadowed)
232
- if (rootKind !== undefined) {
233
- // Bun.env is never statically inlined in browser bundles.
234
- if (rootKind === 'bun') {
235
- found = { offset: n.start, kind: 'bun' }
236
- return
237
- }
238
- const key = propKeyName(n)
239
- if (key === undefined || !isInlined(key))
240
- found = { offset: n.start, kind: 'process' }
241
- return
242
- }
243
- const directKind = isEnvRoot(n, shadowed)
244
- if (directKind !== undefined) {
245
- found = { offset: n.start, kind: directKind }
246
- return
247
- }
248
- }
249
-
250
- if (n.type === 'ArrowFunctionExpression' || n.type === 'FunctionExpression' || n.type === 'FunctionDeclaration') {
251
- const next = new Set(shadowed)
252
- if (n.type !== 'ArrowFunctionExpression' && n.id !== null)
253
- next.add(n.id.name)
254
- for (const p of n.params)
255
- visitPattern(p, next)
256
- visit(n.body, next)
207
+ case 'AssignmentPattern':
208
+ visitEnvNode(pattern.right, shadowed, state)
209
+ visitEnvPattern(pattern.left, shadowed, state)
257
210
  return
258
- }
259
- if (n.type === 'Program' || n.type === 'BlockStatement') {
260
- visitStatements(n.body, new Set(shadowed))
211
+ case 'RestElement':
212
+ visitEnvPattern(pattern.argument, shadowed, state)
261
213
  return
262
- }
263
- if (n.type === 'CatchClause') {
264
- const next = new Set(shadowed)
265
- visitPattern(n.param, next)
266
- visit(n.body, next)
214
+ case 'TSParameterProperty':
215
+ visitEnvPattern(pattern.parameter, shadowed, state)
267
216
  return
268
- }
269
- if (n.type === 'ForInStatement' || n.type === 'ForOfStatement') {
270
- visit(n.right, shadowed)
271
- const next = new Set(shadowed)
272
- if (n.left.type === 'VariableDeclaration') {
273
- for (const d of n.left.declarations)
274
- visitPattern(d.id, next)
275
- }
276
- else {
277
- visit(n.left, shadowed)
217
+ case 'ArrayPattern':
218
+ for (const el of pattern.elements) {
219
+ if (el !== null)
220
+ visitEnvPattern(el, shadowed, state)
278
221
  }
279
- visit(n.body, next)
280
222
  return
281
- }
282
- if (n.type === 'ForStatement') {
283
- const next = new Set(shadowed)
284
- if (n.init !== null) {
285
- if (n.init.type === 'VariableDeclaration') {
286
- for (const d of n.init.declarations) {
287
- visit(d.init, next)
288
- visitPattern(d.id, next)
289
- }
223
+ case 'ObjectPattern':
224
+ for (const prop of pattern.properties) {
225
+ if (prop.type === 'RestElement') {
226
+ visitEnvPattern(prop.argument, shadowed, state)
290
227
  }
291
228
  else {
292
- visit(n.init, next)
229
+ if (prop.computed)
230
+ visitEnvNode(prop.key, shadowed, state)
231
+ visitEnvPattern(prop.value, shadowed, state)
293
232
  }
294
233
  }
295
- visit(n.test, next)
296
- visit(n.update, next)
297
- visit(n.body, next)
298
- return
234
+ }
235
+ }
236
+
237
+ function visitEnvStatements(body: readonly Node[], shadowed: Set<string>, state: EnvScanState): void {
238
+ for (const stmt of body) {
239
+ const decl = (stmt.type === 'ExportNamedDeclaration' || stmt.type === 'ExportDefaultDeclaration') ? stmt.declaration : stmt
240
+ if ((decl?.type === 'FunctionDeclaration' || decl?.type === 'ClassDeclaration') && decl.id !== null)
241
+ shadowed.add(decl.id.name)
242
+ }
243
+ for (const stmt of body) {
244
+ const varDecl = stmt.type === 'VariableDeclaration'
245
+ ? stmt
246
+ : stmt.type === 'ExportNamedDeclaration' && stmt.declaration?.type === 'VariableDeclaration'
247
+ ? stmt.declaration
248
+ : undefined
249
+ if (varDecl !== undefined) {
250
+ for (const d of varDecl.declarations) {
251
+ visitEnvNode(d.init, shadowed, state)
252
+ visitEnvPattern(d.id, shadowed, state)
253
+ }
299
254
  }
300
- if (n.type === 'TSAsExpression' || n.type === 'TSSatisfiesExpression' || n.type === 'TSNonNullExpression' || n.type === 'TSTypeAssertion' || n.type === 'TSInstantiationExpression') {
301
- visit(n.expression, shadowed)
302
- return
255
+ else {
256
+ visitEnvNode(stmt, shadowed, state)
303
257
  }
304
- if (n.type.startsWith('TS'))
305
- return
306
- for (const [key, val] of Object.entries(n)) {
307
- if (key === 'start' || key === 'end' || key === 'type')
308
- continue
309
- if (val !== null && typeof val === 'object')
310
- visit(val, shadowed)
258
+ }
259
+ }
260
+
261
+ function visitEnvScoped(n: Node, shadowed: Set<string>, state: EnvScanState): boolean {
262
+ if (n.type === 'ArrowFunctionExpression' || n.type === 'FunctionExpression' || n.type === 'FunctionDeclaration') {
263
+ const next = new Set(shadowed)
264
+ if (n.type !== 'ArrowFunctionExpression' && n.id !== null)
265
+ next.add(n.id.name)
266
+ for (const p of n.params)
267
+ visitEnvPattern(p, next, state)
268
+ visitEnvNode(n.body, next, state)
269
+ return true
270
+ }
271
+ if (n.type === 'Program' || n.type === 'BlockStatement') {
272
+ visitEnvStatements(n.body, new Set(shadowed), state)
273
+ return true
274
+ }
275
+ if (n.type === 'CatchClause') {
276
+ const next = new Set(shadowed)
277
+ visitEnvPattern(n.param, next, state)
278
+ visitEnvNode(n.body, next, state)
279
+ return true
280
+ }
281
+ if (n.type === 'ForInStatement' || n.type === 'ForOfStatement') {
282
+ visitEnvNode(n.right, shadowed, state)
283
+ const next = new Set(shadowed)
284
+ if (n.left.type === 'VariableDeclaration')
285
+ for (const d of n.left.declarations) visitEnvPattern(d.id, next, state)
286
+ else
287
+ visitEnvNode(n.left, shadowed, state)
288
+ visitEnvNode(n.body, next, state)
289
+ return true
290
+ }
291
+ if (n.type === 'ForStatement') {
292
+ const next = new Set(shadowed)
293
+ if (n.init?.type === 'VariableDeclaration') {
294
+ for (const d of n.init.declarations) {
295
+ visitEnvNode(d.init, next, state)
296
+ visitEnvPattern(d.id, next, state)
297
+ }
298
+ }
299
+ else {
300
+ visitEnvNode(n.init, next, state)
311
301
  }
302
+ visitEnvNode(n.test, next, state)
303
+ visitEnvNode(n.update, next, state)
304
+ visitEnvNode(n.body, next, state)
305
+ return true
312
306
  }
307
+ return false
308
+ }
313
309
 
314
- visit(program, new Set())
315
- return found
310
+ function visitEnvNode(node: unknown, shadowed: Set<string>, state: EnvScanState): void {
311
+ if (state.found !== undefined || node === null || typeof node !== 'object')
312
+ return
313
+ if (Array.isArray(node)) {
314
+ for (let i = 0; i < node.length; i++)
315
+ visitEnvNode(node[i], shadowed, state)
316
+ return
317
+ }
318
+ const n = node as Node
319
+ if (typeof n.type !== 'string' || state.loadCode.has(n))
320
+ return
321
+ if (n.type === 'MemberExpression' && checkEnvMember(n, shadowed, state))
322
+ return
323
+ if (visitEnvScoped(n, shadowed, state))
324
+ return
325
+ if (n.type.startsWith('TS')) {
326
+ if (TS_EXPR_WRAPPERS.has(n.type))
327
+ visitEnvNode((n as { expression?: unknown }).expression, shadowed, state)
328
+ return
329
+ }
330
+ const keys = visitorKeys[n.type]
331
+ if (keys === undefined)
332
+ return
333
+ const rec = n as unknown as Record<string, unknown>
334
+ for (let i = 0; i < keys.length; i++) {
335
+ const val = rec[keys[i]!]
336
+ if (val !== null && typeof val === 'object')
337
+ visitEnvNode(val, shadowed, state)
338
+ }
339
+ }
340
+
341
+ /** Offset and source (`process`, `bun`, or `import.meta`) of the first private env read outside `load()`, or `undefined`. */
342
+ export function findPrivateEnv(
343
+ program: Program,
344
+ isInlined: (key: string) => boolean = key => CLIENT_ENV_KEY.test(key),
345
+ acceptOffset?: (offset: number) => boolean,
346
+ ): PrivateEnvMatch | undefined {
347
+ const state: EnvScanState = {
348
+ found: undefined,
349
+ loadCode: new Set<Node>(loadDeclarations(program)),
350
+ isInlined,
351
+ acceptOffset,
352
+ }
353
+ visitEnvNode(program, new Set(), state)
354
+ return state.found
316
355
  }
317
356
 
318
- /** Byte offset of the first `process.env` or `Bun.env` read outside `load()` for a key the client build does not inline, or `undefined`. */
357
+ /** Byte offset of the first private `process.env`, `Bun.env`, or `import.meta.env` read outside `load()`, or `undefined`. */
319
358
  export function findPrivateEnvOffset(program: Program, isInlined: (key: string) => boolean = key => CLIENT_ENV_KEY.test(key)): number | undefined {
320
359
  return findPrivateEnv(program, isInlined)?.offset
321
360
  }
322
361
 
323
- /** True when the module reads `process.env` or `Bun.env` outside its `load()` (stripped from client code) for a key the client build does not inline. */
362
+ /** True when the module reads private `process.env`, `Bun.env`, or `import.meta.env` outside its `load()` (stripped from client code). */
324
363
  export function readsPrivateEnv(program: Program, isInlined: (key: string) => boolean = key => CLIENT_ENV_KEY.test(key)): boolean {
325
364
  return findPrivateEnvOffset(program, isInlined) !== undefined
326
365
  }
@@ -107,20 +107,48 @@ export function decodeHtmlEntities(str: string): string {
107
107
  })
108
108
  }
109
109
 
110
+ const LINE_STARTS_CACHE_LIMIT = 64
111
+ const lineStartsCache = new Map<string, number[]>()
112
+
113
+ /** Returns sorted 0-based byte offsets where each line begins in `code`, cached in a bounded LRU map. */
114
+ export function getLineStarts(code: string): readonly number[] {
115
+ const cached = lineStartsCache.get(code)
116
+ if (cached !== undefined)
117
+ return cached
118
+ const starts = [0]
119
+ let idx = code.indexOf('\n')
120
+ while (idx !== -1) {
121
+ starts.push(idx + 1)
122
+ idx = code.indexOf('\n', idx + 1)
123
+ }
124
+ if (lineStartsCache.size >= LINE_STARTS_CACHE_LIMIT) {
125
+ const oldest = lineStartsCache.keys().next().value
126
+ if (oldest !== undefined)
127
+ lineStartsCache.delete(oldest)
128
+ }
129
+ lineStartsCache.set(code, starts)
130
+ return starts
131
+ }
132
+
133
+ /** 0-based line index of `offset` in a precomputed `lineStarts` table, in O(log N). */
134
+ export function lineIndexAtOffset(starts: readonly number[], offset: number): number {
135
+ let lo = 0
136
+ let hi = starts.length - 1
137
+ while (lo < hi) {
138
+ const mid = (lo + hi + 1) >> 1
139
+ if (starts[mid]! <= offset)
140
+ lo = mid
141
+ else
142
+ hi = mid - 1
143
+ }
144
+ return lo
145
+ }
146
+
110
147
  export function offsetToLineCol(code: string, offset: number): { line: number, col: number } {
111
- let line = 1
112
- let col = 1
113
148
  const end = Math.min(Math.max(0, offset), code.length)
114
- for (let i = 0; i < end; i++) {
115
- if (code.charCodeAt(i) === 10) {
116
- line++
117
- col = 1
118
- }
119
- else {
120
- col++
121
- }
122
- }
123
- return { line, col }
149
+ const starts = getLineStarts(code)
150
+ const idx = lineIndexAtOffset(starts, end)
151
+ return { line: idx + 1, col: end - starts[idx]! + 1 }
124
152
  }
125
153
 
126
154
  export function formatLoc(filename: string, code: string, offset?: number): string {
@@ -335,9 +363,9 @@ export interface PrunelleTagBinding {
335
363
  specifiers: readonly { local: string, imported: string }[]
336
364
  }
337
365
 
338
- type PrunelleTag = 'Head' | 'Region' | 'Suspense' | 'ConsentGate' | 'Catch'
366
+ type PrunelleTag = 'Head' | 'Region' | 'Suspense' | 'ConsentGate' | 'ConsentBanner' | 'ConsentPreferences' | 'Catch'
339
367
 
340
- const PRUNELLE_TAGS = new Set<string>(['Head', 'Region', 'Suspense', 'ConsentGate', 'Catch'])
368
+ const PRUNELLE_TAGS = new Set<string>(['Head', 'Region', 'Suspense', 'ConsentGate', 'ConsentBanner', 'ConsentPreferences', 'Catch'])
341
369
 
342
370
  /** Resolves a JSX tag (direct, aliased, or namespaced via `prunelle`) to its canonical Prunelle tag. */
343
371
  export function resolvePrunelleTag(tag: string, imports: readonly PrunelleTagBinding[]): PrunelleTag | undefined {
@@ -150,7 +150,7 @@ function bareSignal(expr: Node, lookup: SignalLookup): { name: string, start: nu
150
150
  if (node.type === 'ConditionalExpression')
151
151
  return bareSignal(node.test, lookup) ?? bareSignal(node.consequent, lookup) ?? bareSignal(node.alternate, lookup)
152
152
  if (node.type === 'UnaryExpression')
153
- return bareSignal(node.argument, lookup)
153
+ return node.operator === 'typeof' || node.operator === 'void' ? undefined : bareSignal(node.argument, lookup)
154
154
  if (node.type === 'TemplateLiteral') {
155
155
  for (const part of node.expressions) {
156
156
  const found = bareSignal(part, lookup)
@@ -182,7 +182,7 @@ function jsxReads(node: Node): Node[] {
182
182
  return reads
183
183
  }
184
184
 
185
- // The expressions plain code tests: an if, a loop or a ternary condition, and the operand of `!`.
185
+ // The expressions plain code tests or interpolates: an if/loop/ternary condition, a switch discriminant, template literal slots, and the operand of `!`.
186
186
  function codeTests(node: Node): Node[] {
187
187
  switch (node.type) {
188
188
  case 'IfStatement':
@@ -192,6 +192,10 @@ function codeTests(node: Node): Node[] {
192
192
  return [node.test]
193
193
  case 'ForStatement':
194
194
  return node.test === null ? [] : [node.test]
195
+ case 'SwitchStatement':
196
+ return [node.discriminant]
197
+ case 'TemplateLiteral':
198
+ return node.expressions
195
199
  case 'UnaryExpression':
196
200
  return node.operator === '!' ? [node.argument] : []
197
201
  default: