@barefootjs/jsx 0.38.0 → 0.39.1

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/dist/action-accessor.d.ts +159 -0
  2. package/dist/action-accessor.d.ts.map +1 -0
  3. package/dist/adapters/interface.d.ts +19 -0
  4. package/dist/adapters/interface.d.ts.map +1 -1
  5. package/dist/adapters/jsx-adapter.d.ts.map +1 -1
  6. package/dist/analyzer.d.ts +49 -1
  7. package/dist/analyzer.d.ts.map +1 -1
  8. package/dist/async-action-refusal.d.ts +45 -0
  9. package/dist/async-action-refusal.d.ts.map +1 -0
  10. package/dist/compiler.d.ts.map +1 -1
  11. package/dist/debug.d.ts +61 -5
  12. package/dist/debug.d.ts.map +1 -1
  13. package/dist/errors.d.ts +4 -0
  14. package/dist/errors.d.ts.map +1 -1
  15. package/dist/format-date-refusal.d.ts +2 -6
  16. package/dist/format-date-refusal.d.ts.map +1 -1
  17. package/dist/html-types.d.ts +5 -0
  18. package/dist/html-types.d.ts.map +1 -1
  19. package/dist/index.d.ts +3 -2
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +1261 -422
  22. package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
  23. package/dist/ir-to-client-js/collect-elements.d.ts +17 -1
  24. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  25. package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
  26. package/dist/ir-to-client-js/compute-scope.d.ts.map +1 -1
  27. package/dist/ir-to-client-js/control-flow/plan/insert.d.ts +2 -0
  28. package/dist/ir-to-client-js/control-flow/plan/insert.d.ts.map +1 -1
  29. package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
  30. package/dist/ir-to-client-js/declaration-sort.d.ts +2 -1
  31. package/dist/ir-to-client-js/declaration-sort.d.ts.map +1 -1
  32. package/dist/ir-to-client-js/emit-module-level.d.ts.map +1 -1
  33. package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
  34. package/dist/ir-to-client-js/plan/declaration-emit.d.ts +7 -5
  35. package/dist/ir-to-client-js/plan/declaration-emit.d.ts.map +1 -1
  36. package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
  37. package/dist/ir-to-client-js/reactivity.d.ts +10 -2
  38. package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
  39. package/dist/ir-to-client-js/types.d.ts +4 -0
  40. package/dist/ir-to-client-js/types.d.ts.map +1 -1
  41. package/dist/ir-to-client-js/utils.d.ts +52 -9
  42. package/dist/ir-to-client-js/utils.d.ts.map +1 -1
  43. package/dist/jsx-to-ir.d.ts.map +1 -1
  44. package/dist/signal-initializer.d.ts +24 -0
  45. package/dist/signal-initializer.d.ts.map +1 -0
  46. package/dist/ssr-defaults.d.ts.map +1 -1
  47. package/dist/ssr-seed-plan.d.ts.map +1 -1
  48. package/dist/template-position-walk.d.ts +68 -0
  49. package/dist/template-position-walk.d.ts.map +1 -0
  50. package/dist/types.d.ts +95 -0
  51. package/dist/types.d.ts.map +1 -1
  52. package/package.json +2 -2
  53. package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +194 -4
  54. package/src/__tests__/action-accessor.test.ts +166 -0
  55. package/src/__tests__/adapter-output.test.ts +142 -0
  56. package/src/__tests__/create-mutation.test.ts +250 -0
  57. package/src/__tests__/create-query.test.ts +696 -0
  58. package/src/__tests__/csr-fragment-root-scope-shape.test.ts +156 -0
  59. package/src/__tests__/debug.test.ts +305 -0
  60. package/src/__tests__/doc-examples.test.ts +66 -3
  61. package/src/__tests__/ir-fragment-transparency.test.ts +23 -0
  62. package/src/__tests__/issue-3240-nested-static-map-event-delegation.test.ts +245 -0
  63. package/src/__tests__/jsx-text-linebreak-whitespace.test.ts +80 -0
  64. package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +445 -0
  65. package/src/__tests__/props-destructuring.test.ts +79 -4
  66. package/src/__tests__/reactive-factory-arity.test.ts +311 -0
  67. package/src/__tests__/ssr-portal-ref-callback.test.ts +5 -3
  68. package/src/action-accessor.ts +289 -0
  69. package/src/adapters/interface.ts +19 -0
  70. package/src/adapters/jsx-adapter.ts +47 -20
  71. package/src/analyzer.ts +593 -44
  72. package/src/async-action-refusal.ts +254 -0
  73. package/src/compiler.ts +81 -46
  74. package/src/debug.ts +351 -104
  75. package/src/errors.ts +39 -0
  76. package/src/format-date-refusal.ts +6 -125
  77. package/src/html-types.ts +14 -0
  78. package/src/index.ts +3 -1
  79. package/src/ir-to-client-js/build-references.ts +6 -2
  80. package/src/ir-to-client-js/collect-elements.ts +65 -4
  81. package/src/ir-to-client-js/compute-inlinability.ts +2 -1
  82. package/src/ir-to-client-js/compute-scope.ts +3 -1
  83. package/src/ir-to-client-js/control-flow/plan/build-insert.ts +1 -0
  84. package/src/ir-to-client-js/control-flow/plan/insert.ts +2 -0
  85. package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +112 -3
  86. package/src/ir-to-client-js/control-flow/stringify/insert.ts +1 -1
  87. package/src/ir-to-client-js/csr-substitute.ts +11 -3
  88. package/src/ir-to-client-js/declaration-sort.ts +7 -3
  89. package/src/ir-to-client-js/emit-module-level.ts +7 -4
  90. package/src/ir-to-client-js/emit-registration.ts +5 -10
  91. package/src/ir-to-client-js/index.ts +7 -2
  92. package/src/ir-to-client-js/phases/ref-callbacks.ts +1 -1
  93. package/src/ir-to-client-js/plan/build-declaration-emit.ts +16 -15
  94. package/src/ir-to-client-js/plan/declaration-emit.ts +7 -5
  95. package/src/ir-to-client-js/prop-handling.ts +4 -0
  96. package/src/ir-to-client-js/reactivity.ts +32 -1
  97. package/src/ir-to-client-js/stringify/declaration-emit.ts +14 -7
  98. package/src/ir-to-client-js/types.ts +4 -0
  99. package/src/ir-to-client-js/utils.ts +184 -21
  100. package/src/jsx-to-ir.ts +111 -14
  101. package/src/signal-initializer.ts +36 -0
  102. package/src/ssr-defaults.ts +10 -0
  103. package/src/ssr-seed-plan.ts +12 -1
  104. package/src/template-position-walk.ts +203 -0
  105. package/src/types.ts +99 -0
package/src/debug.ts CHANGED
@@ -23,6 +23,7 @@ import type {
23
23
  SourceLocation,
24
24
  OriginInfo,
25
25
  } from './types.ts'
26
+ import { isElidedFactoryGetter } from './types.ts'
26
27
  import { analyzeComponent, listComponentFunctions } from './analyzer.ts'
27
28
  import { jsxToIR } from './jsx-to-ir.ts'
28
29
  import { buildMetadata } from './compiler.ts'
@@ -32,6 +33,8 @@ import { decideWrapFromAstFlags } from './ir-to-client-js/reactivity.ts'
32
33
  import { tokenContainsIdent } from './ir-to-client-js/utils.ts'
33
34
  import { identifierCallPattern } from './identifier-pattern.ts'
34
35
  import { BindingScope } from './scope/binding-scope.ts'
36
+ import { ACTION_ACCESSOR_NAMES, collectActionAccessorReads, collectActionNames, parsedExprChildren, type ActionAccessorName } from './action-accessor.ts'
37
+ import { parseExpression, tsNodeToParsedExpr, type ParsedExpr } from './expression-parser.ts'
35
38
 
36
39
  // =============================================================================
37
40
  // Types
@@ -44,6 +47,55 @@ export interface SignalNode {
44
47
  initialValue: string
45
48
  consumers: string[] // names of memos/effects/DOM nodes that read this signal
46
49
  loc: { file: string; line: number }
50
+ /**
51
+ * Set when this signal is the value half of an async reactive factory
52
+ * (`createQuery` / `createMutation`, spec/async.md §7.8): the value is
53
+ * written by the factory's request, not by a setter. The same object as the
54
+ * factory's entry in `ComponentGraph.factories`.
55
+ */
56
+ factory?: AsyncFactoryInfo
57
+ }
58
+
59
+ /**
60
+ * The async factory behind a signal node (#3167). A query is a signal
61
+ * source with inputs: `requestDeps` are the signals, memos and props its
62
+ * request function reads, each of which re-sends the request when it changes
63
+ * (so each lists `query:<value>` among its consumers). Only the request
64
+ * function counts: `options` is read once, at creation. A mutation's request
65
+ * function is read untracked, only when the action is called, so its
66
+ * `requestDeps` is always empty.
67
+ */
68
+ export interface AsyncFactoryInfo {
69
+ /**
70
+ * How `query:<key>` edges and `AccessorNode.factory` name the factory: its
71
+ * value signal, or its action when the value isn't destructured
72
+ * (`const [, save] = createMutation(…)` — the graph never shows the
73
+ * synthesized getter such a declaration gets).
74
+ */
75
+ key: string
76
+ kind: 'query' | 'mutation'
77
+ /** The callee as written: `createQuery`, `createMutation`, an alias, or `bf.createQuery`. */
78
+ callee: string
79
+ /** The action binding, or `null` when it isn't destructured. */
80
+ action: string | null
81
+ /** The value signal, or `null` when the value isn't destructured. */
82
+ value: string | null
83
+ requestDeps: string[]
84
+ }
85
+
86
+ /**
87
+ * An async action's reactive accessor, `<action>.isPending()` or
88
+ * `<action>.error()` (#3167). Named `<action>.<accessor>` — the same
89
+ * spelling a binding's `deps` uses for a read of it.
90
+ */
91
+ export interface AccessorNode {
92
+ kind: 'accessor'
93
+ name: string
94
+ action: string
95
+ accessor: ActionAccessorName
96
+ /** The owning factory's `AsyncFactoryInfo.key`. */
97
+ factory: string
98
+ consumers: string[] // names of memos/effects/DOM nodes that read this accessor
47
99
  }
48
100
 
49
101
  export interface MemoNode {
@@ -74,8 +126,10 @@ export interface DomBinding {
74
126
  * member as `<propsObject>.<member>` (`props.value`) — and a prop read
75
127
  * through a prop-derived local const (`class={classes}`) is reported as the
76
128
  * underlying prop(s), never the const. Every entry resolves to a node in
77
- * `ComponentGraph.signals` / `.memos` / `.props`. Event handlers instead list
78
- * the setters / getters the handler body references.
129
+ * `ComponentGraph.signals` / `.memos` / `.props`, or — for an async action's
130
+ * accessor read (`save.isPending()`, #3167) — `<action>.<accessor>`, a node
131
+ * in `ComponentGraph.accessors`. Event handlers instead list the setters /
132
+ * getters the handler body references.
79
133
  */
80
134
  deps: string[]
81
135
  type: 'text' | 'event' | 'conditional' | 'loop' | 'attribute'
@@ -142,18 +196,22 @@ export interface ComponentGraph {
142
196
  memos: MemoNode[]
143
197
  effects: EffectNode[]
144
198
  props: PropNode[]
199
+ /** Async factories (`createQuery` / `createMutation`, #3167), in declaration order. */
200
+ factories: AsyncFactoryInfo[]
201
+ /** Their actions' accessors. */
202
+ accessors: AccessorNode[]
145
203
  domBindings: DomBinding[]
146
204
  }
147
205
 
148
206
  export interface UpdatePath {
149
207
  target: string
150
- kind: 'signal' | 'memo' | 'prop'
208
+ kind: 'signal' | 'memo' | 'prop' | 'accessor'
151
209
  dependents: UpdatePathEntry[]
152
210
  }
153
211
 
154
212
  export interface UpdatePathEntry {
155
213
  name: string
156
- kind: 'memo' | 'effect' | 'dom'
214
+ kind: 'memo' | 'effect' | 'dom' | 'query'
157
215
  label: string
158
216
  /** Transitive dependents — memos/effects that depend on this entry */
159
217
  children: UpdatePathEntry[]
@@ -230,7 +288,7 @@ export interface WhyUpdateResult {
230
288
 
231
289
  export interface WhyUpdateDep {
232
290
  name: string
233
- kind: 'signal' | 'memo' | 'prop'
291
+ kind: 'signal' | 'memo' | 'prop' | 'accessor'
234
292
  dependsOn: string[]
235
293
  changedBy: WhyUpdateSource[]
236
294
  }
@@ -286,6 +344,8 @@ export function buildComponentGraph(source: string, filePath: string, componentN
286
344
  memos: [],
287
345
  effects: [],
288
346
  props: [],
347
+ factories: [],
348
+ accessors: [],
289
349
  domBindings: [],
290
350
  }
291
351
  }
@@ -299,6 +359,8 @@ export function buildComponentGraph(source: string, filePath: string, componentN
299
359
  memos: [],
300
360
  effects: [],
301
361
  props: [],
362
+ factories: [],
363
+ accessors: [],
302
364
  domBindings: [],
303
365
  }
304
366
  }
@@ -415,91 +477,58 @@ export function buildGraphFromIR(ir: ComponentIR): ComponentGraph {
415
477
  return [...out]
416
478
  }
417
479
 
480
+ // Async factories (#3167, spec/async.md §7.8). An action's accessor read
481
+ // (`save.isPending()`) is a reactive source like a signal read, so it lands
482
+ // in a binding's deps as `<action>.<accessor>`. Reads are found by the same
483
+ // `ParsedExpr` traversal BF117 uses, over the ORIGINAL expression text: the
484
+ // IR's own `.parsed` / `.parsedCondition` at an admitted position has
485
+ // already been replaced by the SSR seed (`seedActionAccessorReads`), so the
486
+ // read is no longer there. Parsed only when an action name appears in the
487
+ // text as a token.
488
+ const actions = collectActionNames(meta.signals)
489
+ const accessorNames = new Set<string>()
490
+ for (const action of actions) for (const accessor of ACTION_ACCESSOR_NAMES) accessorNames.add(`${action}.${accessor}`)
491
+ const accessorDepsOf = (expr: string, isShadowed?: (name: string) => boolean): string[] =>
492
+ [...actions].some(action => tokenContainsIdent(expr, action))
493
+ ? collectActionAccessorReads(parseExpression(expr), actions, isShadowed).map(r => `${r.action}.${r.accessor}`)
494
+ : []
495
+
418
496
  // Collect DOM bindings from IR tree
419
497
  const domBindings: DomBinding[] = []
420
- collectDomBindings(ir.root, domBindings, signalGetters, memoNames, undefined, BindingScope.EMPTY, propDepsOf)
421
-
422
- // Build consumer lists for signals
423
- const signalConsumers = new Map<string, string[]>()
424
- for (const s of meta.signals) signalConsumers.set(s.getter, [])
425
-
426
- // Memos consume signals
427
- for (const memo of meta.memos) {
428
- for (const dep of memo.deps) {
429
- signalConsumers.get(dep)?.push(`memo:${memo.name}`)
430
- }
431
- }
432
-
433
- // Effects consume signals
434
- for (let i = 0; i < meta.effects.length; i++) {
435
- const effect = meta.effects[i]
436
- for (const dep of effect.deps) {
437
- signalConsumers.get(dep)?.push(`effect:e${i}`)
438
- }
439
- }
440
-
441
- // DOM bindings consume signals
442
- for (const dom of domBindings) {
443
- for (const dep of dom.deps) {
444
- signalConsumers.get(dep)?.push(`dom:${dom.label}`)
445
- }
446
- }
447
-
448
- // Build consumer lists for memos
449
- const memoConsumers = new Map<string, string[]>()
450
- for (const m of meta.memos) memoConsumers.set(m.name, [])
451
-
452
- for (let i = 0; i < meta.effects.length; i++) {
453
- const effect = meta.effects[i]
454
- for (const dep of effect.deps) {
455
- memoConsumers.get(dep)?.push(`effect:e${i}`)
456
- }
457
- }
458
-
459
- for (const dom of domBindings) {
460
- for (const dep of dom.deps) {
461
- memoConsumers.get(dep)?.push(`dom:${dom.label}`)
462
- }
463
- }
464
-
465
- // Also track memo→memo dependencies
466
- for (const memo of meta.memos) {
467
- for (const dep of memo.deps) {
468
- memoConsumers.get(dep)?.push(`memo:${memo.name}`)
469
- }
470
- }
471
-
472
- // Props are nodes too (#2903): every binding dep that is neither a signal
473
- // nor a memo is a prop read (that is the only other thing `propDepsOf`
474
- // contributes), keyed in first-seen order so the graph is stable.
475
- const propConsumers = new Map<string, string[]>()
476
- for (const dom of domBindings) {
477
- if (dom.type === 'event') continue
478
- for (const dep of dom.deps) {
479
- if (signalGetters.has(dep) || memoNames.has(dep)) continue
480
- let consumers = propConsumers.get(dep)
481
- if (!consumers) {
482
- consumers = []
483
- propConsumers.set(dep, consumers)
484
- }
485
- consumers.push(`dom:${dom.label}`)
486
- }
498
+ collectDomBindings(ir.root, domBindings, signalGetters, memoNames, undefined, BindingScope.EMPTY, propDepsOf, accessorDepsOf)
499
+
500
+ // A query's request function re-sends when a signal, memo or prop it reads
501
+ // changes, so each of those is an input of the query. `options` is read once
502
+ // at creation, and a mutation's request function is read untracked (at call
503
+ // time only): neither has inputs.
504
+ const factories: AsyncFactoryInfo[] = []
505
+ const factoryOf = new Map<SignalInfo, AsyncFactoryInfo>()
506
+ for (const s of meta.signals) {
507
+ if (!s.factory) continue
508
+ const value = s.getterElided ? null : s.getter
509
+ const ids = s.factory.requestFreeIdentifiers
510
+ const info: AsyncFactoryInfo = {
511
+ key: value ?? s.factory.action ?? s.getter,
512
+ kind: s.factory.kind,
513
+ callee: s.factory.callee,
514
+ action: s.factory.action,
515
+ value,
516
+ requestDeps: s.factory.kind === 'query'
517
+ ? [
518
+ ...[...ids].filter(id => id !== s.getter && (signalGetters.has(id) || memoNames.has(id))),
519
+ ...propDepsOf(s.factory.requestText, ids),
520
+ ]
521
+ : [],
522
+ }
523
+ factories.push(info)
524
+ factoryOf.set(s, info)
487
525
  }
488
526
 
489
- const signals: SignalNode[] = meta.signals.map(s => ({
490
- kind: 'signal',
491
- name: s.getter,
492
- setter: s.setter,
493
- initialValue: s.initialValue,
494
- consumers: signalConsumers.get(s.getter) ?? [],
495
- loc: { file: s.loc.file, line: s.loc.start.line },
496
- }))
497
-
498
527
  const memos: MemoNode[] = meta.memos.map(m => ({
499
528
  kind: 'memo',
500
529
  name: m.name,
501
- deps: m.deps,
502
- consumers: memoConsumers.get(m.name) ?? [],
530
+ deps: [...m.deps, ...bodyAccessorDeps(m.computation, actions)],
531
+ consumers: [],
503
532
  computation: m.computation,
504
533
  loc: { file: m.loc.file, line: m.loc.start.line },
505
534
  }))
@@ -507,17 +536,75 @@ export function buildGraphFromIR(ir: ComponentIR): ComponentGraph {
507
536
  const effects: EffectNode[] = meta.effects.map((e, i) => ({
508
537
  kind: 'effect',
509
538
  label: `e${i}`,
510
- deps: e.deps,
539
+ deps: [...e.deps, ...bodyAccessorDeps(e.body, actions)],
511
540
  body: e.body,
512
541
  loc: { file: e.loc.file, line: e.loc.start.line },
513
542
  }))
514
543
 
544
+ // Consumer lists: signals, memos and accessors are keyed up front; a prop
545
+ // is keyed the first time something reads it (#2903), in first-seen order
546
+ // so the graph is stable. A binding dep that is neither a signal, a memo
547
+ // nor an accessor is a prop read.
548
+ const signalConsumers = new Map<string, string[]>(meta.signals.map(s => [s.getter, []]))
549
+ const memoConsumers = new Map<string, string[]>(meta.memos.map(m => [m.name, []]))
550
+ const accessorConsumers = new Map<string, string[]>([...accessorNames].map(name => [name, []]))
551
+ const propConsumers = new Map<string, string[]>()
552
+ const addConsumer = (dep: string, consumer: string, props: boolean): void => {
553
+ const consumers = signalConsumers.get(dep) ?? memoConsumers.get(dep) ?? accessorConsumers.get(dep)
554
+ if (consumers) {
555
+ consumers.push(consumer)
556
+ } else if (props) {
557
+ const list = propConsumers.get(dep) ?? []
558
+ propConsumers.set(dep, list)
559
+ list.push(consumer)
560
+ }
561
+ }
562
+ for (const m of memos) for (const dep of m.deps) addConsumer(dep, `memo:${m.name}`, false)
563
+ for (const e of effects) for (const dep of e.deps) addConsumer(dep, `effect:${e.label}`, false)
564
+ for (const dom of domBindings) {
565
+ for (const dep of dom.deps) addConsumer(dep, `dom:${dom.label}`, dom.type !== 'event')
566
+ }
567
+ // A query's request inputs, props included (#3167).
568
+ for (const f of factories) for (const dep of f.requestDeps) addConsumer(dep, `query:${f.key}`, true)
569
+ for (const m of memos) m.consumers = memoConsumers.get(m.name) ?? []
570
+
571
+ const signals: SignalNode[] = meta.signals.filter(s => !isElidedFactoryGetter(s)).map(s => {
572
+ const factory = factoryOf.get(s)
573
+ return {
574
+ kind: 'signal',
575
+ name: s.getter,
576
+ setter: s.setter,
577
+ initialValue: s.initialValue,
578
+ consumers: signalConsumers.get(s.getter) ?? [],
579
+ loc: { file: s.loc.file, line: s.loc.start.line },
580
+ ...(factory && { factory }),
581
+ }
582
+ })
583
+
515
584
  const props: PropNode[] = [...propConsumers].map(([name, consumers]) => ({
516
585
  kind: 'prop',
517
586
  name,
518
587
  consumers,
519
588
  }))
520
589
 
590
+ // Every accessor of every destructured action is a node, read or not, in
591
+ // declaration order — the same way every signal is listed.
592
+ const accessors: AccessorNode[] = factories.flatMap(f =>
593
+ f.action === null
594
+ ? []
595
+ : [...ACTION_ACCESSOR_NAMES].map(accessor => {
596
+ const name = `${f.action}.${accessor}`
597
+ return {
598
+ kind: 'accessor' as const,
599
+ name,
600
+ action: f.action!,
601
+ accessor: accessor as ActionAccessorName,
602
+ factory: f.key,
603
+ consumers: accessorConsumers.get(name) ?? [],
604
+ }
605
+ }),
606
+ )
607
+
521
608
  return {
522
609
  componentName: meta.componentName,
523
610
  sourceFile: findSourceFile(meta) ?? '',
@@ -525,10 +612,44 @@ export function buildGraphFromIR(ir: ComponentIR): ComponentGraph {
525
612
  memos,
526
613
  effects,
527
614
  props,
615
+ factories,
616
+ accessors,
528
617
  domBindings,
529
618
  }
530
619
  }
531
620
 
621
+ /**
622
+ * Accessor reads in a memo or effect body (#3167). Neither has a `ParsedExpr`
623
+ * that always covers its body — a block body only folds to a value expression
624
+ * in simple cases — so each expression the body holds is converted with
625
+ * `tsNodeToParsedExpr` and read by the same collector as a binding. A
626
+ * conversion that is not complete (it contains an `unsupported` node, such as
627
+ * a block-bodied callback passed to `forEach`) is searched inside instead of
628
+ * accepted, so a read under it is never lost; arrow-parameter shadowing holds
629
+ * wherever a conversion is complete. The body is parsed only when an action
630
+ * name appears in it as a token.
631
+ */
632
+ function bodyAccessorDeps(body: string, actions: ReadonlySet<string>): string[] {
633
+ if (![...actions].some(action => tokenContainsIdent(body, action))) return []
634
+ const found = new Set<string>()
635
+ const visit = (node: ts.Node): void => {
636
+ if (ts.isExpression(node)) {
637
+ const parsed = tsNodeToParsedExpr(node)
638
+ if (!containsUnsupported(parsed)) {
639
+ for (const r of collectActionAccessorReads(parsed, actions)) found.add(`${r.action}.${r.accessor}`)
640
+ return
641
+ }
642
+ }
643
+ ts.forEachChild(node, visit)
644
+ }
645
+ visit(ts.createSourceFile('__body.tsx', `(${body})`, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX))
646
+ return [...found]
647
+ }
648
+
649
+ function containsUnsupported(expr: ParsedExpr): boolean {
650
+ return expr.kind === 'unsupported' || parsedExprChildren(expr).some(child => containsUnsupported(child.expr))
651
+ }
652
+
532
653
  /**
533
654
  * Build both the ComponentIR and the reactive dependency graph in one pass.
534
655
  * Callers that need the raw IR tree (events, loops, why-update) use this
@@ -1120,9 +1241,21 @@ export function traceUpdatePath(graph: ComponentGraph, targetName: string): Upda
1120
1241
  if (memo) return { target: targetName, kind: 'memo', dependents: traceConsumers(memo.consumers, graph) }
1121
1242
  const prop = graph.props.find(p => p.name === targetName)
1122
1243
  if (prop) return { target: targetName, kind: 'prop', dependents: traceConsumers(prop.consumers, graph) }
1244
+ const accessor = graph.accessors.find(a => a.name === targetName)
1245
+ if (accessor) return { target: targetName, kind: 'accessor', dependents: traceConsumers(accessor.consumers, graph) }
1123
1246
  return null
1124
1247
  }
1125
1248
 
1249
+ /** Every name `traceUpdatePath` accepts: signals, memos, props and action accessors (#3167). */
1250
+ export function traceableNames(graph: ComponentGraph): string[] {
1251
+ return [
1252
+ ...graph.signals.map(s => s.name),
1253
+ ...graph.memos.map(m => m.name),
1254
+ ...graph.props.map(p => p.name),
1255
+ ...graph.accessors.map(a => a.name),
1256
+ ]
1257
+ }
1258
+
1126
1259
  function traceConsumers(consumers: string[], graph: ComponentGraph): UpdatePathEntry[] {
1127
1260
  return consumers.map(consumer => buildUpdateEntry(consumer, graph, new Set()))
1128
1261
  }
@@ -1158,6 +1291,19 @@ function buildUpdateEntry(consumer: string, graph: ComponentGraph, visited: Set<
1158
1291
  return { name: name, kind: 'dom', label: name, children: [] }
1159
1292
  }
1160
1293
 
1294
+ // A query re-sends its request (#3167): its value and its accessors update,
1295
+ // so their readers are its dependents. `name` is the factory key: the value
1296
+ // signal, or the action when the value isn't destructured.
1297
+ if (type === 'query') {
1298
+ const value = graph.signals.find(s => s.name === name)
1299
+ const readers = [
1300
+ ...(value?.consumers ?? []),
1301
+ ...graph.accessors.filter(a => a.factory === name).flatMap(a => a.consumers),
1302
+ ]
1303
+ const children = [...new Set(readers)].map(c => buildUpdateEntry(c, graph, new Set(visited)))
1304
+ return { name, kind: 'query', label: `${name} (query)`, children }
1305
+ }
1306
+
1161
1307
  return { name: consumer, kind: 'effect', label: consumer, children: [] }
1162
1308
  }
1163
1309
 
@@ -1188,12 +1334,26 @@ export function buildWhyUpdate(
1188
1334
  }
1189
1335
  const binding = matches[0]
1190
1336
 
1337
+ // An async action is its factory's setter for this purpose (#3167): a
1338
+ // handler that calls it (`onClick={() => save()}`, directly or through a
1339
+ // local function) changes the factory's value and accessors, so it maps to
1340
+ // the factory's key and rides the same setter-call resolution.
1191
1341
  const setterToSignal = new Map<string, string>()
1192
1342
  for (const s of ir.metadata.signals) {
1193
1343
  if (s.setter) setterToSignal.set(s.setter, s.getter)
1194
1344
  }
1345
+ for (const f of graph.factories) {
1346
+ if (f.action) setterToSignal.set(f.action, f.key)
1347
+ }
1195
1348
  const fnSetters = buildLocalFunctionSetterMap(ir.metadata, setterToSignal)
1196
1349
  const events = collectEventBindings(ir.root, setterToSignal, fnSetters)
1350
+ const changedByOf = (key: string): WhyUpdateSource[] =>
1351
+ events.flatMap(ev =>
1352
+ ev.setterCalls
1353
+ .filter(sc => sc.signal === key)
1354
+ .map(sc => ({ handler: ev.eventName, setter: sc.setter, elementContext: ev.elementContext, via: sc.via })),
1355
+ )
1356
+ const factoryByKey = new Map(graph.factories.map(f => [f.key, f]))
1197
1357
 
1198
1358
  const deps: WhyUpdateDep[] = []
1199
1359
  const visited = new Set<string>()
@@ -1202,7 +1362,17 @@ export function buildWhyUpdate(
1202
1362
  if (visited.has(name)) return
1203
1363
  visited.add(name)
1204
1364
 
1205
- const signal = graph.signals.find(s => s.name === name)
1365
+ // A factory's value, or one of its action's accessors (#3167): changed by
1366
+ // a call of the action, and — for a query — by any of its request inputs.
1367
+ const accessor = graph.accessors.find(a => a.name === name)
1368
+ const signal = accessor ? undefined : graph.signals.find(s => s.name === name)
1369
+ const factory = factoryByKey.get(accessor?.factory ?? signal?.factory?.key ?? '')
1370
+ if (factory) {
1371
+ deps.push({ name, kind: accessor ? 'accessor' : 'signal', dependsOn: factory.requestDeps, changedBy: changedByOf(factory.key) })
1372
+ for (const dep of factory.requestDeps) traceDep(dep)
1373
+ return
1374
+ }
1375
+
1206
1376
  if (signal) {
1207
1377
  const changedBy: WhyUpdateSource[] = []
1208
1378
  for (const ev of events) {
@@ -1272,7 +1442,9 @@ export function formatWhyUpdate(result: WhyUpdateResult): string {
1272
1442
  lines.push(' (prop — the parent component passes a new value)')
1273
1443
  } else {
1274
1444
  lines.push(`${dep.name} changes from:`)
1275
- if (dep.changedBy.length === 0) {
1445
+ // A query's value and accessors also change when a request input does (#3167).
1446
+ for (const d of dep.dependsOn) lines.push(` ${d} (re-sends the request)`)
1447
+ if (dep.changedBy.length === 0 && dep.dependsOn.length === 0) {
1276
1448
  lines.push(' (no event handlers found)')
1277
1449
  }
1278
1450
  for (const src of dep.changedBy) {
@@ -1301,7 +1473,25 @@ export function formatComponentGraph(graph: ComponentGraph): string {
1301
1473
  if (graph.signals.length > 0) {
1302
1474
  lines.push(` signals:`)
1303
1475
  for (const s of graph.signals) {
1304
- lines.push(` ${s.name} (initial: ${s.initialValue})`)
1476
+ // An async factory's value (#3167): which factory writes it, its
1477
+ // action, and — for a query — the inputs its request function reads.
1478
+ const factory = s.factory
1479
+ ? ` [${s.factory.callee}${s.factory.action ? `, action ${s.factory.action}` : ''}]` +
1480
+ (s.factory.requestDeps.length > 0 ? ` <- ${s.factory.requestDeps.join(', ')}` : '')
1481
+ : ''
1482
+ lines.push(` ${s.name} (initial: ${s.initialValue})${factory}`)
1483
+ }
1484
+ }
1485
+
1486
+ // Async actions' accessors (#3167), listed so the `<action>.<accessor> ->`
1487
+ // edges below have a node to point at.
1488
+ if (graph.accessors.length > 0) {
1489
+ const factoryByKey = new Map(graph.factories.map(f => [f.key, f]))
1490
+ lines.push(` accessors:`)
1491
+ for (const a of graph.accessors) {
1492
+ const f = factoryByKey.get(a.factory)
1493
+ const owner = f?.value ? `of ${f.value}` : `${f?.callee}, value not destructured`
1494
+ lines.push(` ${a.name}() (${owner})`)
1305
1495
  }
1306
1496
  }
1307
1497
 
@@ -1373,7 +1563,7 @@ export function formatComponentGraph(graph: ComponentGraph): string {
1373
1563
  }
1374
1564
 
1375
1565
  // Dependency graph
1376
- if (graph.signals.length > 0 || graph.memos.length > 0 || graph.props.length > 0) {
1566
+ if (graph.signals.length > 0 || graph.memos.length > 0 || graph.props.length > 0 || graph.accessors.length > 0) {
1377
1567
  lines.push(` dependency graph:`)
1378
1568
  for (const s of graph.signals) {
1379
1569
  for (const consumer of s.consumers) {
@@ -1390,6 +1580,11 @@ export function formatComponentGraph(graph: ComponentGraph): string {
1390
1580
  lines.push(` ${p.name} -> ${consumer}`)
1391
1581
  }
1392
1582
  }
1583
+ for (const a of graph.accessors) {
1584
+ for (const consumer of a.consumers) {
1585
+ lines.push(` ${a.name} -> ${consumer}`)
1586
+ }
1587
+ }
1393
1588
  }
1394
1589
 
1395
1590
  return lines.join('\n')
@@ -1433,6 +1628,7 @@ export function graphToJSON(graph: ComponentGraph): object {
1433
1628
  initialValue: s.initialValue,
1434
1629
  consumers: s.consumers,
1435
1630
  loc: s.loc,
1631
+ ...(s.factory && { factory: s.factory }),
1436
1632
  })),
1437
1633
  memos: graph.memos.map(m => ({
1438
1634
  name: m.name,
@@ -1451,6 +1647,14 @@ export function graphToJSON(graph: ComponentGraph): object {
1451
1647
  name: p.name,
1452
1648
  consumers: p.consumers,
1453
1649
  })),
1650
+ factories: graph.factories,
1651
+ accessors: graph.accessors.map(a => ({
1652
+ name: a.name,
1653
+ action: a.action,
1654
+ accessor: a.accessor,
1655
+ factory: a.factory,
1656
+ consumers: a.consumers,
1657
+ })),
1454
1658
  domBindings: graph.domBindings.map(d => ({
1455
1659
  label: d.label,
1456
1660
  slotId: d.slotId,
@@ -1806,6 +2010,11 @@ function collectDomBindings(
1806
2010
  // the graph prints `(no tracked deps)` for it (#2903). Returns the prop
1807
2011
  // names, which join `deps` alongside signal / memo reads.
1808
2012
  propDepsOf: (expr: string, freeIds?: ReadonlySet<string>, isShadowed?: (name: string) => boolean) => string[] = () => [],
2013
+ // Which async action accessors (`save.isPending()`, #3167) does a binding's
2014
+ // expression read, as `<action>.<accessor>`? A statically known reactive
2015
+ // source like a signal read, under the same loop-scope shadow guard as
2016
+ // `propDepsOf`.
2017
+ accessorDepsOf: (expr: string, isShadowed?: (name: string) => boolean) => string[] = () => [],
1809
2018
  ): void {
1810
2019
  const boundNames = scope.valueBoundNames()
1811
2020
  // Shadow guard for prop deps (see `propDepsOf`'s `isShadowed` in
@@ -1850,7 +2059,10 @@ function collectDomBindings(
1850
2059
  if (attr.name === 'key' && boundNames.size > 0) continue
1851
2060
  const expr = attrValueToString(attr.value)
1852
2061
  if (!expr) continue
1853
- const signalDeps = extractReactiveDeps(expr, signalGetters, memoNames)
2062
+ const signalDeps = [
2063
+ ...extractReactiveDeps(expr, signalGetters, memoNames),
2064
+ ...accessorDepsOf(expr, isShadowed),
2065
+ ]
1854
2066
  const isReactive = signalDeps.length > 0 || attrReadsLoopParam(attr.freeIdentifiers)
1855
2067
  // A prop-driven attribute (`id={props.id}`, `class={`…${props.x}`}`) is
1856
2068
  // wrapped in a `createEffect` by the emitter even with no signal/memo
@@ -1892,7 +2104,7 @@ function collectDomBindings(
1892
2104
  }
1893
2105
  // Recurse — pass element tag as parent context for text bindings
1894
2106
  for (const child of node.children) {
1895
- collectDomBindings(child, bindings, signalGetters, memoNames, node.tag, scope, propDepsOf)
2107
+ collectDomBindings(child, bindings, signalGetters, memoNames, node.tag, scope, propDepsOf, accessorDepsOf)
1896
2108
  }
1897
2109
  break
1898
2110
  }
@@ -1901,9 +2113,11 @@ function collectDomBindings(
1901
2113
  // `node.reactive || node.callsReactiveGetters || node.hasFunctionCalls`.
1902
2114
  const decision = decideWrapFromAstFlags(node)
1903
2115
  const loopReactive = exprReadsLoopParam(node)
1904
- if ((decision.wrap || loopReactive) && node.slotId) {
2116
+ const accessorDeps = accessorDepsOf(node.expr, isShadowed)
2117
+ if ((decision.wrap || loopReactive || accessorDeps.length > 0) && node.slotId) {
1905
2118
  const deps = [
1906
2119
  ...extractReactiveDeps(node.expr, signalGetters, memoNames),
2120
+ ...accessorDeps,
1907
2121
  ...propDepsOf(node.expr, freeIdsOf(node.origin), isShadowed),
1908
2122
  ]
1909
2123
  const preview = parentTag
@@ -1916,11 +2130,11 @@ function collectDomBindings(
1916
2130
  deps,
1917
2131
  type: 'text',
1918
2132
  classification:
1919
- (decision.wrap && decision.reason === 'proven-reactive') || loopReactive
2133
+ (decision.wrap && decision.reason === 'proven-reactive') || loopReactive || accessorDeps.length > 0
1920
2134
  ? 'reactive'
1921
2135
  : 'fallback',
1922
2136
  expression: node.expr,
1923
- wrapReason: decision.wrap ? decision.reason : 'string-reactive',
2137
+ wrapReason: decision.wrap && accessorDeps.length === 0 ? decision.reason : 'string-reactive',
1924
2138
  loc: node.loc,
1925
2139
  jsxPreview: preview,
1926
2140
  })
@@ -1934,9 +2148,11 @@ function collectDomBindings(
1934
2148
  // param is neither signal nor memo. Use the resolved `origin.freeRefs`.
1935
2149
  const loopReactive =
1936
2150
  boundNames.size > 0 && (node.origin?.freeRefs?.some(r => boundNames.has(r.name)) ?? false)
1937
- if ((decision.wrap || loopReactive) && node.slotId) {
2151
+ const accessorDeps = accessorDepsOf(node.condition, isShadowed)
2152
+ if ((decision.wrap || loopReactive || accessorDeps.length > 0) && node.slotId) {
1938
2153
  const deps = [
1939
2154
  ...extractReactiveDeps(node.condition, signalGetters, memoNames),
2155
+ ...accessorDeps,
1940
2156
  ...propDepsOf(node.condition, freeIdsOf(node.origin), isShadowed),
1941
2157
  ]
1942
2158
  bindings.push({
@@ -1946,17 +2162,17 @@ function collectDomBindings(
1946
2162
  deps,
1947
2163
  type: 'conditional',
1948
2164
  classification:
1949
- (decision.wrap && decision.reason === 'proven-reactive') || loopReactive
2165
+ (decision.wrap && decision.reason === 'proven-reactive') || loopReactive || accessorDeps.length > 0
1950
2166
  ? 'reactive'
1951
2167
  : 'fallback',
1952
2168
  expression: node.condition,
1953
- wrapReason: decision.wrap ? decision.reason : 'string-reactive',
2169
+ wrapReason: decision.wrap && accessorDeps.length === 0 ? decision.reason : 'string-reactive',
1954
2170
  loc: node.loc,
1955
2171
  jsxPreview: `{${truncateExpr(node.condition)} ? ... : ...}`,
1956
2172
  })
1957
2173
  }
1958
- collectDomBindings(node.whenTrue, bindings, signalGetters, memoNames, parentTag, scope, propDepsOf)
1959
- collectDomBindings(node.whenFalse, bindings, signalGetters, memoNames, parentTag, scope, propDepsOf)
2174
+ collectDomBindings(node.whenTrue, bindings, signalGetters, memoNames, parentTag, scope, propDepsOf, accessorDepsOf)
2175
+ collectDomBindings(node.whenFalse, bindings, signalGetters, memoNames, parentTag, scope, propDepsOf, accessorDepsOf)
1960
2176
  break
1961
2177
  }
1962
2178
  case 'loop': {
@@ -1966,7 +2182,38 @@ function collectDomBindings(
1966
2182
  // `hasFunctionCalls` without reactive-getter hit is the fallback case
1967
2183
  // (e.g. `getItems().map(...)` with an opaque helper).
1968
2184
  if (node.slotId) {
1969
- const deps = extractReactiveDeps(node.array, signalGetters, memoNames)
2185
+ // #3246: `.filter()` / `.sort()` predicates are lifted off the chain
2186
+ // into `IRLoop.filterPredicate` / `.sortComparator` during IR
2187
+ // construction (`loop-chain.ts`) and kept separate from `array` — so
2188
+ // a signal/memo read inside either callback (`items().filter(t =>
2189
+ // filter() === 'all' || !t.done)`) never showed up in the loop's own
2190
+ // deps, even though that read is exactly why the loop re-renders.
2191
+ // (The `/* @client */` path keeps the whole chain in `array`, which
2192
+ // is why the edge was already there in that case.) Collect each
2193
+ // callback's reads too, shadow-guarded via the shared `BindingScope`
2194
+ // service — `enterCallback` exists for exactly this ("a filter
2195
+ // predicate's `x`, a sort comparator's `(a, b)`", see its doc
2196
+ // comment) — so a predicate/comparator that names its parameter the
2197
+ // same as an unrelated signal isn't mistaken for a real read of it.
2198
+ // Built on `scope` (not `childScope` below): the filter/sort
2199
+ // callback's own param(s) are a sibling scope to the loop row, not
2200
+ // nested under it.
2201
+ //
2202
+ // The `sortComparator` half is defensive: `classifySortOperand`
2203
+ // (expression-parser.ts) can't route a signal call into it under
2204
+ // today's grammar, so this branch is untested in practice.
2205
+ const { filterPredicate, sortComparator } = node
2206
+ const callbackDeps = (raw: string, params: string[]) => {
2207
+ const callbackScope = scope.enterCallback(params)
2208
+ return extractReactiveDeps(raw, signalGetters, memoNames).filter(name => !callbackScope.isBound(name))
2209
+ }
2210
+ const deps = [
2211
+ ...new Set([
2212
+ ...extractReactiveDeps(node.array, signalGetters, memoNames),
2213
+ ...(filterPredicate ? callbackDeps(filterPredicate.raw, [filterPredicate.param]) : []),
2214
+ ...(sortComparator ? callbackDeps(sortComparator.raw, [sortComparator.paramA, sortComparator.paramB]) : []),
2215
+ ]),
2216
+ ]
1970
2217
  // An inner loop whose array reads an outer loop param (`r.tags.map(...)`)
1971
2218
  // is reactive per item — use the resolved `arrayFreeIdentifiers`.
1972
2219
  const loopReactive =
@@ -2007,7 +2254,7 @@ function collectDomBindings(
2007
2254
  // `paramBindings`/`preamble`), so no bespoke Set bookkeeping is needed.
2008
2255
  const childScope = scope.enterLoopRow(node)
2009
2256
  for (const child of node.children) {
2010
- collectDomBindings(child, bindings, signalGetters, memoNames, parentTag, childScope, propDepsOf)
2257
+ collectDomBindings(child, bindings, signalGetters, memoNames, parentTag, childScope, propDepsOf, accessorDepsOf)
2011
2258
  }
2012
2259
  break
2013
2260
  }
@@ -2047,21 +2294,21 @@ function collectDomBindings(
2047
2294
  }
2048
2295
  }
2049
2296
  for (const child of node.children) {
2050
- collectDomBindings(child, bindings, signalGetters, memoNames, parentTag, scope, propDepsOf)
2297
+ collectDomBindings(child, bindings, signalGetters, memoNames, parentTag, scope, propDepsOf, accessorDepsOf)
2051
2298
  }
2052
2299
  break
2053
2300
  }
2054
2301
  case 'fragment':
2055
2302
  case 'provider': {
2056
2303
  for (const child of node.children) {
2057
- collectDomBindings(child, bindings, signalGetters, memoNames, parentTag, scope, propDepsOf)
2304
+ collectDomBindings(child, bindings, signalGetters, memoNames, parentTag, scope, propDepsOf, accessorDepsOf)
2058
2305
  }
2059
2306
  break
2060
2307
  }
2061
2308
  case 'if-statement': {
2062
- collectDomBindings(node.consequent, bindings, signalGetters, memoNames, parentTag, scope, propDepsOf)
2309
+ collectDomBindings(node.consequent, bindings, signalGetters, memoNames, parentTag, scope, propDepsOf, accessorDepsOf)
2063
2310
  if (node.alternate) {
2064
- collectDomBindings(node.alternate, bindings, signalGetters, memoNames, parentTag, scope, propDepsOf)
2311
+ collectDomBindings(node.alternate, bindings, signalGetters, memoNames, parentTag, scope, propDepsOf, accessorDepsOf)
2065
2312
  }
2066
2313
  break
2067
2314
  }