@barefootjs/jsx 0.35.5 → 0.35.6
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.
- package/dist/analyzer-context.d.ts +5 -4
- package/dist/analyzer-context.d.ts.map +1 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/errors.d.ts +0 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/index.js +6711 -6471
- package/dist/ir-to-client-js/compute-prop-usage.d.ts +6 -0
- package/dist/ir-to-client-js/compute-prop-usage.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-event-delegation.d.ts +3 -2
- package/dist/ir-to-client-js/control-flow/plan/build-event-delegation.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-loop.d.ts +12 -2
- package/dist/ir-to-client-js/control-flow/plan/build-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts +40 -0
- package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts.map +1 -0
- package/dist/ir-to-client-js/control-flow/plan/event-delegation.d.ts +13 -0
- package/dist/ir-to-client-js/control-flow/plan/event-delegation.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop-delegation-index.d.ts +45 -0
- package/dist/ir-to-client-js/control-flow/plan/loop-delegation-index.d.ts.map +1 -0
- package/dist/ir-to-client-js/control-flow/plan/types.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-delegation.d.ts +12 -0
- package/dist/ir-to-client-js/control-flow/stringify/event-delegation.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow.d.ts +2 -1
- package/dist/ir-to-client-js/control-flow.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-reactive.d.ts +7 -3
- package/dist/ir-to-client-js/emit-reactive.d.ts.map +1 -1
- package/dist/ir-to-client-js/generate-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/index.d.ts +1 -0
- package/dist/ir-to-client-js/index.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases/event-handlers.d.ts +9 -1
- package/dist/ir-to-client-js/phases/event-handlers.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases/props-event-handlers.d.ts +5 -0
- package/dist/ir-to-client-js/phases/props-event-handlers.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases/props-extraction.d.ts +15 -13
- package/dist/ir-to-client-js/phases/props-extraction.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases.d.ts +12 -1
- package/dist/ir-to-client-js/phases.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/build-declaration-emit.d.ts.map +1 -1
- package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts +50 -0
- package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
- package/dist/ir-to-client-js/rewrite-destructured-props.d.ts +29 -0
- package/dist/ir-to-client-js/rewrite-destructured-props.d.ts.map +1 -0
- package/dist/ir-to-client-js/utils.d.ts +9 -0
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-binding.d.ts +112 -1
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/static-literal.d.ts +20 -8
- package/dist/static-literal.d.ts.map +1 -1
- package/dist/types.d.ts +7 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +31 -35
- package/src/__tests__/aliased-destructured-prop-csr.test.ts +19 -7
- package/src/__tests__/body-destructured-prop-default-propsparams.test.ts +153 -0
- package/src/__tests__/body-destructured-props-live.test.ts +245 -0
- package/src/__tests__/client-js-generation.test.ts +11 -4
- package/src/__tests__/client-only-date-lowering.test.ts +5 -2
- package/src/__tests__/destructured-from-props-localconst-value.test.ts +17 -6
- package/src/__tests__/destructured-props-live.test.ts +167 -0
- package/src/__tests__/doc-examples.test.ts +2 -4
- package/src/__tests__/event-delegation-container-collision-2930.test.ts +197 -0
- package/src/__tests__/format-error.test.ts +5 -3
- package/src/__tests__/live-only-props.test.ts +160 -0
- package/src/__tests__/prop-references.test.ts +26 -17
- package/src/__tests__/props-destructuring.test.ts +6 -275
- package/src/__tests__/prune-unused-prop-extractions.test.ts +11 -5
- package/src/__tests__/signal-getter-not-called.test.ts +5 -0
- package/src/__tests__/ssr-defaults.test.ts +83 -0
- package/src/__tests__/staged-ir/02-shadow-guards.test.ts +12 -5
- package/src/__tests__/static-literal.test.ts +39 -4
- package/src/analyzer-context.ts +8 -7
- package/src/analyzer.ts +76 -84
- package/src/errors.ts +7 -8
- package/src/ir-to-client-js/compute-prop-usage.ts +19 -0
- package/src/ir-to-client-js/control-flow/plan/build-event-delegation.ts +5 -0
- package/src/ir-to-client-js/control-flow/plan/build-loop.ts +27 -11
- package/src/ir-to-client-js/control-flow/plan/event-collision.ts +55 -0
- package/src/ir-to-client-js/control-flow/plan/event-delegation.ts +13 -0
- package/src/ir-to-client-js/control-flow/plan/loop-delegation-index.ts +79 -0
- package/src/ir-to-client-js/control-flow/plan/types.ts +2 -0
- package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +50 -10
- package/src/ir-to-client-js/control-flow.ts +68 -31
- package/src/ir-to-client-js/emit-reactive.ts +12 -8
- package/src/ir-to-client-js/generate-init.ts +6 -0
- package/src/ir-to-client-js/index.ts +45 -3
- package/src/ir-to-client-js/phases/event-handlers.ts +10 -0
- package/src/ir-to-client-js/phases/props-event-handlers.ts +6 -0
- package/src/ir-to-client-js/phases/props-extraction.ts +29 -48
- package/src/ir-to-client-js/phases.ts +17 -3
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +6 -5
- package/src/ir-to-client-js/prune-unused-prop-extractions.ts +31 -10
- package/src/ir-to-client-js/rewrite-destructured-props.ts +210 -0
- package/src/ir-to-client-js/utils.ts +14 -0
- package/src/jsx-to-ir.ts +96 -48
- package/src/prop-rewrite.ts +11 -0
- package/src/props-binding.ts +171 -6
- package/src/ssr-defaults.ts +10 -5
- package/src/static-literal.ts +21 -9
- package/src/types.ts +7 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Destructured props stay reactive: every value-position read of a
|
|
3
|
+
* destructured prop name in the compiled `init*` body is a live `_p.<key>`
|
|
4
|
+
* read (or `(_p.<key> ?? <fallback>)`), the same shape `props.xxx` access
|
|
5
|
+
* compiles to. See `props-binding.ts`'s `livePropReadExpr` and
|
|
6
|
+
* `ir-to-client-js/rewrite-destructured-props.ts`.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { describe, test, expect } from 'bun:test'
|
|
10
|
+
import { compileJSX } from '../compiler'
|
|
11
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
12
|
+
import { ErrorCodes } from '../errors'
|
|
13
|
+
|
|
14
|
+
const adapter = new TestAdapter()
|
|
15
|
+
|
|
16
|
+
function compileClientJs(source: string, filename = 'Component.tsx') {
|
|
17
|
+
const result = compileJSX(source, filename, { adapter })
|
|
18
|
+
const errors = result.errors.filter((e) => e.severity === 'error')
|
|
19
|
+
expect(errors).toEqual([])
|
|
20
|
+
const clientJs = result.files.find((f) => f.type === 'clientJs')
|
|
21
|
+
expect(clientJs).toBeDefined()
|
|
22
|
+
return { content: clientJs!.content, result }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe('destructured props compile to live reads', () => {
|
|
26
|
+
test('BF043 no longer exists as an error code', () => {
|
|
27
|
+
// Checked by string value, not by a removed `ErrorCodes` key (that
|
|
28
|
+
// would be a compile error) — same convention as
|
|
29
|
+
// `invalid-signal-usage.audit.test.ts`'s BF012 deletion audit.
|
|
30
|
+
expect(Object.values(ErrorCodes)).not.toContain('BF043')
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
test('a destructured prop no longer gets a captured-once extraction line, and no BF043 diagnostic appears', () => {
|
|
34
|
+
const source = `
|
|
35
|
+
'use client'
|
|
36
|
+
import { createSignal, createMemo, createEffect } from '@barefootjs/client'
|
|
37
|
+
|
|
38
|
+
function Child({ value, onClick, label = 'n', size = 'md', items = [] }: {
|
|
39
|
+
value: number
|
|
40
|
+
onClick: () => void
|
|
41
|
+
label?: string
|
|
42
|
+
size?: string
|
|
43
|
+
items?: string[]
|
|
44
|
+
}) {
|
|
45
|
+
const doubled = createMemo(() => value * 2)
|
|
46
|
+
const [local, setLocal] = createSignal(value)
|
|
47
|
+
createEffect(() => {
|
|
48
|
+
console.log('value is', value)
|
|
49
|
+
})
|
|
50
|
+
return (
|
|
51
|
+
<div className={size}>
|
|
52
|
+
<span data-label={label}>{value}</span>
|
|
53
|
+
<span>{doubled()}</span>
|
|
54
|
+
<span>{items.length}</span>
|
|
55
|
+
<button onClick={() => { setLocal(value); onClick() }}>click</button>
|
|
56
|
+
</div>
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
`
|
|
60
|
+
const { content, result } = compileClientJs(source, 'Child.tsx')
|
|
61
|
+
|
|
62
|
+
// No captured-once local for any destructured prop.
|
|
63
|
+
expect(content).not.toContain('const value = _p.value')
|
|
64
|
+
expect(content).not.toContain('const onClick = _p.onClick')
|
|
65
|
+
expect(content).not.toContain('const label = ')
|
|
66
|
+
expect(content).not.toContain('const size = ')
|
|
67
|
+
expect(content).not.toContain('const items = ')
|
|
68
|
+
|
|
69
|
+
// Memo computation reads the live prop directly.
|
|
70
|
+
expect(content).toMatch(/createMemo\(\(\) => _p\.value \* 2\)/)
|
|
71
|
+
|
|
72
|
+
// The user effect reads the live prop, not a captured local.
|
|
73
|
+
expect(content).toContain("console.log('value is', _p.value)")
|
|
74
|
+
|
|
75
|
+
// The handler reads the live prop for both the signal update and the
|
|
76
|
+
// callback-prop invocation.
|
|
77
|
+
expect(content).toContain('_p.onClick()')
|
|
78
|
+
expect(content).toContain('setLocal(_p.value)')
|
|
79
|
+
|
|
80
|
+
// `items`'s destructure default survives as a live fallback, not a
|
|
81
|
+
// one-time extraction.
|
|
82
|
+
expect(content).toContain('(_p.items ?? []).length')
|
|
83
|
+
|
|
84
|
+
// No BF043 diagnostic (retired) anywhere in errors/warnings.
|
|
85
|
+
expect(result.errors.some((e) => e.code === 'BF043')).toBe(false)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('an aliased destructured prop (`{ n: count }`) reads the caller-facing key live everywhere', () => {
|
|
89
|
+
const source = `
|
|
90
|
+
'use client'
|
|
91
|
+
import { createEffect } from '@barefootjs/client'
|
|
92
|
+
function Badge({ text, n: count }: { text: string; n: number }) {
|
|
93
|
+
createEffect(() => {
|
|
94
|
+
console.log(count)
|
|
95
|
+
})
|
|
96
|
+
return <span>{text}:{count}</span>
|
|
97
|
+
}
|
|
98
|
+
`
|
|
99
|
+
const { content } = compileClientJs(source, 'Badge.tsx')
|
|
100
|
+
expect(content).not.toContain('const count = ')
|
|
101
|
+
expect(content).not.toContain('_p.count')
|
|
102
|
+
expect(content).toContain('console.log(_p.n)')
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
test('an accessor-typed prop (`value: () => number`) still gets CALLED at the live read site', () => {
|
|
106
|
+
const source = `
|
|
107
|
+
'use client'
|
|
108
|
+
import { createEffect } from '@barefootjs/client'
|
|
109
|
+
function Child({ value }: { value: () => number }) {
|
|
110
|
+
createEffect(() => {
|
|
111
|
+
console.log(value())
|
|
112
|
+
})
|
|
113
|
+
return <span>{value()}</span>
|
|
114
|
+
}
|
|
115
|
+
`
|
|
116
|
+
const { content } = compileClientJs(source, 'Child.tsx')
|
|
117
|
+
expect(content).not.toContain('const value = _p.value')
|
|
118
|
+
expect(content).toContain('_p.value()')
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
test('shadowing is preserved: a `.map()` callback param and a handler-local both keep their own binding', () => {
|
|
122
|
+
const source = `
|
|
123
|
+
'use client'
|
|
124
|
+
import { createSignal } from '@barefootjs/client'
|
|
125
|
+
function List({ title, items }: { title: string; items: { a: string }[] }) {
|
|
126
|
+
const [count, setCount] = createSignal(0)
|
|
127
|
+
return (
|
|
128
|
+
<div>
|
|
129
|
+
<span>{title}</span>
|
|
130
|
+
<ul>
|
|
131
|
+
{items.map((title) => <li key={title.a}>{title.a}</li>)}
|
|
132
|
+
</ul>
|
|
133
|
+
<button onClick={() => {
|
|
134
|
+
const title = 'local'
|
|
135
|
+
console.log(title)
|
|
136
|
+
setCount(count() + 1)
|
|
137
|
+
}}>click</button>
|
|
138
|
+
</div>
|
|
139
|
+
)
|
|
140
|
+
}
|
|
141
|
+
`
|
|
142
|
+
const { content } = compileClientJs(source, 'List.tsx')
|
|
143
|
+
// The outer `title` prop is rewritten wherever it is a free reference.
|
|
144
|
+
expect(content).toContain('_p.title')
|
|
145
|
+
// The `.map()` callback's OWN `title` parameter must never be rewritten
|
|
146
|
+
// — `(_p.title) => _p.title.a` would be invalid JS.
|
|
147
|
+
expect(content).not.toMatch(/\(_p\.title\)\s*=>/)
|
|
148
|
+
expect(content).toMatch(/\(title\)\s*=>[\s\S]*?title\.a/)
|
|
149
|
+
// The handler-local `const title = 'local'` shadows the prop for the
|
|
150
|
+
// rest of that handler — its own reference must stay bare.
|
|
151
|
+
expect(content).toContain("const title = 'local'")
|
|
152
|
+
expect(content).toContain('console.log(title)')
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
test('`children` keeps its one-time captured-once extraction (not rewritten live)', () => {
|
|
156
|
+
const source = `
|
|
157
|
+
'use client'
|
|
158
|
+
import { createSignal } from '@barefootjs/client'
|
|
159
|
+
function Wrapper({ children }: { children: unknown }) {
|
|
160
|
+
const [open, setOpen] = createSignal(true)
|
|
161
|
+
return <div onClick={() => setOpen(!open())}>{open() ? children : null}</div>
|
|
162
|
+
}
|
|
163
|
+
`
|
|
164
|
+
const { content } = compileClientJs(source, 'Wrapper.tsx')
|
|
165
|
+
expect(content).toContain('const children = _p.children')
|
|
166
|
+
})
|
|
167
|
+
})
|
|
@@ -433,9 +433,8 @@ for (const page of PAGES) {
|
|
|
433
433
|
//
|
|
434
434
|
// - "Implemented but doc snippet too minimal": the code IS emitted
|
|
435
435
|
// by the compiler in real programs, but the literal snippet in
|
|
436
|
-
// `error-codes.md` doesn't carry enough context (e.g.
|
|
437
|
-
// a
|
|
438
|
-
// These are doc-edit follow-ups.
|
|
436
|
+
// `error-codes.md` doesn't carry enough context (e.g. BF044 needs
|
|
437
|
+
// a declared signal getter). These are doc-edit follow-ups.
|
|
439
438
|
//
|
|
440
439
|
// Previously-documented-but-unimplemented codes were removed from the
|
|
441
440
|
// doc to keep it honest.
|
|
@@ -458,7 +457,6 @@ interface ErrorCodeContract {
|
|
|
458
457
|
// surrounding context the snippet omits).
|
|
459
458
|
const ERROR_CODES_DOC_TOO_MINIMAL: Record<string, string> = {
|
|
460
459
|
BF021: 'BF021 has multiple ❌ snippets in placeholder form (`.map(...)`) — covered concretely by `jsx-compatibility.md` tests; the doc-form snippets here remain placeholder',
|
|
461
|
-
BF043: 'BF043 fires only for STATEFUL components (signals/memos/effects). The doc snippet `function Child({ count }: Props)` has no reactivity, so the check is correctly silent — doc snippet needs enrichment to be testable as-is',
|
|
462
460
|
BF044: 'BF044 fires only when a real signal getter is bound. The doc snippet `<Child count={count} />` references an undeclared `count`, so the check is silent — doc snippet needs enrichment',
|
|
463
461
|
BF101: 'BF101 is raised by the non-JS template adapters\' own per-adapter checks (Go/Mojo/Xslate/… `renderLoop` / callback lowering), not by the shared `isSupported` gate every adapter shares — `TestAdapter` here is JS-runtime-style like Hono and executes both doc snippets verbatim, so the check is correctly silent. Covered concretely (compiled against the real 8 template adapters, with verbatim diagnostic text) by `jsx-compatibility.md`\'s doc-examples coverage and the `filter-nested-callback-predicate` / `filter-nested-find-predicate` / `static-array-from-props` adapter-conformance fixtures.',
|
|
464
462
|
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BarefootJS Compiler — container's own handler vs. a `.map()` row's
|
|
3
|
+
* delegated handler for the same event (#2930).
|
|
4
|
+
*
|
|
5
|
+
* `.map()` rows compiled via delegation share their delegated listener's
|
|
6
|
+
* DOM node with the loop's static container. When the container ALSO
|
|
7
|
+
* carries its own directly-authored handler for the same event, both used
|
|
8
|
+
* to become separate `addEventListener` calls on the identical node, so a
|
|
9
|
+
* row's `stopPropagation()` could never stop the container's own handler
|
|
10
|
+
* (native DOM: `stopPropagation()` only blocks bubbling to *other nodes*,
|
|
11
|
+
* never other listeners already registered on the *same* node).
|
|
12
|
+
*
|
|
13
|
+
* The fix merges the two into ONE listener: the delegated dispatch runs
|
|
14
|
+
* first, and the container's own handler only fires when the event's
|
|
15
|
+
* `cancelBubble` flag is still false — i.e. no row handler called
|
|
16
|
+
* `stopPropagation()`/`stopImmediatePropagation()`.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { describe, test, expect } from 'bun:test'
|
|
20
|
+
import { compileJSX } from '../compiler'
|
|
21
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
22
|
+
|
|
23
|
+
const adapter = new TestAdapter()
|
|
24
|
+
|
|
25
|
+
function compileClientJs(source: string): string {
|
|
26
|
+
const result = compileJSX(source, 'Repro.tsx', { adapter })
|
|
27
|
+
expect(result.errors).toHaveLength(0)
|
|
28
|
+
const clientJs = result.files.find(f => f.type === 'clientJs')
|
|
29
|
+
expect(clientJs).toBeDefined()
|
|
30
|
+
return clientJs!.content
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
describe('event delegation vs. container own handler (#2930)', () => {
|
|
34
|
+
test('static array: container onContextMenu merges into the row delegation listener', () => {
|
|
35
|
+
const source = `
|
|
36
|
+
'use client'
|
|
37
|
+
const items = ['a', 'b', 'c']
|
|
38
|
+
|
|
39
|
+
export function Repro() {
|
|
40
|
+
return (
|
|
41
|
+
<div
|
|
42
|
+
id="container"
|
|
43
|
+
onContextMenu={(e) => {
|
|
44
|
+
e.preventDefault()
|
|
45
|
+
console.log('container handler')
|
|
46
|
+
}}
|
|
47
|
+
>
|
|
48
|
+
{items.map(item => (
|
|
49
|
+
<div
|
|
50
|
+
key={item}
|
|
51
|
+
data-item={item}
|
|
52
|
+
onContextMenu={(e) => {
|
|
53
|
+
e.preventDefault()
|
|
54
|
+
e.stopPropagation()
|
|
55
|
+
console.log('row handler', item)
|
|
56
|
+
}}
|
|
57
|
+
>
|
|
58
|
+
row {item}
|
|
59
|
+
</div>
|
|
60
|
+
))}
|
|
61
|
+
</div>
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
`
|
|
65
|
+
const content = compileClientJs(source)
|
|
66
|
+
|
|
67
|
+
// Exactly one addEventListener('contextmenu', ...) on the shared
|
|
68
|
+
// container — not two separate registrations.
|
|
69
|
+
const registrations = content.match(/\.addEventListener\('contextmenu'/g) ?? []
|
|
70
|
+
expect(registrations).toHaveLength(1)
|
|
71
|
+
|
|
72
|
+
// The merged listener fires the row handler, then gates the
|
|
73
|
+
// container's own handler on `cancelBubble`, and also fires it
|
|
74
|
+
// unconditionally when no row matched (click on the container's own
|
|
75
|
+
// background).
|
|
76
|
+
expect(content).toContain('row handler')
|
|
77
|
+
expect(content).toContain('container handler')
|
|
78
|
+
expect(content).toContain('!__bfEvt.cancelBubble')
|
|
79
|
+
|
|
80
|
+
// The row handler must be checked (and able to call stopPropagation)
|
|
81
|
+
// BEFORE the cancelBubble-gated container call.
|
|
82
|
+
const rowPos = content.indexOf("console.log('row handler'")
|
|
83
|
+
const gatePos = content.indexOf('!__bfEvt.cancelBubble')
|
|
84
|
+
expect(rowPos).toBeGreaterThan(-1)
|
|
85
|
+
expect(gatePos).toBeGreaterThan(-1)
|
|
86
|
+
expect(rowPos).toBeLessThan(gatePos)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
test('dynamic loop: container onContextMenu merges into the row delegation listener', () => {
|
|
90
|
+
const source = `
|
|
91
|
+
'use client'
|
|
92
|
+
import { createSignal } from '@barefootjs/client'
|
|
93
|
+
|
|
94
|
+
export function Repro() {
|
|
95
|
+
const [items, setItems] = createSignal(['a', 'b', 'c'])
|
|
96
|
+
return (
|
|
97
|
+
<div
|
|
98
|
+
onContextMenu={(e) => {
|
|
99
|
+
e.preventDefault()
|
|
100
|
+
console.log('container handler')
|
|
101
|
+
}}
|
|
102
|
+
>
|
|
103
|
+
{items().map(item => (
|
|
104
|
+
<div
|
|
105
|
+
key={item}
|
|
106
|
+
onContextMenu={(e) => {
|
|
107
|
+
e.preventDefault()
|
|
108
|
+
e.stopPropagation()
|
|
109
|
+
console.log('row handler', item)
|
|
110
|
+
}}
|
|
111
|
+
>
|
|
112
|
+
row {item}
|
|
113
|
+
</div>
|
|
114
|
+
))}
|
|
115
|
+
</div>
|
|
116
|
+
)
|
|
117
|
+
}
|
|
118
|
+
`
|
|
119
|
+
const content = compileClientJs(source)
|
|
120
|
+
|
|
121
|
+
const registrations = content.match(/\.addEventListener\('contextmenu'/g) ?? []
|
|
122
|
+
expect(registrations).toHaveLength(1)
|
|
123
|
+
expect(content).toContain('!__bfEvt.cancelBubble')
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
test('container handler alone (no row collision) stays byte-unaffected', () => {
|
|
127
|
+
const source = `
|
|
128
|
+
'use client'
|
|
129
|
+
const items = ['a', 'b', 'c']
|
|
130
|
+
|
|
131
|
+
export function Repro() {
|
|
132
|
+
return (
|
|
133
|
+
<div onContextMenu={(e) => console.log('container handler')}>
|
|
134
|
+
{items.map(item => <div key={item}>row {item}</div>)}
|
|
135
|
+
</div>
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
`
|
|
139
|
+
const content = compileClientJs(source)
|
|
140
|
+
|
|
141
|
+
// No loop event to delegate at all — the container's own listener is
|
|
142
|
+
// the ordinary direct-listener emission, unaffected by this change.
|
|
143
|
+
expect(content).not.toContain('cancelBubble')
|
|
144
|
+
const registrations = content.match(/\.addEventListener\('contextmenu'/g) ?? []
|
|
145
|
+
expect(registrations).toHaveLength(1)
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
test('row handler alone (no container collision) stays byte-unaffected', () => {
|
|
149
|
+
const source = `
|
|
150
|
+
'use client'
|
|
151
|
+
const items = ['a', 'b', 'c']
|
|
152
|
+
|
|
153
|
+
export function Repro() {
|
|
154
|
+
return (
|
|
155
|
+
<div>
|
|
156
|
+
{items.map(item => (
|
|
157
|
+
<div key={item} onContextMenu={(e) => { e.stopPropagation(); console.log('row handler', item) }}>
|
|
158
|
+
row {item}
|
|
159
|
+
</div>
|
|
160
|
+
))}
|
|
161
|
+
</div>
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
`
|
|
165
|
+
const content = compileClientJs(source)
|
|
166
|
+
|
|
167
|
+
// Delegation-only — no own handler to gate, so no cancelBubble check
|
|
168
|
+
// is introduced.
|
|
169
|
+
expect(content).not.toContain('cancelBubble')
|
|
170
|
+
const registrations = content.match(/\.addEventListener\('contextmenu'/g) ?? []
|
|
171
|
+
expect(registrations).toHaveLength(1)
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
test('two real nested elements (no .map()) keep two independent listeners', () => {
|
|
175
|
+
// The structurally simpler case from the issue report: no delegation
|
|
176
|
+
// involved at all, so native bubbling already does the right thing and
|
|
177
|
+
// this change must not touch it.
|
|
178
|
+
const source = `
|
|
179
|
+
'use client'
|
|
180
|
+
|
|
181
|
+
export function Repro() {
|
|
182
|
+
return (
|
|
183
|
+
<div onContextMenu={(e) => console.log('outer')}>
|
|
184
|
+
<div onContextMenu={(e) => { e.stopPropagation(); console.log('inner') }}>
|
|
185
|
+
inner
|
|
186
|
+
</div>
|
|
187
|
+
</div>
|
|
188
|
+
)
|
|
189
|
+
}
|
|
190
|
+
`
|
|
191
|
+
const content = compileClientJs(source)
|
|
192
|
+
|
|
193
|
+
expect(content).not.toContain('cancelBubble')
|
|
194
|
+
const registrations = content.match(/\.addEventListener\('contextmenu'/g) ?? []
|
|
195
|
+
expect(registrations).toHaveLength(2)
|
|
196
|
+
})
|
|
197
|
+
})
|
|
@@ -28,8 +28,10 @@ describe('formatError — diagnostic rendering contract', () => {
|
|
|
28
28
|
})
|
|
29
29
|
|
|
30
30
|
test('warning severity also lowercases', () => {
|
|
31
|
+
// Any code works: `severity` is an explicit override on `createError`,
|
|
32
|
+
// independent of the code's own default.
|
|
31
33
|
const err = createError(
|
|
32
|
-
ErrorCodes.
|
|
34
|
+
ErrorCodes.MISSING_KEY_IN_LIST,
|
|
33
35
|
{
|
|
34
36
|
file: 'components/Display.tsx',
|
|
35
37
|
start: { line: 1, column: 17 },
|
|
@@ -39,7 +41,7 @@ describe('formatError — diagnostic rendering contract', () => {
|
|
|
39
41
|
)
|
|
40
42
|
|
|
41
43
|
const out = formatError(err)
|
|
42
|
-
expect(out).toContain('warning[
|
|
44
|
+
expect(out).toContain('warning[BF023]:')
|
|
43
45
|
})
|
|
44
46
|
|
|
45
47
|
test('--> row carries the documented file:line:col location', () => {
|
|
@@ -90,7 +92,7 @@ describe('formatError — diagnostic rendering contract', () => {
|
|
|
90
92
|
|
|
91
93
|
test('suggestion renders as a `= help:` row at the tail', () => {
|
|
92
94
|
const err = createError(
|
|
93
|
-
ErrorCodes.
|
|
95
|
+
ErrorCodes.MISSING_KEY_IN_LIST,
|
|
94
96
|
{
|
|
95
97
|
file: 'Display.tsx',
|
|
96
98
|
start: { line: 1, column: 17 },
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `liveOnlyProps` classification (Move C, Prop Boundary Contract).
|
|
3
|
+
*
|
|
4
|
+
* `computeLiveOnlyProps` (`ir-to-client-js/index.ts`) marks which props the
|
|
5
|
+
* client code CALLS rather than reads, so they cannot arrive as JSON. It
|
|
6
|
+
* only classifies — whether that matters for a given mount is decided at
|
|
7
|
+
* render time by the Hono adapter's gated `serializeHydrationProps` call
|
|
8
|
+
* (`serialize-hydration-props.test.ts` covers that half).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { describe, test, expect } from 'bun:test'
|
|
12
|
+
import { analyzeComponent } from '../analyzer'
|
|
13
|
+
import { jsxToIR } from '../jsx-to-ir'
|
|
14
|
+
import { buildMetadata } from '../compiler'
|
|
15
|
+
import { analyzeClientNeeds } from '../ir-to-client-js'
|
|
16
|
+
import type { ComponentIR } from '../types'
|
|
17
|
+
|
|
18
|
+
function analyze(source: string): ReturnType<typeof analyzeClientNeeds> {
|
|
19
|
+
const ctx = analyzeComponent(source, 'Test.tsx')
|
|
20
|
+
const ir = jsxToIR(ctx)
|
|
21
|
+
expect(ir).not.toBeNull()
|
|
22
|
+
const componentIR: ComponentIR = {
|
|
23
|
+
version: '0.1',
|
|
24
|
+
metadata: buildMetadata(ctx),
|
|
25
|
+
root: ir!,
|
|
26
|
+
errors: [],
|
|
27
|
+
}
|
|
28
|
+
return analyzeClientNeeds(componentIR)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe('liveOnlyProps — fires', () => {
|
|
32
|
+
test('a function-typed prop actually called by client code lands in liveOnlyProps', () => {
|
|
33
|
+
const analysis = analyze(`
|
|
34
|
+
'use client'
|
|
35
|
+
export function Display({ value }: { value: () => number }) {
|
|
36
|
+
return <button onClick={() => console.log(value())}>{value()}</button>
|
|
37
|
+
}
|
|
38
|
+
`)
|
|
39
|
+
expect(analysis.needsInit).toBe(true)
|
|
40
|
+
expect(analysis.usedProps).toContain('value')
|
|
41
|
+
expect(Object.keys(analysis.liveOnlyProps)).toContain('value')
|
|
42
|
+
expect(analysis.liveOnlyProps.value).toBe('() => number')
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
test('object-form Function-typed prop actually called by client code also lands in liveOnlyProps', () => {
|
|
46
|
+
const analysis = analyze(`
|
|
47
|
+
'use client'
|
|
48
|
+
export function Foo({ cb }: { cb: Function }) {
|
|
49
|
+
return <button onClick={() => cb()}>go</button>
|
|
50
|
+
}
|
|
51
|
+
`)
|
|
52
|
+
expect(Object.keys(analysis.liveOnlyProps)).toContain('cb')
|
|
53
|
+
expect(analysis.liveOnlyProps.cb).toBe('Function')
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('renamed destructured prop is keyed by its caller-facing (source) name, not the local binding', () => {
|
|
57
|
+
const analysis = analyze(`
|
|
58
|
+
'use client'
|
|
59
|
+
export function Display({ value: getValue }: { value: () => number }) {
|
|
60
|
+
return <button onClick={() => console.log(getValue())}>go</button>
|
|
61
|
+
}
|
|
62
|
+
`)
|
|
63
|
+
expect(Object.keys(analysis.liveOnlyProps)).toContain('value')
|
|
64
|
+
expect(analysis.liveOnlyProps).not.toHaveProperty('getValue')
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('an OPTIONAL function-typed prop still resolves (stripUnion drops the undefined arm)', () => {
|
|
68
|
+
const analysis = analyze(`
|
|
69
|
+
'use client'
|
|
70
|
+
export function Display({ value }: { value?: () => number }) {
|
|
71
|
+
return <button onClick={() => console.log(value?.())}>go</button>
|
|
72
|
+
}
|
|
73
|
+
`)
|
|
74
|
+
expect(Object.keys(analysis.liveOnlyProps)).toContain('value')
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('props-object mode (props.value) resolves the same way', () => {
|
|
78
|
+
const analysis = analyze(`
|
|
79
|
+
'use client'
|
|
80
|
+
export function Display(props: { value: () => number }) {
|
|
81
|
+
return <button onClick={() => console.log(props.value())}>go</button>
|
|
82
|
+
}
|
|
83
|
+
`)
|
|
84
|
+
expect(Object.keys(analysis.liveOnlyProps)).toContain('value')
|
|
85
|
+
})
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
describe('liveOnlyProps — silent', () => {
|
|
89
|
+
test('a function-typed prop declared but never read by client code is not classified', () => {
|
|
90
|
+
// `usedProps` filtering already excludes it upstream (never even
|
|
91
|
+
// reaches the client init), so it can't land in liveOnlyProps either.
|
|
92
|
+
const analysis = analyze(`
|
|
93
|
+
'use client'
|
|
94
|
+
export function Display({ value, label }: { value: () => number; label: string }) {
|
|
95
|
+
return <button onClick={() => console.log(label)}>{label}</button>
|
|
96
|
+
}
|
|
97
|
+
`)
|
|
98
|
+
expect(analysis.usedProps).not.toContain('value')
|
|
99
|
+
expect(analysis.liveOnlyProps).not.toHaveProperty('value')
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
test('onClick-shaped (event-handler-convention) prop is excluded even though it is function-typed', () => {
|
|
103
|
+
const analysis = analyze(`
|
|
104
|
+
interface Props { onClick: () => void }
|
|
105
|
+
export function Clickable({ onClick }: Props) {
|
|
106
|
+
return <div onClick={onClick}>Click me</div>
|
|
107
|
+
}
|
|
108
|
+
`)
|
|
109
|
+
expect(analysis.liveOnlyProps).not.toHaveProperty('onClick')
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
test('__-prefixed internal prop is excluded even though it is function-typed', () => {
|
|
113
|
+
const analysis = analyze(`
|
|
114
|
+
'use client'
|
|
115
|
+
export function Internal({ __cb }: { __cb: () => void }) {
|
|
116
|
+
return <button onClick={() => __cb()}>go</button>
|
|
117
|
+
}
|
|
118
|
+
`)
|
|
119
|
+
expect(analysis.liveOnlyProps).not.toHaveProperty('__cb')
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
test('a non-function-typed prop (number) is never in liveOnlyProps', () => {
|
|
123
|
+
const analysis = analyze(`
|
|
124
|
+
'use client'
|
|
125
|
+
export function Counter({ value }: { value: number }) {
|
|
126
|
+
return <button onClick={() => console.log(value)}>{value}</button>
|
|
127
|
+
}
|
|
128
|
+
`)
|
|
129
|
+
expect(analysis.usedProps).toContain('value')
|
|
130
|
+
expect(analysis.liveOnlyProps).not.toHaveProperty('value')
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
test('an ALIASED function type is a known conservative miss, not a classification', () => {
|
|
134
|
+
// `typeNodeToTypeInfo` lowers a named type reference to
|
|
135
|
+
// `{ kind: 'interface', raw: 'Filter' }` with no member walk, so the
|
|
136
|
+
// function shape behind the alias is invisible here and the prop keeps
|
|
137
|
+
// the pre-Move-C silent drop. BF049 has the same blind spot for aliased
|
|
138
|
+
// rich types. Pinned so a future alias-resolution change is a deliberate
|
|
139
|
+
// flip of this expectation, not an accident.
|
|
140
|
+
const analysis = analyze(`
|
|
141
|
+
'use client'
|
|
142
|
+
type Filter = (s: string) => boolean
|
|
143
|
+
export function Display({ filter }: { filter: Filter }) {
|
|
144
|
+
return <button onClick={() => console.log(filter('x'))}>go</button>
|
|
145
|
+
}
|
|
146
|
+
`)
|
|
147
|
+
expect(analysis.usedProps).toContain('filter')
|
|
148
|
+
expect(analysis.liveOnlyProps).not.toHaveProperty('filter')
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
test('a static (non-client) component has no clientAnalysis needs at all — liveOnlyProps is empty', () => {
|
|
152
|
+
const analysis = analyze(`
|
|
153
|
+
export function Static({ value }: { value: () => number }) {
|
|
154
|
+
return <span>static</span>
|
|
155
|
+
}
|
|
156
|
+
`)
|
|
157
|
+
expect(analysis.needsInit).toBe(false)
|
|
158
|
+
expect(analysis.liveOnlyProps).toEqual({})
|
|
159
|
+
})
|
|
160
|
+
})
|
|
@@ -212,7 +212,7 @@ describe('PropReference extraction', () => {
|
|
|
212
212
|
})
|
|
213
213
|
|
|
214
214
|
describe('ClientJS generation with semantic prop refs', () => {
|
|
215
|
-
test('destructured props
|
|
215
|
+
test('destructured props compile to a live _p.open read, no captured-once local', () => {
|
|
216
216
|
const source = `
|
|
217
217
|
'use client'
|
|
218
218
|
|
|
@@ -231,10 +231,10 @@ describe('ClientJS generation with semantic prop refs', () => {
|
|
|
231
231
|
|
|
232
232
|
const clientJs = result.files.find((f) => f.type === 'clientJs')
|
|
233
233
|
expect(clientJs).toBeDefined()
|
|
234
|
-
//
|
|
235
|
-
expect(clientJs?.content).toContain('const open =
|
|
236
|
-
|
|
237
|
-
//
|
|
234
|
+
// No captured-once local: the conditional reads `_p.open` live.
|
|
235
|
+
expect(clientJs?.content).not.toContain('const open = ')
|
|
236
|
+
expect(clientJs?.content).toContain('insert(__scope, \'s0\', () => _p.open,')
|
|
237
|
+
// Never a stale `props.` prefix either.
|
|
238
238
|
expect(clientJs?.content).not.toMatch(/insert\([^)]*props\.open/)
|
|
239
239
|
})
|
|
240
240
|
|
|
@@ -287,7 +287,7 @@ describe('ClientJS generation with semantic prop refs', () => {
|
|
|
287
287
|
expect(clientJs?.content).not.toContain('window.props.open')
|
|
288
288
|
})
|
|
289
289
|
|
|
290
|
-
test('destructured props with default value
|
|
290
|
+
test('destructured props with default value read live, with the default applied at every read', () => {
|
|
291
291
|
const source = `
|
|
292
292
|
'use client'
|
|
293
293
|
|
|
@@ -306,8 +306,10 @@ describe('ClientJS generation with semantic prop refs', () => {
|
|
|
306
306
|
|
|
307
307
|
const clientJs = result.files.find((f) => f.type === 'clientJs')
|
|
308
308
|
expect(clientJs).toBeDefined()
|
|
309
|
-
//
|
|
310
|
-
|
|
309
|
+
// No captured-once local — the live read carries the default at the
|
|
310
|
+
// read site itself: `(_p.open ?? false)`.
|
|
311
|
+
expect(clientJs?.content).not.toContain('const open = ')
|
|
312
|
+
expect(clientJs?.content).toMatch(/\(_p\.open \?\? false\)/)
|
|
311
313
|
})
|
|
312
314
|
})
|
|
313
315
|
|
|
@@ -319,8 +321,13 @@ describe('ClientJS generation with semantic prop refs', () => {
|
|
|
319
321
|
// reachable), but was never wired to `ctx.clientOnlyElements` either, so no
|
|
320
322
|
// edge of ANY kind reached `neededProps` and `emitPropsExtraction` had no
|
|
321
323
|
// evidence the prop was used at all.
|
|
324
|
+
//
|
|
325
|
+
// The bug class is now structurally impossible for a destructured prop:
|
|
326
|
+
// `rewriteDestructuredPropReads` walks the joined init body and consults
|
|
327
|
+
// neither `neededProps` nor the reference graph, so there is no edge left
|
|
328
|
+
// for an emitter to forget to wire up.
|
|
322
329
|
describe('Issue #2634 regression', () => {
|
|
323
|
-
test('a destructured prop used only inside /* @client */
|
|
330
|
+
test('a destructured prop used only inside /* @client */ reads _p.label directly (no unbound identifier)', () => {
|
|
324
331
|
const source = `
|
|
325
332
|
interface Props {
|
|
326
333
|
label: string
|
|
@@ -336,11 +343,11 @@ describe('Issue #2634 regression', () => {
|
|
|
336
343
|
expect(result.errors).toHaveLength(0)
|
|
337
344
|
const clientJs = result.files.find((f) => f.type === 'clientJs')
|
|
338
345
|
expect(clientJs).toBeDefined()
|
|
339
|
-
//
|
|
340
|
-
|
|
341
|
-
//
|
|
342
|
-
|
|
343
|
-
expect(clientJs?.content).
|
|
346
|
+
// A bare, unbound `label` is the shape that threw ReferenceError at
|
|
347
|
+
// hydrate pre-#2634; the live `_p.label` read is what replaces both
|
|
348
|
+
// that bug and its original captured-once fix.
|
|
349
|
+
expect(clientJs?.content).not.toContain('const label = ')
|
|
350
|
+
expect(clientJs?.content).toContain('_p.label.toUpperCase()')
|
|
344
351
|
})
|
|
345
352
|
|
|
346
353
|
test('a props-object member used only inside /* @client */ needs no extraction (already _p.x)', () => {
|
|
@@ -362,7 +369,7 @@ describe('Issue #2634 regression', () => {
|
|
|
362
369
|
expect(clientJs?.content).toContain('_p.label.toUpperCase()')
|
|
363
370
|
})
|
|
364
371
|
|
|
365
|
-
test('a prop used both in a normal reactive position AND inside /* @client */
|
|
372
|
+
test('a prop used both in a normal reactive position AND inside /* @client */ reads _p.label live at BOTH sites (no extraction)', () => {
|
|
366
373
|
const source = `
|
|
367
374
|
interface Props {
|
|
368
375
|
label: string
|
|
@@ -378,8 +385,10 @@ describe('Issue #2634 regression', () => {
|
|
|
378
385
|
expect(result.errors).toHaveLength(0)
|
|
379
386
|
const clientJs = result.files.find((f) => f.type === 'clientJs')
|
|
380
387
|
expect(clientJs).toBeDefined()
|
|
381
|
-
|
|
382
|
-
expect(
|
|
388
|
+
// Each of the two reference sites reads `_p.label` live, independently.
|
|
389
|
+
expect(clientJs?.content).not.toContain('const label = ')
|
|
390
|
+
const matches = clientJs?.content.match(/_p\.label/g) ?? []
|
|
391
|
+
expect(matches.length).toBeGreaterThanOrEqual(2)
|
|
383
392
|
})
|
|
384
393
|
})
|
|
385
394
|
|