@barefootjs/jsx 0.40.0 → 0.40.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts +41 -0
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -0
- package/dist/adapters/attr-plan.d.ts.map +1 -1
- package/dist/adapters/boolean-carrier.d.ts +31 -0
- package/dist/adapters/boolean-carrier.d.ts.map +1 -0
- package/dist/adapters/component-props.d.ts +22 -7
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +17 -0
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +18 -1
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/number-value.d.ts +43 -0
- package/dist/adapters/number-value.d.ts.map +1 -0
- package/dist/analyzer-context.d.ts +7 -0
- package/dist/analyzer-context.d.ts.map +1 -1
- package/dist/analyzer.d.ts +34 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1006 -351
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts +29 -0
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-lazy-row.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/build-lazy-row.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-reactive-effects.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/inner-loop.d.ts +5 -4
- package/dist/ir-to-client-js/control-flow/plan/inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +15 -0
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/reactive-effects.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/reactive-effects.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +20 -0
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/lazy-row.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts +3 -2
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +7 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
- package/dist/ir-to-client-js/safe-html.d.ts +9 -3
- package/dist/ir-to-client-js/safe-html.d.ts.map +1 -1
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/types.d.ts +30 -0
- package/dist/ir-to-client-js/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +8 -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/nullable-signal-attr.d.ts +29 -3
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/props-binding.d.ts +27 -3
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/scope/binding-scope.d.ts +22 -1
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/scope/root-prop-alias.d.ts +14 -1
- package/dist/scope/root-prop-alias.d.ts.map +1 -1
- package/dist/signal-prop-name-collision.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts +4 -2
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +14 -0
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/template-parts.d.ts.map +1 -1
- package/dist/types.d.ts +30 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/binding-scope-ratchet.test.ts +12 -2
- package/src/__tests__/boolean-carrier.test.ts +74 -0
- package/src/__tests__/component-props.test.ts +14 -0
- package/src/__tests__/computed-const.test.ts +85 -0
- package/src/__tests__/expression-parser.test.ts +24 -0
- package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +142 -0
- package/src/__tests__/loop-row-prop-read.test.ts +111 -0
- package/src/__tests__/memo-body-expr.test.ts +137 -0
- package/src/__tests__/nullable-signal-attr.test.ts +97 -2
- package/src/__tests__/profile-binding-ids.test.ts +14 -0
- package/src/__tests__/root-prop-alias.test.ts +34 -1
- package/src/__tests__/signal-prop-name-collision.test.ts +102 -2
- package/src/__tests__/source-file-cache.test.ts +105 -0
- package/src/__tests__/ssr-seed-plan.test.ts +43 -0
- package/src/__tests__/template-parts-literal-boundary.test.ts +19 -0
- package/src/adapters/ambiguous-nil-arithmetic.ts +94 -0
- package/src/adapters/attr-plan.ts +6 -2
- package/src/adapters/boolean-carrier.ts +76 -0
- package/src/adapters/component-props.ts +28 -8
- package/src/adapters/computed-const.ts +206 -7
- package/src/adapters/dsl-template-adapter.ts +39 -0
- package/src/adapters/number-value.ts +95 -0
- package/src/analyzer-context.ts +8 -0
- package/src/analyzer.ts +187 -66
- package/src/expression-parser.ts +4 -1
- package/src/index.ts +12 -2
- package/src/ir-to-client-js/collect-elements.ts +104 -46
- package/src/ir-to-client-js/compute-inlinability.ts +1 -1
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +15 -1
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +79 -4
- package/src/ir-to-client-js/control-flow/plan/build-lazy-row.ts +3 -0
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/build-reactive-effects.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/inner-loop.ts +5 -4
- package/src/ir-to-client-js/control-flow/plan/loop-child-arm.ts +2 -0
- package/src/ir-to-client-js/control-flow/plan/loop.ts +12 -0
- package/src/ir-to-client-js/control-flow/plan/reactive-effects.ts +2 -0
- package/src/ir-to-client-js/control-flow/shared.ts +3 -2
- package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +27 -0
- package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +8 -2
- package/src/ir-to-client-js/control-flow/stringify/event-listener.ts +8 -1
- package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +8 -7
- package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +8 -7
- package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +4 -4
- package/src/ir-to-client-js/control-flow/stringify/loop.ts +3 -3
- package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +6 -6
- package/src/ir-to-client-js/emit-reactive.ts +1 -1
- package/src/ir-to-client-js/html-template.ts +70 -30
- package/src/ir-to-client-js/plan/build-static-array-child-init.ts +11 -1
- package/src/ir-to-client-js/plan/static-array-child-init.ts +7 -1
- package/src/ir-to-client-js/reactivity.ts +14 -0
- package/src/ir-to-client-js/safe-html.ts +11 -4
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +6 -4
- package/src/ir-to-client-js/types.ts +23 -0
- package/src/ir-to-client-js/utils.ts +15 -1
- package/src/jsx-to-ir.ts +78 -35
- package/src/nullable-signal-attr.ts +134 -13
- package/src/props-binding.ts +54 -3
- package/src/scope/binding-scope.ts +30 -2
- package/src/scope/root-prop-alias.ts +17 -1
- package/src/signal-prop-name-collision.ts +113 -22
- package/src/ssr-defaults.ts +10 -4
- package/src/ssr-seed-plan.ts +82 -17
- package/src/template-parts.ts +21 -1
- package/src/types.ts +30 -0
|
@@ -132,7 +132,14 @@ const ALLOWLIST: Record<string, Partial<Record<Pattern, number>>> = {
|
|
|
132
132
|
// at all), routing it through the same shape-1 shadow-name check as
|
|
133
133
|
// `getBakedStaticChildLoop` below instead — a consolidation onto the
|
|
134
134
|
// existing floor bucket, not a new unguarded use.
|
|
135
|
-
|
|
135
|
+
// Since then the constructor-context lookups (`computeDerivedConstFields`
|
|
136
|
+
// and its `bakesDerivedConstField` check) call `compile-state.ts`'s
|
|
137
|
+
// `findValuedLocalConst`, and `resolveNestedLoopItemTypes`'s module-const
|
|
138
|
+
// lookup calls its `findModuleScopeConst`; those two `.find(` are counted
|
|
139
|
+
// under that file. The 4 left here are `resolveDynamicPropValue`,
|
|
140
|
+
// `findModuleConst`, `refuseComputedConstLoopSource` (the loop-array
|
|
141
|
+
// const lookup) and `resolveLoopArraySourceConst`.
|
|
142
|
+
'localConstants.find(': 4,
|
|
136
143
|
// #2482 Stage 3: `loopParamStack` eliminated entirely (0, down from 35)
|
|
137
144
|
// — replaced by the threaded `this.scope: BindingScope`. The remaining
|
|
138
145
|
// 3 `staticLoopSourceBoundNames` uses (down from 3, then #3164 added a
|
|
@@ -153,7 +160,10 @@ const ALLOWLIST: Record<string, Partial<Record<Pattern, number>>> = {
|
|
|
153
160
|
// file's top-level comment used to carry was reworded in Stage 4 — that
|
|
154
161
|
// stack no longer exists anywhere, so the ledger's last trace of it is
|
|
155
162
|
// gone too).
|
|
156
|
-
|
|
163
|
+
// FLOOR (shape 1 for `localConstants.find(`): `findModuleScopeConst` and
|
|
164
|
+
// `findValuedLocalConst`, the two lookups moved here from
|
|
165
|
+
// `go-template-adapter.ts` (its entry above names their callers).
|
|
166
|
+
'packages/adapter-go-template/src/adapter/lib/compile-state.ts': { staticLoopSourceBoundNames: 1, 'localConstants.find(': 2 },
|
|
157
167
|
// FLOOR (shape 1): `lowerCtorExpr`/`lowerCtorStringArray` lower a derived-
|
|
158
168
|
// state memo's computation into Go CODE evaluated in the `NewXxxProps`
|
|
159
169
|
// constructor — the same no-live-scope bucket as `go-template-adapter.ts`'s
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { describe, test, expect } from 'bun:test'
|
|
2
|
+
import { collectBooleanCarrierNames, mayYieldBooleanCarrier } from '../adapters/boolean-carrier'
|
|
3
|
+
import { parseExpression } from '../expression-parser'
|
|
4
|
+
import type { TypeInfo } from '../types'
|
|
5
|
+
|
|
6
|
+
const bool: TypeInfo = { kind: 'primitive', raw: 'boolean', primitive: 'boolean' }
|
|
7
|
+
const str: TypeInfo = { kind: 'primitive', raw: 'string', primitive: 'string' }
|
|
8
|
+
const undef: TypeInfo = { kind: 'primitive', raw: 'undefined', primitive: 'undefined' }
|
|
9
|
+
const unknown: TypeInfo = { kind: 'unknown', raw: 'unknown' }
|
|
10
|
+
const union = (...unionTypes: TypeInfo[]): TypeInfo => ({ kind: 'union', raw: 'union', unionTypes })
|
|
11
|
+
|
|
12
|
+
describe('collectBooleanCarrierNames (#3783)', () => {
|
|
13
|
+
test('an optional boolean, a boolean union and a destructured union prop carry a boolean', () => {
|
|
14
|
+
const { names, props } = collectBooleanCarrierNames({
|
|
15
|
+
signals: [],
|
|
16
|
+
memos: [],
|
|
17
|
+
propsParams: [
|
|
18
|
+
{ name: 'off', type: bool, optional: true },
|
|
19
|
+
{ name: 'either', type: union(bool, str), optional: false },
|
|
20
|
+
// A destructured union-typed prop: `type` is unresolved, `declaredType` keeps the union.
|
|
21
|
+
{ name: 'flag', type: unknown, declaredType: union(bool, undef), optional: false },
|
|
22
|
+
{ name: 'label', type: str, optional: false },
|
|
23
|
+
],
|
|
24
|
+
})
|
|
25
|
+
expect([...names].sort()).toEqual(['either', 'flag', 'off'])
|
|
26
|
+
expect([...props].sort()).toEqual(['either', 'flag', 'off'])
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test('a props object keeps only the caller-facing keys', () => {
|
|
30
|
+
const { names, props } = collectBooleanCarrierNames({
|
|
31
|
+
signals: [],
|
|
32
|
+
memos: [],
|
|
33
|
+
propsObjectName: 'props',
|
|
34
|
+
propsParams: [{ name: 'off', type: bool, optional: true }],
|
|
35
|
+
})
|
|
36
|
+
expect([...names]).toEqual([])
|
|
37
|
+
expect([...props]).toEqual(['off'])
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
test('a boolean-typed or untyped boolean-seeded signal carries a boolean; a string signal does not', () => {
|
|
41
|
+
// The seed is classified from its parsed tree, as the analyzer attaches it.
|
|
42
|
+
const signal = (getter: string, type: TypeInfo, initialValue: string) =>
|
|
43
|
+
({ getter, setter: null, type, initialValue, parsed: parseExpression(initialValue) }) as never
|
|
44
|
+
const { names } = collectBooleanCarrierNames({
|
|
45
|
+
signals: [signal('flag', union(bool, undef), 'false'), signal('open', unknown, 'true'), signal('text', str, "'x'")],
|
|
46
|
+
memos: [],
|
|
47
|
+
propsParams: [],
|
|
48
|
+
})
|
|
49
|
+
expect([...names].sort()).toEqual(['flag', 'open'])
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
describe('mayYieldBooleanCarrier (#3783)', () => {
|
|
54
|
+
const carriers = new Set(['off', 'flag'])
|
|
55
|
+
const isCarrier = (n: string, componentProperty?: boolean) => (componentProperty ? n === 'off' : carriers.has(n))
|
|
56
|
+
const yields = (src: string) => mayYieldBooleanCarrier(parseExpression(src)!, isCarrier)
|
|
57
|
+
|
|
58
|
+
test('a carrier read through ?? / || / && or a ternary branch may yield it', () => {
|
|
59
|
+
expect(yields("off ?? 'none'")).toBe(true)
|
|
60
|
+
expect(yields("props.off ?? 'none'")).toBe(true)
|
|
61
|
+
expect(yields("flag() ?? 'none'")).toBe(true)
|
|
62
|
+
expect(yields("on && (off ?? 'none')")).toBe(true)
|
|
63
|
+
expect(yields("label || off")).toBe(true)
|
|
64
|
+
expect(yields("on ? (off ?? 'none') : 'x'")).toBe(true)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('a carrier only in a ternary test, a call argument or a computed member does not', () => {
|
|
68
|
+
expect(yields("off ? 'a' : 'b'")).toBe(false)
|
|
69
|
+
expect(yields("label ?? 'none'")).toBe(false)
|
|
70
|
+
expect(yields("fmt(off)")).toBe(false)
|
|
71
|
+
expect(yields("props['off'] ?? 'none'")).toBe(false)
|
|
72
|
+
expect(yields("flag(1) ?? 'none'")).toBe(false)
|
|
73
|
+
})
|
|
74
|
+
})
|
|
@@ -130,6 +130,16 @@ describe('planComponentProps', () => {
|
|
|
130
130
|
expect(spell(comp, withProps)).toEqual([`{tone='a'}`, '...tone?=$tone', `{size=$size, tone='z', _bf_slot='s0'}`])
|
|
131
131
|
})
|
|
132
132
|
|
|
133
|
+
test('with caller keys, {...props} carries every optional param only when the caller passed it', () => {
|
|
134
|
+
const callerKeys: ComponentPropsOptions = {
|
|
135
|
+
propsObjectName: 'props',
|
|
136
|
+
propsParams: [{ name: 'tone', optional: false }, { name: 'size' }],
|
|
137
|
+
callerKeys: true,
|
|
138
|
+
}
|
|
139
|
+
const comp = component([prop('', spread('props'))])
|
|
140
|
+
expect(spell(comp, callerKeys)).toEqual([`{tone=$tone}`, '...size?=$size', `{_bf_slot='s0'}`])
|
|
141
|
+
})
|
|
142
|
+
|
|
133
143
|
test('a key repeated across a spread stays twice: the spread may set it in between', () => {
|
|
134
144
|
const comp = component([prop('tone', literal('a')), prop('', spread('rest')), prop('tone', literal('b'))])
|
|
135
145
|
expect(spell(comp)).toEqual([`{tone='a'}`, '...rest', `{tone='b', _bf_slot='s0'}`])
|
|
@@ -161,6 +171,10 @@ describe('propsSpreadParam', () => {
|
|
|
161
171
|
test('a required param is always passed, so it always wins', () => {
|
|
162
172
|
expect(propsSpreadParam({ name: 'tone', optional: false }, { keys: new Set(['tone']), spread: true })).toBe('entry')
|
|
163
173
|
})
|
|
174
|
+
test('with caller keys, an optional param is carried only when passed even with nothing earlier', () => {
|
|
175
|
+
expect(propsSpreadParam({ name: 'tone' }, none, true)).toBe('present')
|
|
176
|
+
expect(propsSpreadParam({ name: 'tone', optional: false }, none, true)).toBe('entry')
|
|
177
|
+
})
|
|
164
178
|
})
|
|
165
179
|
|
|
166
180
|
describe('resolveComponentPropKeys', () => {
|
|
@@ -99,3 +99,88 @@ describe('inlineComputedConsts (#3494)', () => {
|
|
|
99
99
|
expect(inline('label')).toBe('props.label ?? "Default"')
|
|
100
100
|
})
|
|
101
101
|
})
|
|
102
|
+
|
|
103
|
+
describe('a const hop resolved where each const is declared (#3696)', () => {
|
|
104
|
+
const source = `
|
|
105
|
+
'use client'
|
|
106
|
+
import { createSignal } from '@barefootjs/client'
|
|
107
|
+
const x = 'module'
|
|
108
|
+
const a = x
|
|
109
|
+
const base = 'm-base'
|
|
110
|
+
const tag = base + '!'
|
|
111
|
+
const alias = tag
|
|
112
|
+
const k = 'mk'
|
|
113
|
+
const ka = k
|
|
114
|
+
function helper() { return 'h' }
|
|
115
|
+
const viaHelper = helper
|
|
116
|
+
export function C({ rows, base }: { rows: string[]; base: string }) {
|
|
117
|
+
const [k] = createSignal('sig')
|
|
118
|
+
const x = 'local'
|
|
119
|
+
const b = a
|
|
120
|
+
const c = alias
|
|
121
|
+
const d = ka
|
|
122
|
+
const helper = 'local-helper'
|
|
123
|
+
const e = viaHelper
|
|
124
|
+
return <ul>{rows.map((a, x) => <li key={a}>{b}{c}{d}{e}</li>)}</ul>
|
|
125
|
+
}
|
|
126
|
+
`
|
|
127
|
+
|
|
128
|
+
test('a module const read under a row that shadows its name reads the module binding', () => {
|
|
129
|
+
const inline = inliner(source, ['a', 'x'])
|
|
130
|
+
expect(inline('b')).toBe('"module"')
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
test('a module const initializer reads module consts, not the component prop or const of the same name', () => {
|
|
134
|
+
expect(inliner(source, ['alias', 'tag'])('c')).toBe('"m-base" + "!"')
|
|
135
|
+
expect(inliner(source, ['ka'])('d')).toBe('"mk"')
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('the same module const read with nothing shadowed still reads the module binding', () => {
|
|
139
|
+
const inline = inliner(source)
|
|
140
|
+
expect(inline('b')).toBe('"module"')
|
|
141
|
+
expect(inline('c')).toBe('"m-base" + "!"')
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
test('a component const shadows a module const of the same name', () => {
|
|
145
|
+
expect(inliner(source)('x')).toBe('x')
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
test('a module binding with no const to read is left alone when the component rebinds its name', () => {
|
|
149
|
+
expect(inliner(source, ['viaHelper'])('e')).toBe('e')
|
|
150
|
+
})
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
describe('only names the component scope binds shadow a module const (#3696)', () => {
|
|
154
|
+
const source = `
|
|
155
|
+
'use client'
|
|
156
|
+
const label = 'module' + '!'
|
|
157
|
+
const tagged = label + '?'
|
|
158
|
+
const title = 'm-title'
|
|
159
|
+
const viaTitle = title
|
|
160
|
+
const n = 1
|
|
161
|
+
export function C(props: { label: string; title: string; n: number; rows: string[] }) {
|
|
162
|
+
const { title } = props
|
|
163
|
+
const n = props.n * 2
|
|
164
|
+
const shown = label
|
|
165
|
+
const hop = tagged
|
|
166
|
+
const t = viaTitle
|
|
167
|
+
const m = n
|
|
168
|
+
return <ul>{props.rows.map(tagged => <li key={tagged}>{shown}{hop}{t}{m}</li>)}</ul>
|
|
169
|
+
}
|
|
170
|
+
`
|
|
171
|
+
|
|
172
|
+
test('a prop reached only through the props object does not shadow a module const', () => {
|
|
173
|
+
const inline = inliner(source)
|
|
174
|
+
expect(inline('shown')).toBe('"module" + "!"')
|
|
175
|
+
expect(inline('label')).toBe('"module" + "!"')
|
|
176
|
+
expect(inliner(source, ['tagged'])('hop')).toBe('"module" + "!" + "?"')
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
test('a module const named like a props destructure binding is still the module const', () => {
|
|
180
|
+
expect(inliner(source, ['viaTitle'])('t')).toBe('"m-title"')
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
test('a computed component const shadows a literal module const of the same name', () => {
|
|
184
|
+
expect(inliner(source)('m')).toBe('props.n * 2')
|
|
185
|
+
})
|
|
186
|
+
})
|
|
@@ -110,6 +110,30 @@ describe('expression-parser', () => {
|
|
|
110
110
|
}
|
|
111
111
|
})
|
|
112
112
|
|
|
113
|
+
test('folds a no-substitution template-literal key like the equivalent string key (#3763)', () => {
|
|
114
|
+
// Same static key, same `member` shape — adapters lower `props[`x`]`
|
|
115
|
+
// exactly as `props['x']` (a prop read), not as a dynamic index.
|
|
116
|
+
const pairs: Array<[string, string]> = [
|
|
117
|
+
['props[`title`]', "props['title']"],
|
|
118
|
+
['props?.[`title`]', "props?.['title']"],
|
|
119
|
+
['obj[`a\\u0062`]', "obj['a\\u0062']"],
|
|
120
|
+
['obj[`a\\nb`]', "obj['a\\nb']"],
|
|
121
|
+
['obj[`a\nb`]', "obj['a\\nb']"],
|
|
122
|
+
['obj[``]', "obj['']"],
|
|
123
|
+
]
|
|
124
|
+
for (const [templateKey, stringKey] of pairs) {
|
|
125
|
+
expect(parseExpression(templateKey)).toEqual(parseExpression(stringKey))
|
|
126
|
+
}
|
|
127
|
+
const folded = parseExpression('props[`title`]')
|
|
128
|
+
expect(folded.kind).toBe('member')
|
|
129
|
+
if (folded.kind === 'member') {
|
|
130
|
+
expect(folded.property).toBe('title')
|
|
131
|
+
expect(folded.computed).toBe(true)
|
|
132
|
+
}
|
|
133
|
+
// A key WITH a substitution is dynamic and stays an index access.
|
|
134
|
+
expect(parseExpression('props[`${k}`]').kind).toBe('index-access')
|
|
135
|
+
})
|
|
136
|
+
|
|
113
137
|
test('parses .length access', () => {
|
|
114
138
|
const result = parseExpression('items().length')
|
|
115
139
|
expect(result.kind).toBe('member')
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #3688 — a `{...props}` spread onto a child component in the client JS.
|
|
3
|
+
*
|
|
4
|
+
* Phase 1 marks a child-component spread of the component's own props
|
|
5
|
+
* object (`SpreadAttr.propsObject`). The client then keeps that spread at
|
|
6
|
+
* its source position in the registration template's `renderChild(...)`
|
|
7
|
+
* props, and reads an explicit prop written before it from the caller's
|
|
8
|
+
* props first in `initChild`'s `forwardProps(...)` overrides, so both merge
|
|
9
|
+
* like the JSX call does. A `{...rest}` bag is not the caller's props (it
|
|
10
|
+
* leaves out the destructured keys) and is not marked.
|
|
11
|
+
*
|
|
12
|
+
* The end-to-end runtime check lives in
|
|
13
|
+
* `packages/client/__tests__/runtime/issue-3688-props-spread-merge.test.ts`;
|
|
14
|
+
* the rendered output is pinned by the `component-spread-*` conformance
|
|
15
|
+
* fixtures.
|
|
16
|
+
*/
|
|
17
|
+
import { describe, test, expect } from 'bun:test'
|
|
18
|
+
import { compileJSX } from '../compiler'
|
|
19
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
20
|
+
import { analyzeComponent } from '../analyzer'
|
|
21
|
+
import { jsxToIR } from '../jsx-to-ir'
|
|
22
|
+
import type { IRNode, IRComponent } from '../types'
|
|
23
|
+
|
|
24
|
+
const adapter = new TestAdapter()
|
|
25
|
+
|
|
26
|
+
function clientJs(source: string): string {
|
|
27
|
+
const result = compileJSX(source, 'Repro.tsx', { adapter })
|
|
28
|
+
expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
|
|
29
|
+
return result.files.find(f => f.type === 'clientJs')!.content
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function findComponent(node: IRNode, name: string): IRComponent | null {
|
|
33
|
+
if (node.type === 'component' && node.name === name) return node
|
|
34
|
+
const children = 'children' in node && Array.isArray(node.children) ? (node.children as IRNode[]) : []
|
|
35
|
+
for (const child of children) {
|
|
36
|
+
const found = findComponent(child, name)
|
|
37
|
+
if (found) return found
|
|
38
|
+
}
|
|
39
|
+
return null
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function tagSpread(source: string, componentName: string) {
|
|
43
|
+
const ctx = analyzeComponent(source, 'Repro.tsx', componentName)
|
|
44
|
+
const ir = jsxToIR(ctx)!
|
|
45
|
+
const tag = findComponent(ir, 'Tag')!
|
|
46
|
+
return tag.props.find(p => p.name === '...')!.value
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
describe('#3688 — Phase 1 marks a props-object spread onto a child', () => {
|
|
50
|
+
test('the props object, directly and through an alias, is marked', () => {
|
|
51
|
+
expect(tagSpread(`
|
|
52
|
+
import { Tag } from './tag'
|
|
53
|
+
export function A(props: { tone?: string }) { return <p><Tag {...props} /></p> }
|
|
54
|
+
`, 'A')).toMatchObject({ kind: 'spread', propsObject: true })
|
|
55
|
+
expect(tagSpread(`
|
|
56
|
+
import { Tag } from './tag'
|
|
57
|
+
export function B(props: { tone?: string }) { const p2 = props; return <p><Tag {...p2} /></p> }
|
|
58
|
+
`, 'B')).toMatchObject({ kind: 'spread', propsObject: true })
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('an open rest bag and an ordinary object are not marked', () => {
|
|
62
|
+
const rest = tagSpread(`
|
|
63
|
+
import { Tag } from './tag'
|
|
64
|
+
export function C({ title, ...rest }: { title?: string; [key: string]: any }) {
|
|
65
|
+
return <p title={title}><Tag {...rest} /></p>
|
|
66
|
+
}
|
|
67
|
+
`, 'C')
|
|
68
|
+
expect(rest.kind).toBe('spread')
|
|
69
|
+
expect(rest).not.toHaveProperty('propsObject')
|
|
70
|
+
const local = tagSpread(`
|
|
71
|
+
import { Tag } from './tag'
|
|
72
|
+
export function D(props: { tone?: string }) { const extra = { tone: 'x' }; return <p><Tag {...extra} /></p> }
|
|
73
|
+
`, 'D')
|
|
74
|
+
expect(local).not.toHaveProperty('propsObject')
|
|
75
|
+
})
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
describe('#3688 — the client JS merges a props-object spread in source order', () => {
|
|
79
|
+
const source = `
|
|
80
|
+
import { Tag } from './tag'
|
|
81
|
+
export function Parent(props: { tone?: string; size?: string; label?: string }) {
|
|
82
|
+
return <p><Tag tone="first" size="s" {...props} label="last" /></p>
|
|
83
|
+
}
|
|
84
|
+
`
|
|
85
|
+
|
|
86
|
+
test('the registration template keeps the spread between the explicit props', () => {
|
|
87
|
+
expect(clientJs(source)).toContain(`renderChild('Tag', {tone: "first", size: "s", ..._p, label: "last"}`)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test("initChild reads an explicit prop written before the spread from the caller's props first", () => {
|
|
91
|
+
const js = clientJs(source)
|
|
92
|
+
expect(js).toContain(`get tone() { return ("tone" in _p ? _p["tone"] : "first") }`)
|
|
93
|
+
expect(js).toContain(`get size() { return ("size" in _p ? _p["size"] : "s") }`)
|
|
94
|
+
// A prop after the spread always wins: a plain override.
|
|
95
|
+
expect(js).toContain(`label: "last" }`)
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test('both reactive DOM mirrors read an overridden explicit prop caller-first, like initChild', () => {
|
|
99
|
+
const js = clientJs(`
|
|
100
|
+
'use client'
|
|
101
|
+
import { createSignal } from '@barefootjs/client'
|
|
102
|
+
import { Tag } from './tag'
|
|
103
|
+
export function Live(props: { title?: string }) {
|
|
104
|
+
const [title, setTitle] = createSignal('first')
|
|
105
|
+
return <p onClick={() => setTitle('second')}><Tag title={title()} {...props} last={title()} /></p>
|
|
106
|
+
}
|
|
107
|
+
`)
|
|
108
|
+
const callerFirst = `("title" in _p ? _p["title"] : bfSignal_title())`
|
|
109
|
+
expect(js).toContain(`get title() { return ${callerFirst} }`)
|
|
110
|
+
// The signal-getter mirror (`ctx.reactiveProps`) and the child-prop
|
|
111
|
+
// mirror (`ctx.reactiveChildProps`) write the same merged value.
|
|
112
|
+
expect(js).toContain(`const __v = ${callerFirst};`)
|
|
113
|
+
expect(js).toContain(`const __x = ${callerFirst}`)
|
|
114
|
+
// A prop after the spread is never shadowed.
|
|
115
|
+
expect(js).not.toContain(`"last" in _p`)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
test('a conditional branch template keeps the spread too', () => {
|
|
119
|
+
const js = clientJs(`
|
|
120
|
+
'use client'
|
|
121
|
+
import { createSignal } from '@barefootjs/client'
|
|
122
|
+
import { Tag } from './tag'
|
|
123
|
+
export function Toggle(props: { tone?: string }) {
|
|
124
|
+
const [on, setOn] = createSignal(true)
|
|
125
|
+
return <div onClick={() => setOn(!on())}>{on() ? <Tag {...props} tone="x" /> : <i>off</i>}</div>
|
|
126
|
+
}
|
|
127
|
+
`)
|
|
128
|
+
expect(js).toContain(`renderChild('Tag', {..._p, tone: "x"}`)
|
|
129
|
+
expect(js).not.toContain(`"tone" in _p`)
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
test('an open rest bag stays out of the template props', () => {
|
|
133
|
+
const js = clientJs(`
|
|
134
|
+
import { Tag } from './tag'
|
|
135
|
+
export function Rest({ title, ...rest }: { title?: string; [key: string]: any }) {
|
|
136
|
+
return <p title={title}><Tag title="x" {...rest} /></p>
|
|
137
|
+
}
|
|
138
|
+
`)
|
|
139
|
+
expect(js).toContain(`renderChild('Tag', {title: "x"}`)
|
|
140
|
+
expect(js).not.toContain(`"title" in _p`)
|
|
141
|
+
})
|
|
142
|
+
})
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `IRExpression.loopRowPropRead`: which child-position expressions inside a
|
|
3
|
+
* loop row read one of the component's own props (other than `children`),
|
|
4
|
+
* decided from the binding in Phase 1. The client claims and writes those row
|
|
5
|
+
* slots as markup, so a caller's JSX element at that prop renders once
|
|
6
|
+
* instead of as `[object Object]` beside a duplicate of the element.
|
|
7
|
+
*/
|
|
8
|
+
import { describe, test, expect } from 'bun:test'
|
|
9
|
+
import { compileJSX } from '../compiler'
|
|
10
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
11
|
+
|
|
12
|
+
/** `[expr, loopRowPropRead]` of every expression child, in document order. */
|
|
13
|
+
function propReads(source: string): [string, boolean][] {
|
|
14
|
+
const result = compileJSX(source.trimStart(), 'C.tsx', { adapter: new TestAdapter(), outputIR: true })
|
|
15
|
+
const ir = JSON.parse(result.files.find(f => f.type === 'ir')!.content)
|
|
16
|
+
const out: [string, boolean][] = []
|
|
17
|
+
const walk = (node: unknown): void => {
|
|
18
|
+
if (!node || typeof node !== 'object') return
|
|
19
|
+
const n = node as Record<string, unknown>
|
|
20
|
+
if (n.type === 'expression') out.push([n.expr as string, n.loopRowPropRead === true])
|
|
21
|
+
for (const value of Object.values(n)) {
|
|
22
|
+
if (Array.isArray(value)) value.forEach(walk)
|
|
23
|
+
else if (value && typeof value === 'object') walk(value)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
walk(ir.root)
|
|
27
|
+
return out
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The client JS of the first component in `source`. */
|
|
31
|
+
function clientJs(source: string): string {
|
|
32
|
+
const result = compileJSX(source.trimStart(), 'C.tsx', { adapter: new TestAdapter() })
|
|
33
|
+
return result.files.find(f => f.type === 'clientJs')!.content
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe('loopRowPropRead', () => {
|
|
37
|
+
test('a props-object member read in a row is flagged; the row item, children and an outside read are not', () => {
|
|
38
|
+
expect(propReads(`
|
|
39
|
+
export function List(props: { items: string[]; footer?: unknown; children?: unknown }) {
|
|
40
|
+
return (
|
|
41
|
+
<div>
|
|
42
|
+
{props.footer}
|
|
43
|
+
<ul>{props.items.map(i => <li key={i}>{i}{props.footer}{props.children}</li>)}</ul>
|
|
44
|
+
</div>
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
`)).toEqual([
|
|
48
|
+
['props.footer', false],
|
|
49
|
+
['i', false],
|
|
50
|
+
['props.footer', true],
|
|
51
|
+
['props.children', false],
|
|
52
|
+
])
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('a destructured, renamed, aliased or body-destructured prop read in a row is flagged', () => {
|
|
56
|
+
expect(propReads(`
|
|
57
|
+
export function List({ items, footer: foot, label }: { items: string[]; footer?: unknown; label: string }) {
|
|
58
|
+
const alias = foot
|
|
59
|
+
return <ul>{items.map(i => <li key={i}>{foot}{alias}{label}</li>)}</ul>
|
|
60
|
+
}
|
|
61
|
+
`)).toEqual([
|
|
62
|
+
['foot', true],
|
|
63
|
+
['alias', true],
|
|
64
|
+
['label', true],
|
|
65
|
+
])
|
|
66
|
+
expect(propReads(`
|
|
67
|
+
export function List(props: { items: string[]; footer?: unknown }) {
|
|
68
|
+
const { footer } = props
|
|
69
|
+
const p = props
|
|
70
|
+
return <ul>{props.items.map(i => <li key={i}>{footer}{p.footer}</li>)}</ul>
|
|
71
|
+
}
|
|
72
|
+
`)).toEqual([
|
|
73
|
+
['footer', true],
|
|
74
|
+
['p.footer', true],
|
|
75
|
+
])
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test('a row binding that shadows the prop name or the props object is not flagged', () => {
|
|
79
|
+
expect(propReads(`
|
|
80
|
+
export function List({ items, label }: { items: string[]; label: string }) {
|
|
81
|
+
return <ul>{items.map(label => <li key={label}>{label}</li>)}</ul>
|
|
82
|
+
}
|
|
83
|
+
`)).toEqual([['label', false]])
|
|
84
|
+
expect(propReads(`
|
|
85
|
+
export function List(props: { items: { footer: string }[] }) {
|
|
86
|
+
return <ul>{props.items.map(props => <li key={props.footer}>{props.footer}</li>)}</ul>
|
|
87
|
+
}
|
|
88
|
+
`)).toEqual([['props.footer', false]])
|
|
89
|
+
expect(propReads(`
|
|
90
|
+
export function List({ items, footer }: { items: string[]; footer?: unknown }) {
|
|
91
|
+
return <ul>{items.map(i => { const footer = i + '!'; return <li key={i}>{footer}</li> })}</ul>
|
|
92
|
+
}
|
|
93
|
+
`)).toEqual([['footer', false]])
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
test('the client claims and writes a flagged row slot as markup, and splices it with escapeTextOrMarkup', () => {
|
|
97
|
+
const js = clientJs(`
|
|
98
|
+
'use client'
|
|
99
|
+
import { createSignal } from '@barefootjs/client'
|
|
100
|
+
export function List(props: { footer?: unknown }) {
|
|
101
|
+
const [rows] = createSignal(['a'])
|
|
102
|
+
return <ul>{rows().map(i => <li key={i}>{i}{props.footer}</li>)}</ul>
|
|
103
|
+
}
|
|
104
|
+
`)
|
|
105
|
+
expect(js).toContain("{ id: 's1', kind: 'markup', path: [] }")
|
|
106
|
+
expect(js).toContain("{ id: 's0', kind: 'text', path: [] }")
|
|
107
|
+
expect(js).toContain('escapeTextOrNode(__x)')
|
|
108
|
+
expect(js).toContain('escapeTextOrMarkup(_p.footer)')
|
|
109
|
+
expect(js).toContain('escapeText(i)')
|
|
110
|
+
})
|
|
111
|
+
})
|
|
@@ -118,3 +118,140 @@ describe('MemoInfo.bodyExpr (#3510)', () => {
|
|
|
118
118
|
expect(template({})).toContain('-->0<!--')
|
|
119
119
|
})
|
|
120
120
|
})
|
|
121
|
+
|
|
122
|
+
describe('template text over a body destructure binding in a (props)-arg component', () => {
|
|
123
|
+
function analyze(body: string) {
|
|
124
|
+
return analyzeComponent(`
|
|
125
|
+
'use client'
|
|
126
|
+
import { createSignal, createMemo } from '@barefootjs/client'
|
|
127
|
+
export function C(props: { b?: string[] }) {
|
|
128
|
+
${body}
|
|
129
|
+
return <p>{n()}</p>
|
|
130
|
+
}
|
|
131
|
+
`, 'C.tsx')
|
|
132
|
+
}
|
|
133
|
+
const read = "(_p.b ?? (['y', ''].filter((x) => x !== '')))"
|
|
134
|
+
|
|
135
|
+
test('a memo body reads a renamed binding with a callback default as its prop', () => {
|
|
136
|
+
const ctx = analyze(`
|
|
137
|
+
const { b: local = ['y', ''].filter((x) => x !== '') } = props
|
|
138
|
+
const n = createMemo(() => local.length)
|
|
139
|
+
`)
|
|
140
|
+
expect(ctx.memos.find(m => m.name === 'n')?.templateBodyExpr).toBe(`${read}.length`)
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
test('a memo body reads an un-renamed binding with a callback default as its prop', () => {
|
|
144
|
+
const ctx = analyze(`
|
|
145
|
+
const { b = ['y', ''].filter((x) => x !== '') } = props
|
|
146
|
+
const n = createMemo(() => b.length)
|
|
147
|
+
`)
|
|
148
|
+
expect(ctx.memos.find(m => m.name === 'n')?.templateBodyExpr).toBe(`${read}.length`)
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
test('a signal seed reads the binding as its prop', () => {
|
|
152
|
+
const ctx = analyze(`
|
|
153
|
+
const { b: local = ['y', ''].filter((x) => x !== '') } = props
|
|
154
|
+
const [n] = createSignal(local.length)
|
|
155
|
+
`)
|
|
156
|
+
expect(ctx.signals.find(s => s.getter === 'n')?.templateInitialValue).toBe(`${read}.length`)
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
test('a binding the component mutates later keeps its local read', () => {
|
|
160
|
+
const ctx = analyze(`
|
|
161
|
+
const { b: local = ['y', ''].filter((x) => x !== '') } = props
|
|
162
|
+
local.push('z')
|
|
163
|
+
const n = createMemo(() => local.length)
|
|
164
|
+
`)
|
|
165
|
+
expect(ctx.memos.find(m => m.name === 'n')?.templateBodyExpr).toBeUndefined()
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
test('the CSR template renders the memo over the default', () => {
|
|
169
|
+
const result = compileJSX(`
|
|
170
|
+
'use client'
|
|
171
|
+
import { createMemo } from '@barefootjs/client'
|
|
172
|
+
export function C(props: { b?: string[] }) {
|
|
173
|
+
const { b: local = ['y', ''].filter((x) => x !== '') } = props
|
|
174
|
+
const n = createMemo(() => local.length)
|
|
175
|
+
return <p>{n()}</p>
|
|
176
|
+
}
|
|
177
|
+
`, 'C.tsx', { adapter: new TestAdapter() })
|
|
178
|
+
const line = result.files.find((f) => f.type === 'clientJs')!.content.split('\n').find((l) => l.includes('hydrate('))!
|
|
179
|
+
const lambda = line.match(/template: (\(_p\) => `.*`) \}\)$/)![1]
|
|
180
|
+
const template = new Function('escapeTextOrMarkup', 'escapeText', 'escapeAttr', `return ${lambda}`)(String, String, String)
|
|
181
|
+
expect(template({})).toContain('-->1<!--')
|
|
182
|
+
expect(template({ b: ['p', 'q', 'r'] })).toContain('-->3<!--')
|
|
183
|
+
expect(template({ b: [] })).toContain('-->0<!--')
|
|
184
|
+
})
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
// The rendered values for these shapes (the colliding prop supplied) are
|
|
188
|
+
// asserted by the CSR conformance fixtures `memo-local-function-named-like-prop`,
|
|
189
|
+
// `signal-seed-local-function-named-like-prop`, `memo-module-binding-named-like-prop`,
|
|
190
|
+
// `memo-const-declared-after-named-like-prop` and `markup-local-function-named-like-prop`.
|
|
191
|
+
// Here only the analyzer's template fields and the emitted template text.
|
|
192
|
+
describe('template text keeps a local named like a typed prop (props)-arg component', () => {
|
|
193
|
+
function analyze(body: string, preamble = '') {
|
|
194
|
+
return analyzeComponent(`
|
|
195
|
+
'use client'
|
|
196
|
+
import { createSignal, createMemo } from '@barefootjs/client'
|
|
197
|
+
${preamble}
|
|
198
|
+
export function C(props: { value?: number }) {
|
|
199
|
+
${body}
|
|
200
|
+
return <p>{n()}</p>
|
|
201
|
+
}
|
|
202
|
+
`, 'C.tsx')
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
test('a memo body calling a local function keeps the call', () => {
|
|
206
|
+
const ctx = analyze(`
|
|
207
|
+
function value() { return 1 }
|
|
208
|
+
const n = createMemo(() => value())
|
|
209
|
+
`)
|
|
210
|
+
expect(ctx.memos.find(m => m.name === 'n')?.templateBodyExpr).toBeUndefined()
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
test('a signal seed calling a local function keeps the call', () => {
|
|
214
|
+
const ctx = analyze(`
|
|
215
|
+
function value() { return 1 }
|
|
216
|
+
const [n] = createSignal(value())
|
|
217
|
+
`)
|
|
218
|
+
expect(ctx.signals.find(s => s.getter === 'n')?.templateInitialValue).toBeUndefined()
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
test('a memo body reading an ordinary const declared after it keeps the read', () => {
|
|
222
|
+
const ctx = analyze(`
|
|
223
|
+
const n = createMemo(() => value)
|
|
224
|
+
const value = 1
|
|
225
|
+
`)
|
|
226
|
+
expect(ctx.memos.find(m => m.name === 'n')?.templateBodyExpr).toBeUndefined()
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
test('a memo body reading a module-scope binding keeps the read', () => {
|
|
230
|
+
const ctx = analyze(`
|
|
231
|
+
const n = createMemo(() => value)
|
|
232
|
+
`, 'const value = 1')
|
|
233
|
+
expect(ctx.memos.find(m => m.name === 'n')?.templateBodyExpr).toBeUndefined()
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
test('markup calling a local function does not read the prop', () => {
|
|
237
|
+
const result = compileJSX(`
|
|
238
|
+
'use client'
|
|
239
|
+
import { createSignal } from '@barefootjs/client'
|
|
240
|
+
export function C(props: { value?: number }) {
|
|
241
|
+
function value() { return 1 }
|
|
242
|
+
const [n] = createSignal(0)
|
|
243
|
+
return <p>{value()}|{n()}</p>
|
|
244
|
+
}
|
|
245
|
+
`, 'C.tsx', { adapter: new TestAdapter() })
|
|
246
|
+
const clientJs = result.files.find((f) => f.type === 'clientJs')!.content
|
|
247
|
+
expect(clientJs).not.toContain('_p.value')
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
test('a memo body still reads a body destructure binding as its prop', () => {
|
|
251
|
+
const ctx = analyze(`
|
|
252
|
+
const { value = 4 } = props
|
|
253
|
+
const n = createMemo(() => value * 2)
|
|
254
|
+
`)
|
|
255
|
+
expect(ctx.memos.find(m => m.name === 'n')?.templateBodyExpr).toBe('(_p.value ?? 4) * 2')
|
|
256
|
+
})
|
|
257
|
+
})
|