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.
- package/LICENSE +21 -0
- package/llms.txt +137 -40
- package/package.json +11 -2
- package/src/a11y/declaration.ts +373 -0
- package/src/a11y/index.ts +477 -0
- package/src/a11y/rgaa-map.ts +230 -0
- package/src/attrs.ts +11 -3
- package/src/build/bundle.ts +89 -41
- package/src/build/byte-size.ts +11 -0
- package/src/build/client-entries.ts +3 -2
- package/src/build/client-plugin.ts +11 -5
- package/src/build/client-prune.ts +56 -6
- package/src/build/compile.ts +13 -10
- package/src/build/ecoindex.ts +238 -0
- package/src/build/files.ts +15 -0
- package/src/build/first-load.ts +85 -14
- package/src/build/graph.ts +29 -39
- package/src/build/index.ts +225 -122
- package/src/build/island-promote.ts +311 -0
- package/src/build/islands.ts +14 -203
- package/src/build/manifest.ts +56 -116
- package/src/build/output.ts +3 -6
- package/src/build/plugin-host.ts +115 -41
- package/src/build/precompress.ts +16 -3
- package/src/build/prerender.ts +177 -39
- package/src/build/reach.ts +200 -0
- package/src/build/render-rules.ts +2 -2
- package/src/build/report.ts +48 -5
- package/src/build/resolve.ts +8 -6
- package/src/build/routes.ts +175 -10
- package/src/build/static-headers.ts +112 -0
- package/src/build/static.ts +139 -39
- package/src/build/status-pages.ts +127 -0
- package/src/build/style-bundles.ts +526 -0
- package/src/cli/check-forms.ts +636 -0
- package/src/cli/check-imports.ts +31 -0
- package/src/cli/check-links.ts +632 -0
- package/src/cli/check-security.ts +64 -0
- package/src/cli/check.ts +101 -26
- package/src/cli/commands.ts +120 -11
- package/src/cli/docs.ts +403 -0
- package/src/cli/inspect.ts +105 -15
- package/src/cli/mcp-tools.ts +492 -0
- package/src/cli/mcp.ts +205 -0
- package/src/cli/new.ts +16 -5
- package/src/cli/render.ts +416 -0
- package/src/cli/upgrade-names.ts +73 -13
- package/src/cli/upgrade.ts +156 -22
- package/src/compiler/analyze.ts +87 -54
- package/src/compiler/browser-globals.ts +140 -17
- package/src/compiler/cacheability-private.ts +566 -0
- package/src/compiler/cacheability.ts +335 -0
- package/src/compiler/compliance-eco.ts +71 -0
- package/src/compiler/compliance-rgaa.ts +614 -0
- package/src/compiler/compliance-urls.ts +148 -0
- package/src/compiler/compliance.ts +407 -352
- package/src/compiler/data-keys.ts +199 -192
- package/src/compiler/dev-source.ts +87 -0
- package/src/compiler/dom-attrs.ts +138 -65
- package/src/compiler/dom.ts +517 -442
- package/src/compiler/expr-context.ts +260 -81
- package/src/compiler/foreign-imports.ts +88 -0
- package/src/compiler/mistakes.ts +23 -2
- package/src/compiler/props-body.ts +60 -0
- package/src/compiler/props.ts +87 -36
- package/src/compiler/scope.ts +400 -333
- package/src/compiler/server-only.ts +190 -151
- package/src/compiler/shared.ts +42 -14
- package/src/compiler/signal-reads.ts +6 -2
- package/src/compiler/spread-handlers.ts +295 -0
- package/src/compiler/ssr-prunelle-tags.ts +27 -14
- package/src/compiler/ssr.ts +220 -89
- package/src/context.ts +34 -0
- package/src/dev/error-log.ts +177 -0
- package/src/dev/host-check.ts +32 -0
- package/src/dev/index.ts +94 -34
- package/src/dev/open-editor.ts +228 -0
- package/src/dev/protocol.ts +3 -0
- package/src/dev/reload.ts +5 -3
- package/src/dev/supervisor.ts +50 -25
- package/src/dev/watchers.ts +74 -13
- package/src/dom/action.ts +47 -21
- package/src/dom/attrs.ts +15 -3
- package/src/dom/extras.ts +57 -40
- package/src/dom/index.ts +54 -14
- package/src/dom/island.ts +102 -68
- package/src/dom/loop.ts +66 -32
- package/src/eco/declaration.ts +328 -0
- package/src/eco/index.ts +69 -0
- package/src/index.ts +2 -2
- package/src/jsx-runtime.ts +2 -2
- package/src/plugins/config.ts +18 -12
- package/src/plugins/context.ts +44 -1
- package/src/plugins/index.ts +2 -2
- package/src/plugins/load.ts +30 -10
- package/src/plugins/order.ts +38 -1
- package/src/plugins/public.ts +6 -3
- package/src/plugins/services.ts +84 -0
- package/src/plugins/types.ts +51 -2
- package/src/reactivity/graph.ts +10 -0
- package/src/reactivity/index.ts +24 -16
- package/src/reactivity/selector.ts +31 -4
- package/src/reactivity/store.ts +64 -20
- package/src/server/action-flash.ts +158 -0
- package/src/server/action-protect.ts +199 -0
- package/src/server/action-registry.ts +1 -1
- package/src/server/action.ts +188 -152
- package/src/server/app-secret.ts +63 -0
- package/src/server/assets.ts +34 -2
- package/src/server/compress.ts +96 -10
- package/src/server/consent.ts +59 -22
- package/src/server/cookies.ts +16 -2
- package/src/server/document.ts +25 -10
- package/src/server/env.ts +14 -0
- package/src/server/errors.ts +308 -65
- package/src/server/form-schema.ts +337 -0
- package/src/server/health.ts +275 -0
- package/src/server/http.ts +186 -39
- package/src/server/index.ts +272 -273
- package/src/server/loader.ts +15 -2
- package/src/server/page.ts +15 -0
- package/src/server/param-matchers.ts +112 -0
- package/src/server/plugin-host.ts +125 -88
- package/src/server/public.ts +6 -5
- package/src/server/rate-limit.ts +102 -36
- package/src/server/request-body.ts +1 -1
- package/src/server/route-cache.ts +541 -0
- package/src/server/route-table.ts +356 -11
- package/src/server/router.ts +125 -40
- package/src/server/routing.ts +112 -63
- package/src/server/sanitize-html.ts +309 -0
- package/src/server/security.ts +279 -52
- package/src/server/serve-handlers.ts +31 -7
- package/src/server/standard-schema.ts +17 -0
- package/src/server/start.ts +60 -16
- package/src/server/static-dir.ts +91 -12
- package/src/server/stream.ts +68 -27
- package/src/ssr/consent-banner.ts +58 -0
- package/src/ssr/context.ts +106 -8
- package/src/ssr/index.ts +151 -4
- package/src/ssr/navigation.ts +2 -2
- package/src/ssr/region.ts +40 -19
- package/src/style.ts +64 -1
- package/src/test/browser.ts +28 -13
- package/src/test/index.ts +97 -12
- package/src/ts-plugin/diagnostics.ts +505 -0
- package/src/ts-plugin/fixes.ts +364 -0
- package/src/ts-plugin/index.ts +194 -0
- package/templates/_shared/AGENTS.md +2 -2
- package/templates/_shared/gitignore +5 -0
- package/templates/_shared/mcp.json +11 -0
- package/templates/_shared/src/routes/legal.tsx +15 -1
- package/templates/_shared/tsconfig.json +5 -0
- package/templates/app/.env.example +4 -0
- package/templates/app/package.json +2 -1
- package/templates/app/prunelle.config.ts +2 -0
- package/templates/app/src/auth.ts +4 -6
- package/templates/app/src/mail.ts +4 -0
- package/templates/app/src/routes/_layout.tsx +9 -1
- package/templates/app/src/routes/dashboard/index.tsx +42 -8
- package/templates/app/src/routes/login.tsx +49 -30
- package/templates/app/src/routes/privacy.tsx +25 -0
- package/templates/app/src/styles/app.css +8 -1
- package/templates/app/tests/app.test.ts +44 -3
- package/templates/blog/package.json +1 -1
- package/templates/blog/prunelle.config.ts +1 -1
- package/templates/blog/src/routes/_layout.tsx +6 -1
- package/templates/blog/src/routes/index.tsx +5 -1
- package/templates/blog/src/styles/app.css +6 -1
- package/templates/fullstack/package.json +1 -1
- package/templates/fullstack/src/components/GreetForm.tsx +2 -1
- package/templates/fullstack/src/routes/_layout.tsx +4 -0
- package/templates/fullstack/src/styles/app.css +11 -1
- package/templates/minimal/package.json +1 -1
- package/templates/minimal/src/routes/_layout.tsx +4 -0
- package/templates/minimal/src/styles/app.css +11 -1
- 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>):
|
|
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
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
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
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
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
|
-
|
|
227
|
-
|
|
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
|
-
|
|
260
|
-
visitStatements(n.body, new Set(shadowed))
|
|
211
|
+
case 'RestElement':
|
|
212
|
+
visitEnvPattern(pattern.argument, shadowed, state)
|
|
261
213
|
return
|
|
262
|
-
|
|
263
|
-
|
|
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
|
-
|
|
270
|
-
|
|
271
|
-
|
|
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
|
-
|
|
283
|
-
|
|
284
|
-
|
|
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
|
-
|
|
229
|
+
if (prop.computed)
|
|
230
|
+
visitEnvNode(prop.key, shadowed, state)
|
|
231
|
+
visitEnvPattern(prop.value, shadowed, state)
|
|
293
232
|
}
|
|
294
233
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
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
|
-
|
|
301
|
-
|
|
302
|
-
return
|
|
255
|
+
else {
|
|
256
|
+
visitEnvNode(stmt, shadowed, state)
|
|
303
257
|
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
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
|
-
|
|
315
|
-
|
|
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 `
|
|
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 `
|
|
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
|
}
|
package/src/compiler/shared.ts
CHANGED
|
@@ -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
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
|
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:
|