prunelle 0.1.1 → 0.5.3

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 (120) hide show
  1. package/llms.txt +23 -19
  2. package/package.json +8 -2
  3. package/src/a11y/declaration.ts +335 -0
  4. package/src/a11y/index.ts +446 -0
  5. package/src/a11y/rgaa-map.ts +184 -0
  6. package/src/attrs.ts +11 -3
  7. package/src/build/bundle.ts +80 -36
  8. package/src/build/client-entries.ts +3 -2
  9. package/src/build/client-plugin.ts +8 -4
  10. package/src/build/client-prune.ts +56 -6
  11. package/src/build/compile.ts +11 -9
  12. package/src/build/ecoindex.ts +229 -0
  13. package/src/build/index.ts +174 -108
  14. package/src/build/islands.ts +2 -2
  15. package/src/build/manifest.ts +66 -5
  16. package/src/build/output.ts +2 -2
  17. package/src/build/plugin-host.ts +91 -33
  18. package/src/build/precompress.ts +15 -2
  19. package/src/build/prerender.ts +128 -46
  20. package/src/build/report.ts +23 -2
  21. package/src/build/resolve.ts +8 -6
  22. package/src/build/routes.ts +175 -10
  23. package/src/build/static.ts +137 -39
  24. package/src/build/status-pages.ts +127 -0
  25. package/src/build/style-bundles.ts +526 -0
  26. package/src/cli/check-forms.ts +561 -0
  27. package/src/cli/check-links.ts +632 -0
  28. package/src/cli/check.ts +75 -20
  29. package/src/cli/commands.ts +85 -4
  30. package/src/cli/docs.ts +402 -0
  31. package/src/cli/inspect.ts +103 -14
  32. package/src/cli/mcp-tools.ts +490 -0
  33. package/src/cli/mcp.ts +205 -0
  34. package/src/cli/new.ts +2 -2
  35. package/src/cli/render.ts +421 -0
  36. package/src/cli/upgrade-names.ts +73 -13
  37. package/src/cli/upgrade.ts +137 -16
  38. package/src/compiler/analyze.ts +42 -9
  39. package/src/compiler/browser-globals.ts +140 -17
  40. package/src/compiler/cacheability.ts +636 -0
  41. package/src/compiler/compliance-rgaa.ts +508 -0
  42. package/src/compiler/compliance.ts +241 -289
  43. package/src/compiler/data-keys.ts +199 -192
  44. package/src/compiler/dev-source.ts +87 -0
  45. package/src/compiler/dom-attrs.ts +138 -65
  46. package/src/compiler/dom.ts +517 -442
  47. package/src/compiler/expr-context.ts +167 -57
  48. package/src/compiler/mistakes.ts +4 -1
  49. package/src/compiler/props.ts +87 -36
  50. package/src/compiler/scope.ts +400 -333
  51. package/src/compiler/server-only.ts +190 -151
  52. package/src/compiler/shared.ts +40 -12
  53. package/src/compiler/signal-reads.ts +6 -2
  54. package/src/compiler/ssr-prunelle-tags.ts +27 -14
  55. package/src/compiler/ssr.ts +218 -87
  56. package/src/dev/error-log.ts +151 -0
  57. package/src/dev/index.ts +84 -34
  58. package/src/dev/open-editor.ts +204 -0
  59. package/src/dev/protocol.ts +1 -0
  60. package/src/dev/reload.ts +5 -3
  61. package/src/dev/supervisor.ts +42 -25
  62. package/src/dev/watchers.ts +47 -11
  63. package/src/dom/action.ts +43 -20
  64. package/src/dom/attrs.ts +15 -3
  65. package/src/dom/extras.ts +28 -12
  66. package/src/dom/index.ts +26 -8
  67. package/src/dom/island.ts +35 -28
  68. package/src/dom/loop.ts +10 -9
  69. package/src/plugins/config.ts +8 -7
  70. package/src/plugins/index.ts +2 -2
  71. package/src/plugins/load.ts +26 -7
  72. package/src/plugins/order.ts +38 -1
  73. package/src/plugins/public.ts +5 -2
  74. package/src/plugins/services.ts +62 -0
  75. package/src/plugins/types.ts +32 -0
  76. package/src/reactivity/graph.ts +10 -0
  77. package/src/reactivity/index.ts +24 -16
  78. package/src/reactivity/selector.ts +31 -4
  79. package/src/reactivity/store.ts +45 -13
  80. package/src/server/action-protect.ts +198 -0
  81. package/src/server/action-registry.ts +1 -1
  82. package/src/server/action.ts +82 -27
  83. package/src/server/assets.ts +34 -2
  84. package/src/server/compress.ts +91 -9
  85. package/src/server/document.ts +25 -10
  86. package/src/server/errors.ts +303 -65
  87. package/src/server/form-schema.ts +343 -0
  88. package/src/server/health.ts +244 -0
  89. package/src/server/http.ts +128 -31
  90. package/src/server/index.ts +230 -260
  91. package/src/server/loader.ts +5 -0
  92. package/src/server/param-matchers.ts +112 -0
  93. package/src/server/plugin-host.ts +123 -88
  94. package/src/server/public.ts +3 -3
  95. package/src/server/rate-limit.ts +58 -35
  96. package/src/server/request-body.ts +1 -1
  97. package/src/server/route-cache.ts +423 -0
  98. package/src/server/route-table.ts +339 -8
  99. package/src/server/router.ts +125 -40
  100. package/src/server/routing.ts +196 -63
  101. package/src/server/security.ts +208 -32
  102. package/src/server/serve-handlers.ts +21 -7
  103. package/src/server/start.ts +60 -16
  104. package/src/server/static-dir.ts +61 -8
  105. package/src/server/stream.ts +41 -22
  106. package/src/ssr/context.ts +85 -3
  107. package/src/ssr/index.ts +130 -3
  108. package/src/ssr/region.ts +31 -19
  109. package/src/test/browser.ts +28 -13
  110. package/src/test/index.ts +97 -12
  111. package/src/ts-plugin/diagnostics.ts +505 -0
  112. package/src/ts-plugin/fixes.ts +364 -0
  113. package/src/ts-plugin/index.ts +194 -0
  114. package/templates/_shared/AGENTS.md +2 -2
  115. package/templates/_shared/mcp.json +11 -0
  116. package/templates/_shared/src/routes/legal.tsx +5 -0
  117. package/templates/_shared/tsconfig.json +5 -0
  118. package/templates/blog/prunelle.config.ts +1 -1
  119. package/templates/blog/src/routes/index.tsx +4 -0
  120. package/ts-plugin.cjs +4277 -0
@@ -0,0 +1,364 @@
1
+ // Diagnostic codes and mechanical quick fixes for the Prunelle TypeScript language-service plugin.
2
+
3
+ import type { JSXAttribute, Node } from 'oxc-parser'
4
+ import type ts from 'typescript'
5
+ import { unwrapParens, unwrapTsExpr } from '../compiler/shared'
6
+
7
+ export const PRUNELLE_CODE = {
8
+ ERROR: 98001,
9
+ WARNING: 98002,
10
+ BARE_SIGNAL: 98101,
11
+ LOWERCASE_EVENT: 98102,
12
+ RENAMED_EVENT: 98103,
13
+ NAMESPACED_EVENT: 98104,
14
+ CLIENT_DIRECTIVE: 98105,
15
+ SET_HTML: 98106,
16
+ STRIP_NAMESPACE: 98107,
17
+ CAMEL_ARIA: 98108,
18
+ CAMEL_SVG: 98109,
19
+ REACT_DEFAULT: 98110,
20
+ BOOLEAN_FALSE_STRING: 98111,
21
+ DANGEROUSLY_SET_HTML: 98112,
22
+ INNER_HTML_OBJECT: 98113,
23
+ HANDLER_MUTATION: 98114,
24
+ DIRECTIVE: 98115,
25
+ NON_BUBBLING_FOCUS: 98116,
26
+ TEXT_INPUT_ONCHANGE: 98117,
27
+ KAY_IMPORT: 98118,
28
+ } as const
29
+
30
+ export const PRUNELLE_FIXABLE_CODES: readonly number[] = [
31
+ PRUNELLE_CODE.BARE_SIGNAL,
32
+ PRUNELLE_CODE.LOWERCASE_EVENT,
33
+ PRUNELLE_CODE.RENAMED_EVENT,
34
+ PRUNELLE_CODE.NAMESPACED_EVENT,
35
+ PRUNELLE_CODE.CLIENT_DIRECTIVE,
36
+ PRUNELLE_CODE.SET_HTML,
37
+ PRUNELLE_CODE.STRIP_NAMESPACE,
38
+ PRUNELLE_CODE.CAMEL_ARIA,
39
+ PRUNELLE_CODE.CAMEL_SVG,
40
+ PRUNELLE_CODE.REACT_DEFAULT,
41
+ PRUNELLE_CODE.BOOLEAN_FALSE_STRING,
42
+ PRUNELLE_CODE.DANGEROUSLY_SET_HTML,
43
+ PRUNELLE_CODE.INNER_HTML_OBJECT,
44
+ PRUNELLE_CODE.HANDLER_MUTATION,
45
+ PRUNELLE_CODE.DIRECTIVE,
46
+ PRUNELLE_CODE.NON_BUBBLING_FOCUS,
47
+ PRUNELLE_CODE.TEXT_INPUT_ONCHANGE,
48
+ PRUNELLE_CODE.KAY_IMPORT,
49
+ ]
50
+
51
+ export interface PrunelleFixDescriptor {
52
+ fixName: string
53
+ description: string
54
+ span: { start: number, length: number }
55
+ newText: string
56
+ }
57
+
58
+ export interface ClassifiedFix {
59
+ code: number
60
+ /** Highlight span for the diagnostic itself (may differ from edit span, e.g. handler mutation). */
61
+ diagSpan: { start: number, length: number }
62
+ fix: PrunelleFixDescriptor
63
+ }
64
+
65
+ const CLIENT_HYDRATE_MAP: Record<string, string> = {
66
+ load: 'load',
67
+ visible: 'visible',
68
+ idle: 'idle',
69
+ only: 'client',
70
+ }
71
+
72
+ const WRITE_PROP_RE = /^write ([a-zA-Z0-9_:-]+)=\{\.\.\.\}/
73
+ const WRITE_IMPORT_RE = /^write '([^']+)'/
74
+ const BUBBLING_FOCUS_RE = /focus does not bubble, write (onFocus(?:In|Out))=\{\.\.\.\}/
75
+
76
+ function findNode<T extends Node['type']>(nodes: readonly Node[], type: T): Extract<Node, { type: T }> | undefined {
77
+ for (const node of nodes) {
78
+ if (node.type === type)
79
+ return node as Extract<Node, { type: T }>
80
+ }
81
+ return undefined
82
+ }
83
+
84
+ function spanOf(start: number, end: number): { start: number, length: number } {
85
+ return { start, length: Math.max(1, end - start) }
86
+ }
87
+
88
+ function attrRenameFix(
89
+ code: number,
90
+ attr: JSXAttribute,
91
+ fromName: string,
92
+ toName: string,
93
+ fixName: string,
94
+ ): ClassifiedFix {
95
+ const span = spanOf(attr.name.start, attr.name.end)
96
+ return {
97
+ code,
98
+ diagSpan: span,
99
+ fix: { fixName, description: `Change '${fromName}' to '${toName}'`, span, newText: toName },
100
+ }
101
+ }
102
+
103
+ function extractHtmlObjectExpr(value: JSXAttribute['value'], source: string): string | undefined {
104
+ if (value?.type !== 'JSXExpressionContainer' || value.expression.type === 'JSXEmptyExpression')
105
+ return undefined
106
+ const obj = unwrapParens(value.expression)
107
+ if (obj.type !== 'ObjectExpression')
108
+ return undefined
109
+ const prop = obj.properties.find(p =>
110
+ p.type === 'Property'
111
+ && ((p.key.type === 'Identifier' && p.key.name === '__html')
112
+ || (p.key.type === 'Literal' && p.key.value === '__html')),
113
+ )
114
+ return prop?.type === 'Property' ? source.slice(prop.value.start, prop.value.end) : undefined
115
+ }
116
+
117
+ function staticLiteralString(value: JSXAttribute['value']): string | undefined {
118
+ if (value?.type === 'Literal' && typeof value.value === 'string')
119
+ return value.value
120
+ if (value?.type === 'JSXExpressionContainer' && value.expression.type !== 'JSXEmptyExpression') {
121
+ const inner = unwrapTsExpr(value.expression)
122
+ if (inner.type === 'Literal' && typeof inner.value === 'string')
123
+ return inner.value
124
+ }
125
+ return undefined
126
+ }
127
+
128
+ function directiveDeleteSpan(code: string, start: number, end: number): { start: number, length: number } {
129
+ let delStart = start
130
+ let lineStart = start
131
+ while (lineStart > 0 && code.charCodeAt(lineStart - 1) !== 10 && code.charCodeAt(lineStart - 1) !== 13)
132
+ lineStart--
133
+ if (/^[ \t]*$/.test(code.slice(lineStart, start)))
134
+ delStart = lineStart
135
+
136
+ let delEnd = end
137
+ while (delEnd < code.length && (code.charCodeAt(delEnd) === 32 || code.charCodeAt(delEnd) === 9))
138
+ delEnd++
139
+ if (code.charCodeAt(delEnd) === 13 && code.charCodeAt(delEnd + 1) === 10)
140
+ delEnd += 2
141
+ else if (code.charCodeAt(delEnd) === 10)
142
+ delEnd += 1
143
+ return { start: delStart, length: delEnd - delStart }
144
+ }
145
+
146
+ function classifyNamespacedAttrFix(attr: JSXAttribute, what: string, fixText: string): ClassifiedFix | undefined {
147
+ if (attr.name.type !== 'JSXNamespacedName')
148
+ return undefined
149
+ const ns = attr.name.namespace.name
150
+ const local = attr.name.name.name
151
+ if (ns === 'on') {
152
+ const match = WRITE_PROP_RE.exec(fixText)
153
+ return match !== null ? attrRenameFix(PRUNELLE_CODE.NAMESPACED_EVENT, attr, what, match[1]!, 'prunelle-namespaced-event') : undefined
154
+ }
155
+ if (ns === 'client') {
156
+ let mode = CLIENT_HYDRATE_MAP[local]
157
+ if (mode === undefined && local === 'media') {
158
+ const query = staticLiteralString(attr.value)
159
+ if (query !== undefined && query.trim() !== '')
160
+ mode = `media(${query})`
161
+ }
162
+ if (mode === undefined)
163
+ return undefined
164
+ const span = spanOf(attr.start, attr.end)
165
+ const newText = `$hydrate="${mode}"`
166
+ return {
167
+ code: PRUNELLE_CODE.CLIENT_DIRECTIVE,
168
+ diagSpan: span,
169
+ fix: { fixName: 'prunelle-client-directive', description: `Replace '${what}' with '${newText}'`, span, newText },
170
+ }
171
+ }
172
+ if (what === 'set:html')
173
+ return attrRenameFix(PRUNELLE_CODE.SET_HTML, attr, 'set:html', 'innerHTML', 'prunelle-set-html')
174
+ if (ns === 'prop' || ns === 'attr' || ns === 'bool')
175
+ return attrRenameFix(PRUNELLE_CODE.STRIP_NAMESPACE, attr, what, local, 'prunelle-strip-namespace')
176
+ return undefined
177
+ }
178
+
179
+ function classifyInnerHtmlFix(attr: JSXAttribute, what: string, code: string): ClassifiedFix | undefined {
180
+ const isDangerous = what === 'dangerouslySetInnerHTML'
181
+ if (!isDangerous && what !== 'innerHTML={{ __html }}')
182
+ return undefined
183
+ const expr = extractHtmlObjectExpr(attr.value, code)
184
+ if (expr === undefined)
185
+ return undefined
186
+ const span = spanOf(attr.start, attr.end)
187
+ const newText = `innerHTML={${expr}}`
188
+ return {
189
+ code: isDangerous ? PRUNELLE_CODE.DANGEROUSLY_SET_HTML : PRUNELLE_CODE.INNER_HTML_OBJECT,
190
+ diagSpan: span,
191
+ fix: {
192
+ fixName: isDangerous ? 'prunelle-dangerously-set-html' : 'prunelle-inner-html-object',
193
+ description: `Replace with '${newText}'`,
194
+ span,
195
+ newText,
196
+ },
197
+ }
198
+ }
199
+
200
+ function classifyJsxAttrMistake(attr: JSXAttribute, what: string, fixText: string, code: string): ClassifiedFix | undefined {
201
+ if (attr.name.type === 'JSXNamespacedName')
202
+ return classifyNamespacedAttrFix(attr, what, fixText)
203
+ const attrName = attr.name.name
204
+ const htmlFix = classifyInnerHtmlFix(attr, what, code)
205
+ if (htmlFix !== undefined)
206
+ return htmlFix
207
+ if (fixText.includes('prunelle binds camelCase event props only')) {
208
+ const match = WRITE_PROP_RE.exec(fixText)
209
+ return match !== null ? attrRenameFix(PRUNELLE_CODE.LOWERCASE_EVENT, attr, attrName, match[1]!, 'prunelle-lowercase-event') : undefined
210
+ }
211
+ if (fixText.includes('ARIA attributes use kebab-case')) {
212
+ const match = WRITE_PROP_RE.exec(fixText)
213
+ return match !== null ? attrRenameFix(PRUNELLE_CODE.CAMEL_ARIA, attr, attrName, match[1]!, 'prunelle-kebab-aria') : undefined
214
+ }
215
+ if (fixText.includes('SVG attributes are case-sensitive') || what === 'xlinkHref') {
216
+ const target = what === 'xlinkHref' ? 'xlink:href' : WRITE_PROP_RE.exec(fixText)?.[1]
217
+ return target !== undefined ? attrRenameFix(PRUNELLE_CODE.CAMEL_SVG, attr, attrName, target, 'prunelle-kebab-svg') : undefined
218
+ }
219
+ if (what === 'defaultValue' || what === 'defaultChecked') {
220
+ const target = what === 'defaultValue' ? 'value' : 'checked'
221
+ return attrRenameFix(PRUNELLE_CODE.REACT_DEFAULT, attr, attrName, target, 'prunelle-react-default')
222
+ }
223
+ if (fixText.includes('any string turns it on')) {
224
+ const span = spanOf(attr.start, attr.end)
225
+ const newText = `${attrName}={false}`
226
+ return {
227
+ code: PRUNELLE_CODE.BOOLEAN_FALSE_STRING,
228
+ diagSpan: span,
229
+ fix: { fixName: 'prunelle-boolean-false', description: `Change '${what}' to '${newText}'`, span, newText },
230
+ }
231
+ }
232
+ if (what.includes(' runs ') && what.endsWith(' at render') && attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
233
+ const inner = unwrapParens(attr.value.expression)
234
+ const exprSrc = code.slice(inner.start, inner.end)
235
+ const newText = `() => ${exprSrc}`
236
+ return {
237
+ code: PRUNELLE_CODE.HANDLER_MUTATION,
238
+ diagSpan: spanOf(attr.start, attr.end),
239
+ fix: {
240
+ fixName: 'prunelle-wrap-handler',
241
+ description: `Wrap handler in '${newText}'`,
242
+ span: spanOf(attr.value.expression.start, attr.value.expression.end),
243
+ newText,
244
+ },
245
+ }
246
+ }
247
+ const focusMatch = BUBBLING_FOCUS_RE.exec(fixText)
248
+ if (focusMatch !== null)
249
+ return attrRenameFix(PRUNELLE_CODE.NON_BUBBLING_FOCUS, attr, attrName, focusMatch[1]!, 'prunelle-bubbling-focus')
250
+ if (what === attrName && attrName.startsWith('on')) {
251
+ const match = WRITE_PROP_RE.exec(fixText)
252
+ if (match !== null && match[1]!.startsWith('on'))
253
+ return attrRenameFix(PRUNELLE_CODE.RENAMED_EVENT, attr, attrName, match[1]!, 'prunelle-renamed-event')
254
+ }
255
+ return undefined
256
+ }
257
+
258
+ /** Splits a standard `prunelle compiler: <what> in <file>: <fix>.` message. */
259
+ function parseMistakeParts(message: string, fileName: string): { what: string, fixText: string } | undefined {
260
+ const prefix = 'prunelle compiler: '
261
+ if (!message.startsWith(prefix))
262
+ return undefined
263
+ const marker = ` in ${fileName}: `
264
+ const idx = message.indexOf(marker, prefix.length)
265
+ if (idx === -1)
266
+ return undefined
267
+ const what = message.slice(prefix.length, idx)
268
+ const rawFix = message.slice(idx + marker.length)
269
+ const fixText = rawFix.endsWith('.') ? rawFix.slice(0, -1) : rawFix
270
+ return { what, fixText }
271
+ }
272
+
273
+ /** Classifies a compiler mistake and builds a mechanical quick fix when one exists. */
274
+ export function classifyMistakeFix(
275
+ message: string,
276
+ fileName: string,
277
+ nodesAtStart: readonly Node[],
278
+ code: string,
279
+ ): ClassifiedFix | undefined {
280
+ const parts = parseMistakeParts(message, fileName)
281
+ if (parts === undefined)
282
+ return undefined
283
+ const { what, fixText } = parts
284
+
285
+ if (what.endsWith(' without .value')) {
286
+ const name = what.slice(0, -' without .value'.length)
287
+ const node = name.includes('.') ? findNode(nodesAtStart, 'MemberExpression') : findNode(nodesAtStart, 'Identifier')
288
+ if (node === undefined)
289
+ return undefined
290
+ const span = spanOf(node.start, node.end)
291
+ const exprText = code.slice(node.start, node.end)
292
+ return {
293
+ code: PRUNELLE_CODE.BARE_SIGNAL,
294
+ diagSpan: span,
295
+ fix: { fixName: 'prunelle-add-signal-value', description: `Add '.value' to '${name}'`, span, newText: `${exprText}.value` },
296
+ }
297
+ }
298
+
299
+ if (what === "'use client'" || what === "'use server'") {
300
+ const stmt = findNode(nodesAtStart, 'ExpressionStatement')
301
+ if (stmt === undefined)
302
+ return undefined
303
+ return {
304
+ code: PRUNELLE_CODE.DIRECTIVE,
305
+ diagSpan: spanOf(stmt.start, stmt.end),
306
+ fix: {
307
+ fixName: 'prunelle-remove-directive',
308
+ description: `Remove ${what} directive`,
309
+ span: directiveDeleteSpan(code, stmt.start, stmt.end),
310
+ newText: '',
311
+ },
312
+ }
313
+ }
314
+
315
+ if (what.startsWith("import from '@kayjs/")) {
316
+ const lit = findNode(nodesAtStart, 'Literal')
317
+ const match = WRITE_IMPORT_RE.exec(fixText)
318
+ if (lit === undefined || match === null)
319
+ return undefined
320
+ const quote = code[lit.start] === '"' ? '"' : "'"
321
+ const replacement = match[1]!
322
+ const span = spanOf(lit.start, lit.end)
323
+ return {
324
+ code: PRUNELLE_CODE.KAY_IMPORT,
325
+ diagSpan: span,
326
+ fix: {
327
+ fixName: 'prunelle-rewrite-kay-import',
328
+ description: `Replace import with '${replacement}'`,
329
+ span,
330
+ newText: `${quote}${replacement}${quote}`,
331
+ },
332
+ }
333
+ }
334
+
335
+ const attr = findNode(nodesAtStart, 'JSXAttribute')
336
+ if (attr !== undefined)
337
+ return classifyJsxAttrMistake(attr, what, fixText, code)
338
+ return undefined
339
+ }
340
+
341
+ /** Classifies a compiler warning and builds a mechanical quick fix when one exists. */
342
+ export function classifyWarningFix(message: string, nodesAtStart: readonly Node[]): ClassifiedFix | undefined {
343
+ if (message.includes('fires when the field loses focus, not per keystroke; write onInput={...}')) {
344
+ const attr = findNode(nodesAtStart, 'JSXAttribute')
345
+ if (attr !== undefined)
346
+ return attrRenameFix(PRUNELLE_CODE.TEXT_INPUT_ONCHANGE, attr, 'onChange', 'onInput', 'prunelle-text-input-oninput')
347
+ }
348
+ return undefined
349
+ }
350
+
351
+ /** Converts a Prunelle quick fix descriptor into a TypeScript `CodeFixAction`. */
352
+ export function toCodeFixAction(fileName: string, fix: PrunelleFixDescriptor): ts.CodeFixAction {
353
+ return {
354
+ fixName: fix.fixName,
355
+ description: fix.description,
356
+ changes: [{
357
+ fileName,
358
+ textChanges: [{
359
+ span: { start: fix.span.start, length: fix.span.length },
360
+ newText: fix.newText,
361
+ }],
362
+ }],
363
+ }
364
+ }
@@ -0,0 +1,194 @@
1
+ // TypeScript Language Service plugin surfacing Prunelle compiler errors, compliance warnings, and quick fixes in editors.
2
+
3
+ import type ts from 'typescript'
4
+ import type { ComplianceConfig } from '../compiler/compliance'
5
+ import type { PrunelleDiagnosticItem, PrunellePluginOptions } from './diagnostics'
6
+ import { collectPrunelleDiagnostics, isCheckableFile, PRUNELLE_DIAG_SOURCE, toTsDiagnostic } from './diagnostics'
7
+ import { PRUNELLE_CODE, PRUNELLE_FIXABLE_CODES, toCodeFixAction } from './fixes'
8
+
9
+ const MAX_CACHE_ENTRIES = 200
10
+
11
+ interface CachedFileDiagnostics {
12
+ text: string
13
+ projectVersion: string
14
+ items: PrunelleDiagnosticItem[]
15
+ }
16
+
17
+ function readPluginConfig(raw: unknown): Pick<PrunellePluginOptions, 'compliance' | 'site'> {
18
+ if (raw === null || typeof raw !== 'object')
19
+ return {}
20
+ const rec = raw as Record<string, unknown>
21
+ const site = typeof rec.site === 'string' ? rec.site : undefined
22
+ const compliance = rec.compliance !== null && typeof rec.compliance === 'object'
23
+ ? (rec.compliance as ComplianceConfig)
24
+ : undefined
25
+ return { site, compliance }
26
+ }
27
+
28
+ // Advertises Prunelle's custom fixable diagnostic codes to tsserver/VS Code when TypeScript's supported-codes array is extensible.
29
+ function registerSupportedCodeFixes(tsLib: typeof ts): void {
30
+ if (typeof tsLib.getSupportedCodeFixes !== 'function')
31
+ return
32
+ try {
33
+ const supported = tsLib.getSupportedCodeFixes() as string[]
34
+ if (!Array.isArray(supported) || Object.isFrozen(supported))
35
+ return
36
+ const existing = new Set(supported)
37
+ for (const code of PRUNELLE_FIXABLE_CODES) {
38
+ const str = String(code)
39
+ if (!existing.has(str))
40
+ supported.push(str)
41
+ }
42
+ }
43
+ catch {
44
+ // Some TypeScript builds freeze the supported-codes array; editors still invoke getCodeFixesAtPosition directly.
45
+ return
46
+ }
47
+ }
48
+
49
+ function readFileSource(
50
+ prior: ts.LanguageService,
51
+ host: ts.LanguageServiceHost,
52
+ fileName: string,
53
+ ): { sourceFile: ts.SourceFile | undefined, text: string | undefined } {
54
+ const sourceFile = prior.getProgram()?.getSourceFile(fileName)
55
+ if (sourceFile !== undefined)
56
+ return { sourceFile, text: sourceFile.text }
57
+ const snapshot = host.getScriptSnapshot(fileName)
58
+ if (snapshot !== undefined)
59
+ return { sourceFile: undefined, text: snapshot.getText(0, snapshot.getLength()) }
60
+ return { sourceFile: undefined, text: undefined }
61
+ }
62
+
63
+ /** Direct helper for retrieving TypeScript `ts.Diagnostic`s for a single file. */
64
+ export function getPrunelleDiagnostics(
65
+ tsLib: typeof ts,
66
+ fileName: string,
67
+ code: string,
68
+ options: PrunellePluginOptions = {},
69
+ sourceFile?: ts.SourceFile,
70
+ ): ts.Diagnostic[] {
71
+ return collectPrunelleDiagnostics(fileName, code, options).map(item => toTsDiagnostic(tsLib, item, sourceFile))
72
+ }
73
+
74
+ /** Direct helper for retrieving TypeScript `ts.CodeFixAction`s overlapping `[start, end]`. */
75
+ export function getPrunelleCodeFixes(
76
+ fileName: string,
77
+ code: string,
78
+ start: number,
79
+ end: number,
80
+ errorCodes: readonly number[],
81
+ options: PrunellePluginOptions = {},
82
+ ): ts.CodeFixAction[] {
83
+ const items = collectPrunelleDiagnostics(fileName, code, options)
84
+ return selectCodeFixes(fileName, items, start, end, errorCodes)
85
+ }
86
+
87
+ function selectCodeFixes(
88
+ fileName: string,
89
+ items: readonly PrunelleDiagnosticItem[],
90
+ start: number,
91
+ end: number,
92
+ errorCodes: readonly number[],
93
+ ): ts.CodeFixAction[] {
94
+ const out: ts.CodeFixAction[] = []
95
+ for (const item of items) {
96
+ if (item.fix === undefined)
97
+ continue
98
+ if (errorCodes.length > 0 && !errorCodes.includes(item.code))
99
+ continue
100
+ const itemEnd = item.start + item.length
101
+ if (item.start <= end && start <= itemEnd)
102
+ out.push(toCodeFixAction(fileName, item.fix))
103
+ }
104
+ return out
105
+ }
106
+
107
+ /** Creates the wrapped `ts.LanguageService` for a TypeScript plugin instance. */
108
+ export function createPrunellePlugin(tsLib: typeof ts, info: ts.server.PluginCreateInfo): ts.LanguageService {
109
+ const prior = info.languageService
110
+ const log = (msg: string): void => {
111
+ info.project?.projectService?.logger?.info(`[prunelle/ts-plugin] ${msg}`)
112
+ }
113
+
114
+ registerSupportedCodeFixes(tsLib)
115
+
116
+ const cache = new Map<string, CachedFileDiagnostics>()
117
+ const pluginCfg = readPluginConfig(info.config)
118
+ const readFile = (path: string): string | undefined => readFileSource(prior, info.languageServiceHost, path).text
119
+ const getOptions = (): PrunellePluginOptions => ({
120
+ projectRoot: typeof info.project?.getCurrentDirectory === 'function' ? info.project.getCurrentDirectory() : undefined,
121
+ compliance: pluginCfg.compliance,
122
+ site: pluginCfg.site,
123
+ readFile,
124
+ })
125
+
126
+ const getItems = (fileName: string): { sourceFile: ts.SourceFile | undefined, items: PrunelleDiagnosticItem[] } => {
127
+ if (!isCheckableFile(fileName))
128
+ return { sourceFile: undefined, items: [] }
129
+ const { sourceFile, text } = readFileSource(prior, info.languageServiceHost, fileName)
130
+ if (text === undefined)
131
+ return { sourceFile, items: [] }
132
+ const projectVersion = typeof info.languageServiceHost.getProjectVersion === 'function'
133
+ ? info.languageServiceHost.getProjectVersion()
134
+ : ''
135
+ const cached = cache.get(fileName)
136
+ if (cached !== undefined && cached.text === text && cached.projectVersion === projectVersion)
137
+ return { sourceFile, items: cached.items }
138
+ try {
139
+ const items = collectPrunelleDiagnostics(fileName, text, getOptions())
140
+ if (cache.size >= MAX_CACHE_ENTRIES) {
141
+ const oldest = cache.keys().next().value
142
+ if (oldest !== undefined)
143
+ cache.delete(oldest)
144
+ }
145
+ cache.set(fileName, { text, projectVersion, items })
146
+ return { sourceFile, items }
147
+ }
148
+ catch (err) {
149
+ log(`analysis failed for ${fileName}: ${err instanceof Error ? err.message : String(err)}`)
150
+ return { sourceFile, items: [] }
151
+ }
152
+ }
153
+
154
+ const proxy = Object.create(null) as ts.LanguageService
155
+ for (const key of Object.keys(prior) as (keyof ts.LanguageService)[]) {
156
+ const member = prior[key]
157
+ if (typeof member === 'function') {
158
+ ;(proxy as unknown as Record<string, unknown>)[key] = (...args: unknown[]) =>
159
+ (member as (...a: unknown[]) => unknown).apply(prior, args)
160
+ }
161
+ }
162
+
163
+ proxy.getSemanticDiagnostics = (fileName) => {
164
+ const base = prior.getSemanticDiagnostics(fileName)
165
+ const { sourceFile, items } = getItems(fileName)
166
+ if (items.length === 0)
167
+ return base
168
+ return [...base, ...items.map(item => toTsDiagnostic(tsLib, item, sourceFile))]
169
+ }
170
+
171
+ proxy.getCodeFixesAtPosition = (fileName, start, end, errorCodes, formatOptions, preferences) => {
172
+ const base = prior.getCodeFixesAtPosition(fileName, start, end, errorCodes, formatOptions, preferences)
173
+ const { items } = getItems(fileName)
174
+ if (items.length === 0)
175
+ return base
176
+ const prunelleFixes = selectCodeFixes(fileName, items, start, end, errorCodes)
177
+ return prunelleFixes.length === 0 ? base : [...base, ...prunelleFixes]
178
+ }
179
+
180
+ return proxy
181
+ }
182
+
183
+ function init(modules: { typescript: typeof ts }): ts.server.PluginModule {
184
+ const tsLib = modules.typescript
185
+ return {
186
+ create(info: ts.server.PluginCreateInfo): ts.LanguageService {
187
+ return createPrunellePlugin(tsLib, info)
188
+ },
189
+ }
190
+ }
191
+
192
+ export default init
193
+ export type { PrunelleDiagnosticItem, PrunellePluginOptions }
194
+ export { collectPrunelleDiagnostics, PRUNELLE_CODE, PRUNELLE_DIAG_SOURCE, PRUNELLE_FIXABLE_CODES }
@@ -4,7 +4,7 @@ A prunelle app. Read `node_modules/prunelle/llms.txt` before writing code: it ho
4
4
 
5
5
  ## Workflow
6
6
 
7
- - **Verify changes**: run `bun run check` (`prunelle check` + TypeScript), `bun test` (in-process `testApp` from `prunelle/test` — no dev server needed), and `prunelle build --analyze` to inspect each route's JS weight and islands.
8
- - **Discover, don't guess**: use plugin commands when installed — `prunelle icons-find <words>` for icon names, `prunelle i18n-check` for translation keys, and `prunelle mcp-footprint` for MCP tool token budgets.
7
+ - **Verify changes**: run `bun run check` (`prunelle check` + TypeScript), `prunelle render <url>` to inspect a rendered response without starting a dev server, `bun test` (in-process `testApp` from `prunelle/test` — no dev server needed), and `prunelle build --analyze` to inspect each route's JS weight and islands.
8
+ - **Discover, don't guess**: use `prunelle docs <words> [--json]` to search installed `llms.txt` files, `.mcp.json` (`prunelle mcp`) for MCP tooling, and plugin commands when installed — `prunelle icons-find <words>` for icon names, `prunelle i18n-check` for translation keys, and `prunelle mcp-footprint` for MCP tool token budgets.
9
9
 
10
10
  @node_modules/prunelle/llms.txt
@@ -0,0 +1,11 @@
1
+ {
2
+ "mcpServers": {
3
+ "prunelle": {
4
+ "command": "bunx",
5
+ "args": [
6
+ "prunelle",
7
+ "mcp"
8
+ ]
9
+ }
10
+ }
11
+ }
@@ -1,6 +1,11 @@
1
+ import { Head } from 'prunelle'
2
+
1
3
  export default function LegalNotice() {
2
4
  return (
3
5
  <section>
6
+ <Head>
7
+ <title>Legal Notice</title>
8
+ </Head>
4
9
  <h1>Legal Notice</h1>
5
10
  <p>Publisher: __APP_NAME__</p>
6
11
  </section>
@@ -8,6 +8,11 @@
8
8
  "strict": true,
9
9
  "noEmit": true,
10
10
  "skipLibCheck": true,
11
+ "plugins": [
12
+ {
13
+ "name": "prunelle/ts-plugin"
14
+ }
15
+ ],
11
16
  "types": [
12
17
  "bun"
13
18
  ]
@@ -8,7 +8,7 @@ export default defineConfig({
8
8
  lang: 'en',
9
9
  plugins: [
10
10
  ui({ grammar: 'editorial' }),
11
- seo({ site: 'https://example.com', titleTemplate: '%s — __APP_NAME__' }),
11
+ seo({ site: 'https://example.com', titleTemplate: '%s — __APP_NAME__', description: 'Articles & notes from __APP_NAME__', image: '/og.png' }),
12
12
  content({
13
13
  feeds: [{
14
14
  path: '/rss.xml',
@@ -1,4 +1,5 @@
1
1
  import { Badge, Card, CardBody, CardFooter, CardHeader, Cluster, Stack } from '@prunellejs/ui'
2
+ import { Head } from 'prunelle'
2
3
  import { posts } from '../posts'
3
4
 
4
5
  export const prerender = true
@@ -7,6 +8,9 @@ export default async function BlogIndex() {
7
8
  const list = posts.all()
8
9
  return (
9
10
  <Stack gap="lg">
11
+ <Head>
12
+ <title>Articles</title>
13
+ </Head>
10
14
  <h1 class="page-title">Articles</h1>
11
15
  {list.map(post => (
12
16
  <Card key={post.slug}>