prunelle 0.1.1 → 0.5.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 (105) 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 +654 -0
  27. package/src/cli/check-links.ts +619 -0
  28. package/src/cli/check.ts +52 -5
  29. package/src/cli/commands.ts +86 -4
  30. package/src/cli/docs.ts +402 -0
  31. package/src/cli/inspect.ts +31 -2
  32. package/src/cli/mcp-tools.ts +480 -0
  33. package/src/cli/mcp.ts +205 -0
  34. package/src/cli/new.ts +2 -2
  35. package/src/cli/render.ts +261 -0
  36. package/src/cli/upgrade-names.ts +73 -13
  37. package/src/cli/upgrade.ts +137 -16
  38. package/src/compiler/analyze.ts +20 -6
  39. package/src/compiler/cacheability.ts +592 -0
  40. package/src/compiler/compliance-rgaa.ts +502 -0
  41. package/src/compiler/compliance.ts +120 -221
  42. package/src/compiler/dev-source.ts +87 -0
  43. package/src/compiler/dom-attrs.ts +138 -65
  44. package/src/compiler/dom.ts +12 -11
  45. package/src/compiler/expr-context.ts +167 -57
  46. package/src/compiler/props.ts +87 -36
  47. package/src/compiler/scope.ts +56 -17
  48. package/src/compiler/ssr-prunelle-tags.ts +27 -14
  49. package/src/compiler/ssr.ts +216 -84
  50. package/src/dev/error-log.ts +151 -0
  51. package/src/dev/index.ts +84 -34
  52. package/src/dev/open-editor.ts +204 -0
  53. package/src/dev/protocol.ts +1 -0
  54. package/src/dev/reload.ts +5 -3
  55. package/src/dev/supervisor.ts +42 -25
  56. package/src/dev/watchers.ts +47 -11
  57. package/src/dom/action.ts +4 -1
  58. package/src/dom/attrs.ts +15 -3
  59. package/src/dom/extras.ts +2 -2
  60. package/src/dom/island.ts +11 -10
  61. package/src/plugins/config.ts +4 -4
  62. package/src/plugins/index.ts +2 -2
  63. package/src/plugins/public.ts +5 -2
  64. package/src/plugins/services.ts +62 -0
  65. package/src/plugins/types.ts +32 -0
  66. package/src/server/action-protect.ts +198 -0
  67. package/src/server/action-registry.ts +1 -1
  68. package/src/server/action.ts +82 -27
  69. package/src/server/assets.ts +34 -2
  70. package/src/server/compress.ts +91 -9
  71. package/src/server/errors.ts +303 -65
  72. package/src/server/form-schema.ts +343 -0
  73. package/src/server/health.ts +244 -0
  74. package/src/server/http.ts +128 -31
  75. package/src/server/index.ts +230 -260
  76. package/src/server/loader.ts +5 -0
  77. package/src/server/param-matchers.ts +112 -0
  78. package/src/server/plugin-host.ts +123 -88
  79. package/src/server/public.ts +3 -3
  80. package/src/server/rate-limit.ts +58 -35
  81. package/src/server/request-body.ts +1 -1
  82. package/src/server/route-cache.ts +423 -0
  83. package/src/server/route-table.ts +339 -8
  84. package/src/server/router.ts +125 -40
  85. package/src/server/routing.ts +196 -63
  86. package/src/server/security.ts +208 -32
  87. package/src/server/serve-handlers.ts +21 -7
  88. package/src/server/start.ts +60 -16
  89. package/src/server/static-dir.ts +61 -8
  90. package/src/server/stream.ts +41 -22
  91. package/src/ssr/context.ts +85 -3
  92. package/src/ssr/index.ts +120 -1
  93. package/src/ssr/region.ts +31 -19
  94. package/src/test/browser.ts +6 -0
  95. package/src/test/index.ts +17 -3
  96. package/src/ts-plugin/diagnostics.ts +378 -0
  97. package/src/ts-plugin/fixes.ts +364 -0
  98. package/src/ts-plugin/index.ts +194 -0
  99. package/templates/_shared/AGENTS.md +2 -2
  100. package/templates/_shared/mcp.json +11 -0
  101. package/templates/_shared/src/routes/legal.tsx +5 -0
  102. package/templates/_shared/tsconfig.json +5 -0
  103. package/templates/blog/prunelle.config.ts +1 -1
  104. package/templates/blog/src/routes/index.tsx +4 -0
  105. package/ts-plugin.cjs +4277 -0
@@ -164,67 +164,151 @@ function emitDomExprAttr(
164
164
  return ''
165
165
  }
166
166
 
167
- /** Compiles an HTML/SVG element's attributes, returning the static attribute HTML fragment and pushing DOM ops. */
168
- export function compileElementAttributes(ctx: ElementAttrCompileContext): string {
169
- const { el, tag, elVar, scopeRewrites, ops, postChildOps, delegatedEvents, use, freshVar, compileExpression } = ctx
167
+ function emitInnerHtmlOp(ctx: ElementAttrCompileContext, attr: Extract<Node, { type: 'JSXAttribute' }>, targetOps: string[]): void {
168
+ const { elVar, scopeRewrites, use, compileExpression } = ctx
169
+ if (attr.value?.type === 'Literal') {
170
+ targetOps.push(`${use('setInnerHtml')}(${elVar}, ${JSON.stringify(String(attr.value.value))});`)
171
+ }
172
+ else if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
173
+ const expr = attr.value.expression
174
+ targetOps.push(wrapDomOp(`${use('setInnerHtml')}(${elVar}, ${compileExpression(expr, scopeRewrites)})`, isStaticDomExpr(expr, scopeRewrites), use))
175
+ }
176
+ }
177
+
178
+ function emitRefOp(ctx: ElementAttrCompileContext, expr: Node, targetOps: string[]): void {
179
+ const { elVar, scopeRewrites, compileExpression } = ctx
180
+ const refCode = compileExpression(expr, scopeRewrites)
181
+ const unwrapped = unwrapTsExpr(expr)
182
+ if (unwrapped.type === 'ArrowFunctionExpression' || unwrapped.type === 'FunctionExpression')
183
+ targetOps.push(`(${refCode})(${elVar});`)
184
+ else
185
+ targetOps.push(`(${refCode})?.(${elVar});`)
186
+ }
187
+
188
+ function scanSpreadSpecialIndices(attrs: readonly JSXElement['openingElement']['attributes'][number][]): {
189
+ lastSpreadIdx: number
190
+ lastInnerHtmlIdx: number
191
+ lastRefIdx: number
192
+ } {
193
+ let lastSpreadIdx = -1
194
+ let lastInnerHtmlIdx = -1
195
+ let lastRefIdx = -1
196
+ for (let i = 0; i < attrs.length; i++) {
197
+ const attr = attrs[i]!
198
+ if (attr.type === 'JSXSpreadAttribute') {
199
+ lastSpreadIdx = i
200
+ }
201
+ else {
202
+ const rawName = jsxTagName(attr.name)
203
+ if (rawName === 'innerHTML')
204
+ lastInnerHtmlIdx = i
205
+ else if (rawName === 'ref')
206
+ lastRefIdx = i
207
+ }
208
+ }
209
+ return { lastSpreadIdx, lastInnerHtmlIdx, lastRefIdx }
210
+ }
211
+
212
+ function compileSpreadAttributes(ctx: ElementAttrCompileContext, isSelect: boolean): string {
213
+ const { el, elVar, scopeRewrites, ops, postChildOps, use, freshVar, compileExpression } = ctx
170
214
  const attrs = el.openingElement.attributes
171
- const hasSpread = attrs.some(a => a.type === 'JSXSpreadAttribute')
172
- const isFormValueEl = tag === 'textarea' || tag === 'select'
173
- const isSelect = tag === 'select'
215
+ const entries: string[] = []
216
+ const preSpreadOps: string[] = []
217
+ let soleSpreadExpr: string | undefined
218
+ const { lastSpreadIdx, lastInnerHtmlIdx, lastRefIdx } = scanSpreadSpecialIndices(attrs)
174
219
 
175
- if (hasSpread) {
176
- // Plain object spread inside `spread()`'s reactive effect preserves JSX right-to-left precedence,
177
- // tracks reactive props/getters, avoids double-binding events, and avoids pulling in `mergeProps`.
178
- const entries: string[] = []
179
- let soleSpreadExpr: string | undefined
180
-
181
- for (const attr of attrs) {
182
- if (attr.type === 'JSXSpreadAttribute') {
183
- const argCode = compileExpression(attr.argument, scopeRewrites)
184
- if (attrs.length === 1)
185
- soleSpreadExpr = argCode
186
- entries.push(`...(${argCode})`)
187
- continue
220
+ let innerHtmlAttr: Extract<Node, { type: 'JSXAttribute' }> | undefined
221
+ let innerHtmlAfterSpread = false
222
+ let postSpreadRef: Node | undefined
223
+
224
+ for (let i = 0; i < attrs.length; i++) {
225
+ const attr = attrs[i]!
226
+ if (attr.type === 'JSXSpreadAttribute') {
227
+ const argCode = compileExpression(attr.argument, scopeRewrites)
228
+ if (attrs.length === 1)
229
+ soleSpreadExpr = argCode
230
+ entries.push(`...(${argCode})`)
231
+ continue
232
+ }
233
+ const rawName = jsxTagName(attr.name)
234
+ if (rawName === 'key' || rawName === '$hydrate')
235
+ continue
236
+
237
+ if (rawName === 'innerHTML') {
238
+ if (i === lastInnerHtmlIdx) {
239
+ innerHtmlAttr = attr
240
+ innerHtmlAfterSpread = i > lastSpreadIdx
188
241
  }
189
- const rawName = jsxTagName(attr.name)
190
- if (rawName === 'key' || rawName === '$hydrate')
191
- continue
242
+ continue
243
+ }
192
244
 
193
- if (rawName === 'innerHTML') {
194
- if (attr.value?.type === 'Literal') {
195
- ops.push(`${use('setInnerHtml')}(${elVar}, ${JSON.stringify(String(attr.value.value))});`)
245
+ if (rawName === 'ref') {
246
+ if (i !== lastRefIdx && lastRefIdx > lastSpreadIdx)
247
+ continue
248
+ if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
249
+ const expr = attr.value.expression
250
+ const unwrapped = unwrapTsExpr(expr)
251
+ const isInlineFn = unwrapped.type === 'ArrowFunctionExpression' || unwrapped.type === 'FunctionExpression'
252
+ if (i > lastSpreadIdx && (isInlineFn || isStaticPropExpr(expr, scopeRewrites))) {
253
+ entries.push('ref: undefined')
254
+ postSpreadRef = expr
196
255
  }
197
- else if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
198
- const expr = attr.value.expression
199
- ops.push(wrapDomOp(`${use('setInnerHtml')}(${elVar}, ${compileExpression(expr, scopeRewrites)})`, isStaticDomExpr(expr, scopeRewrites), use))
256
+ else if (isInlineFn) {
257
+ const refVar = freshVar('_r')
258
+ preSpreadOps.push(`const ${refVar} = ${compileExpression(expr, scopeRewrites)};`)
259
+ entries.push(`ref: ${refVar}`)
260
+ }
261
+ else {
262
+ entries.push(`ref: ${compileExpression(expr, scopeRewrites)}`)
200
263
  }
201
- continue
202
264
  }
203
-
204
- const name = rawName === 'className' ? 'class' : rawName === 'htmlFor' ? 'for' : rawName
205
- const isEvent = EVENT_ATTR.test(rawName)
206
- if (!isEvent && rawName !== 'ref' && isBlockedAttr(name))
207
- continue
208
-
209
- const propName = isEvent || rawName === 'ref' ? rawName : name
210
- const propKey = /^[A-Za-z_$][\w$]*$/.test(propName) ? propName : JSON.stringify(propName)
211
- if (attr.value === null)
212
- entries.push(`${propKey}: true`)
213
- else if (attr.value.type === 'Literal')
214
- entries.push(`${propKey}: ${JSON.stringify(attr.value.value)}`)
215
- else if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression')
216
- entries.push(`${propKey}: ${compileExpression(attr.value.expression, scopeRewrites)}`)
265
+ continue
217
266
  }
218
267
 
219
- const targetOps = isSelect ? postChildOps : ops
220
- if (soleSpreadExpr !== undefined)
221
- targetOps.push(`${use('spread')}(${elVar}, () => (${soleSpreadExpr}));`)
222
- else if (entries.length > 0)
223
- targetOps.push(`${use('spread')}(${elVar}, () => ({ ${entries.join(', ')} }));`)
224
- return ''
268
+ const name = rawName === 'className' ? 'class' : rawName === 'htmlFor' ? 'for' : rawName
269
+ const isEvent = EVENT_ATTR.test(rawName)
270
+ if (!isEvent && isBlockedAttr(name))
271
+ continue
272
+
273
+ const propName = isEvent ? rawName : name
274
+ const propKey = /^[A-Za-z_$][\w$]*$/.test(propName) ? propName : JSON.stringify(propName)
275
+ if (attr.value === null)
276
+ entries.push(`${propKey}: true`)
277
+ else if (attr.value.type === 'Literal')
278
+ entries.push(`${propKey}: ${JSON.stringify(attr.value.value)}`)
279
+ else if (attr.value.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression')
280
+ entries.push(`${propKey}: ${compileExpression(attr.value.expression, scopeRewrites)}`)
225
281
  }
226
282
 
283
+ const targetOps = isSelect ? postChildOps : ops
284
+ for (const op of preSpreadOps)
285
+ targetOps.push(op)
286
+ if (innerHtmlAttr !== undefined && !innerHtmlAfterSpread)
287
+ emitInnerHtmlOp(ctx, innerHtmlAttr, targetOps)
288
+ if (soleSpreadExpr !== undefined)
289
+ targetOps.push(`${use('spread')}(${elVar}, () => (${soleSpreadExpr}));`)
290
+ else if (entries.length > 0)
291
+ targetOps.push(`${use('spread')}(${elVar}, () => ({ ${entries.join(', ')} }));`)
292
+ if (innerHtmlAttr !== undefined && innerHtmlAfterSpread)
293
+ emitInnerHtmlOp(ctx, innerHtmlAttr, targetOps)
294
+ if (postSpreadRef !== undefined)
295
+ emitRefOp(ctx, postSpreadRef, targetOps)
296
+ return ''
297
+ }
298
+
299
+ /** Compiles an HTML/SVG element's attributes, returning the static attribute HTML fragment and pushing DOM ops. */
300
+ export function compileElementAttributes(ctx: ElementAttrCompileContext): string {
301
+ const { el, tag, elVar, scopeRewrites, ops, postChildOps, delegatedEvents, use, freshVar, compileExpression } = ctx
302
+ const attrs = el.openingElement.attributes
303
+ const hasSpread = attrs.some(a => a.type === 'JSXSpreadAttribute')
304
+ const isFormValueEl = tag === 'textarea' || tag === 'select'
305
+ const isSelect = tag === 'select'
306
+
307
+ if (hasSpread)
308
+ return compileSpreadAttributes(ctx, isSelect)
309
+
227
310
  let html = ''
311
+ let lastInnerHtml: Extract<Node, { type: 'JSXAttribute' }> | undefined
228
312
  for (const attr of attrs) {
229
313
  if (attr.type === 'JSXSpreadAttribute')
230
314
  continue
@@ -233,26 +317,13 @@ export function compileElementAttributes(ctx: ElementAttrCompileContext): string
233
317
  continue
234
318
 
235
319
  if (rawName === 'ref') {
236
- if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
237
- const expr = attr.value.expression
238
- const refCode = compileExpression(expr, scopeRewrites)
239
- const unwrapped = unwrapTsExpr(expr)
240
- if (unwrapped.type === 'ArrowFunctionExpression' || unwrapped.type === 'FunctionExpression')
241
- ops.push(`(${refCode})(${elVar});`)
242
- else
243
- ops.push(`(${refCode})?.(${elVar});`)
244
- }
320
+ if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression')
321
+ emitRefOp(ctx, attr.value.expression, ops)
245
322
  continue
246
323
  }
247
324
 
248
325
  if (rawName === 'innerHTML') {
249
- if (attr.value?.type === 'Literal') {
250
- ops.push(`${use('setInnerHtml')}(${elVar}, ${JSON.stringify(String(attr.value.value))});`)
251
- }
252
- else if (attr.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression') {
253
- const expr = attr.value.expression
254
- ops.push(wrapDomOp(`${use('setInnerHtml')}(${elVar}, ${compileExpression(expr, scopeRewrites)})`, isStaticDomExpr(expr, scopeRewrites), use))
255
- }
326
+ lastInnerHtml = attr
256
327
  continue
257
328
  }
258
329
 
@@ -305,6 +376,8 @@ export function compileElementAttributes(ctx: ElementAttrCompileContext): string
305
376
  html += emitDomExprAttr(ctx, name, attr.value.expression, isSelect)
306
377
  }
307
378
  }
379
+ if (lastInnerHtml !== undefined)
380
+ emitInnerHtmlOp(ctx, lastInnerHtml, ops)
308
381
  return html
309
382
  }
310
383
 
@@ -16,10 +16,11 @@ import type { HydrationMarker, MapCallMatch } from './shared'
16
16
 
17
17
  import { analyzeModule, loadDeclarations, parseTsx } from './analyze'
18
18
  import { walk } from './ast'
19
+ import { createDevSourceAttr } from './dev-source'
19
20
  import { collectStaticStyleNames, compileElementAttributes, compileRawTextChildren, formatSetTextOp, resolveComponentCallee } from './dom-attrs'
20
21
  import { emitWalkStatements, moduleHeader, SVG_CHILD_TAGS } from './emit'
21
22
  import { SourceEditor } from './patch'
22
- import { createPropCompiler, isStaticPropExpr, withoutBindings } from './props'
23
+ import { compileParamList, createPropCompiler, isStaticPropExpr, withoutBindings } from './props'
23
24
  import { extractBlockKeyExpr, extractKeyExpr, literalArrayConsts, rewriteFreeIdentifiers } from './scope'
24
25
  import {
25
26
  cleanJsxText,
@@ -31,6 +32,7 @@ import {
31
32
  isComponentTag,
32
33
  isJsxNode,
33
34
  jsxTagName,
35
+ matchJsxMapCall,
34
36
  planChildSlots,
35
37
  RAW_TEXT_ELEMENTS,
36
38
  resolvePrunelleTag,
@@ -39,6 +41,8 @@ import {
39
41
 
40
42
  export interface CompileDomOptions {
41
43
  analysis?: ModuleAnalysis | undefined
44
+ /** Project root for dev-only `data-pru-src="file:line:col"` attributes; omit in production. */
45
+ devSourceRoot?: string | undefined
42
46
  }
43
47
 
44
48
  export interface CompileDomResult {
@@ -58,6 +62,7 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
58
62
  const jsxNodes = analysis.jsxNodes
59
63
  const exprContext = createExprContext(program, jsxNodes)
60
64
  const staticStyleNames = collectStaticStyleNames(program, exprContext.primitiveVars)
65
+ const devSrc = createDevSourceAttr(filename, code, options.devSourceRoot)
61
66
 
62
67
  const editor = new SourceEditor(code)
63
68
  const used = new Set<string>()
@@ -156,7 +161,7 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
156
161
  else if (isJsxNode(child))
157
162
  currentObj.push(`get children() { return ${compileRootJsx(child, scopeRewrites)} }`)
158
163
  else if (child.type === 'JSXExpressionContainer' && child.expression.type !== 'JSXEmptyExpression')
159
- currentObj.push(`get children() { return ${compileExpression(child.expression, scopeRewrites)} }`)
164
+ currentObj.push(`get children() { return ${matchJsxMapCall(child.expression, jsxNodes) !== undefined ? compileFragmentChildren(node.children, scopeRewrites) : compileExpression(child.expression, scopeRewrites)} }`)
160
165
  }
161
166
  else if (meaningful.length > 1) {
162
167
  currentObj.push(`get children() { return ${compileFragmentChildren(node.children, scopeRewrites)} }`)
@@ -177,8 +182,6 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
177
182
  function compileLoopCall(
178
183
  match: MapCallMatch,
179
184
  scopeRewrites: ReadonlyMap<string, string>,
180
- parentExpr: string,
181
- anchorExpr: string,
182
185
  skipArg = '',
183
186
  ): { isStatic: boolean, call: (p: string, a: string) => string } {
184
187
  const eachCode = compileExpression(match.target, scopeRewrites)
@@ -194,7 +197,7 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
194
197
  // Fast path: a const array literal (e.g. `NAV.map(...)`) renders once through `eachStatic`.
195
198
  const isStaticArr = match.target.type === 'Identifier' && staticArrays.has(match.target.name) && !scopeRewrites.has(match.target.name)
196
199
  if (isStaticArr) {
197
- const rawParams = params.map(p => code.slice(p.start, p.end)).join(', ')
200
+ const rawParams = compileParamList(code, params, scopeRewrites, compileExpression)
198
201
  const shadowed = withoutBindings(scopeRewrites, params)
199
202
  const rowBody = match.isJsxBody ? compileRootJsx(match.body as JSXElement | JSXFragment, shadowed) : compileExpression(match.body, shadowed)
200
203
  return {
@@ -214,8 +217,6 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
214
217
  const rawRowBody = match.isJsxBody ? compileRootJsx(match.body as JSXElement | JSXFragment, rowRewrites) : compileExpression(match.body, rowRewrites)
215
218
  const rowBody = withPrelude(rawRowBody, match.body, prelude)
216
219
  const rowParams = idxSig !== undefined ? `${rowSig}, ${idxSig}` : rowSig
217
- void parentExpr
218
- void anchorExpr
219
220
  return {
220
221
  isStatic: false,
221
222
  call: (p, a) => `${use('loop')}(${p}, ${a}, () => ${eachCode}, ${keyFn}, (${rowParams}) => ${rowBody}${skipArg})`,
@@ -276,7 +277,7 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
276
277
  compileExpression,
277
278
  })
278
279
 
279
- let html = `<${tag}${attrHtml}>`
280
+ let html = `<${tag}${devSrc(el, tag)}${attrHtml}>`
280
281
  if (VOID_ELEMENTS.has(tag))
281
282
  return html
282
283
 
@@ -323,7 +324,7 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
323
324
  const prevSvgDepth = svgDepth
324
325
  if (tag === 'svg')
325
326
  svgDepth = 1
326
- else if (tag === 'foreignObject')
327
+ else if (tag === 'foreignObject' || tag === 'math')
327
328
  svgDepth = 0
328
329
 
329
330
  try {
@@ -372,7 +373,7 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
372
373
  if (slot.kind === 'loop') {
373
374
  const anchorVar = claimRangeAnchor(marker, open, close)
374
375
  const skipArg = marker === 'none' && domIndex > 0 ? `, ${domIndex}` : ''
375
- const compiled = compileLoopCall(slot.match, scopeRewrites, elVar, anchorVar, skipArg)
376
+ const compiled = compileLoopCall(slot.match, scopeRewrites, skipArg)
376
377
  ops.push(`${compiled.call(elVar, anchorVar)};`)
377
378
  continue
378
379
  }
@@ -489,7 +490,7 @@ export function compileDom(filename: string, code: string, options: CompileDomOp
489
490
  items.push(hasSiblings ? `${use('fragInsert')}(() => ${rendered})` : rendered)
490
491
  }
491
492
  else if (slot.kind === 'loop') {
492
- const compiled = compileLoopCall(slot.match, scopeRewrites, '_s.parentNode', '_s')
493
+ const compiled = compileLoopCall(slot.match, scopeRewrites)
493
494
  if (compiled.isStatic)
494
495
  items.push(`(() => { const _s = ${use('claimRange')}(); if (_s !== null) { ${compiled.call('_s.parentNode', '_s')}; return null; } const _f = document.createDocumentFragment(); ${compiled.call('_f', 'null')}; return _f; })()`)
495
496
  else
@@ -90,23 +90,77 @@ 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 registerTypedParamKeys(
151
+ name: string,
152
+ keys: Set<string>,
153
+ primitivePropKeys: Map<string, Set<string>>,
154
+ ambiguousPropObjects: Set<string>,
155
+ ): void {
156
+ if (ambiguousPropObjects.has(name))
157
+ return
158
+ if (primitivePropKeys.has(name)) {
159
+ primitivePropKeys.delete(name)
160
+ ambiguousPropObjects.add(name)
161
+ return
162
+ }
163
+ primitivePropKeys.set(name, keys)
110
164
  }
111
165
 
112
166
  function rootIdentifierName(node: Node): string | undefined {
@@ -139,6 +193,78 @@ function isSignalCall(node: Node): { kind: 'signal' | 'computed', call: CallExpr
139
193
  return undefined
140
194
  }
141
195
 
196
+ function resolveDeferredInits(
197
+ deferredInits: readonly { name: string, init: Node, isSignal: boolean, signalKind?: 'signal' | 'computed' }[],
198
+ primCandidates: Set<string>,
199
+ sigCandidates: Set<string>,
200
+ nonPrimNames: ReadonlySet<string>,
201
+ nonSigNames: ReadonlySet<string>,
202
+ ctx: ExprContext,
203
+ ): void {
204
+ for (let pass = 0; pass < 2; pass++) {
205
+ for (const item of deferredInits) {
206
+ if (item.isSignal) {
207
+ if (nonSigNames.has(item.name))
208
+ continue
209
+ const call = item.init as CallExpression
210
+ const arg = call.arguments[0]
211
+ if (item.signalKind === 'signal' && arg !== undefined && arg.type !== 'SpreadElement' && isDefinitelyPrimitive(arg, ctx)) {
212
+ sigCandidates.add(item.name)
213
+ }
214
+ else if (item.signalKind === 'computed' && arg !== undefined && (arg.type === 'ArrowFunctionExpression' || arg.type === 'FunctionExpression')) {
215
+ if (arg.body !== null && arg.body.type !== 'BlockStatement' && isDefinitelyPrimitive(arg.body, ctx))
216
+ sigCandidates.add(item.name)
217
+ }
218
+ }
219
+ else if (!nonPrimNames.has(item.name) && isDefinitelyPrimitive(item.init, ctx)) {
220
+ primCandidates.add(item.name)
221
+ }
222
+ }
223
+ }
224
+ }
225
+
226
+ interface ParamTypeState {
227
+ primCandidates: Set<string>
228
+ nonPrimNames: Set<string>
229
+ sigCandidates: Set<string>
230
+ nonSigNames: Set<string>
231
+ primitivePropKeys: Map<string, Set<string>>
232
+ ambiguousPropObjects: Set<string>
233
+ arrayElemPrimKeys: Map<string, Set<string>>
234
+ markNonPrim: (pat: Node | null | undefined) => void
235
+ }
236
+
237
+ function recordObjectPatternTypes(pat: ObjectPattern, typeNode: Node | null | undefined, state: ParamTypeState): void {
238
+ const info = extractTypeLiteralPrimitives(typeNode)
239
+ for (const prop of pat.properties) {
240
+ if (prop.type === 'RestElement') {
241
+ state.markNonPrim(prop.argument)
242
+ continue
243
+ }
244
+ const key = !prop.computed ? memberPropKey(prop.key) : undefined
245
+ const val = prop.value.type === 'AssignmentPattern' ? prop.value.left : prop.value
246
+ if (val.type === 'Identifier' && key !== undefined && info !== undefined) {
247
+ if (info.prims.has(key)) {
248
+ state.primCandidates.add(val.name)
249
+ state.nonSigNames.add(val.name)
250
+ continue
251
+ }
252
+ if (info.primSignals.has(key)) {
253
+ state.sigCandidates.add(val.name)
254
+ state.nonPrimNames.add(val.name)
255
+ continue
256
+ }
257
+ const elemPrims = info.arrayElemPrims.get(key)
258
+ if (elemPrims !== undefined)
259
+ state.arrayElemPrimKeys.set(val.name, elemPrims)
260
+ const nested = info.nestedPrims.get(key)
261
+ if (nested !== undefined)
262
+ registerTypedParamKeys(val.name, nested, state.primitivePropKeys, state.ambiguousPropObjects)
263
+ }
264
+ state.markNonPrim(val)
265
+ }
266
+ }
267
+
142
268
  /**
143
269
  * File-level static primitive/JSX binding analysis shared identically by SSR and DOM compilers
144
270
  * so `planChildSlots` chooses matching `setText` vs `insert` slots and markers on both targets.
@@ -150,6 +276,8 @@ export function createExprContext(program: Program, jsxNodes: WeakSet<Node>): Ex
150
276
  const nonSigNames = new Set<string>()
151
277
  const propsParams = new Set<string>()
152
278
  const primitivePropKeys = new Map<string, Set<string>>()
279
+ const ambiguousPropObjects = new Set<string>()
280
+ const arrayElemPrimKeys = new Map<string, Set<string>>()
153
281
  const jsxVars = new Set<string>()
154
282
  const deferredInits: { name: string, init: Node, isSignal: boolean, signalKind?: 'signal' | 'computed' }[] = []
155
283
 
@@ -160,35 +288,21 @@ export function createExprContext(program: Program, jsxNodes: WeakSet<Node>): Ex
160
288
  collectPatternNames(pat, nonSigNames)
161
289
  }
162
290
 
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
- }
291
+ const paramState: ParamTypeState = {
292
+ primCandidates,
293
+ nonPrimNames,
294
+ sigCandidates,
295
+ nonSigNames,
296
+ primitivePropKeys,
297
+ ambiguousPropObjects,
298
+ arrayElemPrimKeys,
299
+ markNonPrim,
186
300
  }
187
301
 
188
302
  const recordParam = (param: ParamPattern, isComponentFirstParam: boolean, isMapIndexParam: boolean): void => {
189
303
  const inner = param.type === 'AssignmentPattern' ? param.left : param
190
304
  if (inner.type === 'ObjectPattern') {
191
- recordObjectPatternTypes(inner, inner.typeAnnotation?.typeAnnotation)
305
+ recordObjectPatternTypes(inner, inner.typeAnnotation?.typeAnnotation, paramState)
192
306
  return
193
307
  }
194
308
  if (inner.type === 'Identifier') {
@@ -203,21 +317,30 @@ export function createExprContext(program: Program, jsxNodes: WeakSet<Node>): Ex
203
317
  nonPrimNames.add(inner.name)
204
318
  return
205
319
  }
206
- if (isComponentFirstParam) {
320
+ if (isComponentFirstParam)
207
321
  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
- }
322
+ const info = extractTypeLiteralPrimitives(ann)
323
+ if (info !== undefined)
324
+ registerTypedParamKeys(inner.name, info.prims, primitivePropKeys, ambiguousPropObjects)
325
+ const elemPrims = extractArrayElementPrimitives(ann)
326
+ if (elemPrims !== undefined)
327
+ arrayElemPrimKeys.set(inner.name, elemPrims)
212
328
  }
213
329
  markNonPrim(inner)
214
330
  }
215
331
 
216
332
  const mapCallbacks = new WeakSet<Node>()
333
+ const mapItemTargets: { targetName: string, paramName: string }[] = []
217
334
  walk(program, (node) => {
218
335
  const mapMatch = matchJsxMapCall(node, jsxNodes)
219
- if (mapMatch !== undefined)
336
+ if (mapMatch !== undefined) {
220
337
  mapCallbacks.add(mapMatch.cb)
338
+ const targetName = mapTargetArrayName(mapMatch.target)
339
+ const first = mapMatch.params[0]
340
+ const paramName = first?.type === 'Identifier' ? first.name : undefined
341
+ if (targetName !== undefined && paramName !== undefined)
342
+ mapItemTargets.push({ targetName, paramName })
343
+ }
221
344
  })
222
345
 
223
346
  walk(program, (node) => {
@@ -296,6 +419,12 @@ export function createExprContext(program: Program, jsxNodes: WeakSet<Node>): Ex
296
419
  }
297
420
  })
298
421
 
422
+ for (const { targetName, paramName } of mapItemTargets) {
423
+ const elemPrims = arrayElemPrimKeys.get(targetName)
424
+ if (elemPrims !== undefined)
425
+ registerTypedParamKeys(paramName, elemPrims, primitivePropKeys, ambiguousPropObjects)
426
+ }
427
+
299
428
  for (const n of nonPrimNames)
300
429
  primCandidates.delete(n)
301
430
  for (const n of nonSigNames)
@@ -309,26 +438,7 @@ export function createExprContext(program: Program, jsxNodes: WeakSet<Node>): Ex
309
438
  jsxVars,
310
439
  }
311
440
 
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
- }
441
+ resolveDeferredInits(deferredInits, primCandidates, sigCandidates, nonPrimNames, nonSigNames, ctx)
332
442
 
333
443
  for (const n of nonPrimNames)
334
444
  primCandidates.delete(n)
@@ -378,9 +488,9 @@ export function isDefinitelyPrimitive(node: Node, ctx?: ExprContext): boolean {
378
488
  const root = rootIdentifierName(cur.object)
379
489
  if (root !== undefined && ctx?.jsxVars.has(root) === true)
380
490
  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
491
+ if (cur.object.type === 'Identifier' && ctx?.primitivePropKeys.get(cur.object.name)?.has(prop) === true)
492
+ return true
493
+ return false
384
494
  }
385
495
  return false
386
496
  }