@barefootjs/jsx 0.39.6 → 0.39.8
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/adapters/attr-plan.d.ts +8 -1
- package/dist/adapters/attr-plan.d.ts.map +1 -1
- package/dist/adapters/callback-method.d.ts +44 -0
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/component-props.d.ts +80 -13
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +52 -0
- package/dist/adapters/computed-const.d.ts.map +1 -0
- package/dist/adapters/dangerous-inner-html.d.ts +10 -0
- package/dist/adapters/dangerous-inner-html.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +151 -0
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -0
- package/dist/adapters/element-shell.d.ts +44 -0
- package/dist/adapters/element-shell.d.ts.map +1 -0
- package/dist/adapters/interface.d.ts +2 -0
- package/dist/adapters/interface.d.ts.map +1 -1
- package/dist/adapters/jsx-adapter.d.ts +4 -0
- package/dist/adapters/jsx-adapter.d.ts.map +1 -1
- package/dist/adapters/literal-value.d.ts +2 -2
- package/dist/adapters/literal-value.d.ts.map +1 -1
- package/dist/adapters/parsed-expr-emitter.d.ts +27 -0
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/adapters/style-object.d.ts +33 -0
- package/dist/adapters/style-object.d.ts.map +1 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +7 -0
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/children-nullish.d.ts +22 -0
- package/dist/children-nullish.d.ts.map +1 -0
- package/dist/errors.d.ts +1 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/expression-parser.d.ts +9 -19
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/fold-render-reassignments.d.ts +43 -0
- package/dist/fold-render-reassignments.d.ts.map +1 -0
- package/dist/free-refs.d.ts +15 -0
- package/dist/free-refs.d.ts.map +1 -1
- package/dist/index.d.ts +21 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +24620 -23233
- package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts +2 -1
- package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/insert.d.ts +2 -2
- package/dist/ir-to-client-js/control-flow/plan/insert.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-delegation.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts +10 -9
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts.map +1 -1
- package/dist/ir-to-client-js/csr-substitute.d.ts +0 -13
- package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-reactive.d.ts +1 -1
- package/dist/ir-to-client-js/emit-reactive.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-registration.d.ts +10 -8
- package/dist/ir-to-client-js/emit-registration.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 +9 -19
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/identifiers.d.ts +6 -8
- package/dist/ir-to-client-js/identifiers.d.ts.map +1 -1
- package/dist/ir-to-client-js/imports.d.ts +23 -2
- package/dist/ir-to-client-js/imports.d.ts.map +1 -1
- package/dist/ir-to-client-js/index.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases/props-extraction.d.ts +3 -2
- package/dist/ir-to-client-js/phases/props-extraction.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/declaration-emit.d.ts +5 -0
- package/dist/ir-to-client-js/plan/declaration-emit.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts +11 -12
- package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
- package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts +1 -1
- package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
- package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
- package/dist/ir-to-client-js/rewrite-destructured-props.d.ts +1 -1
- package/dist/ir-to-client-js/rewrite-destructured-props.d.ts.map +1 -1
- package/dist/ir-to-client-js/safe-html.d.ts +20 -2
- package/dist/ir-to-client-js/safe-html.d.ts.map +1 -1
- package/dist/ir-to-client-js/types.d.ts +3 -3
- package/dist/ir-to-client-js/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +15 -11
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +43 -12
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/prop-rewrite.d.ts +18 -0
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-binding.d.ts +43 -6
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/relocate.d.ts +16 -0
- package/dist/relocate.d.ts.map +1 -1
- package/dist/scanner/js-scanner.d.ts.map +1 -1
- package/dist/scanner/js-scanner.js +30 -2
- package/dist/signal-prop-name-collision.d.ts +6 -5
- package/dist/signal-prop-name-collision.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts +28 -0
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +37 -18
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/types.d.ts +162 -20
- package/dist/types.d.ts.map +1 -1
- package/dist/value-references.d.ts +21 -0
- package/dist/value-references.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +18 -18
- package/src/__tests__/adapter-output.test.ts +5 -3
- package/src/__tests__/analyzer.test.ts +33 -0
- package/src/__tests__/arithmetic-lowering.test.ts +44 -0
- package/src/__tests__/block-memo-type.test.ts +51 -0
- package/src/__tests__/callback-method.test.ts +75 -0
- package/src/__tests__/children-read.test.ts +226 -0
- package/src/__tests__/client-only-date-lowering.test.ts +2 -2
- package/src/__tests__/client-template-escape-soundness.test.ts +31 -1
- package/src/__tests__/component-props.test.ts +68 -1
- package/src/__tests__/computed-const.test.ts +101 -0
- package/src/__tests__/constant-inlining-tree.test.ts +136 -0
- package/src/__tests__/destructure-default-reads-earlier-param.test.ts +138 -0
- package/src/__tests__/dsl-template-adapter.test.ts +173 -0
- package/src/__tests__/element-shell.test.ts +78 -0
- package/src/__tests__/event-handler-verbatim.test.ts +73 -0
- package/src/__tests__/expression-parser.test.ts +1 -44
- package/src/__tests__/fold-render-reassignments.test.ts +96 -0
- package/src/__tests__/inlinable-local-method-call.test.ts +81 -0
- package/src/__tests__/memo-body-expr.test.ts +120 -0
- package/src/__tests__/nullable-signal-attr.test.ts +67 -3
- package/src/__tests__/prop-handling.test.ts +35 -16
- package/src/__tests__/props-alias-const.test.ts +65 -0
- package/src/__tests__/reference-graph-identifiers.test.ts +161 -0
- package/src/__tests__/runtime-import-detection.test.ts +134 -0
- package/src/__tests__/signal-prop-name-collision.test.ts +30 -0
- package/src/__tests__/signal-prop-seed.test.ts +90 -0
- package/src/__tests__/ssr-defaults.test.ts +19 -1
- package/src/__tests__/ssr-seed-plan.test.ts +173 -1
- package/src/__tests__/static-template-reachability.test.ts +137 -0
- package/src/__tests__/style-object-plan.test.ts +54 -0
- package/src/__tests__/zero-param-helper-inline.test.ts +57 -0
- package/src/adapters/attr-plan.ts +29 -1
- package/src/adapters/callback-method.ts +85 -0
- package/src/adapters/component-props.ts +130 -12
- package/src/adapters/computed-const.ts +185 -0
- package/src/adapters/dangerous-inner-html.ts +33 -0
- package/src/adapters/dsl-template-adapter.ts +248 -0
- package/src/adapters/element-shell.ts +160 -0
- package/src/adapters/interface.ts +8 -0
- package/src/adapters/jsx-adapter.ts +12 -6
- package/src/adapters/literal-value.ts +2 -2
- package/src/adapters/parsed-expr-emitter.ts +34 -0
- package/src/adapters/style-object.ts +61 -0
- package/src/analyzer.ts +382 -94
- package/src/augment-inherited-props.ts +12 -0
- package/src/children-nullish.ts +33 -0
- package/src/debug.ts +2 -2
- package/src/errors.ts +12 -0
- package/src/expression-parser.ts +56 -53
- package/src/fold-render-reassignments.ts +314 -0
- package/src/free-refs.ts +77 -17
- package/src/index.ts +27 -10
- package/src/ir-to-client-js/compute-inlinability.ts +122 -19
- package/src/ir-to-client-js/control-flow/plan/event-collision.ts +2 -1
- package/src/ir-to-client-js/control-flow/plan/insert.ts +2 -2
- package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +3 -2
- package/src/ir-to-client-js/control-flow/stringify/event-listener.ts +13 -12
- package/src/ir-to-client-js/csr-substitute.ts +25 -37
- package/src/ir-to-client-js/emit-reactive.ts +2 -2
- package/src/ir-to-client-js/emit-registration.ts +31 -59
- package/src/ir-to-client-js/generate-init.ts +17 -1
- package/src/ir-to-client-js/html-template.ts +56 -120
- package/src/ir-to-client-js/identifiers.ts +75 -24
- package/src/ir-to-client-js/imports.ts +56 -19
- package/src/ir-to-client-js/index.ts +5 -3
- package/src/ir-to-client-js/init-declarations.ts +1 -1
- package/src/ir-to-client-js/phases/event-handlers.ts +2 -2
- package/src/ir-to-client-js/phases/props-extraction.ts +16 -15
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +26 -52
- package/src/ir-to-client-js/plan/declaration-emit.ts +5 -0
- package/src/ir-to-client-js/prop-handling.ts +13 -54
- package/src/ir-to-client-js/prune-unused-prop-extractions.ts +7 -4
- package/src/ir-to-client-js/reactivity.ts +2 -2
- package/src/ir-to-client-js/rewrite-destructured-props.ts +27 -9
- package/src/ir-to-client-js/safe-html.ts +29 -35
- package/src/ir-to-client-js/stringify/declaration-emit.ts +1 -1
- package/src/ir-to-client-js/types.ts +3 -2
- package/src/ir-to-client-js/utils.ts +13 -26
- package/src/jsx-to-ir.ts +318 -130
- package/src/nullable-signal-attr.ts +156 -37
- package/src/prop-rewrite.ts +41 -0
- package/src/props-binding.ts +117 -7
- package/src/relocate.ts +40 -8
- package/src/scanner/__tests__/js-scanner.test.ts +37 -0
- package/src/scanner/js-scanner.ts +64 -4
- package/src/signal-prop-name-collision.ts +50 -24
- package/src/ssr-defaults.ts +47 -20
- package/src/ssr-seed-plan.ts +121 -29
- package/src/types.ts +165 -21
- package/src/value-references.ts +132 -0
|
@@ -3,10 +3,15 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Verifies that createSignal initializers referencing props are correctly
|
|
5
5
|
* identified so the compiler generates sync effects for controlled components.
|
|
6
|
+
* The answer comes from `SignalInfo.propSeed`, which the analyzer records
|
|
7
|
+
* from the parsed initializer (`signalPropSeed`, #3508); `controlled` below
|
|
8
|
+
* sets it the same way.
|
|
6
9
|
*/
|
|
7
10
|
|
|
8
11
|
import { describe, test, expect } from 'bun:test'
|
|
9
12
|
import { getControlledPropName } from '../ir-to-client-js/prop-handling'
|
|
13
|
+
import { parseExpression } from '../expression-parser'
|
|
14
|
+
import { signalPropSeed } from '../props-binding'
|
|
10
15
|
import type { ParamInfo, SignalInfo, TypeInfo } from '../types'
|
|
11
16
|
|
|
12
17
|
const boolType: TypeInfo = { kind: 'primitive', raw: 'boolean', primitive: 'boolean' }
|
|
@@ -24,6 +29,14 @@ function makeSignal(initialValue: string): SignalInfo {
|
|
|
24
29
|
}
|
|
25
30
|
}
|
|
26
31
|
|
|
32
|
+
function controlled(initialValue: string, params: ParamInfo[], propsObjectName = 'props'): string | null {
|
|
33
|
+
const signal = makeSignal(initialValue)
|
|
34
|
+
const parsed = parseExpression(`(${initialValue})`)
|
|
35
|
+
const seed = signalPropSeed(parsed.kind === 'unsupported' ? undefined : parsed, params, propsObjectName)
|
|
36
|
+
if (seed) signal.propSeed = seed
|
|
37
|
+
return getControlledPropName(signal)
|
|
38
|
+
}
|
|
39
|
+
|
|
27
40
|
const propsParams: ParamInfo[] = [
|
|
28
41
|
{ name: 'checked', type: boolType, optional: true },
|
|
29
42
|
{ name: 'value', type: strType, optional: true },
|
|
@@ -34,68 +47,74 @@ const propsParams: ParamInfo[] = [
|
|
|
34
47
|
|
|
35
48
|
describe('getControlledPropName', () => {
|
|
36
49
|
test('direct props.xxx reference', () => {
|
|
37
|
-
expect(
|
|
50
|
+
expect(controlled('props.checked', propsParams)).toBe('checked')
|
|
38
51
|
})
|
|
39
52
|
|
|
40
53
|
test('props.xxx ?? default (bug fix: #434)', () => {
|
|
41
|
-
expect(
|
|
54
|
+
expect(controlled('props.initial ?? 0', propsParams)).toBe('initial')
|
|
42
55
|
})
|
|
43
56
|
|
|
44
57
|
test('props.xxx ?? string default (bug fix: #434)', () => {
|
|
45
|
-
expect(
|
|
58
|
+
expect(controlled("props.value ?? ''", propsParams)).toBe('value')
|
|
46
59
|
})
|
|
47
60
|
|
|
48
61
|
test('props.xxx || fallback', () => {
|
|
49
|
-
expect(
|
|
62
|
+
expect(controlled("props.value || ''", propsParams)).toBe('value')
|
|
50
63
|
})
|
|
51
64
|
|
|
52
65
|
test('default-prefixed prop is excluded', () => {
|
|
53
|
-
expect(
|
|
66
|
+
expect(controlled('props.defaultChecked', propsParams)).toBeNull()
|
|
54
67
|
})
|
|
55
68
|
|
|
56
69
|
test('default-prefixed prop with ?? is excluded', () => {
|
|
57
|
-
expect(
|
|
70
|
+
expect(controlled('props.defaultChecked ?? false', propsParams)).toBeNull()
|
|
58
71
|
})
|
|
59
72
|
|
|
60
73
|
test('destructured prop name', () => {
|
|
61
|
-
expect(
|
|
74
|
+
expect(controlled('checked', propsParams)).toBe('checked')
|
|
62
75
|
})
|
|
63
76
|
|
|
64
77
|
test('destructured prop with ?? fallback', () => {
|
|
65
|
-
expect(
|
|
78
|
+
expect(controlled('checked ?? false', propsParams)).toBe('checked')
|
|
66
79
|
})
|
|
67
80
|
|
|
68
81
|
test('destructured default-prefixed prop is excluded', () => {
|
|
69
|
-
expect(
|
|
82
|
+
expect(controlled("defaultValue ?? ''", propsParams)).toBeNull()
|
|
70
83
|
})
|
|
71
84
|
|
|
72
85
|
test('complex expression returns null', () => {
|
|
73
|
-
expect(
|
|
86
|
+
expect(controlled('someFunction(props.value)', propsParams)).toBeNull()
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
test('an initializer that only starts with a prop read returns null (#3508)', () => {
|
|
90
|
+
for (const init of ["props.value && 'x'", 'props.initial + 1', "props.value ? 'a' : 'b'", 'props.value.trim()']) {
|
|
91
|
+
expect(controlled(init, propsParams)).toBeNull()
|
|
92
|
+
}
|
|
74
93
|
})
|
|
75
94
|
|
|
76
95
|
test('unknown prop name returns null', () => {
|
|
77
|
-
expect(
|
|
96
|
+
expect(controlled('props.unknown', propsParams)).toBeNull()
|
|
78
97
|
})
|
|
79
98
|
|
|
80
99
|
describe('custom props parameter name', () => {
|
|
81
100
|
test('p.checked with propsObjectName="p"', () => {
|
|
82
|
-
expect(
|
|
101
|
+
expect(controlled('p.checked', propsParams, 'p')).toBe('checked')
|
|
83
102
|
})
|
|
84
103
|
|
|
85
104
|
test('p.initial ?? 0 with propsObjectName="p"', () => {
|
|
86
|
-
expect(
|
|
105
|
+
expect(controlled('p.initial ?? 0', propsParams, 'p')).toBe('initial')
|
|
87
106
|
})
|
|
88
107
|
|
|
89
108
|
test('p.defaultChecked ?? false is excluded', () => {
|
|
90
|
-
expect(
|
|
109
|
+
expect(controlled('p.defaultChecked ?? false', propsParams, 'p')).toBeNull()
|
|
91
110
|
})
|
|
92
111
|
|
|
93
112
|
test('prop.value || "" with propsObjectName="prop"', () => {
|
|
94
|
-
expect(
|
|
113
|
+
expect(controlled("prop.value || ''", propsParams, 'prop')).toBe('value')
|
|
95
114
|
})
|
|
96
115
|
|
|
97
116
|
test('props.checked is not matched when propsObjectName="p"', () => {
|
|
98
|
-
expect(
|
|
117
|
+
expect(controlled('props.checked', propsParams, 'p')).toBeNull()
|
|
99
118
|
})
|
|
100
119
|
})
|
|
101
120
|
})
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { analyzeComponent } from '../analyzer.ts'
|
|
3
|
+
import { isPropsAliasConst } from '../props-binding.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Whether a const reads as its own prop (#3501): a bare `props.X` read, or
|
|
7
|
+
* an un-renamed body destructure (with or without a default), but not a
|
|
8
|
+
* hand-written `const X = props.X ?? d`, whose default the prop does not
|
|
9
|
+
* carry. The destructure is told apart by the analyzer's
|
|
10
|
+
* `ConstantInfo.propsDestructure`, not by the initializer text.
|
|
11
|
+
*/
|
|
12
|
+
function constOf(body: string, name = 'label') {
|
|
13
|
+
const ctx = analyzeComponent(
|
|
14
|
+
`'use client'\nexport function S(props: { label?: string }) {\n ${body}\n return <p>{${name}}</p>\n}`,
|
|
15
|
+
'S.tsx',
|
|
16
|
+
)
|
|
17
|
+
return { c: ctx.localConstants.find(x => x.name === name)!, propsObjectName: ctx.propsObjectName }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function aliasOf(body: string, name = 'label'): boolean {
|
|
21
|
+
const { c, propsObjectName } = constOf(body, name)
|
|
22
|
+
return isPropsAliasConst(c, propsObjectName)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
describe('isPropsAliasConst', () => {
|
|
26
|
+
test('a bare props read is the prop', () => {
|
|
27
|
+
expect(aliasOf('const label = props.label')).toBe(true)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
test('a body destructure is the prop, with or without a default', () => {
|
|
31
|
+
expect(aliasOf('const { label } = props')).toBe(true)
|
|
32
|
+
expect(aliasOf("const { label = 'Default' } = props")).toBe(true)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test('a hand-written `?? default` const is not the prop', () => {
|
|
36
|
+
expect(aliasOf("const label = props.label ?? 'Default'")).toBe(false)
|
|
37
|
+
expect(aliasOf("let label = props.label ?? 'Default'")).toBe(false)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
test('a `?? default` binding whose reassignment cannot fold still reads the prop', () => {
|
|
41
|
+
expect(aliasOf("let label = props.label ?? 'Default'\n for (const t of [1]) label = 'Fallback'")).toBe(true)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('a `?? default` binding whose reassignment folds is not the prop', () => {
|
|
45
|
+
expect(aliasOf("let label = props.label ?? 'Default'\n if (!label) label = 'Fallback'")).toBe(false)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('a renamed destructure binding is not an alias of a prop of its own name', () => {
|
|
49
|
+
expect(aliasOf('const { label: text } = props', 'text')).toBe(false)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('the analyzer records a body destructure on the const, and only that', () => {
|
|
53
|
+
expect(constOf("const { label = 'Default' } = props").c.propsDestructure).toEqual({ sourceKey: 'label' })
|
|
54
|
+
expect(constOf('const { label: text } = props', 'text').c.propsDestructure).toEqual({ sourceKey: 'label' })
|
|
55
|
+
expect(constOf("const label = props.label ?? 'Default'").c.propsDestructure).toBeUndefined()
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test('a read of another prop or a derived value is not the prop', () => {
|
|
59
|
+
expect(aliasOf("const label = props.label + '!'")).toBe(false)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
test('no props object means no alias', () => {
|
|
63
|
+
expect(isPropsAliasConst(constOf('const label = props.label').c, null)).toBe(false)
|
|
64
|
+
})
|
|
65
|
+
})
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The client-JS reference graph finds identifiers with TypeScript's scanner
|
|
3
|
+
* (#3536).
|
|
4
|
+
*
|
|
5
|
+
* The graph decides which locals the emitted `init` keeps. Its identifier
|
|
6
|
+
* scan used to be the regex `\b[a-zA-Z_][a-zA-Z0-9_]*\b`: `$` is not a word
|
|
7
|
+
* character, so a `$`-named local reached only through a helper was dropped
|
|
8
|
+
* and the handler threw ReferenceError. Words inside strings, comments and
|
|
9
|
+
* template static text were collected too.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { describe, test, expect } from 'bun:test'
|
|
13
|
+
import { compileJSX } from '../compiler'
|
|
14
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
15
|
+
import { HonoAdapter } from '../../../adapter-hono/src/adapter/index'
|
|
16
|
+
import {
|
|
17
|
+
extractIdentifiers,
|
|
18
|
+
extractTemplateExpressions,
|
|
19
|
+
extractTemplateIdentifiers,
|
|
20
|
+
} from '../ir-to-client-js/identifiers'
|
|
21
|
+
|
|
22
|
+
const adapter = new TestAdapter()
|
|
23
|
+
|
|
24
|
+
function clientJsFor(locals: string, handlerBody: string): string {
|
|
25
|
+
const source = `
|
|
26
|
+
'use client'
|
|
27
|
+
import { createSignal } from '@barefootjs/client'
|
|
28
|
+
export function T(props: { n: number }) {
|
|
29
|
+
const [count, setCount] = createSignal(0)
|
|
30
|
+
${locals}
|
|
31
|
+
return <button onClick={() => setCount(${handlerBody})}>{count()}</button>
|
|
32
|
+
}
|
|
33
|
+
`
|
|
34
|
+
const result = compileJSX(source, 'T.tsx', { adapter })
|
|
35
|
+
expect(result.errors).toHaveLength(0)
|
|
36
|
+
return result.files.find(f => f.type === 'clientJs')!.content
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
describe('reference graph keeps $-named locals (#3536)', () => {
|
|
40
|
+
for (const name of ['step$', '$step', '$', 'a$b']) {
|
|
41
|
+
test(`a local \`${name}\` reached only through a helper is emitted`, () => {
|
|
42
|
+
const js = clientJsFor(
|
|
43
|
+
`const ${name} = props.n * 2\n const _helper = (x: number) => x + ${name}`,
|
|
44
|
+
'_helper(count())',
|
|
45
|
+
)
|
|
46
|
+
expect(js).toContain(`const ${name} = `)
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
test('a contextual-keyword local (`type`) reached through a helper is emitted', () => {
|
|
51
|
+
const js = clientJsFor('const type = props.n + 1\n const _helper = (x: number) => x + type', '_helper(count())')
|
|
52
|
+
expect(js).toContain('const type = ')
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
describe('extractIdentifiers', () => {
|
|
57
|
+
const ids = (expr: string) => {
|
|
58
|
+
const set = new Set<string>()
|
|
59
|
+
extractIdentifiers(expr, set)
|
|
60
|
+
return [...set].sort()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
test('skips string contents, comments and template static text; keeps member names', () => {
|
|
64
|
+
// A member name stays in: the graph reads `props.value` as a use of the
|
|
65
|
+
// `value` prop in props-object mode.
|
|
66
|
+
expect(ids("obj.member + other?.deep + 'word in a string' /* note */ + `tpl ${inner}`")).toEqual([
|
|
67
|
+
'deep',
|
|
68
|
+
'inner',
|
|
69
|
+
'member',
|
|
70
|
+
'obj',
|
|
71
|
+
'other',
|
|
72
|
+
])
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('reports $ names and contextual keywords, never reserved words', () => {
|
|
76
|
+
expect(ids('(x) => x + step$ + $ + type + of')).toEqual(['$', 'of', 'step$', 'type', 'x'])
|
|
77
|
+
})
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
describe('template substitutions', () => {
|
|
81
|
+
test('a body with an object literal or a nested template stays whole', () => {
|
|
82
|
+
expect(extractTemplateExpressions('<p>${a ? { x: 1 }.x : b}</p>${`n${deep}`}')).toEqual([
|
|
83
|
+
'a ? { x: 1 }.x : b',
|
|
84
|
+
'`n${deep}`',
|
|
85
|
+
])
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('static text is not scanned for identifiers', () => {
|
|
89
|
+
const set = new Set<string>()
|
|
90
|
+
extractTemplateIdentifiers('<div class="btn size-lg ${cls}">${label$}</div>', set)
|
|
91
|
+
expect([...set].sort()).toEqual(['cls', 'label$'])
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
describe('regex literals and keyword-named members (#3536 review)', () => {
|
|
96
|
+
test('a quote inside a regex literal does not hide a later dependency', () => {
|
|
97
|
+
const js = clientJsFor('const step = props.n * 2\n const _helper = (x: string) => (/"/.test(x) ? step : 0)', "_helper('a')")
|
|
98
|
+
expect(js).toContain('const step = ')
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
test('division after a contextual-keyword local does not hide a later dependency', () => {
|
|
102
|
+
const js = clientJsFor('const type = props.n\n const step = props.n * 2\n const _helper = () => type / 2 + step / 3', '_helper()')
|
|
103
|
+
expect(js).toContain('const step = ')
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('division after a keyword-named member does not hide a later dependency', () => {
|
|
107
|
+
const js = clientJsFor('const step = props.n * 2\n const _helper = (o: { default: number }) => o.default / 2 + step / 3', '_helper({ default: 1 })')
|
|
108
|
+
expect(js).toContain('const step = ')
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
test('a regex literal after the for-of separator does not hide a later dependency', () => {
|
|
112
|
+
const js = clientJsFor(
|
|
113
|
+
'const step = props.n * 2\n const _helper = (x: string) => { for (const hit of /"/.exec(x) || []) {} return step }',
|
|
114
|
+
"_helper('a')",
|
|
115
|
+
)
|
|
116
|
+
expect(js).toContain('const step = ')
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
test('a regex literal after a contextual-keyword for-of binding does not hide a later dependency', () => {
|
|
120
|
+
const js = clientJsFor(
|
|
121
|
+
'const step = props.n * 2\n const _helper = (x: string) => { for (const type of /"/.exec(x) || []) {} return step }',
|
|
122
|
+
"_helper('a')",
|
|
123
|
+
)
|
|
124
|
+
expect(js).toContain('const step = ')
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
test('division on an `of` binding after a line break does not hide a later dependency', () => {
|
|
128
|
+
const js = clientJsFor(
|
|
129
|
+
'const step = props.n * 2\n const _helper = (x: number) => {\n let of = x\n of /= step\n return of\n }',
|
|
130
|
+
'_helper(12)',
|
|
131
|
+
)
|
|
132
|
+
expect(js).toContain('const step = ')
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
test('a brace inside a regex literal does not close a template substitution', () => {
|
|
136
|
+
expect(extractTemplateExpressions('<p>${/}/.test(dep) ? dep : fallback}</p>')).toEqual([
|
|
137
|
+
'/}/.test(dep) ? dep : fallback',
|
|
138
|
+
])
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
test('a reserved word read as a member name is reported; as a keyword it is not', () => {
|
|
142
|
+
const set = new Set<string>()
|
|
143
|
+
extractIdentifiers('props.default + obj?.enum + (x instanceof Y)', set)
|
|
144
|
+
expect([...set].sort()).toEqual(['Y', 'default', 'enum', 'obj', 'props', 'x'])
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('a keyword-named prop read only by a handler is still hydrated', () => {
|
|
148
|
+
const source = `
|
|
149
|
+
'use client'
|
|
150
|
+
import { createSignal } from '@barefootjs/client'
|
|
151
|
+
export function T(props: { default: number }) {
|
|
152
|
+
const [count, setCount] = createSignal(0)
|
|
153
|
+
return <button onClick={() => setCount(props.default)}>{count()}</button>
|
|
154
|
+
}
|
|
155
|
+
`
|
|
156
|
+
const hono = compileJSX(source, 'T.tsx', { adapter: new HonoAdapter() })
|
|
157
|
+
expect(hono.errors).toHaveLength(0)
|
|
158
|
+
const template = hono.files.find(f => f.type === 'markedTemplate')!.content
|
|
159
|
+
expect(template).toContain("__hydrateProps['default']")
|
|
160
|
+
})
|
|
161
|
+
})
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime imports come from the names the client JS leaves unbound (#3537).
|
|
3
|
+
*
|
|
4
|
+
* `detectUsedImports` used to regex-scan the emitted code for `name(`, so a
|
|
5
|
+
* user helper that shared a runtime export's name (`function insert`) got a
|
|
6
|
+
* runtime `import { insert }` next to its own `var insert`, a duplicate
|
|
7
|
+
* declaration. Detection now resolves scopes; a module-level binding that
|
|
8
|
+
* collides with a runtime helper is BF057, since it would capture the
|
|
9
|
+
* compiler's own calls to that helper.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { describe, test, expect } from 'bun:test'
|
|
13
|
+
import { compileJSX } from '../compiler'
|
|
14
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
15
|
+
import { detectUsedImports } from '../ir-to-client-js/imports'
|
|
16
|
+
|
|
17
|
+
const adapter = new TestAdapter()
|
|
18
|
+
const transpiler = new Bun.Transpiler({ loader: 'js' })
|
|
19
|
+
|
|
20
|
+
function compile(moduleCode: string, body = '<button onClick={() => setItems(items().concat("a"))}>{items().length}</button>') {
|
|
21
|
+
const source = `
|
|
22
|
+
'use client'
|
|
23
|
+
import { createSignal } from '@barefootjs/client'
|
|
24
|
+
${moduleCode}
|
|
25
|
+
export function T() {
|
|
26
|
+
const [items, setItems] = createSignal<string[]>([])
|
|
27
|
+
return ${body}
|
|
28
|
+
}
|
|
29
|
+
`
|
|
30
|
+
const result = compileJSX(source, 'T.tsx', { adapter })
|
|
31
|
+
const clientJs = result.files.find(f => f.type === 'clientJs')?.content ?? ''
|
|
32
|
+
return { errors: result.errors, clientJs }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const runtimeImportLine = (js: string) => js.split('\n').find(l => l.includes("from '@barefootjs/client/runtime'")) ?? ''
|
|
36
|
+
|
|
37
|
+
describe('runtime import detection resolves scopes (#3537)', () => {
|
|
38
|
+
test('a module-level helper named like a runtime export is BF057', () => {
|
|
39
|
+
const { errors } = compile(
|
|
40
|
+
'function insert(list: string[], x: string) { return [...list, x] }',
|
|
41
|
+
'<button onClick={() => setItems(insert(items(), "a"))}>{items().length}</button>',
|
|
42
|
+
)
|
|
43
|
+
expect(errors.map(e => e.code)).toContain('BF057')
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('a module-level binding named like a runtime export but never called stays legal', () => {
|
|
47
|
+
const { errors, clientJs } = compile(
|
|
48
|
+
'const qsa = { step: 1 }',
|
|
49
|
+
'<button onClick={() => setItems(items().concat(String(qsa.step)))}>{items().length}</button>',
|
|
50
|
+
)
|
|
51
|
+
expect(errors).toHaveLength(0)
|
|
52
|
+
expect(() => transpiler.transformSync(clientJs)).not.toThrow()
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('a value import of a name the compiler always calls is BF057', () => {
|
|
56
|
+
// Every client module registers itself with `hydrate(…)`.
|
|
57
|
+
expect(compile("import hydrate from './lib'").errors.map(e => e.code)).toContain('BF057')
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('an inlined literal const binds nothing in the client JS, so it is not BF057', () => {
|
|
61
|
+
const { errors, clientJs } = compile('const qsa = 1', '<p>{items().length + qsa}</p>')
|
|
62
|
+
expect(errors).toHaveLength(0)
|
|
63
|
+
expect(() => transpiler.transformSync(clientJs)).not.toThrow()
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('a value import named like a runtime export is BF057, an aliased one is not', () => {
|
|
67
|
+
expect(compile("import { hydrate } from './lib'").errors.map(e => e.code)).toContain('BF057')
|
|
68
|
+
expect(compile("import { hydrate as hydrateLib } from './lib'").errors.map(e => e.code)).not.toContain('BF057')
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test('a type-only import and a runtime import of the same name are not BF057', () => {
|
|
72
|
+
expect(compile("import type { insert } from './lib'").errors.map(e => e.code)).not.toContain('BF057')
|
|
73
|
+
expect(compile("import { batch } from '@barefootjs/client'").errors.map(e => e.code)).not.toContain('BF057')
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test('a nested binding that shadows a runtime name neither errors nor drops the import', () => {
|
|
77
|
+
const { errors, clientJs } = compile(
|
|
78
|
+
'',
|
|
79
|
+
'<button onClick={() => { const $ = items(); setItems($.concat("a")) }}>{items().length}</button>',
|
|
80
|
+
)
|
|
81
|
+
expect(errors).toHaveLength(0)
|
|
82
|
+
expect(() => transpiler.transformSync(clientJs)).not.toThrow()
|
|
83
|
+
// The compiler's own slot lookup still needs the runtime `$`.
|
|
84
|
+
expect(runtimeImportLine(clientJs)).toMatch(/[{ ,]\$[ ,}]/)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('a non-colliding module-level helper is emitted once and imported never', () => {
|
|
88
|
+
const { errors, clientJs } = compile(
|
|
89
|
+
'function appendItem(list: string[], x: string) { return [...list, x] }',
|
|
90
|
+
'<button onClick={() => setItems(appendItem(items(), "a"))}>{items().length}</button>',
|
|
91
|
+
)
|
|
92
|
+
expect(errors).toHaveLength(0)
|
|
93
|
+
expect(() => transpiler.transformSync(clientJs)).not.toThrow()
|
|
94
|
+
expect(runtimeImportLine(clientJs)).not.toContain('appendItem')
|
|
95
|
+
})
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
describe('class static blocks (#3537 review)', () => {
|
|
99
|
+
test('a component whose class static block declares `var $` still imports the runtime `$`', () => {
|
|
100
|
+
const source = `
|
|
101
|
+
'use client'
|
|
102
|
+
import { createSignal } from '@barefootjs/client'
|
|
103
|
+
export function T() {
|
|
104
|
+
const [n, setN] = createSignal(0)
|
|
105
|
+
const C = class { static { var $ } }
|
|
106
|
+
return <button onClick={() => { new C(); setN(n() + 1) }}>{n()}</button>
|
|
107
|
+
}
|
|
108
|
+
`
|
|
109
|
+
const result = compileJSX(source, 'T.tsx', { adapter })
|
|
110
|
+
expect(result.errors).toHaveLength(0)
|
|
111
|
+
const clientJs = result.files.find(f => f.type === 'clientJs')!.content
|
|
112
|
+
expect(clientJs).toContain('$(__scope')
|
|
113
|
+
expect(runtimeImportLine(clientJs)).toMatch(/[{,]\s*\$\s*[,}]/)
|
|
114
|
+
})
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
describe('detectUsedImports', () => {
|
|
118
|
+
test('reports a runtime name only where nothing binds it', () => {
|
|
119
|
+
const code = [
|
|
120
|
+
'var insert = insert ?? function (a) { return a }',
|
|
121
|
+
'export function initT(qsa) { qsa(); insert(); createEffect(() => {}) }',
|
|
122
|
+
"hydrate('T', {})",
|
|
123
|
+
].join('\n')
|
|
124
|
+
expect([...detectUsedImports(code)].sort()).toEqual(['createEffect', 'hydrate'])
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
test('a var inside a class static block binds only inside that block', () => {
|
|
128
|
+
expect([...detectUsedImports('const C = class { static { var $ } }; $(scope, "s0")')]).toEqual(['$'])
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
test('an object key or member name spelling a runtime name is not a reference', () => {
|
|
132
|
+
expect([...detectUsedImports('const o = { insert: 1 }; o.hydrate(); qsa.x')]).toEqual(['qsa'])
|
|
133
|
+
})
|
|
134
|
+
})
|
|
@@ -56,6 +56,36 @@ test('same-prop derived seeds and ordinary bindings stay unchanged', () => {
|
|
|
56
56
|
expect(ctx.signals.map(s => s.getter)).toEqual(['count', 'other'])
|
|
57
57
|
})
|
|
58
58
|
|
|
59
|
+
test('a signal seeded from another prop, a memo, and a case-folded name are separated', () => {
|
|
60
|
+
const ctx = analyzeComponent(`
|
|
61
|
+
'use client'
|
|
62
|
+
import { createSignal, createMemo } from '@barefootjs/client'
|
|
63
|
+
export function App(props: { label?: string; count?: number; title?: string; b?: string }) {
|
|
64
|
+
const [label] = createSignal(props.b ?? 'y')
|
|
65
|
+
const count = createMemo(() => label().length)
|
|
66
|
+
const [Title] = createSignal(props.b)
|
|
67
|
+
return <p>{label()}{count()}{Title()}{props.label}{props.count}{props.title}</p>
|
|
68
|
+
}
|
|
69
|
+
`, 'test.tsx')
|
|
70
|
+
expect(ctx.signals.map(s => s.getter)).toEqual(['bfSignal_label', 'bfSignal_Title'])
|
|
71
|
+
expect(ctx.memos.map(m => m.name)).toEqual(['bfMemo_count'])
|
|
72
|
+
expect(ctx.memos[0].computation).toContain('bfSignal_label()')
|
|
73
|
+
expect(ctx.sourceFile.text).toContain('{props.label}{props.count}{props.title}')
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test('a memo derived from its own same-named prop keeps its name', () => {
|
|
77
|
+
const ctx = analyzeComponent(`
|
|
78
|
+
'use client'
|
|
79
|
+
import { createMemo } from '@barefootjs/client'
|
|
80
|
+
export function App(props: { label?: string }) {
|
|
81
|
+
const fallback = props.label
|
|
82
|
+
const label = createMemo(() => (fallback ?? 'none') + '!')
|
|
83
|
+
return <p>{label()}</p>
|
|
84
|
+
}
|
|
85
|
+
`, 'test.tsx')
|
|
86
|
+
expect(ctx.memos.map(m => m.name)).toEqual(['label'])
|
|
87
|
+
})
|
|
88
|
+
|
|
59
89
|
test('signed literal seeds are separated without a type-based program', () => {
|
|
60
90
|
const ctx = analyzeComponent(`
|
|
61
91
|
'use client'
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { compileJSX } from '../compiler'
|
|
3
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The CSR init body (`resolveSignalInitialValue`), the CSR template
|
|
7
|
+
* (`normalizeSignalInitial`) and the controlled-signal sync
|
|
8
|
+
* (`getControlledPropName`) read one fact, the analyzer's
|
|
9
|
+
* `SignalInfo.propSeed`, so they agree on every initializer shape (#3508).
|
|
10
|
+
*/
|
|
11
|
+
function clientJs(init: string, params = 'props: { a?: string; b?: string; n?: number; row?: { count: number } }'): string {
|
|
12
|
+
const source = `'use client'
|
|
13
|
+
import { createSignal } from '@barefootjs/client'
|
|
14
|
+
export function T(${params}) {
|
|
15
|
+
const [v, setV] = createSignal(${init})
|
|
16
|
+
return <button onClick={() => setV(v())}>{String(v())}</button>
|
|
17
|
+
}
|
|
18
|
+
`
|
|
19
|
+
const result = compileJSX(source, 'T.tsx', { adapter: new TestAdapter() })
|
|
20
|
+
const file = result.files.find(f => f.path.endsWith('.client.js'))
|
|
21
|
+
if (!file) throw new Error('no client JS')
|
|
22
|
+
return file.content
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const declaration = (js: string) => js.match(/createSignal\((.*)\)\n/)?.[1]
|
|
26
|
+
const parses = (js: string) => {
|
|
27
|
+
new Function(js.replace(/^import .*$/gm, '').replace(/^export /gm, ''))
|
|
28
|
+
return true
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe('signal prop seed', () => {
|
|
32
|
+
test('a `||` / `&&` initializer is kept as written in the init body and the template', () => {
|
|
33
|
+
for (const init of ["props.a || 'x'", "props.a && 'x'"]) {
|
|
34
|
+
const js = clientJs(init)
|
|
35
|
+
const expected = init.replace('props.', '_p.')
|
|
36
|
+
expect(declaration(js)).toBe(expected)
|
|
37
|
+
expect(js).toContain(`(${expected})`)
|
|
38
|
+
expect(parses(js)).toBe(true)
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('a bare props read gets the same type default in both places', () => {
|
|
43
|
+
const js = clientJs('props.n')
|
|
44
|
+
const seed = declaration(js)
|
|
45
|
+
expect(seed).toMatch(/^_p\.n \?\? \S+$/)
|
|
46
|
+
expect(js).toContain(`(${seed})`)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test('a nested props read gets the type default but is not controlled', () => {
|
|
50
|
+
const js = clientJs('props.row.count')
|
|
51
|
+
expect(declaration(js)).toMatch(/^_p\.row\.count \?\? \S+$/)
|
|
52
|
+
expect(js).not.toContain('if (__val !== undefined)')
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('a controlled `||` seed re-syncs to the initializer, not the raw prop', () => {
|
|
56
|
+
const js = clientJs("props.a || 'x'")
|
|
57
|
+
expect(js).toContain('const __val = _p.a')
|
|
58
|
+
expect(js).toContain("if (__val !== undefined) setV(_p.a || 'x')")
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('a controlled `??` seed re-syncs to the prop', () => {
|
|
62
|
+
const js = clientJs("props.a ?? 'x'")
|
|
63
|
+
expect(js).toContain('if (__val !== undefined) setV(__val)')
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('an expression that only starts with a props read gets no appended default', () => {
|
|
67
|
+
for (const init of ['props.n + 1', "props.a ? 'y' : 'n'"]) {
|
|
68
|
+
expect(declaration(clientJs(init))).toBe(init.replaceAll('props.', '_p.'))
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test('a chained `??` seed stays controlled by its leftmost prop', () => {
|
|
73
|
+
const js = clientJs("props.a ?? props.b ?? 'x'")
|
|
74
|
+
expect(declaration(js)).toBe("_p.a ?? _p.b ?? 'x'")
|
|
75
|
+
expect(js).toContain('const __val = _p.a')
|
|
76
|
+
expect(js).toContain('if (__val !== undefined) setV(__val)')
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test('a chained `||` seed stays controlled and re-syncs to the initializer', () => {
|
|
80
|
+
const js = clientJs("props.a || props.b || 'x'")
|
|
81
|
+
expect(js).toContain('const __val = _p.a')
|
|
82
|
+
expect(js).toContain("if (__val !== undefined) setV(_p.a || _p.b || 'x')")
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test('a chained seed over destructured props stays controlled', () => {
|
|
86
|
+
const js = clientJs("a ?? b ?? 'x'", '{ a, b }: { a?: string; b?: string }')
|
|
87
|
+
expect(js).toContain('const __val = _p.a')
|
|
88
|
+
expect(js).toContain('if (__val !== undefined) setV(__val)')
|
|
89
|
+
})
|
|
90
|
+
})
|
|
@@ -303,8 +303,12 @@ describe('extractSsrDefaults', () => {
|
|
|
303
303
|
}
|
|
304
304
|
`)
|
|
305
305
|
|
|
306
|
+
// Not self-derived, so the analyzer renames the getter apart from the
|
|
307
|
+
// prop (`separateSignalPropBindings`): the signal keeps a plain entry
|
|
308
|
+
// under its own name, and the prop keeps its prop entry.
|
|
306
309
|
const defaults = extractSsrDefaults(metadata)
|
|
307
|
-
expect(defaults?.
|
|
310
|
+
expect(defaults?.bfSignal_label).toEqual({ value: null })
|
|
311
|
+
expect(defaults?.label).toEqual({ propName: 'label', value: null })
|
|
308
312
|
})
|
|
309
313
|
|
|
310
314
|
test('aliased destructured prop (#2460): `propName` is the CALLER-facing key, not the local binding', () => {
|
|
@@ -660,6 +664,20 @@ describe('extractSsrDefaults', () => {
|
|
|
660
664
|
expect(defaults?.setCount).toEqual({ value: null })
|
|
661
665
|
expect(defaults?.setCountAlias).toEqual({ value: null })
|
|
662
666
|
})
|
|
667
|
+
|
|
668
|
+
test('a default reading an earlier prop seeds null, so the template applies it (#3531)', () => {
|
|
669
|
+
const metadata = metadataFor(`
|
|
670
|
+
function C({ off = false, on = !off }: { off?: boolean; on?: boolean }) {
|
|
671
|
+
return <p>{String(on)}</p>
|
|
672
|
+
}
|
|
673
|
+
`)
|
|
674
|
+
|
|
675
|
+
const defaults = extractSsrDefaults(metadata)
|
|
676
|
+
expect(defaults?.off).toEqual({ propName: 'off', value: false })
|
|
677
|
+
// A static `!off` would read `true` and win over the in-template
|
|
678
|
+
// `on ?? !off` whatever `off` the caller passed.
|
|
679
|
+
expect(defaults?.on).toEqual({ propName: 'on', value: null })
|
|
680
|
+
})
|
|
663
681
|
})
|
|
664
682
|
|
|
665
683
|
// TS twin of the Ruby/Python/PHP/Perl/Rust `derive*FromDefaults` runtime
|