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,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
- const decl = (stmt.type === 'ExportNamedDeclaration' || stmt.type === 'ExportDefaultDeclaration') ? stmt.declaration : stmt
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
- /** 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. */
66
- export function checkBrowserGlobals(program: Program, code: string, filename: string, warnings: CompilerWarning[]): void {
67
- if (!MENTIONS_RE.test(code))
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
- const shadowing = topLevelNames(program)
70
- const helpers = topLevelFunctions(program)
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: number[] = []
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
- // Code that checks `typeof window` (or another browser global) is written for both sides, so none of its reads is reported.
87
- const isGuarded = (node: Node): boolean => guards.some(start => start >= node.start && start < node.end)
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
- reads.push({ name: id.name, start: id.start })
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
- reads.push(...collect(helper))
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
  }