@barefootjs/jsx 0.39.0 → 0.39.2
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/interface.d.ts +19 -0
- package/dist/adapters/interface.d.ts.map +1 -1
- package/dist/adapters/jsx-adapter.d.ts.map +1 -1
- package/dist/adapters/parsed-expr-emitter.d.ts +2 -2
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/analyzer.d.ts +19 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/compiler.d.ts.map +1 -1
- package/dist/debug.d.ts.map +1 -1
- package/dist/html-types.d.ts +5 -0
- package/dist/html-types.d.ts.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1455 -1139
- package/dist/ir-to-client-js/collect-elements.d.ts +17 -1
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/reactivity.d.ts +10 -2
- package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
- package/dist/ir-to-client-js/safe-html.d.ts +1 -1
- package/dist/ir-to-client-js/safe-html.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +11 -7
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/literal-member-index.d.ts +6 -0
- package/dist/literal-member-index.d.ts.map +1 -0
- package/dist/scope/binding-scope.d.ts +11 -0
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/signal-prop-name-collision.d.ts +20 -0
- package/dist/signal-prop-name-collision.d.ts.map +1 -0
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +3 -2
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/types.d.ts +44 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +4 -4
- package/src/__tests__/adapter-output.test.ts +142 -0
- package/src/__tests__/binding-scope.test.ts +27 -0
- package/src/__tests__/client-template-escape-soundness.test.ts +17 -0
- package/src/__tests__/conditional-nested-loop-bindings.test.ts +41 -0
- package/src/__tests__/create-mutation.test.ts +30 -0
- package/src/__tests__/create-query.test.ts +52 -0
- package/src/__tests__/debug.test.ts +83 -0
- package/src/__tests__/destructured-from-props-localconst-value.test.ts +3 -2
- package/src/__tests__/ir-fragment-transparency.test.ts +23 -0
- package/src/__tests__/issue-3240-nested-static-map-event-delegation.test.ts +245 -0
- package/src/__tests__/jsx-text-linebreak-whitespace.test.ts +80 -0
- package/src/__tests__/literal-member-index.test.ts +14 -0
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +445 -0
- package/src/__tests__/signal-prop-name-collision.test.ts +171 -0
- package/src/__tests__/ssr-defaults.test.ts +6 -7
- package/src/__tests__/ssr-seed-plan.test.ts +23 -0
- package/src/__tests__/staged-ir/02-shadow-guards.test.ts +1 -1
- package/src/__tests__/staged-ir/08-relocate-unit.test.ts +6 -3
- package/src/adapters/interface.ts +19 -0
- package/src/adapters/jsx-adapter.ts +30 -20
- package/src/adapters/parsed-expr-emitter.ts +3 -3
- package/src/analyzer.ts +45 -11
- package/src/compiler.ts +78 -46
- package/src/debug.ts +34 -2
- package/src/html-types.ts +14 -0
- package/src/index.ts +2 -0
- package/src/ir-to-client-js/collect-elements.ts +72 -13
- package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +112 -3
- package/src/ir-to-client-js/reactivity.ts +31 -1
- package/src/ir-to-client-js/safe-html.ts +19 -6
- package/src/ir-to-client-js/utils.ts +130 -15
- package/src/jsx-to-ir.ts +89 -14
- package/src/literal-member-index.ts +12 -0
- package/src/scope/binding-scope.ts +31 -1
- package/src/signal-prop-name-collision.ts +164 -0
- package/src/ssr-defaults.ts +13 -5
- package/src/ssr-seed-plan.ts +26 -3
- package/src/types.ts +47 -0
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #3239: a reactive `.map()` row's plain (non-destructured) callback param
|
|
3
|
+
* gets accessor-wrapped (`choice` → `choice()`) via a naive
|
|
4
|
+
* `\bchoice\b`-style regex in `wrapLoopParamAsAccessor` (and its
|
|
5
|
+
* `wrapIndexParamAsAccessor` twin). That regex also matches tokens that
|
|
6
|
+
* merely SPELL the param name without referencing it:
|
|
7
|
+
*
|
|
8
|
+
* - an object-literal key (`{ choice: 1 }`)
|
|
9
|
+
* - a shorthand property (`{ choice }`)
|
|
10
|
+
* - a member/property name (`obj.choice`)
|
|
11
|
+
*
|
|
12
|
+
* Depending on position the result is either invalid JS (key/shorthand —
|
|
13
|
+
* esbuild's `Expected "{" but found "}"`, a hard build failure) or
|
|
14
|
+
* valid-but-wrong JS (member access — `obj.choice()` throws `TypeError` at
|
|
15
|
+
* row-creation time). This is the plain-parameter sibling of #1244 (which
|
|
16
|
+
* fixed the equivalent shorthand corruption for *destructured* loop params
|
|
17
|
+
* via `expandShorthandBindings`) and #2856.
|
|
18
|
+
*
|
|
19
|
+
* These tests compile through `compileJSX` — the exact function
|
|
20
|
+
* `packages/vite/src/plugin.ts` calls to produce a component's client JS —
|
|
21
|
+
* with a REACTIVE array (`createSignal`), which is required to reach the
|
|
22
|
+
* `mapArrayLazy`/`createRow`/`applyItem` code path this bug lives in (a
|
|
23
|
+
* plain non-reactive array takes the static `forEach` hydration path and
|
|
24
|
+
* never goes through `wrapLoopParamAsAccessor` at all).
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { describe, test, expect } from 'bun:test'
|
|
28
|
+
import ts from 'typescript'
|
|
29
|
+
import { compileJSX } from '../compiler'
|
|
30
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
31
|
+
|
|
32
|
+
const adapter = new TestAdapter()
|
|
33
|
+
|
|
34
|
+
function getClientJs(source: string, filename = 'App.tsx'): string {
|
|
35
|
+
const result = compileJSX(source, filename, { adapter })
|
|
36
|
+
const hardErrors = result.errors.filter(e => e.severity === 'error')
|
|
37
|
+
expect(hardErrors).toHaveLength(0)
|
|
38
|
+
const clientJs = result.files.find(f => f.type === 'clientJs')
|
|
39
|
+
expect(clientJs).toBeDefined()
|
|
40
|
+
return clientJs!.content
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Every emitted row-construction function (`createRow`, `applyItem`,
|
|
45
|
+
* `applyOuter`, …) must be syntactically valid JS. `ts.transpileModule`'s
|
|
46
|
+
* `reportDiagnostics` runs the host's real JS/TS parser over the whole
|
|
47
|
+
* compiled module as-is — the same class of check esbuild's transform
|
|
48
|
+
* performs during a real `vite build` — so a corrupted key/shorthand
|
|
49
|
+
* position (invalid JS) surfaces as a syntactic diagnostic here exactly as
|
|
50
|
+
* it did in the reported esbuild failure, without needing esbuild itself as
|
|
51
|
+
* a test dependency, and without the module-level `import`/`export`
|
|
52
|
+
* statements needing to be stripped out first (CLAUDE.md forbids
|
|
53
|
+
* regex/string-based handling of compiled client JS — see `combine-client-js.ts`'s
|
|
54
|
+
* AST-walk precedent).
|
|
55
|
+
*/
|
|
56
|
+
function syntaxErrorsOf(clientJs: string): readonly ts.Diagnostic[] {
|
|
57
|
+
const result = ts.transpileModule(clientJs, {
|
|
58
|
+
reportDiagnostics: true,
|
|
59
|
+
compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.Latest },
|
|
60
|
+
})
|
|
61
|
+
return (result.diagnostics ?? []).filter(d => d.category === ts.DiagnosticCategory.Error)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function assertParses(clientJs: string): void {
|
|
65
|
+
expect(syntaxErrorsOf(clientJs)).toHaveLength(0)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The counterpart to `assertParses`: a nested declaration that shadows the
|
|
70
|
+
* loop param (see the `rewriteIdentifierAsAccessor` docstring in
|
|
71
|
+
* `ir-to-client-js/utils.ts`) is expected to fail loudly at this
|
|
72
|
+
* parse-check rather than silently emit a value that's wrong at runtime.
|
|
73
|
+
*/
|
|
74
|
+
function assertDoesNotParse(clientJs: string): void {
|
|
75
|
+
expect(syntaxErrorsOf(clientJs).length).toBeGreaterThan(0)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
describe('.map() row: object-literal key / shorthand / member positions vs. the loop param (#3239)', () => {
|
|
79
|
+
test('shorthand property `{ choice }` expands to `{ choice: choice() }`, not `{ choice() }`', () => {
|
|
80
|
+
const source = `
|
|
81
|
+
'use client'
|
|
82
|
+
import { createSignal } from '@barefootjs/client'
|
|
83
|
+
function f(o: { key: string; choice: string }): string {
|
|
84
|
+
return o.key + ':' + o.choice
|
|
85
|
+
}
|
|
86
|
+
export function App() {
|
|
87
|
+
const [items] = createSignal(['a', 'b'])
|
|
88
|
+
return (
|
|
89
|
+
<div>
|
|
90
|
+
{items().map(choice => (
|
|
91
|
+
<b key={choice}>{f({ key: 'x', choice })}</b>
|
|
92
|
+
))}
|
|
93
|
+
</div>
|
|
94
|
+
)
|
|
95
|
+
}
|
|
96
|
+
`
|
|
97
|
+
const js = getClientJs(source)
|
|
98
|
+
assertParses(js)
|
|
99
|
+
expect(js).toContain("f({ key: 'x', choice: choice() })")
|
|
100
|
+
// The corrupted shape would have been the bare identifier used as a
|
|
101
|
+
// key with no value at all: `f({ key: 'x', choice() })`.
|
|
102
|
+
expect(js).not.toContain("f({ key: 'x', choice() })")
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
test('explicit non-shorthand key `{ choice: 1 }` is left untouched (the key is not a reference)', () => {
|
|
106
|
+
const source = `
|
|
107
|
+
'use client'
|
|
108
|
+
import { createSignal } from '@barefootjs/client'
|
|
109
|
+
export function App() {
|
|
110
|
+
const [items] = createSignal(['a', 'b'])
|
|
111
|
+
return (
|
|
112
|
+
<div>
|
|
113
|
+
{items().map(choice => (
|
|
114
|
+
<b key={choice}>{JSON.stringify({ choice: 1 })}</b>
|
|
115
|
+
))}
|
|
116
|
+
</div>
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
`
|
|
120
|
+
const js = getClientJs(source)
|
|
121
|
+
assertParses(js)
|
|
122
|
+
expect(js).toContain('JSON.stringify({ choice: 1 })')
|
|
123
|
+
expect(js).not.toContain('choice(): 1')
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
test('explicit key with matching value `{ choice: choice }` rewrites only the value', () => {
|
|
127
|
+
const source = `
|
|
128
|
+
'use client'
|
|
129
|
+
import { createSignal } from '@barefootjs/client'
|
|
130
|
+
function f(o: { key: string; choice: string }): string {
|
|
131
|
+
return o.key + ':' + o.choice
|
|
132
|
+
}
|
|
133
|
+
export function App() {
|
|
134
|
+
const [items] = createSignal(['a', 'b'])
|
|
135
|
+
return (
|
|
136
|
+
<div>
|
|
137
|
+
{items().map(choice => (
|
|
138
|
+
<b key={choice}>{f({ key: 'x', choice: choice })}</b>
|
|
139
|
+
))}
|
|
140
|
+
</div>
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
`
|
|
144
|
+
const js = getClientJs(source)
|
|
145
|
+
assertParses(js)
|
|
146
|
+
expect(js).toContain("f({ key: 'x', choice: choice() })")
|
|
147
|
+
expect(js).not.toContain('choice():')
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
test('member access `CONST.choice` leaves the member name alone (not `CONST.choice()`)', () => {
|
|
151
|
+
const source = `
|
|
152
|
+
'use client'
|
|
153
|
+
import { createSignal } from '@barefootjs/client'
|
|
154
|
+
const CONST = { choice: 'z' }
|
|
155
|
+
export function App() {
|
|
156
|
+
const [items] = createSignal(['a', 'b'])
|
|
157
|
+
return (
|
|
158
|
+
<div>
|
|
159
|
+
{items().map(choice => (
|
|
160
|
+
<b key={choice}>{CONST.choice}</b>
|
|
161
|
+
))}
|
|
162
|
+
</div>
|
|
163
|
+
)
|
|
164
|
+
}
|
|
165
|
+
`
|
|
166
|
+
const js = getClientJs(source)
|
|
167
|
+
assertParses(js)
|
|
168
|
+
expect(js).toContain('CONST.choice')
|
|
169
|
+
expect(js).not.toContain('CONST.choice()')
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
test('renamed key `{ a: choice }` (no name collision) keeps working — regression guard', () => {
|
|
173
|
+
const source = `
|
|
174
|
+
'use client'
|
|
175
|
+
import { createSignal } from '@barefootjs/client'
|
|
176
|
+
function f(o: { a: string }): string { return o.a }
|
|
177
|
+
export function App() {
|
|
178
|
+
const [items] = createSignal(['a', 'b'])
|
|
179
|
+
return (
|
|
180
|
+
<div>
|
|
181
|
+
{items().map(choice => (
|
|
182
|
+
<b key={choice}>{f({ a: choice })}</b>
|
|
183
|
+
))}
|
|
184
|
+
</div>
|
|
185
|
+
)
|
|
186
|
+
}
|
|
187
|
+
`
|
|
188
|
+
const js = getClientJs(source)
|
|
189
|
+
assertParses(js)
|
|
190
|
+
expect(js).toContain('f({ a: choice() })')
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
test('plain reference and member access ON the item itself still wrap correctly — regression guard', () => {
|
|
194
|
+
const source = `
|
|
195
|
+
'use client'
|
|
196
|
+
import { createSignal } from '@barefootjs/client'
|
|
197
|
+
export function App() {
|
|
198
|
+
const [items] = createSignal(['aa', 'bb'])
|
|
199
|
+
return (
|
|
200
|
+
<div>
|
|
201
|
+
{items().map(choice => (
|
|
202
|
+
<b key={choice}>{choice.length}</b>
|
|
203
|
+
))}
|
|
204
|
+
</div>
|
|
205
|
+
)
|
|
206
|
+
}
|
|
207
|
+
`
|
|
208
|
+
const js = getClientJs(source)
|
|
209
|
+
assertParses(js)
|
|
210
|
+
expect(js).toContain('choice().length')
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
test('index param (#2859): object key `{ i: 1 }` and member access `obj.i` are left alone on the EAGER (non-lazy) path', () => {
|
|
214
|
+
// A child component inside the row forces the row out of the lazy-row
|
|
215
|
+
// graph (`hasChildComponent`, `lazy-row-eligibility.ts`) and onto the
|
|
216
|
+
// `mapArray` eager path — the only path that runs
|
|
217
|
+
// `wrapIndexParamAsAccessor` at all (`build-plain-row.ts`: the lazy
|
|
218
|
+
// path hands `applyItem`/`createRow` a plain `__e.index` instead, see
|
|
219
|
+
// `loop-index-reactivity.test.ts`).
|
|
220
|
+
const source = `
|
|
221
|
+
'use client'
|
|
222
|
+
import { createSignal } from '@barefootjs/client'
|
|
223
|
+
const obj = { i: 5 }
|
|
224
|
+
function Child({ x }: { x: number }) { return <span>{x}</span> }
|
|
225
|
+
export function App() {
|
|
226
|
+
const [items] = createSignal(['a', 'b'])
|
|
227
|
+
return (
|
|
228
|
+
<ul>
|
|
229
|
+
{items().map((choice, i) => (
|
|
230
|
+
<li key={choice}>
|
|
231
|
+
<Child x={i} />
|
|
232
|
+
{JSON.stringify({ i: 1 })}{obj.i}{i}
|
|
233
|
+
</li>
|
|
234
|
+
))}
|
|
235
|
+
</ul>
|
|
236
|
+
)
|
|
237
|
+
}
|
|
238
|
+
`
|
|
239
|
+
const js = getClientJs(source)
|
|
240
|
+
assertParses(js)
|
|
241
|
+
expect(js).toContain('JSON.stringify({ i: 1 })')
|
|
242
|
+
expect(js).toContain('obj.i')
|
|
243
|
+
expect(js).not.toContain('obj.i()')
|
|
244
|
+
expect(js).not.toContain('i(): 1')
|
|
245
|
+
// The genuine index references (passed to the child, and read directly
|
|
246
|
+
// in the row's own text) are still wrapped as accessor calls.
|
|
247
|
+
expect(js).toContain('return i()')
|
|
248
|
+
expect(js).toContain('String(i())')
|
|
249
|
+
})
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
describe('.map() row: declaration/member positions the old regex skipped via its "followed by (" lookahead', () => {
|
|
253
|
+
test('object-literal method `{ choice() {...} }` is left alone (not `{ choice()() {...} }`)', () => {
|
|
254
|
+
const source = `
|
|
255
|
+
'use client'
|
|
256
|
+
import { createSignal } from '@barefootjs/client'
|
|
257
|
+
export function App() {
|
|
258
|
+
const [items] = createSignal(['a', 'b'])
|
|
259
|
+
return (
|
|
260
|
+
<div>
|
|
261
|
+
{items().map(choice => (
|
|
262
|
+
<b key={choice}>{JSON.stringify({ choice() { return 1 } })}</b>
|
|
263
|
+
))}
|
|
264
|
+
</div>
|
|
265
|
+
)
|
|
266
|
+
}
|
|
267
|
+
`
|
|
268
|
+
const js = getClientJs(source)
|
|
269
|
+
assertParses(js)
|
|
270
|
+
expect(js).toContain('choice() { return 1 }')
|
|
271
|
+
expect(js).not.toContain('choice()()')
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
test('accessor method `{ get choice() {...} }` is left alone (not `{ get choice()() {...} }`)', () => {
|
|
275
|
+
const source = `
|
|
276
|
+
'use client'
|
|
277
|
+
import { createSignal } from '@barefootjs/client'
|
|
278
|
+
export function App() {
|
|
279
|
+
const [items] = createSignal(['a', 'b'])
|
|
280
|
+
return (
|
|
281
|
+
<div>
|
|
282
|
+
{items().map(choice => (
|
|
283
|
+
<b key={choice}>{JSON.stringify({ get choice() { return 1 } })}</b>
|
|
284
|
+
))}
|
|
285
|
+
</div>
|
|
286
|
+
)
|
|
287
|
+
}
|
|
288
|
+
`
|
|
289
|
+
const js = getClientJs(source)
|
|
290
|
+
assertParses(js)
|
|
291
|
+
expect(js).toContain('get choice() { return 1 }')
|
|
292
|
+
expect(js).not.toContain('choice()()')
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
test('nested `function choice() {...}` declaration is left alone (not `function choice()() {...}`)', () => {
|
|
296
|
+
const source = `
|
|
297
|
+
'use client'
|
|
298
|
+
import { createSignal } from '@barefootjs/client'
|
|
299
|
+
export function App() {
|
|
300
|
+
const [items] = createSignal(['a', 'b'])
|
|
301
|
+
return (
|
|
302
|
+
<div>
|
|
303
|
+
{items().map(choice => (
|
|
304
|
+
<b key={choice}>{(function choice() { return 1 })()}</b>
|
|
305
|
+
))}
|
|
306
|
+
</div>
|
|
307
|
+
)
|
|
308
|
+
}
|
|
309
|
+
`
|
|
310
|
+
const js = getClientJs(source)
|
|
311
|
+
assertParses(js)
|
|
312
|
+
expect(js).toContain('function choice() { return 1 }')
|
|
313
|
+
expect(js).not.toContain('choice()()')
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
test('`new choice()` is left alone (not `new choice()()`)', () => {
|
|
317
|
+
const source = `
|
|
318
|
+
'use client'
|
|
319
|
+
import { createSignal } from '@barefootjs/client'
|
|
320
|
+
export function App() {
|
|
321
|
+
const [items] = createSignal(['a', 'b'])
|
|
322
|
+
return (
|
|
323
|
+
<div>
|
|
324
|
+
{items().map(choice => (
|
|
325
|
+
<b key={choice}>{String(new choice())}</b>
|
|
326
|
+
))}
|
|
327
|
+
</div>
|
|
328
|
+
)
|
|
329
|
+
}
|
|
330
|
+
`
|
|
331
|
+
const js = getClientJs(source)
|
|
332
|
+
assertParses(js)
|
|
333
|
+
expect(js).toContain('new choice()')
|
|
334
|
+
expect(js).not.toContain('new choice()()')
|
|
335
|
+
})
|
|
336
|
+
|
|
337
|
+
test('a self-reference inside a named `function choice() {...}` expression refers to itself, not the outer item', () => {
|
|
338
|
+
// Unlike a `function`/`class` DECLARATION (whose name reaches into the
|
|
339
|
+
// surrounding scope), a named function EXPRESSION's name is scoped to
|
|
340
|
+
// exactly its own body — so `choice.length` inside this one can only
|
|
341
|
+
// mean the inner function, never the outer loop item, and must be left
|
|
342
|
+
// completely untouched rather than wrapped into `choice().length`.
|
|
343
|
+
const source = `
|
|
344
|
+
'use client'
|
|
345
|
+
import { createSignal } from '@barefootjs/client'
|
|
346
|
+
export function App() {
|
|
347
|
+
const [items] = createSignal(['aa', 'bb'])
|
|
348
|
+
return (
|
|
349
|
+
<div>
|
|
350
|
+
{items().map(choice => (
|
|
351
|
+
<b key={choice}>{(function choice() { return choice.length })()}</b>
|
|
352
|
+
))}
|
|
353
|
+
</div>
|
|
354
|
+
)
|
|
355
|
+
}
|
|
356
|
+
`
|
|
357
|
+
const js = getClientJs(source)
|
|
358
|
+
assertParses(js)
|
|
359
|
+
expect(js).toContain('function choice() { return choice.length }')
|
|
360
|
+
expect(js).not.toContain('choice().length')
|
|
361
|
+
})
|
|
362
|
+
})
|
|
363
|
+
|
|
364
|
+
describe('.map() row: a nested scope that shadows the loop param fails loudly instead of silently misrewriting', () => {
|
|
365
|
+
// `rewriteIdentifierAsAccessor` has no scope tracking (see its docstring
|
|
366
|
+
// in ir-to-client-js/utils.ts): once a nested scope re-binds the loop
|
|
367
|
+
// param's name, the rewrite can no longer tell a reference to the outer
|
|
368
|
+
// item from a reference to the shadowing local, so it deliberately wraps
|
|
369
|
+
// the shadowing declaration's own name too. That's always a syntax break
|
|
370
|
+
// at the declaration site, so the module fails a syntax check instead of
|
|
371
|
+
// silently compiling into a runtime `TypeError` against the wrong value
|
|
372
|
+
// — matching what the pre-#3239 regex already did for this shape (it had
|
|
373
|
+
// no shadow-awareness either).
|
|
374
|
+
|
|
375
|
+
test('a nested `.map(choice => ...)` that reuses the outer loop param name fails to parse', () => {
|
|
376
|
+
const source = `
|
|
377
|
+
'use client'
|
|
378
|
+
import { createSignal } from '@barefootjs/client'
|
|
379
|
+
export function App() {
|
|
380
|
+
const [items] = createSignal(['ab', 'cd'])
|
|
381
|
+
return (
|
|
382
|
+
<div>
|
|
383
|
+
{items().map(choice => (
|
|
384
|
+
<b key={choice}>{choice.split('').map(choice => choice.toUpperCase()).join('')}</b>
|
|
385
|
+
))}
|
|
386
|
+
</div>
|
|
387
|
+
)
|
|
388
|
+
}
|
|
389
|
+
`
|
|
390
|
+
const js = getClientJs(source)
|
|
391
|
+
assertDoesNotParse(js)
|
|
392
|
+
})
|
|
393
|
+
|
|
394
|
+
test('a nested `const choice = ...` that reuses the outer loop param name fails to parse', () => {
|
|
395
|
+
const source = `
|
|
396
|
+
'use client'
|
|
397
|
+
import { createSignal } from '@barefootjs/client'
|
|
398
|
+
export function App() {
|
|
399
|
+
const [items] = createSignal(['a', 'b'])
|
|
400
|
+
return (
|
|
401
|
+
<div>
|
|
402
|
+
{items().map(choice => (
|
|
403
|
+
<b key={choice}>{(() => { const choice = 1; return choice })()}</b>
|
|
404
|
+
))}
|
|
405
|
+
</div>
|
|
406
|
+
)
|
|
407
|
+
}
|
|
408
|
+
`
|
|
409
|
+
const js = getClientJs(source)
|
|
410
|
+
assertDoesNotParse(js)
|
|
411
|
+
})
|
|
412
|
+
})
|
|
413
|
+
|
|
414
|
+
describe('destructured loop-param path (rewriteLoopBindingRefs) has the same key/member defect as the plain-param path (#3239 follow-up)', () => {
|
|
415
|
+
// `rewriteLoopBindingRefs` (a few lines below `rewriteIdentifierAsAccessor`
|
|
416
|
+
// in ir-to-client-js/utils.ts) rewrites a DESTRUCTURED loop param's
|
|
417
|
+
// bindings (`({ color, label }) => ...`) with the same kind of
|
|
418
|
+
// \bname\b-style regex the plain-param path used before this PR, and has
|
|
419
|
+
// the identical object-literal-key defect: a binding whose name matches
|
|
420
|
+
// an unrelated object-literal key gets that key corrupted into an
|
|
421
|
+
// accessor call. Left out of this PR (see the PR description) because a
|
|
422
|
+
// correct fix means touching the heavily-exercised #951/#1244
|
|
423
|
+
// destructured-binding machinery, which is a larger, separate change.
|
|
424
|
+
// Drop `.todo` once `rewriteLoopBindingRefs` gets the same AST-aware
|
|
425
|
+
// treatment `rewriteIdentifierAsAccessor` got here.
|
|
426
|
+
test.todo('an object-literal key matching a destructured binding name is left alone', () => {
|
|
427
|
+
const source = `
|
|
428
|
+
'use client'
|
|
429
|
+
import { createSignal } from '@barefootjs/client'
|
|
430
|
+
export function App() {
|
|
431
|
+
const [items] = createSignal([{ color: 'red', label: 'a' }])
|
|
432
|
+
return (
|
|
433
|
+
<div>
|
|
434
|
+
{items().map(({ color, label }) => (
|
|
435
|
+
<b key={label}>{JSON.stringify({ color: 1 })}</b>
|
|
436
|
+
))}
|
|
437
|
+
</div>
|
|
438
|
+
)
|
|
439
|
+
}
|
|
440
|
+
`
|
|
441
|
+
const js = getClientJs(source)
|
|
442
|
+
assertParses(js)
|
|
443
|
+
expect(js).toContain('JSON.stringify({ color: 1 })')
|
|
444
|
+
})
|
|
445
|
+
})
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { expect, test } from 'bun:test'
|
|
2
|
+
import ts from 'typescript'
|
|
3
|
+
import { analyzeComponent } from '../analyzer.ts'
|
|
4
|
+
import { compileJSX } from '../compiler.ts'
|
|
5
|
+
import { programWithSeparatedSource } from '../signal-prop-name-collision.ts'
|
|
6
|
+
import { TestAdapter } from '../adapters/test-adapter.ts'
|
|
7
|
+
import { fixture } from '../../../adapter-tests/fixtures/signal-literal-prop-name-collision.ts'
|
|
8
|
+
|
|
9
|
+
test('literal-seeded signal and bare prop have independent binding names', () => {
|
|
10
|
+
const ctx = analyzeComponent(fixture.source, 'test.tsx')
|
|
11
|
+
expect(ctx.signals[0].getter).not.toBe('rows')
|
|
12
|
+
expect(ctx.propsParams[0].name).toBe('rows')
|
|
13
|
+
expect(ctx.sourceFile.text).toContain('props.rows.map')
|
|
14
|
+
expect(ctx.sourceFile.text).toContain(`${ctx.signals[0].getter}().map`)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
test('rename follows binding identity, preserves keys, and avoids source names', () => {
|
|
18
|
+
const source = `
|
|
19
|
+
'use client'
|
|
20
|
+
import { createSignal } from '@barefootjs/client'
|
|
21
|
+
export function App(props: { count: number }) {
|
|
22
|
+
const bfSignal_count = 8
|
|
23
|
+
const BfSignal_count_1 = 9
|
|
24
|
+
const [count, setCount] = createSignal(7)
|
|
25
|
+
const alias = count
|
|
26
|
+
const object = { count }
|
|
27
|
+
const shadow = (count: number) => ({ count, label: 'count', value: count })
|
|
28
|
+
return <button onClick={() => setCount(count() + 1)}>{props.count}{count()}{alias()}{object.count()}{shadow(2).count}</button>
|
|
29
|
+
}
|
|
30
|
+
`
|
|
31
|
+
const ctx = analyzeComponent(source, 'test.tsx')
|
|
32
|
+
const name = ctx.signals[0].getter
|
|
33
|
+
expect(name).not.toBe('count')
|
|
34
|
+
expect(name).not.toBe('bfSignal_count')
|
|
35
|
+
expect(name.toLowerCase()).not.toBe('bfsignal_count_1')
|
|
36
|
+
expect(ctx.sourceFile.text).toContain(`const alias = ${name}`)
|
|
37
|
+
expect(ctx.sourceFile.text).toContain(`{ count: ${name} }`)
|
|
38
|
+
expect(ctx.sourceFile.text).toContain("(count: number) => ({ count, label: 'count', value: count })")
|
|
39
|
+
expect(ctx.sourceFile.text).toContain('object.count()')
|
|
40
|
+
expect(ctx.sourceFile.text).toContain(`setCount(${name}() + 1)`)
|
|
41
|
+
const result = compileJSX(source, 'test.tsx', { adapter: new TestAdapter() })
|
|
42
|
+
expect(result.errors.filter(e => e.severity === 'error')).toEqual([])
|
|
43
|
+
expect(result.files.find(f => f.type === 'clientJs')?.content).toContain(name)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('same-prop derived seeds and ordinary bindings stay unchanged', () => {
|
|
47
|
+
const ctx = analyzeComponent(`
|
|
48
|
+
'use client'
|
|
49
|
+
import { createSignal } from '@barefootjs/client'
|
|
50
|
+
export function App(props: { count: number }) {
|
|
51
|
+
const [count] = createSignal(props.count ?? 7)
|
|
52
|
+
const [other] = createSignal(2)
|
|
53
|
+
return <p>{count()}{other()}</p>
|
|
54
|
+
}
|
|
55
|
+
`, 'test.tsx')
|
|
56
|
+
expect(ctx.signals.map(s => s.getter)).toEqual(['count', 'other'])
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('signed literal seeds are separated without a type-based program', () => {
|
|
60
|
+
const ctx = analyzeComponent(`
|
|
61
|
+
'use client'
|
|
62
|
+
import { createSignal } from '@barefootjs/client'
|
|
63
|
+
export function App(props: { count: number }) {
|
|
64
|
+
const [count] = createSignal(-7)
|
|
65
|
+
return <p>{props.count}{count()}</p>
|
|
66
|
+
}
|
|
67
|
+
`, 'test.tsx')
|
|
68
|
+
expect(ctx.signals[0].getter).not.toBe('count')
|
|
69
|
+
expect(ctx.signals[0].initialValue).toBe('-7')
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test('re-analysis preserves a supplied Program and its virtual imports', () => {
|
|
73
|
+
const source = `
|
|
74
|
+
'use client'
|
|
75
|
+
import { createSignal } from '@barefootjs/client'
|
|
76
|
+
import { n } from './virtual'
|
|
77
|
+
export function App(props: { count: number }) {
|
|
78
|
+
const [count] = createSignal(7)
|
|
79
|
+
const extra = n
|
|
80
|
+
return <p>{props.count}{count()}{extra}</p>
|
|
81
|
+
}
|
|
82
|
+
`
|
|
83
|
+
const files = new Map([
|
|
84
|
+
['/virtual/app.tsx', source],
|
|
85
|
+
['/virtual/virtual.ts', 'export const n = 42'],
|
|
86
|
+
])
|
|
87
|
+
const options: ts.CompilerOptions = {
|
|
88
|
+
noLib: true, module: ts.ModuleKind.ESNext,
|
|
89
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler, jsx: ts.JsxEmit.Preserve,
|
|
90
|
+
}
|
|
91
|
+
const program = ts.createProgram([...files.keys()], options, {
|
|
92
|
+
...ts.createCompilerHost(options),
|
|
93
|
+
getCurrentDirectory: () => '/virtual',
|
|
94
|
+
directoryExists: name => name === '/virtual',
|
|
95
|
+
fileExists: name => files.has(name),
|
|
96
|
+
readFile: name => files.get(name),
|
|
97
|
+
getSourceFile: (name, version) => files.has(name)
|
|
98
|
+
? ts.createSourceFile(name, files.get(name)!, version, true, ts.ScriptKind.TSX) : undefined,
|
|
99
|
+
})
|
|
100
|
+
const ctx = analyzeComponent(source, '/virtual/app.tsx', undefined, program)
|
|
101
|
+
expect(ctx.signals[0].getter).not.toBe('count')
|
|
102
|
+
expect(ctx.checker).not.toBeNull()
|
|
103
|
+
let extra: ts.VariableDeclaration | undefined
|
|
104
|
+
function visit(node: ts.Node): void {
|
|
105
|
+
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.name.text === 'extra') extra = node
|
|
106
|
+
ts.forEachChild(node, visit)
|
|
107
|
+
}
|
|
108
|
+
visit(ctx.sourceFile)
|
|
109
|
+
expect(ctx.checker!.typeToString(ctx.checker!.getTypeAtLocation(extra!.initializer!))).toBe('42')
|
|
110
|
+
expect(program.getSourceFile('/virtual/app.tsx')!.text).toBe(source)
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('multi-component production compilation separates each component independently', () => {
|
|
114
|
+
const source = `
|
|
115
|
+
'use client'
|
|
116
|
+
import { createSignal } from '@barefootjs/client'
|
|
117
|
+
export function First(props: { count: number }) {
|
|
118
|
+
const [count] = createSignal(7)
|
|
119
|
+
return <p>{props.count}{count()}</p>
|
|
120
|
+
}
|
|
121
|
+
export function Second(props: { count: number }) {
|
|
122
|
+
const [count] = createSignal(9)
|
|
123
|
+
return <p>{props.count}{count()}</p>
|
|
124
|
+
}
|
|
125
|
+
`
|
|
126
|
+
const result = compileJSX(source, 'test.tsx', { adapter: new TestAdapter() })
|
|
127
|
+
expect(result.errors.filter(e => e.severity === 'error')).toEqual([])
|
|
128
|
+
const clientJs = result.files.find(f => f.type === 'clientJs')!.content
|
|
129
|
+
expect(clientJs).toContain('initFirst')
|
|
130
|
+
expect(clientJs).toContain('initSecond')
|
|
131
|
+
expect(clientJs.match(/\[bfSignal_count\]/g)).toHaveLength(2)
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
test.each([false, true])('custom module and type resolutions survive reanalysis (automatic: %s)', automatic => {
|
|
135
|
+
const source = `${automatic ? '' : '/// <reference types="custom-globals" />'}
|
|
136
|
+
'use client'
|
|
137
|
+
import { n } from 'custom-values'
|
|
138
|
+
export function App(props: { count: number }) {
|
|
139
|
+
const [count] = createSignal(7)
|
|
140
|
+
const extra = n
|
|
141
|
+
return <p>{props.count}{count()}{extra}</p>
|
|
142
|
+
}
|
|
143
|
+
`
|
|
144
|
+
const files = new Map([
|
|
145
|
+
['/virtual/app.tsx', source],
|
|
146
|
+
['/virtual/value.ts', 'export const n = 42'],
|
|
147
|
+
['/virtual/globals.d.ts', 'declare function createSignal(v: number): [() => number]'],
|
|
148
|
+
])
|
|
149
|
+
const options: ts.CompilerOptions = { noLib: true, module: ts.ModuleKind.ESNext, jsx: ts.JsxEmit.Preserve, types: automatic ? ['custom-globals'] : [] }
|
|
150
|
+
const program = ts.createProgram(['/virtual/app.tsx'], options, {
|
|
151
|
+
...ts.createCompilerHost(options),
|
|
152
|
+
getCurrentDirectory: () => '/virtual',
|
|
153
|
+
directoryExists: name => name === '/virtual',
|
|
154
|
+
fileExists: name => files.has(name),
|
|
155
|
+
readFile: name => files.get(name),
|
|
156
|
+
getSourceFile: (name, version) => files.has(name)
|
|
157
|
+
? ts.createSourceFile(name, files.get(name)!, version, true, ts.ScriptKind.TSX) : undefined,
|
|
158
|
+
resolveModuleNames: names => names.map(name => name === 'custom-values'
|
|
159
|
+
? { resolvedFileName: '/virtual/value.ts', extension: ts.Extension.Ts } : undefined),
|
|
160
|
+
resolveTypeReferenceDirectives: names => names.map(() => ({ resolvedFileName: '/virtual/globals.d.ts', primary: true })),
|
|
161
|
+
})
|
|
162
|
+
const ctx = analyzeComponent(source, '/virtual/app.tsx', undefined, program)
|
|
163
|
+
expect(ctx.signals[0].getter).not.toBe('count')
|
|
164
|
+
const rebuilt = programWithSeparatedSource(program, '/virtual/app.tsx', ctx.sourceFile.text)
|
|
165
|
+
const originalDiagnostics = program.getSemanticDiagnostics().map(d => d.code)
|
|
166
|
+
expect(rebuilt.getSemanticDiagnostics().map(d => d.code)).toEqual(originalDiagnostics)
|
|
167
|
+
const module = rebuilt.getSourceFile('/virtual/value.ts')!
|
|
168
|
+
const declaration = (module.statements[0] as ts.VariableStatement).declarationList.declarations[0]
|
|
169
|
+
expect(rebuilt.getTypeChecker().typeToString(rebuilt.getTypeChecker().getTypeAtLocation(declaration.name))).toBe('42')
|
|
170
|
+
expect(rebuilt.getSourceFile('/virtual/globals.d.ts')).toBeDefined()
|
|
171
|
+
})
|
|
@@ -173,15 +173,13 @@ describe('extractSsrDefaults', () => {
|
|
|
173
173
|
expect(defaults?.n).toEqual({ value: 0 })
|
|
174
174
|
})
|
|
175
175
|
|
|
176
|
-
test('
|
|
176
|
+
test('literal-seeded collision has distinct raw-prop and internal-signal defaults', () => {
|
|
177
177
|
// Same-named `label` getter and `label` prop, but the signal's OWN
|
|
178
178
|
// initializer does NOT derive from `props.label` (it's a plain
|
|
179
179
|
// string literal) — the JSX body separately reads `label()` AND
|
|
180
|
-
// `props.label`.
|
|
181
|
-
//
|
|
182
|
-
//
|
|
183
|
-
// separately, and this fix must leave it byte-identical: the entry
|
|
184
|
-
// stays the plain evaluated-signal-value shape with no `propName`.
|
|
180
|
+
// `props.label`. Analysis separates the local binding before IR is
|
|
181
|
+
// built, so the prop keeps its caller-facing seed while the signal
|
|
182
|
+
// remains an internal entry without `propName`.
|
|
185
183
|
const metadata = metadataFor(`
|
|
186
184
|
'use client'
|
|
187
185
|
import { createSignal } from '@barefootjs/client'
|
|
@@ -192,7 +190,8 @@ describe('extractSsrDefaults', () => {
|
|
|
192
190
|
`)
|
|
193
191
|
|
|
194
192
|
const defaults = extractSsrDefaults(metadata)
|
|
195
|
-
expect(defaults?.label).toEqual({
|
|
193
|
+
expect(defaults?.label).toEqual({ propName: 'label', value: null })
|
|
194
|
+
expect(defaults?.[metadata.signals[0].getter]).toEqual({ value: 'sig' })
|
|
196
195
|
})
|
|
197
196
|
|
|
198
197
|
test('self-derived signal collision THROUGH a component-scope const (#2685 review): entry stays a PROP entry', () => {
|
|
@@ -23,6 +23,29 @@ function step(plan: SsrSeedPlan, name: string): SsrSeedStep {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
describe('computeSsrSeedPlan', () => {
|
|
26
|
+
test('getter aliases seed after their prop-derived memo, including alias hops', () => {
|
|
27
|
+
const plan = planFor(`
|
|
28
|
+
'use client'
|
|
29
|
+
import { createSignal, createMemo } from '@barefootjs/client'
|
|
30
|
+
function App(props: { count: number }) {
|
|
31
|
+
const [n] = createSignal(1.5)
|
|
32
|
+
const total = createMemo(() => n() + props.count)
|
|
33
|
+
const totalAlias = total
|
|
34
|
+
const secondAlias = totalAlias
|
|
35
|
+
return <p>{totalAlias()}{secondAlias()}</p>
|
|
36
|
+
}
|
|
37
|
+
`)
|
|
38
|
+
expect(plan.steps.map(s => s.name)).toEqual(['n', 'total', 'totalAlias', 'secondAlias'])
|
|
39
|
+
for (const name of ['totalAlias', 'secondAlias']) {
|
|
40
|
+
const alias = step(plan, name)
|
|
41
|
+
expect(alias.kind).toBe('derived')
|
|
42
|
+
if (alias.kind === 'derived') {
|
|
43
|
+
expect(alias.parsed).toEqual({ kind: 'call', callee: { kind: 'identifier', name: 'total' }, args: [] })
|
|
44
|
+
expect(alias.frees).toContain('total')
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
|
|
26
49
|
test('env signal (aliased) → env-reader step; derived memo over it', () => {
|
|
27
50
|
const plan = planFor(`
|
|
28
51
|
'use client'
|
|
@@ -33,7 +33,7 @@ describe('Shadow guards: bare names that shadow props are NOT rewritten to _p.X'
|
|
|
33
33
|
|
|
34
34
|
expect(errors).toEqual([])
|
|
35
35
|
// The local must use the signal getter, not _p.count.
|
|
36
|
-
expect(initBody).toMatch(/doubled\s*=\s*
|
|
36
|
+
expect(initBody).toMatch(/doubled\s*=\s*bfSignal_count\(\)\s*\*\s*2/)
|
|
37
37
|
expect(initBody).not.toMatch(/_p\.count\(\)/)
|
|
38
38
|
})
|
|
39
39
|
|