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,6 @@
|
|
|
1
1
|
// Warns on a browser global read while a component renders or its module loads: the server has no `window`, so the first request throws.
|
|
2
2
|
|
|
3
|
-
import type { ArrowFunctionExpression, Node, Function as OxcFunction, Program } from 'oxc-parser'
|
|
3
|
+
import type { ArrowFunctionExpression, Node, Function as OxcFunction, Program, Statement } from 'oxc-parser'
|
|
4
4
|
import type { CompilerWarning } from './mistakes'
|
|
5
5
|
import { walk } from './ast'
|
|
6
6
|
import { walkScoped } from './scope'
|
|
@@ -20,14 +20,35 @@ const MENTIONS_RE = new RegExp(`\\b(?:${[...BROWSER_GLOBALS].join('|')})\\b`)
|
|
|
20
20
|
// Array methods whose callback runs at once, so while the component renders.
|
|
21
21
|
const EAGER_CALLBACK_METHODS = new Set(['map', 'flatMap', 'filter', 'forEach', 'reduce', 'some', 'every', 'find', 'findIndex', 'sort', 'toSorted'])
|
|
22
22
|
|
|
23
|
+
const DEFINED_TYPEOF_VALUES = new Set(['object', 'function'])
|
|
24
|
+
|
|
23
25
|
type FunctionNode = ArrowFunctionExpression | OxcFunction
|
|
24
26
|
|
|
25
27
|
interface Read { name: string, start: number }
|
|
26
28
|
|
|
29
|
+
interface GuardSpan {
|
|
30
|
+
start: number
|
|
31
|
+
end: number
|
|
32
|
+
globals: ReadonlySet<string>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface GuardPolarity {
|
|
36
|
+
positive: Set<string>
|
|
37
|
+
negative: Set<string>
|
|
38
|
+
}
|
|
39
|
+
|
|
27
40
|
function topLevelFunctions(program: Program): Map<string, FunctionNode> {
|
|
28
41
|
const fns = new Map<string, FunctionNode>()
|
|
29
42
|
for (const stmt of program.body) {
|
|
30
|
-
|
|
43
|
+
if (stmt.type === 'ExportDefaultDeclaration') {
|
|
44
|
+
const decl = unwrapTsExpr(stmt.declaration)
|
|
45
|
+
if (decl.type === 'FunctionDeclaration' || decl.type === 'FunctionExpression')
|
|
46
|
+
fns.set(decl.id?.name ?? 'default', decl)
|
|
47
|
+
else if (decl.type === 'ArrowFunctionExpression')
|
|
48
|
+
fns.set('default', decl)
|
|
49
|
+
continue
|
|
50
|
+
}
|
|
51
|
+
const decl = stmt.type === 'ExportNamedDeclaration' ? stmt.declaration : stmt
|
|
31
52
|
if (decl?.type === 'FunctionDeclaration' && decl.id !== null) {
|
|
32
53
|
fns.set(decl.id.name, decl)
|
|
33
54
|
}
|
|
@@ -62,15 +83,87 @@ export function topLevelNames(program: Program): Set<string> {
|
|
|
62
83
|
return names
|
|
63
84
|
}
|
|
64
85
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (
|
|
86
|
+
function typeofBrowserGlobal(node: Node): string | undefined {
|
|
87
|
+
const inner = unwrapTsExpr(node)
|
|
88
|
+
if (inner.type === 'UnaryExpression' && inner.operator === 'typeof' && inner.argument.type === 'Identifier' && BROWSER_GLOBALS.has(inner.argument.name))
|
|
89
|
+
return inner.argument.name
|
|
90
|
+
return undefined
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function stringLiteralValue(node: Node): string | undefined {
|
|
94
|
+
const inner = unwrapTsExpr(node)
|
|
95
|
+
return inner.type === 'Literal' && typeof inner.value === 'string' ? inner.value : undefined
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function extractTypeofGuard(node: Node): GuardPolarity {
|
|
99
|
+
const positive = new Set<string>()
|
|
100
|
+
const negative = new Set<string>()
|
|
101
|
+
const inner = unwrapTsExpr(node)
|
|
102
|
+
if (inner.type === 'UnaryExpression' && inner.operator === '!') {
|
|
103
|
+
const sub = extractTypeofGuard(inner.argument)
|
|
104
|
+
return { positive: sub.negative, negative: sub.positive }
|
|
105
|
+
}
|
|
106
|
+
if (inner.type === 'LogicalExpression') {
|
|
107
|
+
const left = extractTypeofGuard(inner.left)
|
|
108
|
+
const right = extractTypeofGuard(inner.right)
|
|
109
|
+
if (inner.operator === '&&') {
|
|
110
|
+
for (const g of left.positive) positive.add(g)
|
|
111
|
+
for (const g of right.positive) positive.add(g)
|
|
112
|
+
}
|
|
113
|
+
else if (inner.operator === '||') {
|
|
114
|
+
for (const g of left.negative) negative.add(g)
|
|
115
|
+
for (const g of right.negative) negative.add(g)
|
|
116
|
+
}
|
|
117
|
+
return { positive, negative }
|
|
118
|
+
}
|
|
119
|
+
if (inner.type === 'BinaryExpression' && (inner.operator === '===' || inner.operator === '==' || inner.operator === '!==' || inner.operator === '!=')) {
|
|
120
|
+
const g = typeofBrowserGlobal(inner.left) ?? typeofBrowserGlobal(inner.right)
|
|
121
|
+
const lit = typeofBrowserGlobal(inner.left) !== undefined ? stringLiteralValue(inner.right) : stringLiteralValue(inner.left)
|
|
122
|
+
if (g !== undefined && lit !== undefined) {
|
|
123
|
+
const isNeg = inner.operator === '!==' || inner.operator === '!='
|
|
124
|
+
if (lit === 'undefined')
|
|
125
|
+
(isNeg ? positive : negative).add(g)
|
|
126
|
+
else if (DEFINED_TYPEOF_VALUES.has(lit))
|
|
127
|
+
(isNeg ? negative : positive).add(g)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return { positive, negative }
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function definitelyExits(stmt: Statement): boolean {
|
|
134
|
+
if (stmt.type === 'ReturnStatement' || stmt.type === 'ThrowStatement')
|
|
135
|
+
return true
|
|
136
|
+
if (stmt.type === 'BlockStatement')
|
|
137
|
+
return stmt.body.length > 0 && definitelyExits(stmt.body[stmt.body.length - 1]!)
|
|
138
|
+
if (stmt.type === 'IfStatement')
|
|
139
|
+
return stmt.alternate !== null && definitelyExits(stmt.consequent) && definitelyExits(stmt.alternate)
|
|
140
|
+
return false
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function collectEarlyReturnGuards(stmts: readonly Statement[], guards: GuardSpan[]): void {
|
|
144
|
+
const lastEnd = stmts[stmts.length - 1]?.end
|
|
145
|
+
if (lastEnd === undefined)
|
|
68
146
|
return
|
|
69
|
-
|
|
70
|
-
|
|
147
|
+
for (let i = 0; i < stmts.length - 1; i++) {
|
|
148
|
+
const stmt = stmts[i]!
|
|
149
|
+
if (stmt.type !== 'IfStatement' || !definitelyExits(stmt.consequent))
|
|
150
|
+
continue
|
|
151
|
+
const { negative } = extractTypeofGuard(stmt.test)
|
|
152
|
+
if (negative.size > 0)
|
|
153
|
+
guards.push({ start: stmts[i + 1]!.start, end: lastEnd, globals: negative })
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function collectProgramMetadata(program: Program): { eagerCallbacks: WeakSet<Node>, calleeStarts: Set<number>, guards: GuardSpan[] } {
|
|
71
158
|
const eagerCallbacks = new WeakSet<Node>()
|
|
72
159
|
const calleeStarts = new Set<number>()
|
|
73
|
-
const guards:
|
|
160
|
+
const guards: GuardSpan[] = []
|
|
161
|
+
|
|
162
|
+
const addGuard = (node: Node | null | undefined, globals: ReadonlySet<string>): void => {
|
|
163
|
+
if (node !== null && node !== undefined && globals.size > 0)
|
|
164
|
+
guards.push({ start: node.start, end: node.end, globals })
|
|
165
|
+
}
|
|
166
|
+
|
|
74
167
|
walk(program, (node) => {
|
|
75
168
|
if (node.type === 'CallExpression') {
|
|
76
169
|
const callee = unwrapTsExpr(node.callee)
|
|
@@ -80,11 +173,38 @@ export function checkBrowserGlobals(program: Program, code: string, filename: st
|
|
|
80
173
|
eagerCallbacks.add(unwrapTsExpr(node.arguments[0]))
|
|
81
174
|
}
|
|
82
175
|
else if (node.type === 'UnaryExpression' && node.operator === 'typeof' && node.argument.type === 'Identifier' && BROWSER_GLOBALS.has(node.argument.name)) {
|
|
83
|
-
guards.push(node.start)
|
|
176
|
+
guards.push({ start: node.argument.start, end: node.argument.end, globals: new Set([node.argument.name]) })
|
|
177
|
+
}
|
|
178
|
+
else if (node.type === 'IfStatement' || node.type === 'ConditionalExpression') {
|
|
179
|
+
const { positive, negative } = extractTypeofGuard(node.test)
|
|
180
|
+
addGuard(node.consequent, positive)
|
|
181
|
+
addGuard(node.alternate, negative)
|
|
182
|
+
}
|
|
183
|
+
else if (node.type === 'LogicalExpression') {
|
|
184
|
+
const { positive, negative } = extractTypeofGuard(node.left)
|
|
185
|
+
if (node.operator === '&&')
|
|
186
|
+
addGuard(node.right, positive)
|
|
187
|
+
else if (node.operator === '||')
|
|
188
|
+
addGuard(node.right, negative)
|
|
189
|
+
}
|
|
190
|
+
else if (node.type === 'BlockStatement' || node.type === 'Program') {
|
|
191
|
+
collectEarlyReturnGuards(node.body, guards)
|
|
84
192
|
}
|
|
85
193
|
})
|
|
86
|
-
|
|
87
|
-
|
|
194
|
+
|
|
195
|
+
return { eagerCallbacks, calleeStarts, guards }
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Warns on each browser global the module reads at its top level, or while one of its components renders, itself or through a top-level helper it calls. */
|
|
199
|
+
export function checkBrowserGlobals(program: Program, code: string, filename: string, warnings: CompilerWarning[], jsxNodes?: WeakSet<Node>): void {
|
|
200
|
+
if (!MENTIONS_RE.test(code))
|
|
201
|
+
return
|
|
202
|
+
const shadowing = topLevelNames(program)
|
|
203
|
+
const helpers = topLevelFunctions(program)
|
|
204
|
+
const { eagerCallbacks, calleeStarts, guards } = collectProgramMetadata(program)
|
|
205
|
+
|
|
206
|
+
const isNameGuarded = (offset: number, name: string): boolean =>
|
|
207
|
+
guards.some(g => offset >= g.start && offset < g.end && (g.globals.has(name) || g.globals.has('window')))
|
|
88
208
|
|
|
89
209
|
const readsOf = new Map<Node, Read[]>()
|
|
90
210
|
const collect = (root: Node): Read[] => {
|
|
@@ -93,18 +213,21 @@ export function checkBrowserGlobals(program: Program, code: string, filename: st
|
|
|
93
213
|
return known
|
|
94
214
|
const reads: Read[] = []
|
|
95
215
|
readsOf.set(root, reads)
|
|
96
|
-
if (isGuarded(root))
|
|
97
|
-
return reads
|
|
98
216
|
walkScoped(root, {
|
|
99
217
|
onFunction: fn => !eagerCallbacks.has(fn),
|
|
100
218
|
onIdentifier: (id) => {
|
|
101
219
|
if (BROWSER_GLOBALS.has(id.name) && !shadowing.has(id.name)) {
|
|
102
|
-
|
|
220
|
+
if (!isNameGuarded(id.start, id.name))
|
|
221
|
+
reads.push({ name: id.name, start: id.start })
|
|
103
222
|
return
|
|
104
223
|
}
|
|
105
224
|
const helper = calleeStarts.has(id.start) ? helpers.get(id.name) : undefined
|
|
106
|
-
if (helper !== undefined)
|
|
107
|
-
|
|
225
|
+
if (helper !== undefined) {
|
|
226
|
+
for (const read of collect(helper)) {
|
|
227
|
+
if (!isNameGuarded(id.start, read.name))
|
|
228
|
+
reads.push(read)
|
|
229
|
+
}
|
|
230
|
+
}
|
|
108
231
|
},
|
|
109
232
|
})
|
|
110
233
|
return reads
|
|
@@ -121,7 +244,7 @@ export function checkBrowserGlobals(program: Program, code: string, filename: st
|
|
|
121
244
|
}
|
|
122
245
|
report(collect(program), 'when the module loads')
|
|
123
246
|
for (const [name, fn] of helpers) {
|
|
124
|
-
if (containsJsx(fn))
|
|
247
|
+
if (containsJsx(fn, jsxNodes))
|
|
125
248
|
report(collect(fn), `while <${name}> renders`)
|
|
126
249
|
}
|
|
127
250
|
}
|