@barefootjs/jsx 0.35.5 → 0.35.7
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 +6745 -6488
- 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/html-template.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 +123 -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-arrow.test.ts +142 -0
- 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__/issue-2960-multi-root-cond-same-tag.test.ts +141 -0
- 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 +88 -89
- 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/html-template.ts +65 -9
- 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 +222 -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 +185 -6
- package/src/ssr-defaults.ts +10 -5
- package/src/static-literal.ts +21 -9
- package/src/types.ts +7 -0
|
@@ -9,8 +9,12 @@
|
|
|
9
9
|
* (the caller passes `n`) and the local binding hydrated to `undefined`.
|
|
10
10
|
*
|
|
11
11
|
* Mirrors `ssr-defaults.test.ts`'s aliased-prop describes (#2460) for the
|
|
12
|
-
* SSR-defaults half; this covers the generated `initXxx`
|
|
13
|
-
*
|
|
12
|
+
* SSR-defaults half; this covers the generated `initXxx` body and the CSR
|
|
13
|
+
* `template:` lambda.
|
|
14
|
+
*
|
|
15
|
+
* The rule pinned here is the caller-facing key: `_p` is keyed by
|
|
16
|
+
* `sourceName ?? name`, read at every reference site (there is no
|
|
17
|
+
* captured-once `const <local> = _p.<callerKey>` extraction).
|
|
14
18
|
*/
|
|
15
19
|
|
|
16
20
|
import { describe, test, expect } from 'bun:test'
|
|
@@ -37,10 +41,11 @@ describe('aliased destructured props reach client JS under the caller-facing key
|
|
|
37
41
|
|
|
38
42
|
const clientJs = result.files.find((f) => f.type === 'clientJs')
|
|
39
43
|
expect(clientJs).toBeDefined()
|
|
40
|
-
//
|
|
41
|
-
//
|
|
42
|
-
expect(clientJs!.content).toContain('const count =
|
|
44
|
+
// Every reference reads the CALLER-facing key `n`, never the local
|
|
45
|
+
// key `count`, and no local binding is introduced for it.
|
|
46
|
+
expect(clientJs!.content).not.toContain('const count = ')
|
|
43
47
|
expect(clientJs!.content).not.toContain('_p.count')
|
|
48
|
+
expect(clientJs!.content).toContain('console.log(_p.n)')
|
|
44
49
|
// The CSR `template:` lambda (module-scope SSR fallback) reads the
|
|
45
50
|
// same caller-facing key.
|
|
46
51
|
// `escapeTextOrMarkup`, not bare `escapeText` — this slot is
|
|
@@ -68,7 +73,11 @@ describe('aliased destructured props reach client JS under the caller-facing key
|
|
|
68
73
|
|
|
69
74
|
const clientJs = result.files.find((f) => f.type === 'clientJs')
|
|
70
75
|
expect(clientJs).toBeDefined()
|
|
71
|
-
|
|
76
|
+
// `sourceName ?? name` is an identity for an un-aliased prop, so this
|
|
77
|
+
// is indistinguishable from the aliased case above once the
|
|
78
|
+
// caller-facing key is substituted.
|
|
79
|
+
expect(clientJs!.content).not.toContain('const n = ')
|
|
80
|
+
expect(clientJs!.content).toContain('console.log(_p.n)')
|
|
72
81
|
// `escapeTextOrMarkup`, not bare `escapeText` — this slot is
|
|
73
82
|
// claim-plan `kind: 'markup'` (#2651), unrelated to the #2524 aliasing
|
|
74
83
|
// concern this test pins; only the escape call's name changed.
|
|
@@ -112,7 +121,10 @@ describe('aliased destructured props reach client JS under the caller-facing key
|
|
|
112
121
|
|
|
113
122
|
const clientJs = result.files.find((f) => f.type === 'clientJs')
|
|
114
123
|
expect(clientJs).toBeDefined()
|
|
115
|
-
|
|
124
|
+
// The handler reference reads the CALLER-facing key `onPress`, never
|
|
125
|
+
// the local key `handlePress`.
|
|
126
|
+
expect(clientJs!.content).not.toContain('const handlePress = ')
|
|
127
|
+
expect(clientJs!.content).toContain('_p.onPress')
|
|
116
128
|
expect(clientJs!.content).not.toContain('_p.handlePress')
|
|
117
129
|
})
|
|
118
130
|
})
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Regression for #2940: a BODY-destructured prop's arrow-function default
|
|
3
|
+
* (`const { fmt = (v) => 'v' + v } = props`) used to build
|
|
4
|
+
* `ConstantInfo.value` as a bare `props.fmt ?? (v) => 'v' + v` —
|
|
5
|
+
* `collectConstant`'s body-destructure branch (`analyzer.ts`) never checked
|
|
6
|
+
* whether the default contained an arrow/function expression before
|
|
7
|
+
* splicing it after `??`, unlike the PARAMETER-destructured sibling form,
|
|
8
|
+
* which already parenthesized via `ParamInfo.defaultContainsArrow`
|
|
9
|
+
* (`propReadFallback`, `props-binding.ts`).
|
|
10
|
+
*
|
|
11
|
+
* `a ?? (v) => expr` is not valid JS — an arrow function cannot appear
|
|
12
|
+
* directly as the right operand of `??` without parens — so this was a
|
|
13
|
+
* genuine SyntaxError in three places that all read `ConstantInfo.value`
|
|
14
|
+
* verbatim: the emitted client-JS init body, the Hono SSR component (the
|
|
15
|
+
* reference adapter also splices `value` unchanged), and every downstream
|
|
16
|
+
* pass that re-parses it (which silently gave up and skipped instead of
|
|
17
|
+
* crashing, masking the bug as a missed optimization rather than a syntax
|
|
18
|
+
* error).
|
|
19
|
+
*
|
|
20
|
+
* The fix routes the analyzer's `value`-building through the same
|
|
21
|
+
* `coalesceDefaultText` decision `propReadFallback` already used, so there
|
|
22
|
+
* is one implementation of "how does a `??` default get parenthesized",
|
|
23
|
+
* not two.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { describe, test, expect } from 'bun:test'
|
|
27
|
+
import ts from 'typescript'
|
|
28
|
+
import { analyzeComponent } from '../analyzer'
|
|
29
|
+
import { compileJSX } from '../index'
|
|
30
|
+
import { HonoAdapter } from '../../../adapter-hono/src/adapter/index'
|
|
31
|
+
|
|
32
|
+
const ARROW_DEFAULT_SOURCE = `
|
|
33
|
+
'use client'
|
|
34
|
+
function Child(props: { fmt?: (v: number) => string }) {
|
|
35
|
+
const { fmt = (v) => 'v' + v } = props
|
|
36
|
+
return <span>{fmt(1)}</span>
|
|
37
|
+
}
|
|
38
|
+
export { Child }
|
|
39
|
+
`
|
|
40
|
+
|
|
41
|
+
const RENAMED_ARROW_DEFAULT_SOURCE = `
|
|
42
|
+
'use client'
|
|
43
|
+
function Child(props: { fmt?: (v: number) => string }) {
|
|
44
|
+
const { fmt: f = (v) => 'v' + v } = props
|
|
45
|
+
return <span>{f(1)}</span>
|
|
46
|
+
}
|
|
47
|
+
export { Child }
|
|
48
|
+
`
|
|
49
|
+
|
|
50
|
+
// A HANDLER-scoped read (as opposed to a top-level reactive text position)
|
|
51
|
+
// goes through `rewriteBodyAliasReads` (`rewrite-destructured-props.ts`),
|
|
52
|
+
// which re-parses the analyzer's already-wrapped `ConstantInfo.value` and
|
|
53
|
+
// used to feed the still-`true` `defaultContainsArrow` flag back into
|
|
54
|
+
// `coalesceDefaultText`, double-wrapping the default a second time
|
|
55
|
+
// (`(_p.fmt ?? (((v) => 'v' + v)))(1)` instead of the minimal
|
|
56
|
+
// `(_p.fmt ?? ((v) => 'v' + v))(1)`) — pullfrog review on #2966.
|
|
57
|
+
const HANDLER_SCOPED_ARROW_DEFAULT_SOURCE = `
|
|
58
|
+
'use client'
|
|
59
|
+
import { createSignal } from '@barefootjs/client'
|
|
60
|
+
function ArrowDefaultFmt(props: { fmt?: (v: number) => string }) {
|
|
61
|
+
const { fmt = (v) => 'v' + v } = props
|
|
62
|
+
const [label, setLabel] = createSignal('none')
|
|
63
|
+
return <button onClick={() => setLabel(fmt(1))}>{label()}</button>
|
|
64
|
+
}
|
|
65
|
+
export { ArrowDefaultFmt }
|
|
66
|
+
`
|
|
67
|
+
|
|
68
|
+
function compile(source: string) {
|
|
69
|
+
return compileJSX(source, 'Child.tsx', { adapter: new HonoAdapter() })
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function parseDiagnosticCount(js: string): number {
|
|
73
|
+
const sourceFile = ts.createSourceFile('generated.js', js, ts.ScriptTarget.Latest, false, ts.ScriptKind.JS)
|
|
74
|
+
// @ts-expect-error - internal, but the established way this repo checks
|
|
75
|
+
// "did the generated code actually parse" (see client-template-escape-
|
|
76
|
+
// soundness.test.ts / map-body-no-silent-divergence.test.ts).
|
|
77
|
+
return sourceFile.parseDiagnostics?.length ?? 0
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
describe('body-destructured prop with an arrow-function default (#2940)', () => {
|
|
81
|
+
test('analyzer: `value` parenthesizes the arrow default', () => {
|
|
82
|
+
const ctx = analyzeComponent(ARROW_DEFAULT_SOURCE, 'Child.tsx')
|
|
83
|
+
const fmt = ctx.localConstants.find(c => c.name === 'fmt')
|
|
84
|
+
expect(fmt).toBeDefined()
|
|
85
|
+
expect(fmt?.value).toBe("props.fmt ?? ((v) => 'v' + v)")
|
|
86
|
+
expect(fmt?.containsArrow).toBe(true)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
test('emitted client JS parses as valid JS, with no bare `?? (v) =>`', () => {
|
|
90
|
+
const warnings: unknown[][] = []
|
|
91
|
+
const originalWarn = console.warn
|
|
92
|
+
console.warn = (...args: unknown[]) => { warnings.push(args) }
|
|
93
|
+
let result: ReturnType<typeof compile>
|
|
94
|
+
try {
|
|
95
|
+
result = compile(ARROW_DEFAULT_SOURCE)
|
|
96
|
+
} finally {
|
|
97
|
+
console.warn = originalWarn
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const clientJs = result.files.find(f => f.type === 'clientJs')
|
|
101
|
+
expect(clientJs).toBeDefined()
|
|
102
|
+
const js = clientJs!.content
|
|
103
|
+
|
|
104
|
+
expect(js).not.toMatch(/\?\?\s*\(v\)\s*=>/)
|
|
105
|
+
expect(parseDiagnosticCount(js)).toBe(0)
|
|
106
|
+
|
|
107
|
+
// The two downstream passes that used to silently bail because the
|
|
108
|
+
// generated code didn't parse must now run to completion.
|
|
109
|
+
expect(warnings.some(w => String(w[0]).includes('rewriteDestructuredPropReads'))).toBe(false)
|
|
110
|
+
expect(warnings.some(w => String(w[0]).includes('pruneUnusedPropExtractions'))).toBe(false)
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('Hono SSR component also parenthesizes the default (the reference adapter splices `value` verbatim too)', () => {
|
|
114
|
+
const result = compile(ARROW_DEFAULT_SOURCE)
|
|
115
|
+
const ssrComponent = result.files.find(f => f.type === 'markedTemplate')
|
|
116
|
+
expect(ssrComponent).toBeDefined()
|
|
117
|
+
expect(ssrComponent!.content).not.toMatch(/\?\?\s*\(v\)\s*=>/)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
test('renamed alias (`{ fmt: f = (v) => ... }`) gets the same paren-wrap', () => {
|
|
121
|
+
const ctx = analyzeComponent(RENAMED_ARROW_DEFAULT_SOURCE, 'Child.tsx')
|
|
122
|
+
const f = ctx.localConstants.find(c => c.name === 'f')
|
|
123
|
+
expect(f).toBeDefined()
|
|
124
|
+
expect(f?.value).toBe("props.fmt ?? ((v) => 'v' + v)")
|
|
125
|
+
expect(f?.containsArrow).toBe(true)
|
|
126
|
+
|
|
127
|
+
const result = compile(RENAMED_ARROW_DEFAULT_SOURCE)
|
|
128
|
+
const clientJs = result.files.find(f => f.type === 'clientJs')
|
|
129
|
+
expect(parseDiagnosticCount(clientJs!.content)).toBe(0)
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
test('a handler-scoped read wraps the arrow default exactly once, not twice', () => {
|
|
133
|
+
const result = compile(HANDLER_SCOPED_ARROW_DEFAULT_SOURCE)
|
|
134
|
+
const clientJs = result.files.find(f => f.type === 'clientJs')
|
|
135
|
+
expect(clientJs).toBeDefined()
|
|
136
|
+
const js = clientJs!.content
|
|
137
|
+
|
|
138
|
+
expect(parseDiagnosticCount(js)).toBe(0)
|
|
139
|
+
expect(js).toContain("(_p.fmt ?? ((v) => 'v' + v))(1)")
|
|
140
|
+
expect(js).not.toContain("(_p.fmt ?? (((v) => 'v' + v)))(1)")
|
|
141
|
+
})
|
|
142
|
+
})
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pins #2943's fix: a BODY-destructured prop's default (`const { label =
|
|
3
|
+
* 'none' } = props`) is now visible on `ir.metadata.propsParams` itself —
|
|
4
|
+
* the SAME shared `ParamInfo` a parameter-destructured default
|
|
5
|
+
* (`function Foo({ label = 'none' })`) already populated via
|
|
6
|
+
* `extractPropsFromTypeMembers`'s sibling path. Before this fix,
|
|
7
|
+
* `extractPropsFromTypeMembers` built `propsParams` purely from the TYPE
|
|
8
|
+
* annotation, with no notion of a body destructure's own default — every
|
|
9
|
+
* downstream SSR consumer of `ParamInfo.defaultValue`/`.optional` (each
|
|
10
|
+
* template adapter's presence-guard classification, `extractSsrDefaults`'s
|
|
11
|
+
* stash seed) then treated the prop as defaultless, guarding the attribute
|
|
12
|
+
* and omitting it entirely instead of falling back to the default the way
|
|
13
|
+
* Hono's real JS destructuring naturally does.
|
|
14
|
+
*
|
|
15
|
+
* See `analyzer.ts`'s `collectConstant` (the body-destructure branch) and
|
|
16
|
+
* `bindingElementDefaultFields` (the helper it now shares with the
|
|
17
|
+
* parameter-destructured path in `extractProps`).
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { describe, test, expect } from 'bun:test'
|
|
21
|
+
import { analyzeComponent } from '../analyzer'
|
|
22
|
+
import { buildMetadata } from '../compiler'
|
|
23
|
+
|
|
24
|
+
function propsParamsFor(source: string) {
|
|
25
|
+
const ctx = analyzeComponent(source, 'test.tsx')
|
|
26
|
+
return buildMetadata(ctx).propsParams
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe('body-destructured prop defaults overlay onto propsParams (#2943)', () => {
|
|
30
|
+
test('unrenamed default: the type-member entry gets defaultValue/parsed/optional', () => {
|
|
31
|
+
const propsParams = propsParamsFor(`
|
|
32
|
+
function Foo(props: { label?: string }) {
|
|
33
|
+
const { label = 'none' } = props
|
|
34
|
+
return <div data-label={label} />
|
|
35
|
+
}
|
|
36
|
+
`)
|
|
37
|
+
expect(propsParams).toEqual([
|
|
38
|
+
{
|
|
39
|
+
name: 'label',
|
|
40
|
+
type: { kind: 'primitive', raw: 'string', primitive: 'string' },
|
|
41
|
+
optional: true,
|
|
42
|
+
defaultValue: "'none'",
|
|
43
|
+
parsed: { kind: 'literal', value: 'none', literalType: 'string' },
|
|
44
|
+
},
|
|
45
|
+
])
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('renamed default: ADDS a second entry (sourceName-bearing) rather than replacing the original', () => {
|
|
49
|
+
// `label` itself stays a real, separately-classified binding (no
|
|
50
|
+
// default of its own — a bare `props.label` read elsewhere is still
|
|
51
|
+
// correctly guarded/stash-seeded), and `text` carries `sourceName:
|
|
52
|
+
// 'label'` so every `sourceName ?? name` consumer resolves the
|
|
53
|
+
// caller-facing key.
|
|
54
|
+
const propsParams = propsParamsFor(`
|
|
55
|
+
function Foo(props: { label?: string }) {
|
|
56
|
+
const { label: text = 'none' } = props
|
|
57
|
+
return <div data-label={text} />
|
|
58
|
+
}
|
|
59
|
+
`)
|
|
60
|
+
expect(propsParams).toHaveLength(2)
|
|
61
|
+
expect(propsParams.find(p => p.name === 'label')).toEqual({
|
|
62
|
+
name: 'label',
|
|
63
|
+
type: { kind: 'primitive', raw: 'string', primitive: 'string' },
|
|
64
|
+
optional: true,
|
|
65
|
+
})
|
|
66
|
+
expect(propsParams.find(p => p.name === 'text')).toEqual({
|
|
67
|
+
name: 'text',
|
|
68
|
+
type: { kind: 'primitive', raw: 'string', primitive: 'string' },
|
|
69
|
+
optional: true,
|
|
70
|
+
sourceName: 'label',
|
|
71
|
+
defaultValue: "'none'",
|
|
72
|
+
parsed: { kind: 'literal', value: 'none', literalType: 'string' },
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test('untyped `props` param: synthesizes a propsParams entry with `type: unknown` (matching the parameter form\'s own fallback)', () => {
|
|
77
|
+
const propsParams = propsParamsFor(`
|
|
78
|
+
function Foo(props) {
|
|
79
|
+
const { label = 'none' } = props
|
|
80
|
+
return <div data-label={label} />
|
|
81
|
+
}
|
|
82
|
+
`)
|
|
83
|
+
expect(propsParams).toEqual([
|
|
84
|
+
{
|
|
85
|
+
name: 'label',
|
|
86
|
+
type: { kind: 'unknown', raw: 'unknown' },
|
|
87
|
+
optional: true,
|
|
88
|
+
defaultValue: "'none'",
|
|
89
|
+
parsed: { kind: 'literal', value: 'none', literalType: 'string' },
|
|
90
|
+
},
|
|
91
|
+
])
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test('arrow-function default (#2940): defaultContainsArrow is set and `parsed` is a real structured arrow, not a truncated re-parse', () => {
|
|
95
|
+
const propsParams = propsParamsFor(`
|
|
96
|
+
function Foo(props: { fmt?: (v: number) => string }) {
|
|
97
|
+
const { fmt = (v) => 'v' + v } = props
|
|
98
|
+
return <div>{fmt(1)}</div>
|
|
99
|
+
}
|
|
100
|
+
`)
|
|
101
|
+
expect(propsParams).toHaveLength(1)
|
|
102
|
+
const fmt = propsParams[0]!
|
|
103
|
+
expect(fmt.name).toBe('fmt')
|
|
104
|
+
expect(fmt.optional).toBe(true)
|
|
105
|
+
expect(fmt.defaultValue).toBe("(v) => 'v' + v")
|
|
106
|
+
expect(fmt.defaultContainsArrow).toBe(true)
|
|
107
|
+
expect(fmt.parsed?.kind).toBe('arrow')
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
test('renamed, NO default (#2788 shape, unaffected): still just one propsParams entry — no `kids` synthesized', () => {
|
|
111
|
+
// Defaultless renames stay on the pre-existing #2788 path
|
|
112
|
+
// (`resolveBodyDestructuredPropAliases`'s SSR-stash local-name-seeding
|
|
113
|
+
// safety net) — this fix's overlay only fires when `el.initializer`
|
|
114
|
+
// is present, so it must never duplicate that mechanism's work.
|
|
115
|
+
const propsParams = propsParamsFor(`
|
|
116
|
+
function Foo(props: { children?: string }) {
|
|
117
|
+
const { children: kids } = props
|
|
118
|
+
return <span>{kids}</span>
|
|
119
|
+
}
|
|
120
|
+
`)
|
|
121
|
+
expect(propsParams).toEqual([
|
|
122
|
+
{
|
|
123
|
+
name: 'children',
|
|
124
|
+
type: { kind: 'primitive', raw: 'string', primitive: 'string' },
|
|
125
|
+
optional: true,
|
|
126
|
+
},
|
|
127
|
+
])
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
test('a `let` binding still gets the default overlaid — the default is a declaration-time fact, independent of live-rewrite safety', () => {
|
|
131
|
+
// Unlike `resolveBodyPropAliases`'s `requireLiveRewriteSafe` gate
|
|
132
|
+
// (which excludes `let`/mutated locals because turning them into a
|
|
133
|
+
// LIVE `_p.X` read could silently write through to the caller's
|
|
134
|
+
// prop), a static SSR default has no such hazard — it describes what
|
|
135
|
+
// the source looked like at its own declaration line, nothing more.
|
|
136
|
+
const propsParams = propsParamsFor(`
|
|
137
|
+
function Foo(props: { label?: string }) {
|
|
138
|
+
let { label = 'none' } = props
|
|
139
|
+
label = label.toUpperCase()
|
|
140
|
+
return <div data-label={label} />
|
|
141
|
+
}
|
|
142
|
+
`)
|
|
143
|
+
expect(propsParams).toEqual([
|
|
144
|
+
{
|
|
145
|
+
name: 'label',
|
|
146
|
+
type: { kind: 'primitive', raw: 'string', primitive: 'string' },
|
|
147
|
+
optional: true,
|
|
148
|
+
defaultValue: "'none'",
|
|
149
|
+
parsed: { kind: 'literal', value: 'none', literalType: 'string' },
|
|
150
|
+
},
|
|
151
|
+
])
|
|
152
|
+
})
|
|
153
|
+
})
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Body-destructured (or plain member-access alias) props stay reactive too
|
|
3
|
+
* (#2934) — the body-destructure twin of `destructured-props-live.test.ts`.
|
|
4
|
+
* A pure single-prop passthrough local in a bare-props-form component
|
|
5
|
+
* (`function Foo(props) { const { value } = props; ... }`, or the
|
|
6
|
+
* equivalent `const value = props.value`) has its own extraction dropped
|
|
7
|
+
* and every reference rewritten to a live `_p.<key>` read (or
|
|
8
|
+
* `(_p.<key> ?? <fallback>)`), same as `props.xxx` access and parameter
|
|
9
|
+
* destructuring. See `props-binding.ts`'s `resolveBodyPropAliases` and
|
|
10
|
+
* `ir-to-client-js/rewrite-destructured-props.ts`'s `rewriteBodyAliasReads`.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { describe, test, expect } from 'bun:test'
|
|
14
|
+
import { compileJSX } from '../compiler'
|
|
15
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
16
|
+
|
|
17
|
+
const adapter = new TestAdapter()
|
|
18
|
+
|
|
19
|
+
function compileClientJs(source: string, filename = 'Component.tsx') {
|
|
20
|
+
const result = compileJSX(source, filename, { adapter })
|
|
21
|
+
const errors = result.errors.filter((e) => e.severity === 'error')
|
|
22
|
+
expect(errors).toEqual([])
|
|
23
|
+
const clientJs = result.files.find((f) => f.type === 'clientJs')
|
|
24
|
+
expect(clientJs).toBeDefined()
|
|
25
|
+
return { content: clientJs!.content, result }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe('body-destructured props compile to live reads', () => {
|
|
29
|
+
test('a body-destructured prop no longer gets a captured-once extraction line', () => {
|
|
30
|
+
const source = `
|
|
31
|
+
'use client'
|
|
32
|
+
import { createSignal, createMemo, createEffect } from '@barefootjs/client'
|
|
33
|
+
|
|
34
|
+
function Child(props: {
|
|
35
|
+
value: number
|
|
36
|
+
onClick: () => void
|
|
37
|
+
label?: string
|
|
38
|
+
size?: string
|
|
39
|
+
items?: string[]
|
|
40
|
+
}) {
|
|
41
|
+
const { value, onClick, label = 'n', size = 'md', items = [] } = props
|
|
42
|
+
const doubled = createMemo(() => value * 2)
|
|
43
|
+
const [local, setLocal] = createSignal(value)
|
|
44
|
+
createEffect(() => {
|
|
45
|
+
console.log('value is', value)
|
|
46
|
+
})
|
|
47
|
+
return (
|
|
48
|
+
<div className={size}>
|
|
49
|
+
<span data-label={label}>{value}</span>
|
|
50
|
+
<span>{doubled()}</span>
|
|
51
|
+
<span>{items.length}</span>
|
|
52
|
+
<button onClick={() => { setLocal(value); onClick() }}>click</button>
|
|
53
|
+
</div>
|
|
54
|
+
)
|
|
55
|
+
}
|
|
56
|
+
`
|
|
57
|
+
const { content } = compileClientJs(source, 'Child.tsx')
|
|
58
|
+
|
|
59
|
+
// No captured-once local for any body-destructured prop.
|
|
60
|
+
expect(content).not.toMatch(/const value = /)
|
|
61
|
+
expect(content).not.toMatch(/const onClick = /)
|
|
62
|
+
expect(content).not.toMatch(/const label = /)
|
|
63
|
+
expect(content).not.toMatch(/const size = /)
|
|
64
|
+
expect(content).not.toMatch(/const items = /)
|
|
65
|
+
|
|
66
|
+
// Memo computation reads the live prop directly.
|
|
67
|
+
expect(content).toMatch(/createMemo\(\(\) => _p\.value \* 2\)/)
|
|
68
|
+
|
|
69
|
+
// The user effect reads the live prop, not a captured local.
|
|
70
|
+
expect(content).toContain("console.log('value is', _p.value)")
|
|
71
|
+
|
|
72
|
+
// The handler reads the live prop for both the signal update and the
|
|
73
|
+
// callback-prop invocation.
|
|
74
|
+
expect(content).toContain('_p.onClick()')
|
|
75
|
+
expect(content).toContain('setLocal(_p.value)')
|
|
76
|
+
|
|
77
|
+
// `items`'s destructure default survives as a live fallback, not a
|
|
78
|
+
// one-time extraction.
|
|
79
|
+
expect(content).toContain('(_p.items ?? []).length')
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
test('the CSR template lambda also reads a defaulted body alias live, matching the parameter form (#2934)', () => {
|
|
83
|
+
// This is the sibling bug the init-body fix alone doesn't cover: the
|
|
84
|
+
// CSR `template:` lambda (used for a fresh client-side mount with no
|
|
85
|
+
// SSR markup) is built at Phase 1 from `ctx.analyzer.propsParams`
|
|
86
|
+
// (via `jsx-to-ir.ts`'s `_destructuredPropInfoByName` cache), which
|
|
87
|
+
// for a props-object-mode component used to hold only type-member
|
|
88
|
+
// info with no default of its own — so `data-label={label}` compiled
|
|
89
|
+
// to a bare `_p.label` there, omitting the attribute entirely on a
|
|
90
|
+
// CSR-fresh mount where `_p.label` is `undefined`, instead of
|
|
91
|
+
// applying the `'none'` default like the parameter-destructured form
|
|
92
|
+
// already did. Fixed at the source (#2943): the analyzer now overlays
|
|
93
|
+
// a body-destructure default directly onto `propsParams` itself
|
|
94
|
+
// (`collectConstant`, `analyzer.ts`) — the SAME shared `ParamInfo`
|
|
95
|
+
// every other consumer (SSR prop classification, `extractSsrDefaults`)
|
|
96
|
+
// reads, so this CSR cache needs no separate default-resolution logic
|
|
97
|
+
// of its own anymore.
|
|
98
|
+
const source = `
|
|
99
|
+
'use client'
|
|
100
|
+
function Child(props: { label?: string }) {
|
|
101
|
+
const { label = 'none' } = props
|
|
102
|
+
return <div data-label={label}>{label}</div>
|
|
103
|
+
}
|
|
104
|
+
`
|
|
105
|
+
const { content } = compileClientJs(source, 'Child.tsx')
|
|
106
|
+
expect(content).not.toMatch(/const label = /)
|
|
107
|
+
expect(content).toContain("escapeAttr((_p.label ?? 'none'))")
|
|
108
|
+
expect(content).toContain("escapeTextOrMarkup((_p.label ?? 'none'))")
|
|
109
|
+
// Never the bare, default-dropping form.
|
|
110
|
+
expect(content).not.toMatch(/\(_p\.label\) != null/)
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('a RENAMED body-destructured prop WITH A DEFAULT (`label: text = "none"`) applies the default in the CSR template lambda too (#2943)', () => {
|
|
114
|
+
// The renamed twin of the unrenamed test above. Before #2943 this
|
|
115
|
+
// combination had no fixture and no dedicated check — the CSR
|
|
116
|
+
// template lambda's default application only ever reached an
|
|
117
|
+
// UNRENAMED alias.
|
|
118
|
+
const source = `
|
|
119
|
+
'use client'
|
|
120
|
+
function Child(props: { label?: string }) {
|
|
121
|
+
const { label: text = 'none' } = props
|
|
122
|
+
return <div data-label={text}>{text}</div>
|
|
123
|
+
}
|
|
124
|
+
`
|
|
125
|
+
const { content } = compileClientJs(source, 'Child.tsx')
|
|
126
|
+
expect(content).not.toMatch(/const text = /)
|
|
127
|
+
expect(content).toContain("escapeAttr((_p.label ?? 'none'))")
|
|
128
|
+
expect(content).toContain("escapeTextOrMarkup((_p.label ?? 'none'))")
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test('a renamed body-destructured prop (`onPick: pick`) reads the caller-facing key live everywhere', () => {
|
|
132
|
+
const source = `
|
|
133
|
+
'use client'
|
|
134
|
+
import { createEffect } from '@barefootjs/client'
|
|
135
|
+
function Badge(props: { text: string; n: number }) {
|
|
136
|
+
const { text, n: count } = props
|
|
137
|
+
createEffect(() => {
|
|
138
|
+
console.log(count)
|
|
139
|
+
})
|
|
140
|
+
return <span>{text}:{count}</span>
|
|
141
|
+
}
|
|
142
|
+
`
|
|
143
|
+
const { content } = compileClientJs(source, 'Badge.tsx')
|
|
144
|
+
expect(content).not.toMatch(/const count = /)
|
|
145
|
+
expect(content).not.toContain('_p.count')
|
|
146
|
+
expect(content).toContain('console.log(_p.n)')
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
test('a plain member-access alias (`const label = props.label`, no destructure) gets identical treatment', () => {
|
|
150
|
+
const source = `
|
|
151
|
+
'use client'
|
|
152
|
+
import { createEffect } from '@barefootjs/client'
|
|
153
|
+
function Child(props: { label: string }) {
|
|
154
|
+
const label = props.label
|
|
155
|
+
createEffect(() => {
|
|
156
|
+
console.log(label)
|
|
157
|
+
})
|
|
158
|
+
return <span>{label}</span>
|
|
159
|
+
}
|
|
160
|
+
`
|
|
161
|
+
const { content } = compileClientJs(source, 'Child.tsx')
|
|
162
|
+
expect(content).not.toMatch(/const label = /)
|
|
163
|
+
expect(content).toContain('console.log(_p.label)')
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
test('a computation derived from a body-destructured prop stays an ordinary once-evaluated local', () => {
|
|
167
|
+
const source = `
|
|
168
|
+
'use client'
|
|
169
|
+
function Child(props: { value: number }) {
|
|
170
|
+
const { value } = props
|
|
171
|
+
const doubled = value * 2
|
|
172
|
+
return <span>{doubled}</span>
|
|
173
|
+
}
|
|
174
|
+
`
|
|
175
|
+
const { content } = compileClientJs(source, 'Child.tsx')
|
|
176
|
+
expect(content).toContain('const doubled = _p.value * 2')
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
test('a `let` body destructure is never treated as a live alias (reassignment stays a real local)', () => {
|
|
180
|
+
const source = `
|
|
181
|
+
'use client'
|
|
182
|
+
import { createEffect } from '@barefootjs/client'
|
|
183
|
+
function Child(props: { count: number }) {
|
|
184
|
+
let { count } = props
|
|
185
|
+
count += 1
|
|
186
|
+
createEffect(() => {
|
|
187
|
+
console.log(count)
|
|
188
|
+
})
|
|
189
|
+
return <span>{count}</span>
|
|
190
|
+
}
|
|
191
|
+
`
|
|
192
|
+
const { content } = compileClientJs(source, 'Child.tsx')
|
|
193
|
+
expect(content).toMatch(/let count = _p\.count/)
|
|
194
|
+
expect(content).toContain('count += 1')
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
test('shadowing is preserved: a `.map()` callback param and a handler-local both keep their own binding', () => {
|
|
198
|
+
const source = `
|
|
199
|
+
'use client'
|
|
200
|
+
import { createSignal } from '@barefootjs/client'
|
|
201
|
+
function List(props: { title: string; items: { a: string }[] }) {
|
|
202
|
+
const { title, items } = props
|
|
203
|
+
const [count, setCount] = createSignal(0)
|
|
204
|
+
return (
|
|
205
|
+
<div>
|
|
206
|
+
<span>{title}</span>
|
|
207
|
+
<ul>
|
|
208
|
+
{items.map((title) => <li key={title.a}>{title.a}</li>)}
|
|
209
|
+
</ul>
|
|
210
|
+
<button onClick={() => {
|
|
211
|
+
const title = 'local'
|
|
212
|
+
console.log(title)
|
|
213
|
+
setCount(count() + 1)
|
|
214
|
+
}}>click</button>
|
|
215
|
+
</div>
|
|
216
|
+
)
|
|
217
|
+
}
|
|
218
|
+
`
|
|
219
|
+
const { content } = compileClientJs(source, 'List.tsx')
|
|
220
|
+
// The outer `title` prop is rewritten wherever it is a free reference.
|
|
221
|
+
expect(content).toContain('_p.title')
|
|
222
|
+
// The `.map()` callback's OWN `title` parameter must never be rewritten
|
|
223
|
+
// — `(_p.title) => _p.title.a` would be invalid JS.
|
|
224
|
+
expect(content).not.toMatch(/\(_p\.title\)\s*=>/)
|
|
225
|
+
expect(content).toMatch(/\(title\)\s*=>[\s\S]*?title\.a/)
|
|
226
|
+
// The handler-local `const title = 'local'` shadows the prop for the
|
|
227
|
+
// rest of that handler — its own reference must stay bare.
|
|
228
|
+
expect(content).toContain("const title = 'local'")
|
|
229
|
+
expect(content).toContain('console.log(title)')
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
test('`children` keeps its one-time captured-once extraction (not rewritten live)', () => {
|
|
233
|
+
const source = `
|
|
234
|
+
'use client'
|
|
235
|
+
import { createSignal } from '@barefootjs/client'
|
|
236
|
+
function Wrapper(props: { children: unknown }) {
|
|
237
|
+
const { children } = props
|
|
238
|
+
const [open, setOpen] = createSignal(true)
|
|
239
|
+
return <div onClick={() => setOpen(!open())}>{open() ? children : null}</div>
|
|
240
|
+
}
|
|
241
|
+
`
|
|
242
|
+
const { content } = compileClientJs(source, 'Wrapper.tsx')
|
|
243
|
+
expect(content).toContain('const children = _p.children')
|
|
244
|
+
})
|
|
245
|
+
})
|
|
@@ -301,9 +301,12 @@ describe('Client JS generation', () => {
|
|
|
301
301
|
expect(clientJs).toBeDefined()
|
|
302
302
|
const content = clientJs!.content
|
|
303
303
|
|
|
304
|
-
// Props used as conditional guards
|
|
305
|
-
|
|
306
|
-
expect(content).toContain('const
|
|
304
|
+
// Props used as conditional guards must NOT get a `?? {}` default,
|
|
305
|
+
// at the live read sites they now compile to.
|
|
306
|
+
expect(content).not.toContain('const prev = ')
|
|
307
|
+
expect(content).not.toContain('const next = ')
|
|
308
|
+
expect(content).toContain('() => _p.prev,')
|
|
309
|
+
expect(content).toContain('() => _p.next,')
|
|
307
310
|
expect(content).not.toContain('_p.prev ?? {}')
|
|
308
311
|
expect(content).not.toContain('_p.next ?? {}')
|
|
309
312
|
})
|
|
@@ -2874,7 +2877,11 @@ describe('Client JS generation', () => {
|
|
|
2874
2877
|
const result = compileJSX(source, 'Probe.tsx', { adapter })
|
|
2875
2878
|
expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
|
|
2876
2879
|
const js = result.files.find(f => f.type === 'clientJs')!.content
|
|
2877
|
-
|
|
2880
|
+
// The bare `size` inside the user's own `size ?? 1` reads live as
|
|
2881
|
+
// `_p.size`, keeping their `?? 1` default exactly as written — the
|
|
2882
|
+
// compiler must not synthesize a competing one.
|
|
2883
|
+
expect(js).not.toContain('const size = ')
|
|
2884
|
+
expect(js).toContain('createSignal(_p.size ?? 1)')
|
|
2878
2885
|
expect(js).not.toContain('_p.size ?? 0')
|
|
2879
2886
|
})
|
|
2880
2887
|
|
|
@@ -70,7 +70,9 @@ describe('#2640 — /* @client */ text expressions route catalogued calls throug
|
|
|
70
70
|
}
|
|
71
71
|
`)
|
|
72
72
|
expect(bf021).toHaveLength(0)
|
|
73
|
-
|
|
73
|
+
// The receiver is a destructured prop, so it reads live as
|
|
74
|
+
// `_p.createdAt`; the catalogued-call lowering itself is unaffected.
|
|
75
|
+
expect(clientJs).toContain('date(_p.createdAt, "toISOString")')
|
|
74
76
|
expect(clientJs).not.toContain('createdAt.toISOString()')
|
|
75
77
|
expect(clientJs).toMatch(/import\s*\{[^}]*\bdate\b[^}]*\}\s*from\s*'@barefootjs\/client\/runtime'/)
|
|
76
78
|
})
|
|
@@ -82,7 +84,8 @@ describe('#2640 — /* @client */ text expressions route catalogued calls throug
|
|
|
82
84
|
}
|
|
83
85
|
`)
|
|
84
86
|
expect(bf021).toHaveLength(0)
|
|
85
|
-
|
|
87
|
+
// Same live-receiver rewrite as the zero-arg case above.
|
|
88
|
+
expect(clientJs).toContain('formatDate(_p.createdAt, "YYYY/M/D", "UTC")')
|
|
86
89
|
expect(clientJs).not.toContain(".toLocaleDateString('ja-JP'")
|
|
87
90
|
})
|
|
88
91
|
|