@barefootjs/jsx 0.40.1 → 0.41.0
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/boolean-result.d.ts +32 -10
- package/dist/adapters/boolean-result.d.ts.map +1 -1
- package/dist/adapters/brace-delimiter-text.d.ts +18 -0
- package/dist/adapters/brace-delimiter-text.d.ts.map +1 -0
- package/dist/adapters/callback-method.d.ts +45 -5
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/component-props.d.ts +3 -4
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +11 -1
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +24 -12
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/loop-bound-names.d.ts +9 -1
- package/dist/adapters/loop-bound-names.d.ts.map +1 -1
- package/dist/adapters/loop-row-type.d.ts +59 -0
- package/dist/adapters/loop-row-type.d.ts.map +1 -0
- package/dist/adapters/nil-reading.d.ts +63 -0
- package/dist/adapters/nil-reading.d.ts.map +1 -0
- package/dist/adapters/non-primitive-equality.d.ts +33 -0
- package/dist/adapters/non-primitive-equality.d.ts.map +1 -0
- package/dist/adapters/number-value.d.ts +5 -3
- package/dist/adapters/number-value.d.ts.map +1 -1
- package/dist/adapters/operand-typing.d.ts +126 -0
- package/dist/adapters/operand-typing.d.ts.map +1 -0
- package/dist/adapters/parsed-expr-emitter.d.ts +80 -12
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/adapters/primitive-plan.d.ts +320 -0
- package/dist/adapters/primitive-plan.d.ts.map +1 -0
- package/dist/adapters/prop-classes.d.ts +5 -0
- package/dist/adapters/prop-classes.d.ts.map +1 -1
- package/dist/analyzer-context.d.ts +4 -0
- package/dist/analyzer-context.d.ts.map +1 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +63 -8
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/compiler.d.ts.map +1 -1
- package/dist/event-handler-name.d.ts +12 -0
- package/dist/event-handler-name.d.ts.map +1 -1
- package/dist/expression-parser.d.ts +8 -0
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/index.d.ts +20 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4903 -2311
- package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
- package/dist/ir-to-client-js/child-props-object.d.ts +106 -0
- package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -0
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- 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-loop-child-arm.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 +4 -2
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/imports.d.ts +2 -2
- package/dist/ir-to-client-js/imports.d.ts.map +1 -1
- package/dist/ir-to-client-js/index.d.ts +2 -6
- package/dist/ir-to-client-js/index.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases/props-extraction.d.ts +3 -3
- package/dist/ir-to-client-js/plan/build-declaration-emit.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 +30 -18
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
- 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 +3 -1
- package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +3 -2
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts +1 -0
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +51 -34
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/prop-rewrite.d.ts +7 -0
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-binding.d.ts +58 -13
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/props-object-whole-read.d.ts +15 -0
- package/dist/props-object-whole-read.d.ts.map +1 -0
- package/dist/rich-type-refusal.d.ts.map +1 -1
- package/dist/scope/binding-scope.d.ts +85 -10
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/scope/lexical-scope-walk.d.ts +3 -2
- package/dist/scope/lexical-scope-walk.d.ts.map +1 -1
- package/dist/scope/root-prop-alias.d.ts +10 -7
- package/dist/scope/root-prop-alias.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts +39 -3
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/strip-types.d.ts +13 -0
- package/dist/strip-types.d.ts.map +1 -1
- package/dist/template-position-walk.d.ts +21 -3
- package/dist/template-position-walk.d.ts.map +1 -1
- package/dist/types.d.ts +90 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +9 -9
- package/src/__tests__/binding-scope.test.ts +89 -1
- package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -0
- package/src/__tests__/body-destructured-prop-default-arrow.test.ts +2 -2
- package/src/__tests__/body-destructured-props-live.test.ts +5 -5
- package/src/__tests__/boolean-result.test.ts +22 -5
- package/src/__tests__/brace-delimiter-text.test.ts +26 -0
- package/src/__tests__/callback-method.test.ts +62 -2
- package/src/__tests__/child-components-in-map.test.ts +1 -1
- package/src/__tests__/client-js-generation.test.ts +3 -3
- package/src/__tests__/computed-const.test.ts +103 -0
- package/src/__tests__/destructure-default-reads-earlier-param.test.ts +7 -7
- package/src/__tests__/destructured-props-live.test.ts +1 -1
- package/src/__tests__/emit-to-string-call.test.ts +2 -1
- package/src/__tests__/hydration-payload-analysis.test.ts +166 -0
- package/src/__tests__/issue-2724-prop-alias-loop-not-static.test.ts +7 -7
- package/src/__tests__/issue-2754-rest-spread-needs-slot.test.ts +5 -2
- package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +1 -1
- package/src/__tests__/issue-2835-props-type-member-name-collision.test.ts +4 -4
- package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +1 -1
- package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +2 -1
- package/src/__tests__/issue-3713-props-spread-loop-row.test.ts +65 -0
- package/src/__tests__/issue-3861-object-spread-child.test.ts +102 -0
- package/src/__tests__/lookup-literal-const.test.ts +9 -3
- package/src/__tests__/loop-index-reactivity.test.ts +2 -2
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +1 -1
- package/src/__tests__/loop-plan.test.ts +1 -1
- package/src/__tests__/loop-row-const-initializer-shadow.test.ts +146 -0
- package/src/__tests__/loop-row-type.test.ts +64 -0
- package/src/__tests__/lowering-value-position.test.ts +4 -0
- package/src/__tests__/memo-body-expr.test.ts +2 -2
- package/src/__tests__/module-const-destructure-default.test.ts +156 -0
- package/src/__tests__/nested-loop-index-param.test.ts +1 -1
- package/src/__tests__/nested-loop-reactive-attrs.test.ts +2 -2
- package/src/__tests__/nested-row-shadowing.test.ts +2 -2
- package/src/__tests__/nil-reading.test.ts +78 -0
- package/src/__tests__/nullable-signal-attr.test.ts +109 -48
- package/src/__tests__/primitive-plan.test.ts +494 -0
- package/src/__tests__/profile-loop-binding-ids.test.ts +2 -2
- package/src/__tests__/profile-nested-binding-ids.test.ts +2 -2
- package/src/__tests__/prop-default-undefined-only.test.ts +115 -0
- package/src/__tests__/prop-references.test.ts +2 -2
- package/src/__tests__/props-destructuring.test.ts +17 -0
- package/src/__tests__/props-object-whole-read.test.ts +40 -0
- package/src/__tests__/prune-unused-prop-extractions.test.ts +3 -3
- package/src/__tests__/reactive-attrs-in-map.test.ts +1 -1
- package/src/__tests__/reactive-factory-inlining.test.ts +4 -1
- package/src/__tests__/renamed-body-destructure-template.test.ts +7 -7
- package/src/__tests__/rest-binding-gaps.test.ts +192 -0
- package/src/__tests__/rewrite-destructured-props.test.ts +1 -1
- package/src/__tests__/root-prop-alias.test.ts +53 -8
- package/src/__tests__/ssr-defaults.test.ts +113 -25
- package/src/__tests__/static-loop-csr-materialize.test.ts +2 -2
- package/src/__tests__/static-record-literal-array.test.ts +171 -0
- package/src/__tests__/string-concat-identifier.test.ts +40 -1
- package/src/__tests__/written-binding-substitution.test.ts +8 -8
- package/src/adapters/boolean-result.ts +72 -66
- package/src/adapters/brace-delimiter-text.ts +25 -0
- package/src/adapters/callback-method.ts +159 -27
- package/src/adapters/component-props.ts +3 -4
- package/src/adapters/computed-const.ts +238 -4
- package/src/adapters/dsl-template-adapter.ts +62 -26
- package/src/adapters/loop-bound-names.ts +9 -1
- package/src/adapters/loop-plan.ts +1 -1
- package/src/adapters/loop-row-type.ts +149 -0
- package/src/adapters/nil-reading.ts +295 -0
- package/src/adapters/non-primitive-equality.ts +92 -0
- package/src/adapters/number-value.ts +17 -4
- package/src/adapters/operand-typing.ts +522 -0
- package/src/adapters/parsed-expr-emitter.ts +183 -19
- package/src/adapters/primitive-plan.ts +765 -0
- package/src/adapters/prop-classes.ts +16 -3
- package/src/analyzer-context.ts +6 -0
- package/src/analyzer.ts +532 -26
- package/src/augment-inherited-props.ts +108 -10
- package/src/compiler.ts +17 -0
- package/src/event-handler-name.ts +15 -0
- package/src/expression-parser.ts +70 -1
- package/src/index.ts +35 -16
- package/src/ir-to-client-js/build-references.ts +69 -0
- package/src/ir-to-client-js/child-props-object.ts +240 -0
- package/src/ir-to-client-js/collect-elements.ts +31 -63
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +8 -1
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +31 -28
- package/src/ir-to-client-js/control-flow/shared.ts +20 -22
- package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +5 -3
- package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +2 -2
- package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +1 -1
- package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +2 -2
- package/src/ir-to-client-js/control-flow/stringify/loop.ts +1 -1
- package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +3 -3
- package/src/ir-to-client-js/html-template.ts +43 -17
- package/src/ir-to-client-js/imports.ts +2 -2
- package/src/ir-to-client-js/index.ts +53 -11
- package/src/ir-to-client-js/phases/props-extraction.ts +11 -7
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +40 -7
- package/src/ir-to-client-js/plan/build-static-array-child-init.ts +31 -14
- package/src/ir-to-client-js/plan/static-array-child-init.ts +27 -18
- package/src/ir-to-client-js/prop-handling.ts +22 -3
- package/src/ir-to-client-js/prune-unused-prop-extractions.ts +58 -5
- package/src/ir-to-client-js/rewrite-destructured-props.ts +31 -1
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +26 -10
- package/src/jsx-to-ir.ts +459 -37
- package/src/nullable-signal-attr.ts +200 -110
- package/src/prop-rewrite.ts +30 -6
- package/src/props-binding.ts +84 -19
- package/src/props-object-whole-read.ts +87 -0
- package/src/rich-type-refusal.ts +2 -1
- package/src/scope/binding-scope.ts +182 -18
- package/src/scope/lexical-scope-walk.ts +4 -3
- package/src/scope/root-prop-alias.ts +15 -8
- package/src/ssr-defaults.ts +100 -11
- package/src/strip-types.ts +31 -3
- package/src/template-position-walk.ts +57 -28
- package/src/types.ts +87 -0
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts +0 -41
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +0 -1
- package/src/adapters/ambiguous-nil-arithmetic.ts +0 -94
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #3736: a component-scope const read in a loop row whose bindings shadow a
|
|
3
|
+
* name the const's initializer reads (`const b = a` read as `{b}` in
|
|
4
|
+
* `rows.map(a => …)`). `expandConstantForReactivity` /
|
|
5
|
+
* `expandDynamicPropValue` (`prop-handling.ts`) used to splice the
|
|
6
|
+
* initializer text (`a`) into the row, where it re-resolved to the row's own
|
|
7
|
+
* binding, so a client-built or updated row read the row value (`a()`).
|
|
8
|
+
* The initializer resolves in the component scope; when a row binding would
|
|
9
|
+
* capture one of its free names, the row reads the const itself, which init
|
|
10
|
+
* declares.
|
|
11
|
+
*
|
|
12
|
+
* The emitted client JS is read with the TypeScript AST: every per-row slot
|
|
13
|
+
* value is a `const __x = <expr>` declaration inside a row function.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { describe, test, expect } from 'bun:test'
|
|
17
|
+
import ts from 'typescript'
|
|
18
|
+
import { compileJSX } from '../compiler'
|
|
19
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
20
|
+
|
|
21
|
+
const adapter = new TestAdapter()
|
|
22
|
+
|
|
23
|
+
function clientJsFor(source: string): string {
|
|
24
|
+
const result = compileJSX(source, 'Repro.tsx', { adapter })
|
|
25
|
+
expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
|
|
26
|
+
const clientJs = result.files.find(f => f.type === 'clientJs')
|
|
27
|
+
expect(clientJs).toBeDefined()
|
|
28
|
+
return clientJs!.content
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The initializer text of every `const __x = …` slot-value declaration. */
|
|
32
|
+
function slotValues(js: string): string[] {
|
|
33
|
+
const sf = ts.createSourceFile('out.js', js, ts.ScriptTarget.Latest, true, ts.ScriptKind.JS)
|
|
34
|
+
const values: string[] = []
|
|
35
|
+
const visit = (node: ts.Node): void => {
|
|
36
|
+
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.name.text === '__x' && node.initializer) {
|
|
37
|
+
values.push(node.initializer.getText(sf))
|
|
38
|
+
}
|
|
39
|
+
ts.forEachChild(node, visit)
|
|
40
|
+
}
|
|
41
|
+
visit(sf)
|
|
42
|
+
return values
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe('a const read in a loop row that shadows a name its initializer reads (#3736)', () => {
|
|
46
|
+
test('destructured prop hop: the row reads the const, not the row value', () => {
|
|
47
|
+
const values = slotValues(clientJsFor(`
|
|
48
|
+
'use client'
|
|
49
|
+
export function C({ a, rows }: { a: string; rows: string[] }) {
|
|
50
|
+
const b = a
|
|
51
|
+
return <ul>{rows.map(a => <li key={a} title={b}>{b}/{a}</li>)}</ul>
|
|
52
|
+
}
|
|
53
|
+
`))
|
|
54
|
+
// `title={b}` and `{b}` read the const; only `{a}` reads the row.
|
|
55
|
+
expect(values).toContain('b')
|
|
56
|
+
expect(values).toContain('a()')
|
|
57
|
+
expect(values.every(v => v === 'b' || v === 'a()')).toBe(true)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('a two-hop alias whose middle name the row binds', () => {
|
|
61
|
+
const values = slotValues(clientJsFor(`
|
|
62
|
+
'use client'
|
|
63
|
+
export function C({ label, rows }: { label: string; rows: string[] }) {
|
|
64
|
+
const a = label
|
|
65
|
+
const b = a
|
|
66
|
+
return <ul>{rows.map(a => <li key={a}>{b}</li>)}</ul>
|
|
67
|
+
}
|
|
68
|
+
`))
|
|
69
|
+
expect(values.length).toBeGreaterThan(0)
|
|
70
|
+
expect(values.every(v => v === 'b')).toBe(true)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
test('a row that binds the prop itself', () => {
|
|
74
|
+
const values = slotValues(clientJsFor(`
|
|
75
|
+
'use client'
|
|
76
|
+
export function C({ label, rows }: { label: string; rows: string[] }) {
|
|
77
|
+
const c = label
|
|
78
|
+
return <ul>{rows.map(label => <li key={label} title={c}>{c}</li>)}</ul>
|
|
79
|
+
}
|
|
80
|
+
`))
|
|
81
|
+
expect(values.length).toBeGreaterThan(0)
|
|
82
|
+
expect(values.every(v => v === 'c')).toBe(true)
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test('an index param and a callback preamble local shadow the name too', () => {
|
|
86
|
+
const indexValues = slotValues(clientJsFor(`
|
|
87
|
+
'use client'
|
|
88
|
+
export function C({ a, rows }: { a: string; rows: string[] }) {
|
|
89
|
+
const b = a
|
|
90
|
+
return <ul>{rows.map((r, a) => <li key={r}>{b}</li>)}</ul>
|
|
91
|
+
}
|
|
92
|
+
`))
|
|
93
|
+
expect(indexValues.length).toBeGreaterThan(0)
|
|
94
|
+
expect(indexValues.every(v => v === 'b')).toBe(true)
|
|
95
|
+
|
|
96
|
+
const preambleValues = slotValues(clientJsFor(`
|
|
97
|
+
'use client'
|
|
98
|
+
export function C({ a, rows }: { a: string; rows: string[] }) {
|
|
99
|
+
const b = a
|
|
100
|
+
return <ul>{rows.map(r => { const a = r + '!'; return <li key={r} title={a}>{b}</li> })}</ul>
|
|
101
|
+
}
|
|
102
|
+
`))
|
|
103
|
+
expect(preambleValues).toContain('b')
|
|
104
|
+
expect(preambleValues).not.toContain('_p.a')
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
test('function C(props) with a renamed body destructure', () => {
|
|
108
|
+
const values = slotValues(clientJsFor(`
|
|
109
|
+
'use client'
|
|
110
|
+
export function C(props: { label: string; rows: string[] }) {
|
|
111
|
+
const { label: text } = props
|
|
112
|
+
const c = text
|
|
113
|
+
return <ul>{props.rows.map(text => <li key={text} title={c}>{c}/{text}</li>)}</ul>
|
|
114
|
+
}
|
|
115
|
+
`))
|
|
116
|
+
expect(values).toContain('c')
|
|
117
|
+
expect(values.filter(v => v !== 'c').every(v => v === 'text()')).toBe(true)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
test('a const reading a signal getter the row shadows', () => {
|
|
121
|
+
const values = slotValues(clientJsFor(`
|
|
122
|
+
'use client'
|
|
123
|
+
import { createSignal } from '@barefootjs/client'
|
|
124
|
+
export function C({ rows }: { rows: string[] }) {
|
|
125
|
+
const [count] = createSignal(7)
|
|
126
|
+
const b = count()
|
|
127
|
+
return <ul>{rows.map(count => <li key={count}>{b}/{count}</li>)}</ul>
|
|
128
|
+
}
|
|
129
|
+
`))
|
|
130
|
+
expect(values).toContain('b')
|
|
131
|
+
expect(values).toContain('count()')
|
|
132
|
+
expect(values.every(v => v === 'b' || v === 'count()')).toBe(true)
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
test('a row that shadows nothing the initializer reads still expands it', () => {
|
|
136
|
+
const values = slotValues(clientJsFor(`
|
|
137
|
+
'use client'
|
|
138
|
+
export function C({ a, rows }: { a: string; rows: string[] }) {
|
|
139
|
+
const b = a
|
|
140
|
+
return <ul>{rows.map(r => <li key={r}>{b}</li>)}</ul>
|
|
141
|
+
}
|
|
142
|
+
`))
|
|
143
|
+
expect(values.length).toBeGreaterThan(0)
|
|
144
|
+
expect(values.every(v => v === '_p.a')).toBe(true)
|
|
145
|
+
})
|
|
146
|
+
})
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { describe, test, expect } from 'bun:test'
|
|
2
|
+
import { concatType, loopRowType, type DeclaredValueTypes } from '../adapters/loop-row-type'
|
|
3
|
+
import { parseExpression } from '../expression-parser'
|
|
4
|
+
import { BindingScope } from '../scope/binding-scope'
|
|
5
|
+
import type { PropertyInfo, TypeInfo } from '../types'
|
|
6
|
+
|
|
7
|
+
// #3762 review: a `concat` row's field is a definite string only when it is
|
|
8
|
+
// one in the receiver's element type and in every argument's, whatever the
|
|
9
|
+
// types are named.
|
|
10
|
+
describe('concat row type', () => {
|
|
11
|
+
const str: TypeInfo = { kind: 'primitive', raw: 'string', primitive: 'string' }
|
|
12
|
+
const num: TypeInfo = { kind: 'primitive', raw: 'number', primitive: 'number' }
|
|
13
|
+
const field = (name: string, type: TypeInfo, optional = false): PropertyInfo => ({ name, type, optional, readonly: false })
|
|
14
|
+
const named: Record<string, PropertyInfo[]> = {
|
|
15
|
+
Row: [field('id', str)],
|
|
16
|
+
Other: [field('id', str)],
|
|
17
|
+
Numeric: [field('id', num)],
|
|
18
|
+
Optional: [field('id', str, true)],
|
|
19
|
+
Bare: [field('n', num)],
|
|
20
|
+
}
|
|
21
|
+
const arrayOf = (raw: string): TypeInfo => ({ kind: 'array', raw: `${raw}[]`, elementType: { kind: 'interface', raw } })
|
|
22
|
+
const types: DeclaredValueTypes = {
|
|
23
|
+
propsParams: [
|
|
24
|
+
...Object.keys(named).map(name => ({ name: name.toLowerCase(), type: arrayOf(name) })),
|
|
25
|
+
{ name: 'extras', type: { kind: 'array', raw: 'any[]', elementType: { kind: 'unknown', raw: 'any' } } },
|
|
26
|
+
{ name: 'one', type: { kind: 'interface', raw: 'Other' } },
|
|
27
|
+
],
|
|
28
|
+
signals: [],
|
|
29
|
+
memos: [],
|
|
30
|
+
propsObjectName: 'props',
|
|
31
|
+
namedFields: name => named[name],
|
|
32
|
+
}
|
|
33
|
+
const idIsString = (array: string): boolean => {
|
|
34
|
+
const rowType = loopRowType(types, { arrayParsed: parseExpression(array) }, BindingScope.EMPTY)
|
|
35
|
+
if (!rowType) return false
|
|
36
|
+
const scope = BindingScope.EMPTY.enterLoopRow({ param: 'r', rowType })
|
|
37
|
+
return scope.isStringValueName('r', () => false, false, ['id'], types.namedFields)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
test('a field that is a string in every member stays one, across differently named types', () => {
|
|
41
|
+
expect(idIsString('props.row.concat(props.row)')).toBe(true)
|
|
42
|
+
expect(idIsString('props.row.concat(props.other)')).toBe(true)
|
|
43
|
+
expect(idIsString('props.row.concat(props.one)')).toBe(true)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('an any, a number, an optional or a missing field in one member types nothing', () => {
|
|
47
|
+
expect(idIsString('props.row.concat(props.extras)')).toBe(false)
|
|
48
|
+
expect(idIsString('props.row.concat(props.numeric)')).toBe(false)
|
|
49
|
+
expect(idIsString('props.row.concat(props.optional)')).toBe(false)
|
|
50
|
+
expect(idIsString('props.row.concat(props.bare)')).toBe(false)
|
|
51
|
+
expect(idIsString('props.row.concat(props.other).concat(props.extras)')).toBe(false)
|
|
52
|
+
expect(idIsString('props.row.concat(props.missing)')).toBe(false)
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
describe('concat of the same row type keeps the plain array shape (#3762 review)', () => {
|
|
57
|
+
test('Row[] concat Row[] is Row[], not a union', () => {
|
|
58
|
+
const rows: TypeInfo = { kind: 'array', raw: 'Row[]', elementType: { kind: 'interface', raw: 'Row' } }
|
|
59
|
+
const copy: TypeInfo = { kind: 'array', raw: 'Row[]', elementType: { kind: 'interface', raw: 'Row' } }
|
|
60
|
+
const result = concatType(rows, [copy])
|
|
61
|
+
expect(result?.elementType).toBe(rows.elementType)
|
|
62
|
+
expect(result?.unionTypes).toBeUndefined()
|
|
63
|
+
})
|
|
64
|
+
})
|
|
@@ -27,6 +27,9 @@ import {
|
|
|
27
27
|
isSupported,
|
|
28
28
|
isSupportedValue,
|
|
29
29
|
emitParsedExpr,
|
|
30
|
+
primitivesAt,
|
|
31
|
+
EMPTY_OPERAND_TYPING,
|
|
32
|
+
BindingScope,
|
|
30
33
|
type LoweringPlugin,
|
|
31
34
|
type LoweringEmitter,
|
|
32
35
|
type ParsedExprEmitter,
|
|
@@ -174,6 +177,7 @@ describe('emitParsedExpr dispatch with an emitter.lowering seam (#2843)', () =>
|
|
|
174
177
|
throw new Error(`unexpected emitter.${name} call in this test`)
|
|
175
178
|
}
|
|
176
179
|
return {
|
|
180
|
+
primitives: primitivesAt(EMPTY_OPERAND_TYPING, BindingScope.EMPTY, {}),
|
|
177
181
|
identifier: (name: string) => `ID(${name})`,
|
|
178
182
|
literal: (v) => `LIT(${JSON.stringify(v)})`,
|
|
179
183
|
call: (callee, args, emit) => `CALL(${emit(callee)}, ${args.map(emit).join(', ')})`,
|
|
@@ -130,7 +130,7 @@ describe('template text over a body destructure binding in a (props)-arg compone
|
|
|
130
130
|
}
|
|
131
131
|
`, 'C.tsx')
|
|
132
132
|
}
|
|
133
|
-
const read = "(_p.b
|
|
133
|
+
const read = "(_p.b === undefined ? (['y', ''].filter((x) => x !== '')) : _p.b)"
|
|
134
134
|
|
|
135
135
|
test('a memo body reads a renamed binding with a callback default as its prop', () => {
|
|
136
136
|
const ctx = analyze(`
|
|
@@ -252,6 +252,6 @@ describe('template text keeps a local named like a typed prop (props)-arg compon
|
|
|
252
252
|
const { value = 4 } = props
|
|
253
253
|
const n = createMemo(() => value * 2)
|
|
254
254
|
`)
|
|
255
|
-
expect(ctx.memos.find(m => m.name === 'n')?.templateBodyExpr).toBe('(_p.value
|
|
255
|
+
expect(ctx.memos.find(m => m.name === 'n')?.templateBodyExpr).toBe('(_p.value === undefined ? 4 : _p.value) * 2')
|
|
256
256
|
})
|
|
257
257
|
})
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A destructured prop's client read is `_p.m ?? <default>`, so a module
|
|
3
|
+
* declaration the default names (`{ m = DEFAULT }`) is a client read even
|
|
4
|
+
* when nothing else in the component names it. The reference graph used to
|
|
5
|
+
* miss it, and the emitted client module read an undeclared name — a
|
|
6
|
+
* `ReferenceError` whenever the prop was absent (a client-side mount).
|
|
7
|
+
*
|
|
8
|
+
* Declarations are read from the emitted client JS with the TypeScript AST.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { describe, test, expect } from 'bun:test'
|
|
12
|
+
import ts from 'typescript'
|
|
13
|
+
import { compileJSX } from '../compiler'
|
|
14
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
15
|
+
|
|
16
|
+
function clientJs(source: string): string {
|
|
17
|
+
const result = compileJSX(source, 'C.tsx', { adapter: new TestAdapter() })
|
|
18
|
+
expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
|
|
19
|
+
return result.files.find(f => f.type === 'clientJs')!.content
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Every name the client module declares (variables and functions, any depth), in order. */
|
|
23
|
+
function declarations(js: string): string[] {
|
|
24
|
+
const sf = ts.createSourceFile('c.js', js, ts.ScriptTarget.Latest, true, ts.ScriptKind.JS)
|
|
25
|
+
const names: string[] = []
|
|
26
|
+
const visit = (node: ts.Node): void => {
|
|
27
|
+
if ((ts.isVariableDeclaration(node) || ts.isFunctionDeclaration(node)) && node.name && ts.isIdentifier(node.name)) {
|
|
28
|
+
names.push(node.name.text)
|
|
29
|
+
}
|
|
30
|
+
ts.forEachChild(node, visit)
|
|
31
|
+
}
|
|
32
|
+
visit(sf)
|
|
33
|
+
return names
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const declaredNames = (js: string): Set<string> => new Set(declarations(js))
|
|
37
|
+
const declarationCount = (js: string, name: string): number => declarations(js).filter(n => n === name).length
|
|
38
|
+
|
|
39
|
+
describe('module declarations read only by a destructure default are emitted', () => {
|
|
40
|
+
test('a module const default read as text', () => {
|
|
41
|
+
const js = clientJs(`'use client'
|
|
42
|
+
const DEFAULT = 'm'
|
|
43
|
+
export function C({ m = DEFAULT }: { m?: string }) {
|
|
44
|
+
return <p>{m}</p>
|
|
45
|
+
}`)
|
|
46
|
+
expect(declaredNames(js).has('DEFAULT')).toBe(true)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test('a chain: the later prop reads the earlier one, whose default reads the const', () => {
|
|
50
|
+
const js = clientJs(`'use client'
|
|
51
|
+
const DEFAULT = 'm'
|
|
52
|
+
export function C({ a = DEFAULT, b = a }: { a?: string; b?: string }) {
|
|
53
|
+
return <p>{b}</p>
|
|
54
|
+
}`)
|
|
55
|
+
expect(declaredNames(js).has('DEFAULT')).toBe(true)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test('a member read of a module object const', () => {
|
|
59
|
+
const js = clientJs(`'use client'
|
|
60
|
+
const D = { label: 'm' }
|
|
61
|
+
export function C({ m = D.label }: { m?: string }) {
|
|
62
|
+
return <p title={m}>{m}</p>
|
|
63
|
+
}`)
|
|
64
|
+
expect(declaredNames(js).has('D')).toBe(true)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('a module helper function called by the default', () => {
|
|
68
|
+
const js = clientJs(`'use client'
|
|
69
|
+
function fmt(s: string) { return s + '!' }
|
|
70
|
+
export function C({ m = fmt('x') }: { m?: string }) {
|
|
71
|
+
return <p>{m}</p>
|
|
72
|
+
}`)
|
|
73
|
+
expect(declaredNames(js).has('fmt')).toBe(true)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test('a signal seeded from the defaulted prop', () => {
|
|
77
|
+
const js = clientJs(`'use client'
|
|
78
|
+
import { createSignal } from '@barefootjs/client'
|
|
79
|
+
const START = 2
|
|
80
|
+
export function C({ start = START }: { start?: number }) {
|
|
81
|
+
const [c, setC] = createSignal(start)
|
|
82
|
+
return <button onClick={() => setC(c() + 1)}>{c()}</button>
|
|
83
|
+
}`)
|
|
84
|
+
expect(declaredNames(js).has('START')).toBe(true)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('a body local of the same name does not get a second declaration', () => {
|
|
88
|
+
const js = clientJs(`'use client'
|
|
89
|
+
const LABEL = 'a'
|
|
90
|
+
export function C({ m = LABEL }: { m?: string }) {
|
|
91
|
+
const LABEL = 'b'
|
|
92
|
+
return <p title={LABEL}>{m}</p>
|
|
93
|
+
}`)
|
|
94
|
+
// Two `const LABEL` in one init body would be a SyntaxError for the module.
|
|
95
|
+
expect(declarationCount(js, 'LABEL')).toBeLessThanOrEqual(1)
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
test('the default of a prop the client never reads adds nothing', () => {
|
|
99
|
+
const js = clientJs(`'use client'
|
|
100
|
+
import { createSignal } from '@barefootjs/client'
|
|
101
|
+
const UNUSED = 'u'
|
|
102
|
+
export function C({ m = UNUSED }: { m?: string }) {
|
|
103
|
+
const [c, setC] = createSignal(0)
|
|
104
|
+
return <button onClick={() => setC(c() + 1)}>{c()}</button>
|
|
105
|
+
}`)
|
|
106
|
+
expect(declaredNames(js).has('UNUSED')).toBe(false)
|
|
107
|
+
})
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
describe('a destructure default names only its free references (#3792 review)', () => {
|
|
111
|
+
// A property key, a member name and a callback parameter inside the
|
|
112
|
+
// default are not reads of an outer binding; tracing them kept an
|
|
113
|
+
// unrelated module declaration in `initC` (here one reading `process`,
|
|
114
|
+
// which the browser lacks — a `ReferenceError` at mount).
|
|
115
|
+
test('an object literal key and a member name of the default', () => {
|
|
116
|
+
const js = clientJs(`'use client'
|
|
117
|
+
const label = process.env.SERVER_ONLY
|
|
118
|
+
export function C({ m = ({ label: 'm' }).label }: { m?: string }) {
|
|
119
|
+
return <p>{m}</p>
|
|
120
|
+
}`)
|
|
121
|
+
expect(declaredNames(js).has('label')).toBe(false)
|
|
122
|
+
expect(js).not.toContain('process')
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
test('a callback parameter of the default', () => {
|
|
126
|
+
const js = clientJs(`'use client'
|
|
127
|
+
const label = process.env.SERVER_ONLY
|
|
128
|
+
export function C({ m = ['m'].map(label => label)[0] }: { m?: string }) {
|
|
129
|
+
return <p>{m}</p>
|
|
130
|
+
}`)
|
|
131
|
+
expect(declaredNames(js).has('label')).toBe(false)
|
|
132
|
+
expect(js).not.toContain('process')
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
test('a genuine outer read beside a colliding key is still traced', () => {
|
|
136
|
+
const js = clientJs(`'use client'
|
|
137
|
+
const label = process.env.SERVER_ONLY
|
|
138
|
+
const D = { text: 'm' }
|
|
139
|
+
export function C({ m = ({ label: D.text }).label }: { m?: string }) {
|
|
140
|
+
return <p>{m}</p>
|
|
141
|
+
}`)
|
|
142
|
+
expect(declaredNames(js).has('D')).toBe(true)
|
|
143
|
+
expect(declaredNames(js).has('label')).toBe(false)
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
test('a chain through an earlier prop whose default has a colliding key', () => {
|
|
147
|
+
const js = clientJs(`'use client'
|
|
148
|
+
const label = process.env.SERVER_ONLY
|
|
149
|
+
const DEFAULT = 'm'
|
|
150
|
+
export function C({ a = ({ label: DEFAULT }).label, b = a }: { a?: string; b?: string }) {
|
|
151
|
+
return <p>{b}</p>
|
|
152
|
+
}`)
|
|
153
|
+
expect(declaredNames(js).has('DEFAULT')).toBe(true)
|
|
154
|
+
expect(declaredNames(js).has('label')).toBe(false)
|
|
155
|
+
})
|
|
156
|
+
})
|
|
@@ -115,7 +115,7 @@ describe('nested .map() index param referenced in key/text/attr (#2218)', () =>
|
|
|
115
115
|
// `loop-index` source fixes this: the index is wired through the same
|
|
116
116
|
// accessor `mapArray` already hands every row (#2859/#2860), and the
|
|
117
117
|
// slot now gets a real update effect.
|
|
118
|
-
expect(content).toContain(`createEffect(() => { __bfw_s0('s0',
|
|
118
|
+
expect(content).toContain(`createEffect(() => { __bfw_s0('s0', textOrNode(i())) })`)
|
|
119
119
|
})
|
|
120
120
|
|
|
121
121
|
test('index referenced in a reactive attribute expression is bound', () => {
|
|
@@ -347,7 +347,7 @@ describe('reactive attributes inside a nested .map() body (#135)', () => {
|
|
|
347
347
|
// The innermost loop's per-item renderItem must emit BOTH a
|
|
348
348
|
// reactive-text effect for `panel.text` and a reactive-attr effect
|
|
349
349
|
// for `panel.cls` — the bug dropped only the former.
|
|
350
|
-
expect(content).toMatch(/createEffect\(\(\) => \{[\s\S]*?__bfw_\w+\('s\d+',
|
|
350
|
+
expect(content).toMatch(/createEffect\(\(\) => \{[\s\S]*?__bfw_\w+\('s\d+', textOrNode\(panel\(\)\.text\)\)/)
|
|
351
351
|
expect(content).toContain("setAttribute('class'")
|
|
352
352
|
})
|
|
353
353
|
|
|
@@ -405,7 +405,7 @@ describe('reactive attributes inside a nested .map() body (#135)', () => {
|
|
|
405
405
|
// effect missing (the exact regression here). `pi` reads the row's
|
|
406
406
|
// live index accessor (#2859), so `labelAt(pi())` also stays correct
|
|
407
407
|
// if this innermost loop's rows ever survive a same-key reorder.
|
|
408
|
-
expect(content).toMatch(/createEffect\(\(\) => \{[\s\S]*?__bfw_\w+\('s\d+',
|
|
408
|
+
expect(content).toMatch(/createEffect\(\(\) => \{[\s\S]*?__bfw_\w+\('s\d+', textOrNode\(labelAt\(pi\(\)\)\)\)/)
|
|
409
409
|
expect(content).toContain("setAttribute('class'")
|
|
410
410
|
})
|
|
411
411
|
})
|
|
@@ -30,7 +30,7 @@ export function N(props: { groups: { name: string; tags: string[] }[] }) {
|
|
|
30
30
|
expect(js).not.toContain('const __bfItem().name')
|
|
31
31
|
expect(js).toContain("const name = t() + '!'")
|
|
32
32
|
expect(js).toContain('String(name)')
|
|
33
|
-
expect(js).toContain('
|
|
33
|
+
expect(js).toContain('textOrNode(__bfItem().name)')
|
|
34
34
|
})
|
|
35
35
|
|
|
36
36
|
test('a row two loops deep still reads the outermost item through its accessor', () => {
|
|
@@ -51,7 +51,7 @@ export function Feed() {
|
|
|
51
51
|
</article>)}</main>
|
|
52
52
|
}
|
|
53
53
|
`)
|
|
54
|
-
expect(js).toContain('
|
|
54
|
+
expect(js).toContain('textOrNode(post().text + reply().text)')
|
|
55
55
|
expect(js).not.toContain('String(post.text + reply().text)')
|
|
56
56
|
})
|
|
57
57
|
})
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The nil-reading rewrite (#3824 step 4a, `spec/js-primitive-semantics.md`
|
|
3
|
+
* "Nil reading is a tree rewrite"): an operand whose nil Phase 1 can read is
|
|
4
|
+
* made non-nil before the plan, by its consumer's coercion.
|
|
5
|
+
*/
|
|
6
|
+
import { describe, test, expect } from 'bun:test'
|
|
7
|
+
import {
|
|
8
|
+
analyzeComponent,
|
|
9
|
+
buildMetadata,
|
|
10
|
+
collectOperandTyping,
|
|
11
|
+
rewriteNilReadings,
|
|
12
|
+
rewriteNilReadingsDeep,
|
|
13
|
+
parseExpression,
|
|
14
|
+
serializeParsedExpr,
|
|
15
|
+
stringifyParsedExpr,
|
|
16
|
+
BindingScope,
|
|
17
|
+
type ParsedExpr,
|
|
18
|
+
} from '../index'
|
|
19
|
+
|
|
20
|
+
const SRC = `export function C(props: { n?: number; nn: number | null; e?: number | null; s?: string; sn: string | null; c?: string | null; m: number; t: string; x: any }) { return <p>{props.m}</p> }`
|
|
21
|
+
|
|
22
|
+
function typing() {
|
|
23
|
+
const metadata = buildMetadata(analyzeComponent(SRC, 'c.tsx'))
|
|
24
|
+
return collectOperandTyping(metadata).at(BindingScope.forComponent(metadata.propsObjectName))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** `e` with every operator node parenthesised, so the tests read the tree's grouping. */
|
|
28
|
+
function show(e: ParsedExpr): string {
|
|
29
|
+
switch (e.kind) {
|
|
30
|
+
case 'binary':
|
|
31
|
+
case 'logical':
|
|
32
|
+
return `(${show(e.left)} ${e.op} ${show(e.right)})`
|
|
33
|
+
case 'unary':
|
|
34
|
+
return `${e.op}${show(e.argument)}`
|
|
35
|
+
case 'literal':
|
|
36
|
+
return e.literalType === 'string' ? `'${e.value}'` : String(e.value)
|
|
37
|
+
case 'template-literal':
|
|
38
|
+
return '`' + e.parts.map(p => (p.type === 'string' ? p.value : `\${${show(p.expr)}}`)).join('') + '`'
|
|
39
|
+
default:
|
|
40
|
+
return stringifyParsedExpr(e)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const rewrite = (src: string): string => show(rewriteNilReadings(parseExpression(src), typing()))
|
|
45
|
+
|
|
46
|
+
describe('rewriteNilReadings', () => {
|
|
47
|
+
test('a numeric consumer reads an undefined nil as NaN and a null one as 0', () => {
|
|
48
|
+
expect(rewrite('props.n * 2')).toBe('((props.n ?? NaN) * 2)')
|
|
49
|
+
expect(rewrite('props.nn - props.m')).toBe('((props.nn ?? 0) - props.m)')
|
|
50
|
+
expect(rewrite('-props.n')).toBe('-(props.n ?? NaN)')
|
|
51
|
+
expect(rewrite('props.s < props.t')).toBe('((props.s ?? NaN) < props.t)')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('a string consumer reads it as "undefined" / "null"', () => {
|
|
55
|
+
expect(rewrite("props.s + '!'")).toBe("((props.s ?? 'undefined') + '!')")
|
|
56
|
+
expect(rewrite("props.t + props.sn")).toBe("(props.t + (props.sn ?? 'null'))")
|
|
57
|
+
expect(rewrite('`${props.n}`')).toBe("`${(props.n ?? 'undefined')}`")
|
|
58
|
+
expect(rewrite("null + 'x'")).toBe("('null' + 'x')")
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('a string operand under + number is split on nil by the numeric row', () => {
|
|
62
|
+
expect(rewrite('props.s + 1')).toBe('((props.s ?? NaN) + 1)')
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('an either nil and a dynamic + are left to the helper', () => {
|
|
66
|
+
expect(rewrite('(props.e * 2)')).toBe('(props.e * 2)')
|
|
67
|
+
expect(rewrite("'btn ' + props.c")).toBe("('btn ' + props.c)")
|
|
68
|
+
expect(rewrite('`${props.c}`')).toBe('`${props.c}`')
|
|
69
|
+
expect(rewrite('(props.n + props.x)')).toBe('(props.n + props.x)')
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test('the deep form enters callbacks and serializes NaN through Number', () => {
|
|
73
|
+
const deep = rewriteNilReadingsDeep(parseExpression('props.items.filter(i => i > props.n)'), typing())
|
|
74
|
+
const arrow = deep.kind === 'call' ? deep.args[0] : undefined
|
|
75
|
+
expect(arrow?.kind === 'arrow' ? show(arrow.body) : null).toBe('(i > (props.n ?? NaN))')
|
|
76
|
+
expect(serializeParsedExpr(rewriteNilReadings(parseExpression('props.n * 2'), typing()))).toContain('"name":"Number"')
|
|
77
|
+
})
|
|
78
|
+
})
|