@barefootjs/jsx 0.39.3 → 0.39.4

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 (68) hide show
  1. package/dist/adapters/boolean-result.d.ts +32 -0
  2. package/dist/adapters/boolean-result.d.ts.map +1 -0
  3. package/dist/errors.d.ts +1 -0
  4. package/dist/errors.d.ts.map +1 -1
  5. package/dist/index.d.ts +2 -1
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +335 -49
  8. package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
  9. package/dist/ir-to-client-js/control-flow/plan/build-composite-loop.d.ts.map +1 -1
  10. package/dist/ir-to-client-js/control-flow/plan/build-event-delegation.d.ts.map +1 -1
  11. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts +5 -0
  12. package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
  13. package/dist/ir-to-client-js/control-flow/plan/event-delegation.d.ts +7 -0
  14. package/dist/ir-to-client-js/control-flow/plan/event-delegation.d.ts.map +1 -1
  15. package/dist/ir-to-client-js/control-flow/plan/insert.d.ts +2 -0
  16. package/dist/ir-to-client-js/control-flow/plan/insert.d.ts.map +1 -1
  17. package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +2 -0
  18. package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
  19. package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
  20. package/dist/ir-to-client-js/control-flow/stringify/composite-loop.d.ts.map +1 -1
  21. package/dist/ir-to-client-js/control-flow/stringify/event-delegation.d.ts.map +1 -1
  22. package/dist/ir-to-client-js/control-flow/stringify/loop.d.ts +14 -0
  23. package/dist/ir-to-client-js/control-flow/stringify/loop.d.ts.map +1 -1
  24. package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
  25. package/dist/ir-to-client-js/imports.d.ts +2 -2
  26. package/dist/ir-to-client-js/imports.d.ts.map +1 -1
  27. package/dist/ir-to-client-js/phases/rest-attr-applications.d.ts +3 -1
  28. package/dist/ir-to-client-js/phases/rest-attr-applications.d.ts.map +1 -1
  29. package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
  30. package/dist/ir-to-client-js/safe-html.d.ts +6 -0
  31. package/dist/ir-to-client-js/safe-html.d.ts.map +1 -1
  32. package/dist/ir-to-client-js/types.d.ts +12 -0
  33. package/dist/ir-to-client-js/types.d.ts.map +1 -1
  34. package/dist/nullable-signal-attr.d.ts +21 -0
  35. package/dist/nullable-signal-attr.d.ts.map +1 -1
  36. package/dist/profiler.d.ts +9 -1
  37. package/dist/profiler.d.ts.map +1 -1
  38. package/package.json +2 -2
  39. package/src/__tests__/boolean-result.test.ts +27 -0
  40. package/src/__tests__/csr-template-loop-shadow.test.ts +83 -0
  41. package/src/__tests__/profile-turn-markers.test.ts +22 -0
  42. package/src/__tests__/profiler.test.ts +10 -9
  43. package/src/__tests__/ssr-portal-ref-callback.test.ts +88 -13
  44. package/src/adapters/boolean-result.ts +89 -0
  45. package/src/errors.ts +11 -0
  46. package/src/index.ts +3 -0
  47. package/src/ir-to-client-js/collect-elements.ts +11 -3
  48. package/src/ir-to-client-js/control-flow/plan/build-composite-loop.ts +2 -0
  49. package/src/ir-to-client-js/control-flow/plan/build-event-delegation.ts +13 -0
  50. package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +7 -2
  51. package/src/ir-to-client-js/control-flow/plan/event-delegation.ts +7 -0
  52. package/src/ir-to-client-js/control-flow/plan/insert.ts +2 -0
  53. package/src/ir-to-client-js/control-flow/plan/loop.ts +2 -0
  54. package/src/ir-to-client-js/control-flow/shared.ts +2 -0
  55. package/src/ir-to-client-js/control-flow/stringify/branch-loop.ts +6 -3
  56. package/src/ir-to-client-js/control-flow/stringify/composite-loop.ts +7 -4
  57. package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +21 -6
  58. package/src/ir-to-client-js/control-flow/stringify/insert.ts +6 -1
  59. package/src/ir-to-client-js/control-flow/stringify/loop.ts +55 -8
  60. package/src/ir-to-client-js/html-template.ts +59 -8
  61. package/src/ir-to-client-js/imports.ts +3 -1
  62. package/src/ir-to-client-js/phases/rest-attr-applications.ts +8 -2
  63. package/src/ir-to-client-js/reactivity.ts +1 -0
  64. package/src/ir-to-client-js/safe-html.ts +10 -0
  65. package/src/ir-to-client-js/types.ts +12 -0
  66. package/src/jsx-to-ir.ts +138 -32
  67. package/src/nullable-signal-attr.ts +46 -0
  68. package/src/profiler.ts +79 -10
@@ -81,12 +81,19 @@ export function nullableSignalAttrGetter(
81
81
  */
82
82
  export interface NullishAttrContext {
83
83
  readonly nullableSignals: ReadonlySet<string>
84
+ /** Signals seeded with a literal `null`: their SSR value is `null`, not
85
+ * `undefined` (`arithmeticOperandIsNull`). */
86
+ readonly nullSeededSignals?: ReadonlySet<string>
84
87
  readonly memos: ReadonlyMap<string, MemoInfo>
85
88
  }
86
89
 
87
90
  export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
91
+ const signals = ir.metadata.signals ?? []
88
92
  return {
89
93
  nullableSignals: collectNullableSignalGetters(ir),
94
+ nullSeededSignals: new Set(
95
+ signals.filter(s => s.parsed?.kind === 'literal' && s.parsed.literalType === 'null').map(s => s.getter),
96
+ ),
90
97
  memos: new Map((ir.metadata.memos ?? []).map(m => [m.name, m])),
91
98
  }
92
99
  }
@@ -154,3 +161,42 @@ export function attrValueMayBeNullish(
154
161
  }
155
162
  return parsed !== undefined && walk(parsed, isShadowed)
156
163
  }
164
+
165
+ /**
166
+ * Whether an arithmetic operand's SSR value is certainly `null` — a literal
167
+ * `null`, a read of a signal seeded with `null`, or a memo whose body is
168
+ * such a read (`same = () => s()`). JS `ToNumber(null)` is `0` but
169
+ * `ToNumber(undefined)` is `NaN`, and a template engine holds both as one
170
+ * nil, so only a value known to be `null` may be coerced to `0` (#3350).
171
+ * A bare identifier is read as the getter call: a memo seed lowers `s()` to
172
+ * `s` before the adapter sees it.
173
+ */
174
+ export function arithmeticOperandIsNull(
175
+ parsed: ParsedExpr | undefined,
176
+ ctx: NullishAttrContext,
177
+ isShadowed: (name: string) => boolean = () => false,
178
+ ): boolean {
179
+ const visiting = new Set<string>()
180
+ const walk = (expr: ParsedExpr, shadowed: (name: string) => boolean): boolean => {
181
+ if (expr.kind === 'literal') return expr.literalType === 'null'
182
+ const getter = expr.kind === 'identifier' ? expr.name : bareGetterCallName(expr)
183
+ if (getter === null || shadowed(getter)) return false
184
+ if (ctx.nullSeededSignals?.has(getter)) return true
185
+ const memo = ctx.memos.get(getter)
186
+ if (!memo?.parsed || visiting.has(getter)) return false
187
+ visiting.add(getter)
188
+ const result = walk(memo.parsed, () => false)
189
+ visiting.delete(getter)
190
+ return result
191
+ }
192
+ return parsed !== undefined && walk(parsed, isShadowed)
193
+ }
194
+
195
+ /**
196
+ * Arithmetic operators JS applies `ToNumber` to, so a `null` operand reads
197
+ * as `0` (`null * 2` is `0`). A template engine's native operator rejects a
198
+ * nil operand instead and the render throws (#3350), so adapters coerce an
199
+ * operand that `arithmeticOperandIsNull` matches. `+` is excluded: it
200
+ * concatenates when either side is a string.
201
+ */
202
+ export const NULL_COERCING_ARITHMETIC_OPS: ReadonlySet<string> = new Set(['-', '*', '/', '%', '**'])
package/src/profiler.ts CHANGED
@@ -30,6 +30,8 @@ import {
30
30
  type UpdatePathEntry,
31
31
  } from './debug.ts'
32
32
  import { listComponentFunctions, createProgramForFile } from './analyzer.ts'
33
+ import { walkIR } from './ir-to-client-js/walker.ts'
34
+ import type { ComponentIR } from './types.ts'
33
35
  import type { ProfilerEvent } from '@barefootjs/shared'
34
36
 
35
37
  /**
@@ -509,6 +511,46 @@ export function buildIdIndex(graph: ComponentGraph): IdIndex {
509
511
  return index
510
512
  }
511
513
 
514
+ /**
515
+ * Handler sites behind a rest/props spread on an intrinsic element
516
+ * (`<button {...props}>`), keyed by their turn-id prefix
517
+ * `<Component>#handler:<slotId>` (#3376). The compiler wires the event handlers
518
+ * such a spread carries at runtime (`applyRestAttrs`), so which events they are
519
+ * is only known once a caller passes them — the turn id appends the DOM event
520
+ * name the runtime saw. Resolving a recorded turn id to its spread site needs
521
+ * only the prefix, so every spread with a slot is listed: one the compiler
522
+ * applies statically never produces such a turn id, and its entry stays unused.
523
+ */
524
+ function buildRestHandlerSites(componentName: string, ir: ComponentIR): Map<string, ResolvedNode> {
525
+ const sites = new Map<string, ResolvedNode>()
526
+ walkIR(ir.root, undefined, {
527
+ element({ node, descend }) {
528
+ const spread = node.slotId ? node.attrs.find(a => a.name === '...') : undefined
529
+ if (spread && node.slotId) {
530
+ const loc = spread.loc ?? node.loc
531
+ sites.set(`${componentName}#handler:${node.slotId}`, {
532
+ kind: 'handler',
533
+ // Completed with the event name per turn id: `click@s0 {...}`.
534
+ name: `@${node.slotId} {...}`,
535
+ loc: { file: loc.file, line: loc.start.line },
536
+ })
537
+ }
538
+ descend()
539
+ },
540
+ component({ descend, descendJsxChildren }) {
541
+ descend()
542
+ descendJsxChildren()
543
+ },
544
+ })
545
+ return sites
546
+ }
547
+
548
+ /** The spread site a rest-forwarded turn id (`<site>:<event>`) belongs to. */
549
+ function restSiteOf(handlerId: string, sites: ReadonlyMap<string, ResolvedNode>): ResolvedNode | undefined {
550
+ const colon = handlerId.lastIndexOf(':')
551
+ return colon < 0 ? undefined : sites.get(handlerId.slice(0, colon))
552
+ }
553
+
512
554
  /** A profiler id that no IR node could be found for (SR4 coverage gap). */
513
555
  export interface UnattributedId {
514
556
  id: string
@@ -1358,7 +1400,15 @@ export interface DiagnosticsSummary {
1358
1400
  export interface ProfileCoverage {
1359
1401
  /** Distinct handlers exercised (turns observed). */
1360
1402
  handlersFired: number
1361
- /** Handlers the IR knows about (`buildEventSummary`). */
1403
+ /**
1404
+ * DOM handler sites in the run (#3377): one per instrumented DOM listener —
1405
+ * every `<el on*={…}>` event binding the IR knows about, plus each
1406
+ * rest-forwarded listener (`<el {...props}>`, #3376) the run observed (its
1407
+ * events are decided by the caller, so it is known only once wired). The same
1408
+ * unit the numerator counts — a turn is recorded per DOM listener — so a
1409
+ * callback prop a parent hands a child (`<Child onClick={…}>`) is not a unit
1410
+ * of its own: it runs inside the child's DOM turn.
1411
+ */
1362
1412
  handlersTotal: number
1363
1413
  /**
1364
1414
  * `handlersFired / handlersTotal` in `[0,1]` — the fraction of known handlers
@@ -1607,7 +1657,11 @@ export function buildProfileReport(input: ProfileReportInput): ProfileReport {
1607
1657
  // turn id → the handler binding + the component graph that owns it (so the
1608
1658
  // safety oracle reasons over the right component's memos).
1609
1659
  const turnBindings = new Map<string, { binding: EventBinding; graph: ComponentGraph }>()
1610
- let handlersTotal = 0
1660
+ // Coverage inventory (#3377): the turn ids of every DOM event binding — the
1661
+ // same inventory the auto scenario fires and the static budget lists.
1662
+ const staticHandlerIds = new Set<string>()
1663
+ // Spread sites whose runtime-wired handlers carry their own turn ids (#3376).
1664
+ const restSites = new Map<string, ResolvedNode>()
1611
1665
  // Per-file lines of the `createEffect` calls the compiler instrumented (top
1612
1666
  // level, carry a `__bfId`). Subtracted from a source scan to find the
1613
1667
  // uninstrumented ones behind anonymous `e<n>` ids (#1849 B6).
@@ -1628,12 +1682,17 @@ export function buildProfileReport(input: ProfileReportInput): ProfileReport {
1628
1682
  if (componentNames.length === 0) componentNames = [undefined as unknown as string]
1629
1683
  for (const name of componentNames) {
1630
1684
  let graph: ComponentGraph
1685
+ let ir: ComponentIR
1631
1686
  try {
1632
- graph = buildComponentAnalysis(s.source, s.filePath, name, program).graph
1687
+ ;({ graph, ir } = buildComponentAnalysis(s.source, s.filePath, name, program))
1633
1688
  } catch {
1634
1689
  continue
1635
1690
  }
1636
- for (const [k, v] of buildIdIndex(graph)) index.set(k, v)
1691
+ for (const [k, v] of buildIdIndex(graph)) {
1692
+ index.set(k, v)
1693
+ if (v.kind === 'handler') staticHandlerIds.add(k)
1694
+ }
1695
+ for (const [k, v] of buildRestHandlerSites(graph.componentName, ir)) restSites.set(k, v)
1637
1696
  for (const e of graph.effects) {
1638
1697
  const set = instrumentedEffectLines.get(e.loc.file) ?? new Set<number>()
1639
1698
  set.add(e.loc.line)
@@ -1641,7 +1700,6 @@ export function buildProfileReport(input: ProfileReportInput): ProfileReport {
1641
1700
  }
1642
1701
  try {
1643
1702
  const summary = buildEventSummary(s.source, s.filePath, name, program)
1644
- handlersTotal += summary.events.length
1645
1703
  for (const [turn, binding] of turnToEventBinding(graph, summary.events)) {
1646
1704
  turnBindings.set(turn, { binding, graph })
1647
1705
  }
@@ -1651,6 +1709,14 @@ export function buildProfileReport(input: ProfileReportInput): ProfileReport {
1651
1709
  }
1652
1710
  }
1653
1711
 
1712
+ // A rest-forwarded turn id resolves to its spread site (#3376), so its batch
1713
+ // candidate and findings cite source like any explicit handler's.
1714
+ for (const e of events) {
1715
+ if (e.turn === null || index.has(e.turn)) continue
1716
+ const site = restSiteOf(e.turn, restSites)
1717
+ if (site) index.set(e.turn, { ...site, name: `${e.turn.slice(e.turn.lastIndexOf(':') + 1)}${site.name}` })
1718
+ }
1719
+
1654
1720
  // Candidate sites for uninstrumented `createEffect` ids, across every source
1655
1721
  // in the run (a compound scenario spans several files), so an `e<n>` row can
1656
1722
  // cite where to look (#1849 B6). The per-file AST scan is only ever consumed
@@ -1701,6 +1767,11 @@ export function buildProfileReport(input: ProfileReportInput): ProfileReport {
1701
1767
  handlerIds.add(e.turn)
1702
1768
  }
1703
1769
  }
1770
+ // The inventory is every static DOM handler site plus every one the run
1771
+ // observed — a turn id the IR could not list (a rest-forwarded listener) is
1772
+ // still a DOM handler that exists. Numerator and denominator count the same
1773
+ // unit, so a fully exercised component reads 1 and the ratio can't pass 1.
1774
+ const handlersTotal = new Set([...staticHandlerIds, ...handlerIds]).size
1704
1775
 
1705
1776
  const findings = buildAgentFindings(primary.componentName, hotSubscribers, wastedReReruns, batchAdvisor, unattributed)
1706
1777
  // Status is measurement-only here: any warning/error finding ⇒ `warning`. A
@@ -1709,11 +1780,9 @@ export function buildProfileReport(input: ProfileReportInput): ProfileReport {
1709
1780
  const status: ProfileStatus = findings.some(f => f.severity === 'warning' || f.severity === 'error') ? 'warning' : 'ok'
1710
1781
 
1711
1782
  // Coverage ratio: 1 when there is nothing to cover (no handlers), else the
1712
- // exercised fraction. Clamped to `[0,1]` — a malformed stream (more distinct
1713
- // turn ids than `buildEventSummary` knows about, e.g. missing `extraSources`)
1714
- // must not push the ratio past 1 and break a gate's assumptions. Drives
1715
- // `--min-coverage` and the guidance below.
1716
- const ratio = handlersTotal > 0 ? Math.min(1, handlerIds.size / handlersTotal) : 1
1783
+ // exercised fraction — in `[0,1]` by construction, since every exercised id
1784
+ // is in the inventory. Drives `--min-coverage` and the guidance below.
1785
+ const ratio = handlersTotal > 0 ? handlerIds.size / handlersTotal : 1
1717
1786
  let guidance: ScenarioGuidance | undefined
1718
1787
  if (turnSeqs.size === 0) {
1719
1788
  guidance =