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
@@ -90,23 +90,97 @@ function memberPropKey(node: Node): string | undefined {
90
90
  return undefined
91
91
  }
92
92
 
93
- function extractTypeLiteralPrimitives(typeNode: Node | null | undefined): { prims: Set<string>, primSignals: Set<string> } | undefined {
93
+ function extractTypeLiteralPrimitives(typeNode: Node | null | undefined): { prims: Set<string>, primSignals: Set<string>, arrayElemPrims: Map<string, Set<string>>, nestedPrims: Map<string, Set<string>> } | undefined {
94
94
  if (typeNode === null || typeNode === undefined || typeNode.type !== 'TSTypeLiteral')
95
95
  return undefined
96
96
  const prims = new Set<string>()
97
97
  const primSignals = new Set<string>()
98
+ const arrayElemPrims = new Map<string, Set<string>>()
99
+ const nestedPrims = new Map<string, Set<string>>()
98
100
  for (const m of typeNode.members) {
99
101
  if (m.type !== 'TSPropertySignature' || m.computed || m.typeAnnotation === null || m.typeAnnotation === undefined)
100
102
  continue
101
103
  const key = memberPropKey(m.key)
102
104
  if (key === undefined)
103
105
  continue
104
- if (isPrimitiveTypeNode(m.typeAnnotation.typeAnnotation))
106
+ const ann = m.typeAnnotation.typeAnnotation
107
+ if (isPrimitiveTypeNode(ann))
105
108
  prims.add(key)
106
- else if (isPrimitiveSignalTypeNode(m.typeAnnotation.typeAnnotation))
109
+ else if (isPrimitiveSignalTypeNode(ann))
107
110
  primSignals.add(key)
111
+ else if (ann.type === 'TSTypeLiteral') {
112
+ const nested = extractTypeLiteralPrimitives(ann)
113
+ if (nested !== undefined && nested.prims.size > 0)
114
+ nestedPrims.set(key, nested.prims)
115
+ }
116
+ else {
117
+ const elem = extractArrayElementPrimitives(ann)
118
+ if (elem !== undefined && elem.size > 0)
119
+ arrayElemPrims.set(key, elem)
120
+ }
108
121
  }
109
- return { prims, primSignals }
122
+ return { prims, primSignals, arrayElemPrims, nestedPrims }
123
+ }
124
+
125
+ function extractArrayElementPrimitives(typeNode: Node | null | undefined): Set<string> | undefined {
126
+ if (typeNode === null || typeNode === undefined)
127
+ return undefined
128
+ if (typeNode.type === 'TSParenthesizedType' || typeNode.type === 'TSTypeOperator')
129
+ return extractArrayElementPrimitives(typeNode.typeAnnotation)
130
+ if (typeNode.type === 'TSArrayType')
131
+ return extractTypeLiteralPrimitives(typeNode.elementType)?.prims
132
+ if (typeNode.type === 'TSTypeReference' && typeNode.typeName.type === 'Identifier') {
133
+ const name = typeNode.typeName.name
134
+ const params = typeNode.typeArguments?.params
135
+ if ((name === 'Array' || name === 'ReadonlyArray') && params !== undefined && params.length === 1)
136
+ return extractTypeLiteralPrimitives(params[0])?.prims
137
+ }
138
+ return undefined
139
+ }
140
+
141
+ function mapTargetArrayName(target: Node): string | undefined {
142
+ const cur = unwrapTsExpr(target)
143
+ if (cur.type === 'Identifier')
144
+ return cur.name
145
+ if (cur.type === 'MemberExpression' && !cur.computed && cur.object.type === 'Identifier' && cur.property.type === 'Identifier' && cur.property.name === 'value')
146
+ return cur.object.name
147
+ return undefined
148
+ }
149
+
150
+ function markAmbiguousPropObject(
151
+ name: string,
152
+ map: Map<string, Set<string>>,
153
+ ambiguous: Set<string>,
154
+ ): void {
155
+ map.delete(name)
156
+ ambiguous.add(name)
157
+ }
158
+
159
+ function registerTypedParamKeys(
160
+ name: string,
161
+ keys: Set<string>,
162
+ map: Map<string, Set<string>>,
163
+ ambiguous: Set<string>,
164
+ ): void {
165
+ if (ambiguous.has(name))
166
+ return
167
+ const prev = map.get(name)
168
+ if (prev !== undefined) {
169
+ const shared = new Set<string>()
170
+ for (const k of prev) {
171
+ if (keys.has(k))
172
+ shared.add(k)
173
+ }
174
+ if (shared.size > 0)
175
+ map.set(name, shared)
176
+ else
177
+ markAmbiguousPropObject(name, map, ambiguous)
178
+ return
179
+ }
180
+ if (keys.size > 0)
181
+ map.set(name, keys)
182
+ else
183
+ markAmbiguousPropObject(name, map, ambiguous)
110
184
  }
111
185
 
112
186
  function rootIdentifierName(node: Node): string | undefined {
@@ -139,6 +213,146 @@ function isSignalCall(node: Node): { kind: 'signal' | 'computed', call: CallExpr
139
213
  return undefined
140
214
  }
141
215
 
216
+ function resolveDeferredInits(
217
+ deferredInits: readonly { name: string, init: Node, isSignal: boolean, signalKind?: 'signal' | 'computed' }[],
218
+ primCandidates: Set<string>,
219
+ sigCandidates: Set<string>,
220
+ nonPrimNames: ReadonlySet<string>,
221
+ nonSigNames: ReadonlySet<string>,
222
+ ctx: ExprContext,
223
+ ): void {
224
+ for (let pass = 0; pass < 2; pass++) {
225
+ for (const item of deferredInits) {
226
+ if (item.isSignal) {
227
+ if (nonSigNames.has(item.name))
228
+ continue
229
+ const call = item.init as CallExpression
230
+ const arg = call.arguments[0]
231
+ if (item.signalKind === 'signal' && arg !== undefined && arg.type !== 'SpreadElement' && isDefinitelyPrimitive(arg, ctx)) {
232
+ sigCandidates.add(item.name)
233
+ }
234
+ else if (item.signalKind === 'computed' && arg !== undefined && (arg.type === 'ArrowFunctionExpression' || arg.type === 'FunctionExpression')) {
235
+ if (arg.body !== null && arg.body.type !== 'BlockStatement' && isDefinitelyPrimitive(arg.body, ctx))
236
+ sigCandidates.add(item.name)
237
+ }
238
+ }
239
+ else if (!nonPrimNames.has(item.name) && isDefinitelyPrimitive(item.init, ctx)) {
240
+ primCandidates.add(item.name)
241
+ }
242
+ }
243
+ }
244
+ }
245
+
246
+ interface ParamTypeState {
247
+ primCandidates: Set<string>
248
+ nonPrimNames: Set<string>
249
+ sigCandidates: Set<string>
250
+ nonSigNames: Set<string>
251
+ propsParams: Set<string>
252
+ primitivePropKeys: Map<string, Set<string>>
253
+ ambiguousPropObjects: Set<string>
254
+ arrayElemPrimKeys: Map<string, Set<string>>
255
+ ambiguousArrayObjects: Set<string>
256
+ markNonPrim: (pat: Node | null | undefined) => void
257
+ }
258
+
259
+ function markPatternUnknownObjects(pat: Node | null | undefined, state: ParamTypeState): void {
260
+ if (pat === null || pat === undefined)
261
+ return
262
+ const names = new Set<string>()
263
+ collectPatternNames(pat, names)
264
+ for (const name of names) {
265
+ markAmbiguousPropObject(name, state.primitivePropKeys, state.ambiguousPropObjects)
266
+ markAmbiguousPropObject(name, state.arrayElemPrimKeys, state.ambiguousArrayObjects)
267
+ }
268
+ }
269
+
270
+ function recordObjectPatternTypes(pat: ObjectPattern, typeNode: Node | null | undefined, state: ParamTypeState): void {
271
+ const info = extractTypeLiteralPrimitives(typeNode)
272
+ for (const prop of pat.properties) {
273
+ if (prop.type === 'RestElement') {
274
+ state.markNonPrim(prop.argument)
275
+ markPatternUnknownObjects(prop.argument, state)
276
+ continue
277
+ }
278
+ const key = !prop.computed ? memberPropKey(prop.key) : undefined
279
+ const val = prop.value.type === 'AssignmentPattern' ? prop.value.left : prop.value
280
+ if (val.type === 'Identifier' && key !== undefined && info !== undefined) {
281
+ if (info.prims.has(key)) {
282
+ state.primCandidates.add(val.name)
283
+ state.nonSigNames.add(val.name)
284
+ markPatternUnknownObjects(val, state)
285
+ continue
286
+ }
287
+ if (info.primSignals.has(key)) {
288
+ state.sigCandidates.add(val.name)
289
+ state.nonPrimNames.add(val.name)
290
+ markPatternUnknownObjects(val, state)
291
+ continue
292
+ }
293
+ const elemPrims = info.arrayElemPrims.get(key)
294
+ if (elemPrims !== undefined)
295
+ registerTypedParamKeys(val.name, elemPrims, state.arrayElemPrimKeys, state.ambiguousArrayObjects)
296
+ else
297
+ markAmbiguousPropObject(val.name, state.arrayElemPrimKeys, state.ambiguousArrayObjects)
298
+ const nested = info.nestedPrims.get(key)
299
+ if (nested !== undefined)
300
+ registerTypedParamKeys(val.name, nested, state.primitivePropKeys, state.ambiguousPropObjects)
301
+ else
302
+ markAmbiguousPropObject(val.name, state.primitivePropKeys, state.ambiguousPropObjects)
303
+ state.markNonPrim(val)
304
+ continue
305
+ }
306
+ state.markNonPrim(val)
307
+ markPatternUnknownObjects(val, state)
308
+ }
309
+ }
310
+
311
+ function recordParam(
312
+ param: ParamPattern,
313
+ isComponentFirstParam: boolean,
314
+ isMapItemParam: boolean,
315
+ isMapIndexParam: boolean,
316
+ state: ParamTypeState,
317
+ ): void {
318
+ const inner = param.type === 'AssignmentPattern' ? param.left : param
319
+ if (inner.type === 'ObjectPattern') {
320
+ recordObjectPatternTypes(inner, inner.typeAnnotation?.typeAnnotation, state)
321
+ return
322
+ }
323
+ if (inner.type === 'Identifier') {
324
+ const ann = inner.typeAnnotation?.typeAnnotation
325
+ if (isMapIndexParam || isPrimitiveTypeNode(ann)) {
326
+ state.primCandidates.add(inner.name)
327
+ state.nonSigNames.add(inner.name)
328
+ markPatternUnknownObjects(inner, state)
329
+ return
330
+ }
331
+ if (isPrimitiveSignalTypeNode(ann)) {
332
+ state.sigCandidates.add(inner.name)
333
+ state.nonPrimNames.add(inner.name)
334
+ markPatternUnknownObjects(inner, state)
335
+ return
336
+ }
337
+ if (isComponentFirstParam)
338
+ state.propsParams.add(inner.name)
339
+ const info = extractTypeLiteralPrimitives(ann)
340
+ if (info !== undefined)
341
+ registerTypedParamKeys(inner.name, info.prims, state.primitivePropKeys, state.ambiguousPropObjects)
342
+ else if (!isMapItemParam)
343
+ markAmbiguousPropObject(inner.name, state.primitivePropKeys, state.ambiguousPropObjects)
344
+ const elemPrims = extractArrayElementPrimitives(ann)
345
+ if (elemPrims !== undefined)
346
+ registerTypedParamKeys(inner.name, elemPrims, state.arrayElemPrimKeys, state.ambiguousArrayObjects)
347
+ else
348
+ markAmbiguousPropObject(inner.name, state.arrayElemPrimKeys, state.ambiguousArrayObjects)
349
+ state.markNonPrim(inner)
350
+ return
351
+ }
352
+ state.markNonPrim(inner)
353
+ markPatternUnknownObjects(inner, state)
354
+ }
355
+
142
356
  /**
143
357
  * File-level static primitive/JSX binding analysis shared identically by SSR and DOM compilers
144
358
  * so `planChildSlots` chooses matching `setText` vs `insert` slots and markers on both targets.
@@ -150,6 +364,9 @@ export function createExprContext(program: Program, jsxNodes: WeakSet<Node>): Ex
150
364
  const nonSigNames = new Set<string>()
151
365
  const propsParams = new Set<string>()
152
366
  const primitivePropKeys = new Map<string, Set<string>>()
367
+ const ambiguousPropObjects = new Set<string>()
368
+ const arrayElemPrimKeys = new Map<string, Set<string>>()
369
+ const ambiguousArrayObjects = new Set<string>()
153
370
  const jsxVars = new Set<string>()
154
371
  const deferredInits: { name: string, init: Node, isSignal: boolean, signalKind?: 'signal' | 'computed' }[] = []
155
372
 
@@ -160,69 +377,37 @@ export function createExprContext(program: Program, jsxNodes: WeakSet<Node>): Ex
160
377
  collectPatternNames(pat, nonSigNames)
161
378
  }
162
379
 
163
- const recordObjectPatternTypes = (pat: ObjectPattern, typeNode: Node | null | undefined): void => {
164
- const info = extractTypeLiteralPrimitives(typeNode)
165
- for (const prop of pat.properties) {
166
- if (prop.type === 'RestElement') {
167
- markNonPrim(prop.argument)
168
- continue
169
- }
170
- const key = !prop.computed ? memberPropKey(prop.key) : undefined
171
- const val = prop.value.type === 'AssignmentPattern' ? prop.value.left : prop.value
172
- if (val.type === 'Identifier' && key !== undefined && info !== undefined) {
173
- if (info.prims.has(key)) {
174
- primCandidates.add(val.name)
175
- nonSigNames.add(val.name)
176
- continue
177
- }
178
- if (info.primSignals.has(key)) {
179
- sigCandidates.add(val.name)
180
- nonPrimNames.add(val.name)
181
- continue
182
- }
183
- }
184
- markNonPrim(val)
185
- }
186
- }
187
-
188
- const recordParam = (param: ParamPattern, isComponentFirstParam: boolean, isMapIndexParam: boolean): void => {
189
- const inner = param.type === 'AssignmentPattern' ? param.left : param
190
- if (inner.type === 'ObjectPattern') {
191
- recordObjectPatternTypes(inner, inner.typeAnnotation?.typeAnnotation)
192
- return
193
- }
194
- if (inner.type === 'Identifier') {
195
- const ann = inner.typeAnnotation?.typeAnnotation
196
- if (isMapIndexParam || isPrimitiveTypeNode(ann)) {
197
- primCandidates.add(inner.name)
198
- nonSigNames.add(inner.name)
199
- return
200
- }
201
- if (isPrimitiveSignalTypeNode(ann)) {
202
- sigCandidates.add(inner.name)
203
- nonPrimNames.add(inner.name)
204
- return
205
- }
206
- if (isComponentFirstParam) {
207
- propsParams.add(inner.name)
208
- const info = extractTypeLiteralPrimitives(ann)
209
- if (info !== undefined && !primitivePropKeys.has(inner.name))
210
- primitivePropKeys.set(inner.name, info.prims)
211
- }
212
- }
213
- markNonPrim(inner)
380
+ const paramState: ParamTypeState = {
381
+ primCandidates,
382
+ nonPrimNames,
383
+ sigCandidates,
384
+ nonSigNames,
385
+ propsParams,
386
+ primitivePropKeys,
387
+ ambiguousPropObjects,
388
+ arrayElemPrimKeys,
389
+ ambiguousArrayObjects,
390
+ markNonPrim,
214
391
  }
215
392
 
216
393
  const mapCallbacks = new WeakSet<Node>()
394
+ const mapItemTargets = new Map<Node, { targetName: string, paramName: string }>()
217
395
  walk(program, (node) => {
218
396
  const mapMatch = matchJsxMapCall(node, jsxNodes)
219
- if (mapMatch !== undefined)
397
+ if (mapMatch !== undefined) {
220
398
  mapCallbacks.add(mapMatch.cb)
399
+ const targetName = mapTargetArrayName(mapMatch.target)
400
+ const first = mapMatch.params[0]
401
+ const paramName = first?.type === 'Identifier' && first.typeAnnotation == null ? first.name : undefined
402
+ if (targetName !== undefined && paramName !== undefined)
403
+ mapItemTargets.set(mapMatch.cb, { targetName, paramName })
404
+ }
221
405
  })
222
406
 
223
407
  walk(program, (node) => {
224
408
  if (node.type === 'VariableDeclaration') {
225
409
  for (const d of node.declarations) {
410
+ markPatternUnknownObjects(d.id, paramState)
226
411
  if (d.init !== null && containsJsx(d.init, jsxNodes))
227
412
  collectPatternNames(d.id, jsxVars)
228
413
  if (d.id.type === 'Identifier') {
@@ -259,19 +444,24 @@ export function createExprContext(program: Program, jsxNodes: WeakSet<Node>): Ex
259
444
  }
260
445
  if (node.type === 'ImportSpecifier' || node.type === 'ImportDefaultSpecifier' || node.type === 'ImportNamespaceSpecifier') {
261
446
  markNonPrim(node.local)
447
+ markPatternUnknownObjects(node.local, paramState)
262
448
  return
263
449
  }
264
- if (node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration')
450
+ if (node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration') {
265
451
  markNonPrim(node.id)
452
+ markPatternUnknownObjects(node.id, paramState)
453
+ }
266
454
  if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') {
267
455
  const isMapCb = mapCallbacks.has(node)
456
+ const isMapItemCb = mapItemTargets.has(node)
268
457
  const isComponent = !isMapCb && node.body !== null && containsJsx(node.body, jsxNodes)
269
458
  for (let i = 0; i < node.params.length; i++)
270
- recordParam(node.params[i]!, isComponent && i === 0, isMapCb && i === 1)
459
+ recordParam(node.params[i]!, isComponent && i === 0, isMapItemCb && i === 0, isMapCb && i === 1, paramState)
271
460
  return
272
461
  }
273
462
  if (node.type === 'CatchClause') {
274
463
  markNonPrim(node.param)
464
+ markPatternUnknownObjects(node.param, paramState)
275
465
  return
276
466
  }
277
467
  if (node.type === 'AssignmentExpression') {
@@ -296,6 +486,14 @@ export function createExprContext(program: Program, jsxNodes: WeakSet<Node>): Ex
296
486
  }
297
487
  })
298
488
 
489
+ for (const { targetName, paramName } of mapItemTargets.values()) {
490
+ const elemPrims = arrayElemPrimKeys.get(targetName)
491
+ if (elemPrims !== undefined)
492
+ registerTypedParamKeys(paramName, elemPrims, primitivePropKeys, ambiguousPropObjects)
493
+ else
494
+ markAmbiguousPropObject(paramName, primitivePropKeys, ambiguousPropObjects)
495
+ }
496
+
299
497
  for (const n of nonPrimNames)
300
498
  primCandidates.delete(n)
301
499
  for (const n of nonSigNames)
@@ -309,26 +507,7 @@ export function createExprContext(program: Program, jsxNodes: WeakSet<Node>): Ex
309
507
  jsxVars,
310
508
  }
311
509
 
312
- for (let pass = 0; pass < 2; pass++) {
313
- for (const item of deferredInits) {
314
- if (item.isSignal) {
315
- if (nonSigNames.has(item.name))
316
- continue
317
- const call = item.init as CallExpression
318
- const arg = call.arguments[0]
319
- if (item.signalKind === 'signal' && arg !== undefined && arg.type !== 'SpreadElement' && isDefinitelyPrimitive(arg, ctx)) {
320
- sigCandidates.add(item.name)
321
- }
322
- else if (item.signalKind === 'computed' && arg !== undefined && (arg.type === 'ArrowFunctionExpression' || arg.type === 'FunctionExpression')) {
323
- if (arg.body !== null && arg.body.type !== 'BlockStatement' && isDefinitelyPrimitive(arg.body, ctx))
324
- sigCandidates.add(item.name)
325
- }
326
- }
327
- else if (!nonPrimNames.has(item.name) && isDefinitelyPrimitive(item.init, ctx)) {
328
- primCandidates.add(item.name)
329
- }
330
- }
331
- }
510
+ resolveDeferredInits(deferredInits, primCandidates, sigCandidates, nonPrimNames, nonSigNames, ctx)
332
511
 
333
512
  for (const n of nonPrimNames)
334
513
  primCandidates.delete(n)
@@ -378,9 +557,9 @@ export function isDefinitelyPrimitive(node: Node, ctx?: ExprContext): boolean {
378
557
  const root = rootIdentifierName(cur.object)
379
558
  if (root !== undefined && ctx?.jsxVars.has(root) === true)
380
559
  return false
381
- if (cur.object.type === 'Identifier' && ctx?.propsParams.has(cur.object.name) === true)
382
- return ctx.primitivePropKeys.get(cur.object.name)?.has(prop) === true
383
- return true
560
+ if (cur.object.type === 'Identifier' && ctx?.primitivePropKeys.get(cur.object.name)?.has(prop) === true)
561
+ return true
562
+ return false
384
563
  }
385
564
  return false
386
565
  }
@@ -0,0 +1,88 @@
1
+ // Refuses a reactive primitive imported from another framework, with the prunelle name to write instead.
2
+
3
+ import { mistake } from './mistakes'
4
+
5
+ const REACT: Record<string, string> = {
6
+ useState: 'signal() from \'prunelle\' (count.value reads and writes it)',
7
+ useReducer: 'signal() from \'prunelle\' and a plain function that sets .value',
8
+ useEffect: 'effect() or onMount() from \'prunelle\'',
9
+ useLayoutEffect: 'effect() or onMount() from \'prunelle\'',
10
+ useMemo: 'computed() from \'prunelle\'',
11
+ useCallback: 'a plain function: a component runs once, so nothing is recreated',
12
+ useRef: 'a ref={el => ...} callback on the element, or a plain variable',
13
+ memo: 'nothing: a component runs once, so there is no re-render to skip',
14
+ useId: 'useId from \'prunelle\'',
15
+ useContext: 'useContext from \'prunelle\'',
16
+ createContext: 'createContext from \'prunelle\'',
17
+ Suspense: 'Suspense from \'prunelle\'',
18
+ Fragment: '<>...</>, which needs no import',
19
+ createRoot: 'nothing: prunelle hydrates islands itself',
20
+ hydrateRoot: 'nothing: prunelle hydrates islands itself',
21
+ render: 'nothing: prunelle renders pages and hydrates islands itself',
22
+ }
23
+
24
+ const SOLID: Record<string, string> = {
25
+ createSignal: 'signal() from \'prunelle\'',
26
+ createMemo: 'computed() from \'prunelle\'',
27
+ createEffect: 'effect() from \'prunelle\'',
28
+ createRenderEffect: 'effect() from \'prunelle\'',
29
+ createComputed: 'computed() from \'prunelle\'',
30
+ createRoot: 'root() from \'prunelle\'',
31
+ createSelector: 'selector() from \'prunelle\'',
32
+ createStore: 'store() from \'prunelle\'',
33
+ createContext: 'createContext from \'prunelle\'',
34
+ useContext: 'useContext from \'prunelle\'',
35
+ createUniqueId: 'useId from \'prunelle\'',
36
+ batch: 'batch from \'prunelle\'',
37
+ untrack: 'untrack from \'prunelle\'',
38
+ onMount: 'onMount from \'prunelle\'',
39
+ onCleanup: 'onCleanup from \'prunelle\'',
40
+ Show: 'a ternary or && in the JSX',
41
+ For: 'items.map(item => ...) in the JSX',
42
+ Index: 'items.map(item => ...) in the JSX',
43
+ Suspense: 'Suspense from \'prunelle\'',
44
+ }
45
+
46
+ const VUE: Record<string, string> = {
47
+ ref: 'signal() from \'prunelle\'',
48
+ shallowRef: 'signal() from \'prunelle\'',
49
+ reactive: 'store() from \'prunelle\'',
50
+ computed: 'computed from \'prunelle\'',
51
+ watch: 'effect() from \'prunelle\'',
52
+ watchEffect: 'effect() from \'prunelle\'',
53
+ onMounted: 'onMount() from \'prunelle\'',
54
+ onUnmounted: 'onCleanup() from \'prunelle\'',
55
+ provide: 'provideContext from \'prunelle\'',
56
+ inject: 'useContext from \'prunelle\'',
57
+ }
58
+
59
+ const SVELTE: Record<string, string> = {
60
+ onMount: 'onMount from \'prunelle\'',
61
+ onDestroy: 'onCleanup from \'prunelle\'',
62
+ writable: 'signal() from \'prunelle\'',
63
+ readable: 'signal() from \'prunelle\'',
64
+ derived: 'computed() from \'prunelle\'',
65
+ setContext: 'provideContext from \'prunelle\'',
66
+ getContext: 'useContext from \'prunelle\'',
67
+ }
68
+
69
+ const FOREIGN = new Map<string, Record<string, string>>([
70
+ ['react', REACT],
71
+ ['preact', REACT],
72
+ ['preact/hooks', REACT],
73
+ ['preact/compat', REACT],
74
+ ['react-dom', REACT],
75
+ ['react-dom/client', REACT],
76
+ ['solid-js', SOLID],
77
+ ['solid-js/store', SOLID],
78
+ ['vue', VUE],
79
+ ['svelte', SVELTE],
80
+ ['svelte/store', SVELTE],
81
+ ])
82
+
83
+ /** Throws on a name a React, Solid, Vue or Svelte app imports for what prunelle spells differently, naming the prunelle form. */
84
+ export function assertNoForeignImport(source: string, imported: string, filename: string): void {
85
+ const table = FOREIGN.get(source)
86
+ if (table !== undefined && Object.hasOwn(table, imported))
87
+ throw mistake(`import { ${imported} } from '${source}'`, filename, `prunelle has no ${source}; write ${table[imported]}`)
88
+ }
@@ -66,6 +66,13 @@ export function mistake(what: string, filename: string, fix: string): PrunelleMi
66
66
  return new PrunelleMistake(`prunelle compiler: ${what} in ${filename}: ${fix}.`)
67
67
  }
68
68
 
69
+ /** `$hydrate` where nothing can hydrate: on an element (`component` false) or on a component that is not an island. The one wording the compiler, the build and `prunelle check` share. */
70
+ export function hydrateMistake(tag: string, filename: string, component: boolean): PrunelleMistake {
71
+ return mistake(`$hydrate on <${tag}>`, filename, component
72
+ ? `${tag} is not an island, so it never hydrates; drop $hydrate, or give ${tag} state or handlers`
73
+ : 'only components can be islands; drop $hydrate, or move it onto a component tag')
74
+ }
75
+
69
76
  let collected: PrunelleMistake[] | undefined
70
77
 
71
78
  /** Runs one check, a mistake it throws placed at `start`. The build stops at the first mistake; while `collectMistakes` runs, the mistake is recorded and the caller goes on. */
@@ -112,6 +119,13 @@ export function serverOnlyTagError(filename: string, { tag, island, reason, offs
112
119
  return err
113
120
  }
114
121
 
122
+ /** The error for an Astro `---` block opening a file, at the first dash. */
123
+ export function frontmatterMistake(filename: string, start: number): PrunelleMistake {
124
+ const err = mistake('a --- frontmatter block', filename, 'prunelle files are plain TypeScript with no --- fence; write that code above the component, or inside it, and delete both --- lines')
125
+ err.start = start
126
+ return err
127
+ }
128
+
115
129
  const KAY_SOURCE_RE = /^@kayjs\/(kay(?=\/|$))?/
116
130
 
117
131
  /** Refuses an import of kay, the name prunelle had before: the app would load both. */
@@ -289,7 +303,7 @@ function checkElementAttribute(attr: JSXAttribute, tag: string, isCustomElement:
289
303
  return
290
304
  const name = attr.name.name
291
305
  if (name === '$hydrate')
292
- throw mistake(`$hydrate on <${tag}>`, filename, 'only components can be islands; drop $hydrate, or move it onto a component tag')
306
+ throw hydrateMistake(tag, filename, false)
293
307
  if (name.toLowerCase() === 'srcdoc')
294
308
  throw mistake(`${name} on <${tag}>`, filename, 'prunelle blocks srcdoc for XSS safety; serve the document from a route and load it with src')
295
309
  if (name === 'dangerouslySetInnerHTML')
@@ -373,6 +387,10 @@ export function checkMistakes(
373
387
  const canonical = resolveTag(tag)
374
388
  const isElement = tag !== '' && !isComponentTag(tag)
375
389
  const isCustomElement = tag.includes('-')
390
+ if (tag === 'slot') {
391
+ const named = staticAttr(element.openingElement.attributes, 'name') !== undefined
392
+ guarded(element.start, () => { throw mistake('<slot>', filename, named ? 'prunelle has no slots, pass the named part as its own prop, <Card footer={<p />} />, and write {footer} where it goes' : 'prunelle has no slots, take children as a prop, function Card({ children }) { return <div>{children}</div> }, and write {children} where the content goes') })
393
+ }
376
394
  if (canonical === 'Suspense') {
377
395
  const children = filterMeaningfulChildren(element.children).length
378
396
  if (children > 1)
@@ -382,8 +400,11 @@ export function checkMistakes(
382
400
  guarded(element.start, () => checkRegionMarkup(tag, canonical, isElement, attributes, filename, element, resolveTag))
383
401
  if (isElement)
384
402
  guarded(element.start, () => checkKeepScroll(element, attributes, filename))
385
- if (isElement && VOID_ELEMENTS.has(tag) && (filterMeaningfulChildren(element.children).length > 0 || staticAttr(attributes, 'children') !== undefined))
403
+ const childrenAttr = isElement ? staticAttr(attributes, 'children') : undefined
404
+ if (isElement && VOID_ELEMENTS.has(tag) && (filterMeaningfulChildren(element.children).length > 0 || childrenAttr !== undefined))
386
405
  guarded(element.start, () => { throw mistake(`<${tag}> with children`, filename, `a void element has no content; write <${tag} />`) })
406
+ else if (childrenAttr !== undefined)
407
+ guarded(childrenAttr.start, () => { throw mistake(`children attribute on <${tag}>`, filename, `pass children between tags, <${tag}>...</${tag}>`) })
387
408
  const className = isElement && staticAttr(attributes, 'class') !== undefined ? staticAttr(attributes, 'className') : undefined
388
409
  if (className !== undefined)
389
410
  guarded(className.start, () => { throw mistake('class and className', filename, 'keep one, they set the same attribute') })
@@ -0,0 +1,60 @@
1
+ // Warns on `const { a } = props` in a component that runs in the browser: only a destructured parameter is rewritten to stay reactive, and a read of stable objects in the body can be meant.
2
+
3
+ import type { Function as FunctionNode, Node, Program } from 'oxc-parser'
4
+ import type { CompilerWarning } from './mistakes'
5
+ import { containsJsx, unwrapTsExpr } from './shared'
6
+
7
+ function isFunction(node: Node | null | undefined): node is FunctionNode {
8
+ return node !== null && node !== undefined && (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression')
9
+ }
10
+
11
+ // The functions a top-level statement declares: `function C`, `export default function`, `const C = () => ...`.
12
+ function declaredFunctions(stmt: Node): FunctionNode[] {
13
+ const decl = stmt.type === 'ExportNamedDeclaration' || stmt.type === 'ExportDefaultDeclaration' ? stmt.declaration : stmt
14
+ if (decl === null || decl === undefined)
15
+ return []
16
+ if (isFunction(decl))
17
+ return [decl]
18
+ if (decl.type !== 'VariableDeclaration')
19
+ return []
20
+ const found: FunctionNode[] = []
21
+ for (const d of decl.declarations) {
22
+ const init = d.init === null ? undefined : unwrapTsExpr(d.init)
23
+ if (isFunction(init))
24
+ found.push(init)
25
+ }
26
+ return found
27
+ }
28
+
29
+ /** Warns on a top-level statement of a browser component's body that destructures its first parameter, with the parameter form as the fix. */
30
+ export function checkBodyPropsDestructure(
31
+ program: Program,
32
+ code: string,
33
+ browserSpans: readonly [number, number][],
34
+ jsxNodes: WeakSet<Node>,
35
+ filename: string,
36
+ warnings: CompilerWarning[],
37
+ ): void {
38
+ if (browserSpans.length === 0)
39
+ return
40
+ for (const stmt of program.body) {
41
+ for (const fn of declaredFunctions(stmt)) {
42
+ if (!browserSpans.some(([s, e]) => fn.start >= s && fn.end <= e))
43
+ continue
44
+ const rawParam = fn.params[0]
45
+ const param = rawParam?.type === 'AssignmentPattern' ? rawParam.left : rawParam
46
+ if (param?.type !== 'Identifier' || fn.body?.type !== 'BlockStatement' || !containsJsx(fn.body, jsxNodes))
47
+ continue
48
+ for (const inner of fn.body.body) {
49
+ if (inner.type !== 'VariableDeclaration')
50
+ continue
51
+ for (const d of inner.declarations) {
52
+ const init = d.init === null ? undefined : unwrapTsExpr(d.init)
53
+ if (d.id.type !== 'ObjectPattern' || init?.type !== 'Identifier' || init.name !== param.name)
54
+ continue
55
+ warnings.push({ start: d.start, message: `prunelle compiler: \`${inner.kind} ${code.slice(d.start, d.end)}\` in ${filename} reads the props once, so a change to them never reaches this component: destructure in the parameter list, (${code.slice(d.id.start, d.id.end)}), which stays reactive, or read props.<name> where it is used.` })
56
+ }
57
+ }
58
+ }
59
+ }
60
+ }