@barefootjs/jsx 0.39.0 → 0.39.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/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/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 +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +195 -68
- 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/utils.d.ts +11 -7
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts.map +1 -1
- 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__/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__/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__/loop-param-accessor-key-member-safety.test.ts +445 -0
- package/src/adapters/interface.ts +19 -0
- package/src/adapters/jsx-adapter.ts +30 -20
- package/src/analyzer.ts +35 -11
- package/src/compiler.ts +78 -46
- package/src/debug.ts +34 -2
- package/src/html-types.ts +14 -0
- package/src/index.ts +1 -0
- package/src/ir-to-client-js/collect-elements.ts +64 -4
- 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/utils.ts +130 -15
- package/src/jsx-to-ir.ts +75 -12
- package/src/ssr-defaults.ts +10 -0
- package/src/ssr-seed-plan.ts +12 -1
- 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
|
+
})
|
|
@@ -242,6 +242,25 @@ export interface TemplateAdapter {
|
|
|
242
242
|
* Required for adapters that look up templates by filename (e.g. Mojolicious).
|
|
243
243
|
*/
|
|
244
244
|
templatesPerComponent?: boolean
|
|
245
|
+
/**
|
|
246
|
+
* `false` for an adapter whose `markedTemplate` output has no consumer at
|
|
247
|
+
* all — the whole file is discarded after compilation (CSR: the browser
|
|
248
|
+
* runs the compiled client JS, not a rendered template). Defaults to
|
|
249
|
+
* `true` (every other adapter: a template file is read by something,
|
|
250
|
+
* whether that's a backend framework's renderer or, for the reference
|
|
251
|
+
* `HonoAdapter`, the JS module itself).
|
|
252
|
+
*
|
|
253
|
+
* When `false`, `compileJSX` skips assembling `markedTemplate` content
|
|
254
|
+
* ENTIRELY for that adapter, including the file's module-level value
|
|
255
|
+
* exports (`export const` / `export function`) that would otherwise be
|
|
256
|
+
* appended unconditionally regardless of the adapter's own `generate()`
|
|
257
|
+
* output. Without this, a component file that also exports a plain value
|
|
258
|
+
* produces a `markedTemplate` whose only content is that export — which
|
|
259
|
+
* looks like real template output to a consumer like `@barefootjs/vite`'s
|
|
260
|
+
* `assertNoRealTemplateOutput`, even though nothing will ever read it
|
|
261
|
+
* (#3234).
|
|
262
|
+
*/
|
|
263
|
+
emitsTemplates?: boolean
|
|
245
264
|
/**
|
|
246
265
|
* Module specifier of the SSR shim for `@barefootjs/client` (and
|
|
247
266
|
* `/runtime`). When set, the compiler rewrites client-package imports in
|
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
IRTemplatePart,
|
|
13
13
|
ImportSpecifier,
|
|
14
14
|
} from '../types.ts'
|
|
15
|
+
import { isElidedFactoryGetter } from '../types.ts'
|
|
15
16
|
import { templatePartsToJsExpr } from '../template-parts.ts'
|
|
16
17
|
import { BF_SCOPE, BF_SLOT, BF_COND } from '@barefootjs/shared'
|
|
17
18
|
import { BaseAdapter } from './interface.ts'
|
|
@@ -150,26 +151,35 @@ export abstract class JsxAdapter extends BaseAdapter {
|
|
|
150
151
|
}
|
|
151
152
|
continue
|
|
152
153
|
}
|
|
153
|
-
//
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
//
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
154
|
+
// An elided async-factory value (#3245) has no getter binding in the
|
|
155
|
+
// source at all — `signal.getter` is a synthesized internal name that
|
|
156
|
+
// nothing in the template or event handlers ever references. Emitting
|
|
157
|
+
// its SSR stub would leak that compiler-internal name into the SSR
|
|
158
|
+
// module for a binding nothing reads; the action's own SSR stub
|
|
159
|
+
// (below) is unaffected and still emitted when referenced. See
|
|
160
|
+
// `isElidedFactoryGetter`'s docstring for the full rationale.
|
|
161
|
+
if (!isElidedFactoryGetter(signal)) {
|
|
162
|
+
// Create a getter that returns the initial value for SSR
|
|
163
|
+
const rawInitialValue = preserveTypes
|
|
164
|
+
? (signal.typedInitialValue ?? signal.initialValue)
|
|
165
|
+
: signal.initialValue
|
|
166
|
+
const initialValue = rawInitialValue.trim().startsWith('{') ? `(${rawInitialValue})` : rawInitialValue
|
|
167
|
+
|
|
168
|
+
// When preserveTypes and typedInitialValue is absent but signal.type has a meaningful
|
|
169
|
+
// type from a generic parameter, add a type assertion to prevent TS inference issues.
|
|
170
|
+
// A bare `object` raw is the analyzer's coarse KIND, not a real type — asserting
|
|
171
|
+
// `as object` only destroys the initializer's inferred literal type (a
|
|
172
|
+
// `{ x: 0, y: 0 }` signal getter stops matching `() => { x: number; y: number }`).
|
|
173
|
+
const needsTypeAssertion = preserveTypes
|
|
174
|
+
&& !signal.typedInitialValue
|
|
175
|
+
&& signal.type.kind !== 'unknown'
|
|
176
|
+
&& signal.type.kind !== 'primitive'
|
|
177
|
+
&& signal.type.raw !== 'object'
|
|
178
|
+
if (needsTypeAssertion) {
|
|
179
|
+
lines.push(` const ${signal.getter} = () => ${initialValue} as ${signal.type.raw}`)
|
|
180
|
+
} else {
|
|
181
|
+
lines.push(` const ${signal.getter} = () => ${initialValue}`)
|
|
182
|
+
}
|
|
173
183
|
}
|
|
174
184
|
|
|
175
185
|
// An async factory's action (#3165): the request function never runs on
|
package/src/analyzer.ts
CHANGED
|
@@ -47,6 +47,40 @@ const REACTIVE_BRAND_PACKAGES = [
|
|
|
47
47
|
'@barefootjs/form',
|
|
48
48
|
]
|
|
49
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Is `id` sitting in a position where it names something rather than
|
|
52
|
+
* referencing a value — an object/class member key, a JSX attribute or
|
|
53
|
+
* intrinsic tag name, or a statement label? These positions merely SPELL an
|
|
54
|
+
* identifier's text without evaluating it as a variable reference, so any
|
|
55
|
+
* pass that resolves identifier text to a binding (a rename, an
|
|
56
|
+
* accessor-wrap, a free-reference scan) must leave them untouched.
|
|
57
|
+
*
|
|
58
|
+
* Shared by `classify()` below (factory-rename sites) and
|
|
59
|
+
* `rewriteIdentifierAsAccessor` (`ir-to-client-js/utils.ts`, #3239) — see
|
|
60
|
+
* CLAUDE.md's "one decision, two implementations" rule. Declaration-name
|
|
61
|
+
* positions (a parameter, `const`, `function`/`class` name) are
|
|
62
|
+
* deliberately NOT covered here: those introduce a new binding rather than
|
|
63
|
+
* naming an existing one, and the two callers disagree on how to handle a
|
|
64
|
+
* shadowing declaration (renaming wants to rename it too; accessor-wrapping
|
|
65
|
+
* deliberately wraps it so the shadow fails to parse), so each classifies
|
|
66
|
+
* those positions itself.
|
|
67
|
+
*/
|
|
68
|
+
export function isNonReferenceIdentifierPosition(id: ts.Identifier): boolean {
|
|
69
|
+
const p = id.parent
|
|
70
|
+
if (ts.isPropertyAccessExpression(p) && p.name === id) return true // obj.name tail (incl. ?. chains)
|
|
71
|
+
if (ts.isPropertyAssignment(p) && p.name === id) return true // { name: v } key
|
|
72
|
+
if (ts.isBindingElement(p) && p.propertyName === id) return true // { name: local } pattern key
|
|
73
|
+
if ((ts.isMethodDeclaration(p) || ts.isGetAccessorDeclaration(p) ||
|
|
74
|
+
ts.isSetAccessorDeclaration(p) || ts.isPropertyDeclaration(p) ||
|
|
75
|
+
ts.isEnumMember(p)) && p.name === id) return true // member keys
|
|
76
|
+
if (ts.isJsxAttribute(p) && p.name === id) return true // JSX attr name
|
|
77
|
+
if ((ts.isLabeledStatement(p) && p.label === id)
|
|
78
|
+
|| ((ts.isBreakStatement(p) || ts.isContinueStatement(p)) && p.label === id)) return true
|
|
79
|
+
if ((ts.isJsxOpeningElement(p) || ts.isJsxSelfClosingElement(p) || ts.isJsxClosingElement(p))
|
|
80
|
+
&& p.tagName === id && /^[a-z]/.test(id.text)) return true // intrinsic tag <div>
|
|
81
|
+
return false
|
|
82
|
+
}
|
|
83
|
+
|
|
50
84
|
/**
|
|
51
85
|
* Check if a source file needs ts.TypeChecker for static analysis.
|
|
52
86
|
* Reactive-brand detection requires the checker for library accessor patterns.
|
|
@@ -5804,17 +5838,7 @@ function detectReactiveFactory(
|
|
|
5804
5838
|
if (!relevantNames.has(id.text)) return
|
|
5805
5839
|
const p = id.parent
|
|
5806
5840
|
// Pure-key / non-reference positions — never rename:
|
|
5807
|
-
if (
|
|
5808
|
-
if (ts.isPropertyAssignment(p) && p.name === id) return // { name: v } key
|
|
5809
|
-
if (ts.isBindingElement(p) && p.propertyName === id) return // { name: local } pattern key
|
|
5810
|
-
if ((ts.isMethodDeclaration(p) || ts.isGetAccessorDeclaration(p) ||
|
|
5811
|
-
ts.isSetAccessorDeclaration(p) || ts.isPropertyDeclaration(p) ||
|
|
5812
|
-
ts.isEnumMember(p)) && p.name === id) return // member keys
|
|
5813
|
-
if (ts.isJsxAttribute(p) && p.name === id) return // JSX attr name
|
|
5814
|
-
if ((ts.isLabeledStatement(p) && p.label === id) ||
|
|
5815
|
-
((ts.isBreakStatement(p) || ts.isContinueStatement(p)) && p.label === id)) return
|
|
5816
|
-
if ((ts.isJsxOpeningElement(p) || ts.isJsxSelfClosingElement(p) || ts.isJsxClosingElement(p)) &&
|
|
5817
|
-
p.tagName === id && /^[a-z]/.test(id.text)) return // intrinsic tag <div>
|
|
5841
|
+
if (isNonReferenceIdentifierPosition(id)) return
|
|
5818
5842
|
|
|
5819
5843
|
// Shorthand dual-role positions → expansion form (renaming must
|
|
5820
5844
|
// preserve the implied key): `{ name }` object literal / pattern.
|