@barefootjs/jsx 0.39.9 → 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 +1 -20
- 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 +95 -2
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/loop-plan.d.ts +41 -1
- package/dist/adapters/loop-plan.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/augment-inherited-props.d.ts.map +1 -1
- package/dist/css-layer-prefixer.d.ts.map +1 -1
- package/dist/debug.d.ts.map +1 -1
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/index.d.ts +10 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2102 -1344
- 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 +9 -1
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/module-exports.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 +24 -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 +1 -1
- package/dist/template-parts.d.ts.map +1 -1
- package/dist/types.d.ts +35 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/attr-plan.test.ts +1 -14
- package/src/__tests__/binding-scope-ratchet.test.ts +12 -2
- package/src/__tests__/boolean-carrier.test.ts +74 -0
- package/src/__tests__/client-template-escape-soundness.test.ts +17 -4
- package/src/__tests__/client-template-static-delimiters.test.ts +57 -0
- package/src/__tests__/component-props.test.ts +14 -0
- package/src/__tests__/computed-const.test.ts +85 -0
- package/src/__tests__/dsl-template-adapter.test.ts +70 -0
- package/src/__tests__/expression-parser.test.ts +24 -0
- package/src/__tests__/extract-function-params.test.ts +37 -0
- package/src/__tests__/issue-2960-multi-root-cond-same-tag.test.ts +18 -0
- package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +142 -0
- package/src/__tests__/jsx-text-linebreak-whitespace.test.ts +11 -0
- package/src/__tests__/loop-plan.test.ts +66 -1
- 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 +41 -0
- package/src/adapters/ambiguous-nil-arithmetic.ts +94 -0
- package/src/adapters/attr-plan.ts +8 -56
- 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 +183 -2
- package/src/adapters/loop-plan.ts +67 -2
- package/src/adapters/number-value.ts +95 -0
- package/src/analyzer-context.ts +8 -0
- package/src/analyzer.ts +187 -66
- package/src/augment-inherited-props.ts +5 -9
- package/src/css-layer-prefixer.ts +4 -5
- package/src/debug.ts +1 -0
- package/src/expression-parser.ts +4 -1
- package/src/index.ts +23 -5
- package/src/ir-to-client-js/collect-elements.ts +104 -46
- package/src/ir-to-client-js/compute-inlinability.ts +2 -2
- 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 +122 -75
- 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 +16 -2
- package/src/jsx-to-ir.ts +112 -55
- package/src/loop-destructure.ts +4 -1
- package/src/module-exports.ts +13 -21
- package/src/nullable-signal-attr.ts +134 -13
- package/src/props-binding.ts +54 -3
- package/src/rich-type-refusal.ts +3 -1
- package/src/scope/binding-scope.ts +35 -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 +25 -3
- package/src/template-position-walk.ts +3 -1
- package/src/types.ts +32 -1
|
@@ -139,3 +139,21 @@ describe('#2960: multi-root cond branch whose roots share a tag name', () => {
|
|
|
139
139
|
expect(result).not.toContain('bf-c=')
|
|
140
140
|
})
|
|
141
141
|
})
|
|
142
|
+
|
|
143
|
+
for (const entry of [
|
|
144
|
+
{ name: 'long single-root tag name', html: `<${'a'.repeat(20_000)}></${'a'.repeat(20_000)}>`, single: true },
|
|
145
|
+
{ name: 'unterminated long nested tag', html: `<div><${'a'.repeat(20_000)}`, single: false },
|
|
146
|
+
{ name: 'many comment-like tokens without terminators', html: `<div>${'<!--x>'.repeat(5_000)}</div>`, single: true },
|
|
147
|
+
{ name: 'many marker comments', html: `<div>${'<!--bf:s0--><!--/-->'.repeat(5_000)}</div>`, single: true },
|
|
148
|
+
{ name: 'spaced self-closing root', html: '<input />', single: true },
|
|
149
|
+
]) {
|
|
150
|
+
test(`conditional root classification: ${entry.name}`, () => {
|
|
151
|
+
const result = addCondAttrToTemplate(entry.html, 'probe')
|
|
152
|
+
if (entry.single) {
|
|
153
|
+
expect(result).toContain(' bf-c="probe"')
|
|
154
|
+
expect(result).not.toContain('<!--bf-cond-start:probe-->')
|
|
155
|
+
} else {
|
|
156
|
+
expect(result).toBe(`<!--bf-cond-start:probe-->${entry.html}<!--bf-cond-end:probe-->`)
|
|
157
|
+
}
|
|
158
|
+
})
|
|
159
|
+
}
|
|
@@ -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
|
+
})
|
|
@@ -78,3 +78,14 @@ describe('JSX text whitespace across line breaks (#3237)', () => {
|
|
|
78
78
|
expect(js).toContain('<div><b>a</b> <i>b</i></div>')
|
|
79
79
|
})
|
|
80
80
|
})
|
|
81
|
+
|
|
82
|
+
test('long interior space runs survive line cleanup', () => {
|
|
83
|
+
const text = `start${' '.repeat(30_000)}end`
|
|
84
|
+
const source = `'use client'; export function Probe() { return <pre>\n${text} \n</pre> }`
|
|
85
|
+
expect(getClientJs(source)).toContain(`<pre>${text}</pre>`)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('line cleanup removes ASCII indentation but preserves nonbreaking spaces', () => {
|
|
89
|
+
const source = `'use client'; export function Probe() { return <pre>\n \u00a0text\u00a0 \n</pre> }`
|
|
90
|
+
expect(getClientJs(source)).toContain('<pre>\u00a0text\u00a0</pre>')
|
|
91
|
+
})
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { describe, test, expect } from 'bun:test'
|
|
7
7
|
import { analyzeComponent } from '../analyzer'
|
|
8
8
|
import { jsxToIR } from '../jsx-to-ir'
|
|
9
|
-
import { planLoop, spellDestructureLocals, spellFilteredRow, spellSortedSource, type LoopPlanContext, type LoopSortSpelling } from '../adapters/loop-plan'
|
|
9
|
+
import { loopHeaderTargets, planLoop, savedLoopBindings, spellDestructureLocals, spellFilteredRow, spellSortedSource, type LoopPlanContext, type LoopSortSpelling } from '../adapters/loop-plan'
|
|
10
10
|
import { BindingScope } from '../scope/binding-scope'
|
|
11
11
|
import type { IRLoop, IRNode } from '../types'
|
|
12
12
|
|
|
@@ -232,3 +232,68 @@ describe('planLoop', () => {
|
|
|
232
232
|
expect(refused).toMatchObject({ destructure: 'unlowerable', destructureLocals: [] })
|
|
233
233
|
})
|
|
234
234
|
})
|
|
235
|
+
|
|
236
|
+
function loopOf(source: string): IRLoop {
|
|
237
|
+
return firstLoop(jsxToIR(analyzeComponent(source, 'Test.tsx')))
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
describe('loopHeaderTargets', () => {
|
|
241
|
+
test('an array iteration binds its param, unless the param is not the item', () => {
|
|
242
|
+
const items = loopOf(`export function Test(props: { items: string[] }) { return <ul>{props.items.map((x, i) => <li key={x}>{i}</li>)}</ul> }`)
|
|
243
|
+
expect(loopHeaderTargets(items, { itemVar: 'x' })).toEqual(['x'])
|
|
244
|
+
const keys = loopOf(`export function Test(props: { items: string[] }) { return <ul>{props.items.keys().map(k => <li key={k}>{k}</li>)}</ul> }`)
|
|
245
|
+
expect(loopHeaderTargets(keys, { itemVar: '__bf_item' })).toEqual([])
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
test('an entries object iteration binds its key and value', () => {
|
|
249
|
+
const entries = loopOf(`export function Test(props: { m: Record<string, string> }) { return <ul>{Object.entries(props.m).map(([k, v]) => <li key={k}>{v}</li>)}</ul> }`)
|
|
250
|
+
expect(loopHeaderTargets(entries, { itemVar: 'v' })).toEqual(['k', 'v'])
|
|
251
|
+
})
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
describe('savedLoopBindings', () => {
|
|
255
|
+
const source = `export function Test(props: { items: string[] }) {
|
|
256
|
+
return <ul>{props.items.map((label, i) => { const n = label.length; return <li key={label}>{i}{n}</li> })}</ul>
|
|
257
|
+
}`
|
|
258
|
+
|
|
259
|
+
function saved(outside: BindingScope, templateLevelNames: string[], skip: string[] = []) {
|
|
260
|
+
const loop = loopOf(source)
|
|
261
|
+
return savedLoopBindings(
|
|
262
|
+
{ outside, row: outside.enterLoopRow(loop), templateLevelNames: new Set(templateLevelNames) },
|
|
263
|
+
skip,
|
|
264
|
+
(name, ordinal) => `held_${ordinal}_${name}`,
|
|
265
|
+
)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
test('saves each row binding a template-level name or an enclosing row also binds, in the order the template binds them', () => {
|
|
269
|
+
const outside = BindingScope.EMPTY.enterCallback(['n'])
|
|
270
|
+
expect(saved(outside, ['i', 'label', 'other'])).toEqual([
|
|
271
|
+
{ name: 'label', temporary: 'held_0_label' },
|
|
272
|
+
{ name: 'i', temporary: 'held_1_i' },
|
|
273
|
+
{ name: 'n', temporary: 'held_2_n' },
|
|
274
|
+
])
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
test('an index local binds before a destructure local', () => {
|
|
278
|
+
const loop = loopOf(`export function Test(props: { rows: { name: string }[] }) { return <ul>{props.rows.map(({ name }, count) => <li key={name}>{count}</li>)}</ul> }`)
|
|
279
|
+
const saved = savedLoopBindings(
|
|
280
|
+
{ outside: BindingScope.EMPTY, row: BindingScope.EMPTY.enterLoopRow(loop), templateLevelNames: new Set(['name', 'count']) },
|
|
281
|
+
[],
|
|
282
|
+
(_name, ordinal) => `held_${ordinal}`,
|
|
283
|
+
)
|
|
284
|
+
expect(saved.map((b) => b.name)).toEqual(['count', 'name'])
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
test('a row binding nothing outside shares is not saved', () => {
|
|
288
|
+
expect(saved(BindingScope.EMPTY, ['other'])).toEqual([])
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
test('a skipped name is restored by the backend itself', () => {
|
|
292
|
+
expect(saved(BindingScope.EMPTY, ['label', 'i'], ['label'])).toEqual([{ name: 'i', temporary: 'held_0_i' }])
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
test('a holder that collides with a row binding or a template-level name takes a suffix', () => {
|
|
296
|
+
expect(saved(BindingScope.EMPTY, ['i', 'held_0_i'])).toEqual([{ name: 'i', temporary: 'held_0_i_' }])
|
|
297
|
+
expect(saved(BindingScope.EMPTY.enterCallback(['held_0_i']), ['i'])).toEqual([{ name: 'i', temporary: 'held_0_i_' }])
|
|
298
|
+
})
|
|
299
|
+
})
|
|
@@ -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
|
+
})
|
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import { describe, test, expect } from 'bun:test'
|
|
2
2
|
import { analyzeComponent } from '../analyzer'
|
|
3
|
-
import { parseExpression } from '../expression-parser'
|
|
3
|
+
import { exprToString, parseExpression } from '../expression-parser'
|
|
4
4
|
import {
|
|
5
|
+
ambiguousNilArithmeticOperands,
|
|
5
6
|
arithmeticOperandCoercion,
|
|
7
|
+
arithmeticOperandIsNull,
|
|
6
8
|
arithmeticOperandIsUndefined,
|
|
9
|
+
arithmeticOperandNilIsAmbiguous,
|
|
7
10
|
attrValueMayBeNullish,
|
|
8
11
|
collectAbsentPropSeedReads,
|
|
9
12
|
collectNullableSignalGetters,
|
|
10
13
|
collectNullishAttrContext,
|
|
11
14
|
nullableSignalAttrGetter,
|
|
12
15
|
} from '../nullable-signal-attr'
|
|
16
|
+
import { BindingScope } from '../scope/binding-scope'
|
|
13
17
|
import type { ComponentIR } from '../types'
|
|
14
18
|
|
|
15
19
|
function nullableGetters(decls: string): Set<string> {
|
|
@@ -120,6 +124,7 @@ describe('attrValueMayBeNullish (#3322)', () => {
|
|
|
120
124
|
const plain = createMemo(() => t())
|
|
121
125
|
const typed = createMemo<string | undefined>(() => t())
|
|
122
126
|
const loop = createMemo(() => loop())
|
|
127
|
+
const tail = createMemo(() => t().at(-1))
|
|
123
128
|
return <p>x</p>
|
|
124
129
|
}
|
|
125
130
|
`
|
|
@@ -157,6 +162,20 @@ describe('attrValueMayBeNullish (#3322)', () => {
|
|
|
157
162
|
expect(mayBe('0')).toBe(false)
|
|
158
163
|
})
|
|
159
164
|
|
|
165
|
+
test('`.at(i)` / `.find()` / `.findLast()` read undefined, directly or through a memo', () => {
|
|
166
|
+
expect(mayBe('t().at(-1)')).toBe(true)
|
|
167
|
+
expect(mayBe('tail()')).toBe(true)
|
|
168
|
+
expect(mayBe("t() ? t().at(9) : 'x'")).toBe(true)
|
|
169
|
+
expect(mayBe("t().at(-1) ?? 'x'")).toBe(false)
|
|
170
|
+
expect(mayBe('t().slice(-1)')).toBe(false)
|
|
171
|
+
expect(mayBe("t().indexOf('a')")).toBe(false)
|
|
172
|
+
expect(mayBe("xs.find(x => x === 'z')")).toBe(true)
|
|
173
|
+
expect(mayBe('xs.findLast(x => x)')).toBe(true)
|
|
174
|
+
expect(mayBe("xs.find(x => x === 'z').name")).toBe(true)
|
|
175
|
+
expect(mayBe('xs.findIndex(x => x)')).toBe(false)
|
|
176
|
+
expect(mayBe('xs.filter(x => x)')).toBe(false)
|
|
177
|
+
})
|
|
178
|
+
|
|
160
179
|
test('a getter a loop binding shadows is not the signal or memo', () => {
|
|
161
180
|
expect(mayBe('label()', ['label'])).toBe(false)
|
|
162
181
|
})
|
|
@@ -196,7 +215,8 @@ describe('arithmeticOperandCoercion (#3474)', () => {
|
|
|
196
215
|
for (const op of ['-', '*', '/', '%', '**']) expect(coercion(ctx, op, 'props.b', 'x')).toBe('undefined')
|
|
197
216
|
expect(coercion(ctx, '+', 'props.b', '-2')).toBe('undefined')
|
|
198
217
|
expect(coercion(ctx, '*', 'props.u', '2')).toBe('undefined')
|
|
199
|
-
// A type that admits both keeps the null
|
|
218
|
+
// A type that admits both keeps the null spelling; the arithmetic over it
|
|
219
|
+
// is refused (`arithmeticOperandNilIsAmbiguous`, #3700).
|
|
200
220
|
expect(coercion(ctx, '*', 'props.both', '2')).toBe('null')
|
|
201
221
|
})
|
|
202
222
|
|
|
@@ -231,3 +251,78 @@ describe('arithmeticOperandCoercion (#3474)', () => {
|
|
|
231
251
|
expect(coercion(ctxOf('', 'const [t] = createSignal<number>()'), '+', 't()', '1')).toBe('undefined')
|
|
232
252
|
})
|
|
233
253
|
})
|
|
254
|
+
|
|
255
|
+
describe('arithmeticOperandNilIsAmbiguous (#3700)', () => {
|
|
256
|
+
function ctxOf(signature: string, decls = ''): ReturnType<typeof collectNullishAttrContext> {
|
|
257
|
+
const source = `
|
|
258
|
+
'use client'
|
|
259
|
+
import { createMemo } from '@barefootjs/client'
|
|
260
|
+
export function C(${signature}) {
|
|
261
|
+
${decls}
|
|
262
|
+
return <p>x</p>
|
|
263
|
+
}
|
|
264
|
+
`
|
|
265
|
+
return collectNullishAttrContext({ metadata: analyzeComponent(source, 'C.tsx') } as unknown as ComponentIR)
|
|
266
|
+
}
|
|
267
|
+
const ambiguous = (ctx: ReturnType<typeof collectNullishAttrContext>, op: string, operand: string, other?: string) =>
|
|
268
|
+
arithmeticOperandNilIsAmbiguous(op, parseExpression(operand), other === undefined ? undefined : parseExpression(other), ctx)
|
|
269
|
+
|
|
270
|
+
test('a number prop that admits both null and absence, in either props form', () => {
|
|
271
|
+
for (const signature of ['props: { n?: number | null }', 'props: { n: number | null | undefined }']) {
|
|
272
|
+
const ctx = ctxOf(signature)
|
|
273
|
+
for (const op of ['-', '*', '/', '%', '**']) expect(ambiguous(ctx, op, 'props.n', 'x')).toBe(true)
|
|
274
|
+
expect(ambiguous(ctx, '+', 'props.n', '1')).toBe(true)
|
|
275
|
+
expect(ambiguous(ctx, '-', 'props.n')).toBe(true)
|
|
276
|
+
// `+` against a non-number may concatenate: no `ToNumber`, no ambiguity.
|
|
277
|
+
expect(ambiguous(ctx, '+', 'props.n', "'px'")).toBe(false)
|
|
278
|
+
}
|
|
279
|
+
const destructured = ctxOf('{ n, m: alias }: { n?: number | null; m?: null | number }')
|
|
280
|
+
expect(ambiguous(destructured, '*', 'n', '2')).toBe(true)
|
|
281
|
+
expect(ambiguous(destructured, '*', 'alias', '2')).toBe(true)
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
test('a prop with one nil reading, a defaulted one and a shadowed one are not ambiguous', () => {
|
|
285
|
+
const ctx = ctxOf('props: { a: number | null; b?: number; c: number }')
|
|
286
|
+
for (const operand of ['props.a', 'props.b', 'props.c']) expect(ambiguous(ctx, '*', operand, '2')).toBe(false)
|
|
287
|
+
expect(ambiguous(ctxOf('{ n = 1 }: { n?: number | null }'), '*', 'n', '2')).toBe(false)
|
|
288
|
+
const destructured = ctxOf('{ n }: { n?: number | null }')
|
|
289
|
+
expect(arithmeticOperandNilIsAmbiguous('*', parseExpression('n'), parseExpression('2'), destructured, name => name === 'n')).toBe(false)
|
|
290
|
+
})
|
|
291
|
+
|
|
292
|
+
test('an identity memo over the prop is ambiguous too; the null spelling is kept for it', () => {
|
|
293
|
+
const ctx = ctxOf('props: { n?: number | null }', 'const m = createMemo(() => props.n)')
|
|
294
|
+
expect(ambiguous(ctx, '*', 'm()', '2')).toBe(true)
|
|
295
|
+
expect(arithmeticOperandCoercion('*', parseExpression('m()'), parseExpression('2'), ctx)).toBe('null')
|
|
296
|
+
expect(arithmeticOperandIsNull(parseExpression('m()'), ctx)).toBe(true)
|
|
297
|
+
})
|
|
298
|
+
|
|
299
|
+
test('a destructured union binding keeps its declared type for the classification', () => {
|
|
300
|
+
const ctx = ctxOf('{ a, u }: { a: number | null; u?: number | undefined }')
|
|
301
|
+
expect(arithmeticOperandCoercion('*', parseExpression('a'), parseExpression('2'), ctx)).toBe('null')
|
|
302
|
+
expect(arithmeticOperandCoercion('*', parseExpression('u'), parseExpression('2'), ctx)).toBe('undefined')
|
|
303
|
+
})
|
|
304
|
+
})
|
|
305
|
+
|
|
306
|
+
describe('ambiguousNilArithmeticOperands (#3700)', () => {
|
|
307
|
+
const ctx = collectNullishAttrContext({
|
|
308
|
+
metadata: analyzeComponent(
|
|
309
|
+
`export function C({ n, items }: { n?: number | null; items: number[] }) { return <p>x</p> }`,
|
|
310
|
+
'C.tsx',
|
|
311
|
+
),
|
|
312
|
+
} as unknown as ComponentIR)
|
|
313
|
+
const found = (source: string, scope?: BindingScope) =>
|
|
314
|
+
ambiguousNilArithmeticOperands(parseExpression(source), ctx, scope).map(exprToString)
|
|
315
|
+
|
|
316
|
+
test('finds the operands in nested positions', () => {
|
|
317
|
+
expect(found("items.filter(i => i > n * 2).length")).toEqual(['n'])
|
|
318
|
+
expect(found("(-n > 1 ? `a${n + 1}` : 'b')")).toEqual(['n', 'n'])
|
|
319
|
+
})
|
|
320
|
+
|
|
321
|
+
test('an arrow param or an enclosing binding of the same name shadows the prop', () => {
|
|
322
|
+
expect(found("items.filter(n => n * 2 > 1).length")).toEqual([])
|
|
323
|
+
expect(found("items.map(i => items.filter(n => n > i).length * n)")).toEqual(['n'])
|
|
324
|
+
expect(found('n * 2', BindingScope.EMPTY.enterCallback(['n']))).toEqual([])
|
|
325
|
+
// A sibling arrow's param does not leak out of its own body.
|
|
326
|
+
expect(found('items.filter(n => n > 1).length * n')).toEqual(['n'])
|
|
327
|
+
})
|
|
328
|
+
})
|