@barefootjs/jsx 0.38.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/action-accessor.d.ts +159 -0
- package/dist/action-accessor.d.ts.map +1 -0
- 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 +49 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/async-action-refusal.d.ts +45 -0
- package/dist/async-action-refusal.d.ts.map +1 -0
- package/dist/compiler.d.ts.map +1 -1
- package/dist/debug.d.ts +61 -5
- package/dist/debug.d.ts.map +1 -1
- package/dist/errors.d.ts +4 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/format-date-refusal.d.ts +2 -6
- package/dist/format-date-refusal.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 +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1261 -422
- package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
- 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/compute-inlinability.d.ts.map +1 -1
- package/dist/ir-to-client-js/compute-scope.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/insert.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/insert.d.ts.map +1 -1
- package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
- package/dist/ir-to-client-js/declaration-sort.d.ts +2 -1
- package/dist/ir-to-client-js/declaration-sort.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-module-level.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/declaration-emit.d.ts +7 -5
- package/dist/ir-to-client-js/plan/declaration-emit.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.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/types.d.ts +4 -0
- package/dist/ir-to-client-js/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +52 -9
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/signal-initializer.d.ts +24 -0
- package/dist/signal-initializer.d.ts.map +1 -0
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/template-position-walk.d.ts +68 -0
- package/dist/template-position-walk.d.ts.map +1 -0
- package/dist/types.d.ts +95 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +194 -4
- package/src/__tests__/action-accessor.test.ts +166 -0
- package/src/__tests__/adapter-output.test.ts +142 -0
- package/src/__tests__/create-mutation.test.ts +250 -0
- package/src/__tests__/create-query.test.ts +696 -0
- package/src/__tests__/csr-fragment-root-scope-shape.test.ts +156 -0
- package/src/__tests__/debug.test.ts +305 -0
- package/src/__tests__/doc-examples.test.ts +66 -3
- 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/__tests__/props-destructuring.test.ts +79 -4
- package/src/__tests__/reactive-factory-arity.test.ts +311 -0
- package/src/__tests__/ssr-portal-ref-callback.test.ts +5 -3
- package/src/action-accessor.ts +289 -0
- package/src/adapters/interface.ts +19 -0
- package/src/adapters/jsx-adapter.ts +47 -20
- package/src/analyzer.ts +593 -44
- package/src/async-action-refusal.ts +254 -0
- package/src/compiler.ts +81 -46
- package/src/debug.ts +351 -104
- package/src/errors.ts +39 -0
- package/src/format-date-refusal.ts +6 -125
- package/src/html-types.ts +14 -0
- package/src/index.ts +3 -1
- package/src/ir-to-client-js/build-references.ts +6 -2
- package/src/ir-to-client-js/collect-elements.ts +65 -4
- package/src/ir-to-client-js/compute-inlinability.ts +2 -1
- package/src/ir-to-client-js/compute-scope.ts +3 -1
- package/src/ir-to-client-js/control-flow/plan/build-insert.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/insert.ts +2 -0
- package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +112 -3
- package/src/ir-to-client-js/control-flow/stringify/insert.ts +1 -1
- package/src/ir-to-client-js/csr-substitute.ts +11 -3
- package/src/ir-to-client-js/declaration-sort.ts +7 -3
- package/src/ir-to-client-js/emit-module-level.ts +7 -4
- package/src/ir-to-client-js/emit-registration.ts +5 -10
- package/src/ir-to-client-js/index.ts +7 -2
- package/src/ir-to-client-js/phases/ref-callbacks.ts +1 -1
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +16 -15
- package/src/ir-to-client-js/plan/declaration-emit.ts +7 -5
- package/src/ir-to-client-js/prop-handling.ts +4 -0
- package/src/ir-to-client-js/reactivity.ts +32 -1
- package/src/ir-to-client-js/stringify/declaration-emit.ts +14 -7
- package/src/ir-to-client-js/types.ts +4 -0
- package/src/ir-to-client-js/utils.ts +184 -21
- package/src/jsx-to-ir.ts +111 -14
- package/src/signal-initializer.ts +36 -0
- package/src/ssr-defaults.ts +10 -0
- package/src/ssr-seed-plan.ts +12 -1
- package/src/template-position-walk.ts +203 -0
- package/src/types.ts +99 -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
|
+
})
|
|
@@ -117,12 +117,13 @@ describe('Destructured props keep their declared types (#2150)', () => {
|
|
|
117
117
|
})
|
|
118
118
|
})
|
|
119
119
|
|
|
120
|
-
// A non-primitive optional
|
|
121
|
-
//
|
|
122
|
-
//
|
|
120
|
+
// A non-primitive optional whose type doesn't resolve (here: an imported
|
|
121
|
+
// element type) keeps `unknown` (interface{}-based lowering) but still
|
|
122
|
+
// reports the type's `?` — the adversarial catalogue derives absent
|
|
123
|
+
// points from `optional` alone.
|
|
123
124
|
test('marks OPTIONAL non-primitive members optional while keeping unknown type (#2259)', () => {
|
|
124
125
|
const source = `
|
|
125
|
-
type Todo
|
|
126
|
+
import type { Todo } from './todo'
|
|
126
127
|
type Props = { items?: Todo[] }
|
|
127
128
|
|
|
128
129
|
export function Component({ items }: Props) {
|
|
@@ -208,4 +209,78 @@ describe('Destructured props keep their declared types (#2150)', () => {
|
|
|
208
209
|
expect(typeOf(ctx, 'handlers')).toEqual({ kind: 'unknown', raw: 'unknown' })
|
|
209
210
|
expect(typeOf(ctx, 'configs')).toEqual({ kind: 'unknown', raw: 'unknown' })
|
|
210
211
|
})
|
|
212
|
+
|
|
213
|
+
// A same-file object type referenced BY NAME resolves like its inline
|
|
214
|
+
// spelling — the `props`-object form (`props: { initial: State }`) always
|
|
215
|
+
// kept it; the destructured form declined it to `unknown`.
|
|
216
|
+
test('resolves a member typed by a same-file object type, named element types included', () => {
|
|
217
|
+
const source = `
|
|
218
|
+
type Item = { id: string; label: string }
|
|
219
|
+
interface State { items: Item[]; nextId: number }
|
|
220
|
+
|
|
221
|
+
export function Component({ initial, rows }: { initial: State; rows: Item[] }) {
|
|
222
|
+
return <div>{initial.nextId}{rows.length}</div>
|
|
223
|
+
}
|
|
224
|
+
`
|
|
225
|
+
|
|
226
|
+
const ctx = analyzeComponent(source, 'Component.tsx')
|
|
227
|
+
|
|
228
|
+
expect(typeOf(ctx, 'initial')).toEqual({ kind: 'interface', raw: 'State' })
|
|
229
|
+
expect(typeOf(ctx, 'rows')).toEqual({
|
|
230
|
+
kind: 'array',
|
|
231
|
+
raw: 'Item[]',
|
|
232
|
+
elementType: { kind: 'interface', raw: 'Item' },
|
|
233
|
+
})
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
// The named type is checked through its declaration's members, so the
|
|
237
|
+
// same all-or-nothing rule applies: a union/function leaf, a generic, an
|
|
238
|
+
// `extends` clause, a non-property member, an import, or a recursive
|
|
239
|
+
// reference still declines the whole member.
|
|
240
|
+
test('declines a named type that is not a plain, fully-resolvable same-file object type', () => {
|
|
241
|
+
const source = `
|
|
242
|
+
import type { External } from './external'
|
|
243
|
+
type WithUnion = { kind: 'a' | 'b' }
|
|
244
|
+
type Generic<T> = { value: T }
|
|
245
|
+
interface Base { id: string }
|
|
246
|
+
interface Derived extends Base { label: string }
|
|
247
|
+
interface WithMethod { run(): void }
|
|
248
|
+
type Tree = { children: Tree[] }
|
|
249
|
+
|
|
250
|
+
export function Component({ a, b, c, d, e, f }: {
|
|
251
|
+
a: WithUnion; b: Generic<string>; c: Derived; d: WithMethod; e: External; f: Tree
|
|
252
|
+
}) {
|
|
253
|
+
return <div />
|
|
254
|
+
}
|
|
255
|
+
`
|
|
256
|
+
|
|
257
|
+
const ctx = analyzeComponent(source, 'Component.tsx')
|
|
258
|
+
|
|
259
|
+
for (const name of ['a', 'b', 'c', 'd', 'e', 'f']) {
|
|
260
|
+
expect(typeOf(ctx, name)).toEqual({ kind: 'unknown', raw: 'unknown' })
|
|
261
|
+
}
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
// Interface declaration merging: the type is the union of both blocks,
|
|
265
|
+
// so either half alone can't vouch for it. Declines whichever order the
|
|
266
|
+
// halves come in, rather than admitting on whichever one a walk keeps.
|
|
267
|
+
test('declines a named type declared more than once (interface merging)', () => {
|
|
268
|
+
const merged = (first: string, second: string) => `
|
|
269
|
+
interface State { ${first} }
|
|
270
|
+
interface State { ${second} }
|
|
271
|
+
|
|
272
|
+
export function Component({ initial }: { initial: State }) {
|
|
273
|
+
return <div />
|
|
274
|
+
}
|
|
275
|
+
`
|
|
276
|
+
|
|
277
|
+
for (const source of [
|
|
278
|
+
merged('label: string', "kind: 'a' | 'b'"),
|
|
279
|
+
merged("kind: 'a' | 'b'", 'label: string'),
|
|
280
|
+
merged('label: string', 'count: number'),
|
|
281
|
+
]) {
|
|
282
|
+
const ctx = analyzeComponent(source, 'Component.tsx')
|
|
283
|
+
expect(typeOf(ctx, 'initial')).toEqual({ kind: 'unknown', raw: 'unknown' })
|
|
284
|
+
}
|
|
285
|
+
})
|
|
211
286
|
})
|