@barefootjs/jsx 0.39.8 → 0.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/attr-plan.d.ts +1 -20
- package/dist/adapters/attr-plan.d.ts.map +1 -1
- package/dist/adapters/callback-method.d.ts +19 -7
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +79 -1
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/loop-plan.d.ts +173 -9
- package/dist/adapters/loop-plan.d.ts.map +1 -1
- package/dist/adapters/parsed-expr-emitter.d.ts +20 -3
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/assignment-target.d.ts +26 -0
- package/dist/assignment-target.d.ts.map +1 -0
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/css-layer-prefixer.d.ts.map +1 -1
- package/dist/debug.d.ts.map +1 -1
- package/dist/errors.d.ts +1 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/index.d.ts +6 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2566 -2007
- package/dist/ir-to-client-js/compute-inlinability.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +24 -1
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts +16 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts +6 -1
- package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/csr-substitute.d.ts +13 -2
- package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases/props-extraction.d.ts +5 -3
- package/dist/ir-to-client-js/phases/props-extraction.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +8 -0
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/rewrite-props-object.d.ts.map +1 -1
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +1 -0
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +1 -1
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/module-exports.d.ts.map +1 -1
- package/dist/prop-rewrite.d.ts +17 -8
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-binding.d.ts +57 -0
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/scope/binding-scope.d.ts +32 -2
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/scope/lexical-scope-walk.d.ts +26 -0
- package/dist/scope/lexical-scope-walk.d.ts.map +1 -0
- package/dist/template-parts.d.ts +1 -1
- package/dist/template-parts.d.ts.map +1 -1
- package/dist/types.d.ts +30 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/arithmetic-lowering.test.ts +5 -1
- package/src/__tests__/attr-plan.test.ts +1 -14
- package/src/__tests__/binding-scope.test.ts +29 -0
- package/src/__tests__/client-template-escape-soundness.test.ts +17 -4
- package/src/__tests__/client-template-static-delimiters.test.ts +57 -0
- package/src/__tests__/create-query.test.ts +1 -1
- package/src/__tests__/dsl-template-adapter.test.ts +70 -0
- package/src/__tests__/emit-to-string-call.test.ts +58 -0
- package/src/__tests__/extract-function-params.test.ts +37 -0
- package/src/__tests__/issue-2960-multi-root-cond-same-tag.test.ts +18 -0
- package/src/__tests__/jsx-text-linebreak-whitespace.test.ts +11 -0
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +90 -31
- package/src/__tests__/loop-plan.test.ts +196 -1
- package/src/__tests__/loop-row-child-children-handler.test.ts +129 -0
- package/src/__tests__/loop-row-child-children-inner-loop-shadow.test.ts +74 -0
- package/src/__tests__/loop-row-child-children-ref.test.ts +117 -0
- package/src/__tests__/props-alias-const.test.ts +60 -1
- package/src/__tests__/props-named-callback-param.test.ts +256 -0
- package/src/__tests__/renamed-body-destructure-template.test.ts +86 -0
- package/src/__tests__/rewrite-props-object.test.ts +90 -0
- package/src/__tests__/template-parts-literal-boundary.test.ts +22 -0
- package/src/__tests__/written-binding-substitution.test.ts +430 -0
- package/src/adapters/attr-plan.ts +2 -54
- package/src/adapters/callback-method.ts +25 -10
- package/src/adapters/dsl-template-adapter.ts +155 -2
- package/src/adapters/loop-plan.ts +267 -14
- package/src/adapters/parsed-expr-emitter.ts +37 -4
- package/src/analyzer.ts +283 -2
- package/src/assignment-target.ts +79 -0
- package/src/augment-inherited-props.ts +5 -9
- package/src/css-layer-prefixer.ts +4 -5
- package/src/debug.ts +1 -0
- package/src/errors.ts +11 -0
- package/src/index.ts +23 -4
- package/src/ir-to-client-js/compute-inlinability.ts +7 -4
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +21 -5
- package/src/ir-to-client-js/control-flow/plan/loop.ts +25 -1
- package/src/ir-to-client-js/control-flow/shared.ts +41 -3
- package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +23 -3
- package/src/ir-to-client-js/csr-substitute.ts +70 -22
- package/src/ir-to-client-js/emit-reactive.ts +2 -2
- package/src/ir-to-client-js/emit-registration.ts +6 -1
- package/src/ir-to-client-js/html-template.ts +70 -52
- package/src/ir-to-client-js/phases/props-extraction.ts +26 -5
- package/src/ir-to-client-js/plan/build-static-array-child-init.ts +7 -0
- package/src/ir-to-client-js/plan/static-array-child-init.ts +8 -0
- package/src/ir-to-client-js/rewrite-destructured-props.ts +2 -2
- package/src/ir-to-client-js/rewrite-props-object.ts +14 -8
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +4 -1
- package/src/ir-to-client-js/utils.ts +41 -97
- package/src/jsx-to-ir.ts +65 -32
- package/src/loop-destructure.ts +4 -1
- package/src/module-exports.ts +13 -21
- package/src/prop-rewrite.ts +31 -72
- package/src/props-binding.ts +73 -1
- package/src/rich-type-refusal.ts +3 -1
- package/src/scope/binding-scope.ts +39 -3
- package/src/scope/lexical-scope-walk.ts +167 -0
- package/src/template-parts.ts +4 -2
- package/src/template-position-walk.ts +3 -1
- package/src/types.ts +27 -2
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `recv.toString()` reaches an adapter's `call()` as `String(recv)` (#3621).
|
|
3
|
+
* Every template adapter registers the `String` primitive; a zero-arg method
|
|
4
|
+
* call otherwise falls to `call()`'s getter fallback, which reads the callee
|
|
5
|
+
* `recv.toString` as a property and drops the call.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, test, expect } from 'bun:test'
|
|
9
|
+
import { emitParsedExpr, parseExpression, type ParsedExpr, type ParsedExprEmitter } from '../index'
|
|
10
|
+
|
|
11
|
+
// Spells every node as a JS-like string so the dispatched shape is visible.
|
|
12
|
+
function makeEmitter(): ParsedExprEmitter {
|
|
13
|
+
const callName = (callee: ParsedExpr, emit: (e: ParsedExpr) => string) =>
|
|
14
|
+
callee.kind === 'identifier' ? callee.name : emit(callee)
|
|
15
|
+
return {
|
|
16
|
+
identifier: name => name,
|
|
17
|
+
literal: value => JSON.stringify(value),
|
|
18
|
+
call: (callee, args, emit) => `${callName(callee, emit)}(${args.map(emit).join(', ')})`,
|
|
19
|
+
member: (object, property, _computed, optional, emit) => `${emit(object)}${optional ? '?.' : '.'}${property}`,
|
|
20
|
+
indexAccess: (object, index, emit) => `${emit(object)}[${emit(index)}]`,
|
|
21
|
+
binary: (op, l, r, emit) => `${emit(l)} ${op} ${emit(r)}`,
|
|
22
|
+
unary: (op, a, emit) => `${op}${emit(a)}`,
|
|
23
|
+
logical: (op, l, r, emit) => `${emit(l)} ${op} ${emit(r)}`,
|
|
24
|
+
conditional: () => 'COND',
|
|
25
|
+
templateLiteral: () => 'TPL',
|
|
26
|
+
arrow: () => 'ARROW',
|
|
27
|
+
regex: raw => raw,
|
|
28
|
+
arrayLiteral: (els, emit) => `[${els.map(emit).join(', ')}]`,
|
|
29
|
+
objectLiteral: (_p, raw) => raw,
|
|
30
|
+
callbackMethod: (method, object, _arrow, _rest, emit) => `${emit(object)}.${method}(CB)`,
|
|
31
|
+
arrayMethod: (method, object, args, emit) => `${emit(object)}.${method}(${args.map(emit).join(', ')})`,
|
|
32
|
+
flatMethod: (object, _depth, emit) => `${emit(object)}.flat()`,
|
|
33
|
+
unsupported: raw => `UNSUPPORTED(${raw})`,
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const emit = (src: string) => emitParsedExpr(parseExpression(src), makeEmitter())
|
|
38
|
+
|
|
39
|
+
describe('recv.toString() dispatches as String(recv)', () => {
|
|
40
|
+
test('a row value, an index and a getter result', () => {
|
|
41
|
+
expect(emit('x.toString()')).toBe('String(x)')
|
|
42
|
+
expect(emit('i.toString()')).toBe('String(i)')
|
|
43
|
+
expect(emit('count().toString()')).toBe('String(count())')
|
|
44
|
+
expect(emit('row.meta.id.toString()')).toBe('String(row.meta.id)')
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test('nested inside another expression', () => {
|
|
48
|
+
expect(emit("'#' + n.toString()")).toBe("\"#\" + String(n)")
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('other shapes keep the generic call dispatch', () => {
|
|
52
|
+
// A radix argument changes the result; `String` takes none.
|
|
53
|
+
expect(emit('n.toString(16)')).toBe('n.toString(16)')
|
|
54
|
+
// `x?.toString()` is `undefined` for a nullish receiver, `String` is not.
|
|
55
|
+
expect(emit('x?.toString()')).not.toContain('String(')
|
|
56
|
+
expect(emit('x.valueOf()')).toBe('x.valueOf()')
|
|
57
|
+
})
|
|
58
|
+
})
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { expect, test } from 'bun:test'
|
|
2
|
+
import { extractFunctionParams } from '../index.ts'
|
|
3
|
+
|
|
4
|
+
const cases = [
|
|
5
|
+
['(a, b) => a + b', 'a, b'],
|
|
6
|
+
['async value => value', 'value'],
|
|
7
|
+
['function named(a: string, b = 1) { return a }', 'a, b'],
|
|
8
|
+
['async function named(a: string) {}', 'a'],
|
|
9
|
+
['(handler: (value: string) => void, label = "a,b") => handler(label)', 'handler, label'],
|
|
10
|
+
['(value = build(1, 2), next = () => 1) => value', 'value, next'],
|
|
11
|
+
['(value: Record<string, number>, ...rest: string[]) => value', 'value, ...rest'],
|
|
12
|
+
['<T,>(value: T): T => value', 'value'],
|
|
13
|
+
['<T>(value: T): T => value', 'value'],
|
|
14
|
+
['(value: string) => <span>{value}</span>', 'value'],
|
|
15
|
+
['(value = <span />, next) => value', 'value, next'],
|
|
16
|
+
['function f(value = <span />, next) {}', 'value, next'],
|
|
17
|
+
['async (value = <span title="a,b" />, next = 1) => value', 'value, next'],
|
|
18
|
+
['function* f(value = <span>{1}</span>, ...rest) {}', 'value, ...rest'],
|
|
19
|
+
['(value = <number>1, next) => value', 'value, next'],
|
|
20
|
+
['(value = , next) => value', ''],
|
|
21
|
+
['(値: number, $other?: string) => 値', '値, $other'],
|
|
22
|
+
['({ value: renamed }, [first, second]) => renamed', '{ value: renamed }, [first, second]'],
|
|
23
|
+
['/* input */ ((value: string) => value)', 'value'],
|
|
24
|
+
['() => 1', ''],
|
|
25
|
+
['"(fake) => fake"', ''],
|
|
26
|
+
['call((nested) => nested)', ''],
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
for (const [source, expected] of cases) {
|
|
30
|
+
test(`extractFunctionParams: ${source}`, () => {
|
|
31
|
+
expect(extractFunctionParams(source)).toBe(expected)
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
test('long non-function input has no parameters', () => {
|
|
36
|
+
expect(extractFunctionParams(`async ${' '.repeat(20_000)} notAFunction`)).toBe('')
|
|
37
|
+
})
|
|
@@ -139,3 +139,21 @@ describe('#2960: multi-root cond branch whose roots share a tag name', () => {
|
|
|
139
139
|
expect(result).not.toContain('bf-c=')
|
|
140
140
|
})
|
|
141
141
|
})
|
|
142
|
+
|
|
143
|
+
for (const entry of [
|
|
144
|
+
{ name: 'long single-root tag name', html: `<${'a'.repeat(20_000)}></${'a'.repeat(20_000)}>`, single: true },
|
|
145
|
+
{ name: 'unterminated long nested tag', html: `<div><${'a'.repeat(20_000)}`, single: false },
|
|
146
|
+
{ name: 'many comment-like tokens without terminators', html: `<div>${'<!--x>'.repeat(5_000)}</div>`, single: true },
|
|
147
|
+
{ name: 'many marker comments', html: `<div>${'<!--bf:s0--><!--/-->'.repeat(5_000)}</div>`, single: true },
|
|
148
|
+
{ name: 'spaced self-closing root', html: '<input />', single: true },
|
|
149
|
+
]) {
|
|
150
|
+
test(`conditional root classification: ${entry.name}`, () => {
|
|
151
|
+
const result = addCondAttrToTemplate(entry.html, 'probe')
|
|
152
|
+
if (entry.single) {
|
|
153
|
+
expect(result).toContain(' bf-c="probe"')
|
|
154
|
+
expect(result).not.toContain('<!--bf-cond-start:probe-->')
|
|
155
|
+
} else {
|
|
156
|
+
expect(result).toBe(`<!--bf-cond-start:probe-->${entry.html}<!--bf-cond-end:probe-->`)
|
|
157
|
+
}
|
|
158
|
+
})
|
|
159
|
+
}
|
|
@@ -78,3 +78,14 @@ describe('JSX text whitespace across line breaks (#3237)', () => {
|
|
|
78
78
|
expect(js).toContain('<div><b>a</b> <i>b</i></div>')
|
|
79
79
|
})
|
|
80
80
|
})
|
|
81
|
+
|
|
82
|
+
test('long interior space runs survive line cleanup', () => {
|
|
83
|
+
const text = `start${' '.repeat(30_000)}end`
|
|
84
|
+
const source = `'use client'; export function Probe() { return <pre>\n${text} \n</pre> }`
|
|
85
|
+
expect(getClientJs(source)).toContain(`<pre>${text}</pre>`)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('line cleanup removes ASCII indentation but preserves nonbreaking spaces', () => {
|
|
89
|
+
const source = `'use client'; export function Probe() { return <pre>\n \u00a0text\u00a0 \n</pre> }`
|
|
90
|
+
expect(getClientJs(source)).toContain('<pre>\u00a0text\u00a0</pre>')
|
|
91
|
+
})
|
|
@@ -65,16 +65,6 @@ function assertParses(clientJs: string): void {
|
|
|
65
65
|
expect(syntaxErrorsOf(clientJs)).toHaveLength(0)
|
|
66
66
|
}
|
|
67
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
68
|
describe('.map() row: object-literal key / shorthand / member positions vs. the loop param (#3239)', () => {
|
|
79
69
|
test('shorthand property `{ choice }` expands to `{ choice: choice() }`, not `{ choice() }`', () => {
|
|
80
70
|
const source = `
|
|
@@ -361,18 +351,14 @@ describe('.map() row: declaration/member positions the old regex skipped via its
|
|
|
361
351
|
})
|
|
362
352
|
})
|
|
363
353
|
|
|
364
|
-
describe('.map() row: a nested scope that shadows the loop param
|
|
365
|
-
//
|
|
366
|
-
//
|
|
367
|
-
//
|
|
368
|
-
//
|
|
369
|
-
//
|
|
370
|
-
|
|
371
|
-
|
|
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', () => {
|
|
354
|
+
describe('.map() row: a nested scope that shadows the loop param keeps its own binding (#3595)', () => {
|
|
355
|
+
// The accessor rewrite walks the text with the shared lexical-scope model
|
|
356
|
+
// (`walkWithScope`): an inner binding of the loop param's name — its binder
|
|
357
|
+
// and every reference it shadows — is the inner binding's, so neither is
|
|
358
|
+
// wrapped. Before #3595 the rewrite had no scope tracking and wrapped the
|
|
359
|
+
// binder too (`(choice()) =>`, `const choice() = 1`), a syntax error.
|
|
360
|
+
|
|
361
|
+
test('a nested `.map(choice => ...)` that reuses the outer loop param name reads the inner param', () => {
|
|
376
362
|
const source = `
|
|
377
363
|
'use client'
|
|
378
364
|
import { createSignal } from '@barefootjs/client'
|
|
@@ -388,10 +374,11 @@ describe('.map() row: a nested scope that shadows the loop param fails loudly in
|
|
|
388
374
|
}
|
|
389
375
|
`
|
|
390
376
|
const js = getClientJs(source)
|
|
391
|
-
|
|
377
|
+
assertParses(js)
|
|
378
|
+
expect(js).toContain("choice().split('').map(choice => choice.toUpperCase())")
|
|
392
379
|
})
|
|
393
380
|
|
|
394
|
-
test('a nested `const choice = ...` that reuses the outer loop param name
|
|
381
|
+
test('a nested `const choice = ...` that reuses the outer loop param name reads the local', () => {
|
|
395
382
|
const source = `
|
|
396
383
|
'use client'
|
|
397
384
|
import { createSignal } from '@barefootjs/client'
|
|
@@ -407,7 +394,73 @@ describe('.map() row: a nested scope that shadows the loop param fails loudly in
|
|
|
407
394
|
}
|
|
408
395
|
`
|
|
409
396
|
const js = getClientJs(source)
|
|
410
|
-
|
|
397
|
+
assertParses(js)
|
|
398
|
+
expect(js).toContain('const choice = 1; return choice })()')
|
|
399
|
+
})
|
|
400
|
+
|
|
401
|
+
test('an inner callback that reuses the index name reads its own index', () => {
|
|
402
|
+
const source = `
|
|
403
|
+
'use client'
|
|
404
|
+
import { createSignal } from '@barefootjs/client'
|
|
405
|
+
export function App() {
|
|
406
|
+
const [items] = createSignal(['ab', 'cd'])
|
|
407
|
+
return (
|
|
408
|
+
<div>
|
|
409
|
+
{items().map((choice, i) => (
|
|
410
|
+
<b key={choice}>{String(i) + choice.split('').map((c, i) => c + i).join('')}</b>
|
|
411
|
+
))}
|
|
412
|
+
</div>
|
|
413
|
+
)
|
|
414
|
+
}
|
|
415
|
+
`
|
|
416
|
+
const js = getClientJs(source)
|
|
417
|
+
assertParses(js)
|
|
418
|
+
// A lazy row hands the index as a plain number (`const i = __e.index`);
|
|
419
|
+
// the inner `(c, i)` keeps its binder either way.
|
|
420
|
+
expect(js).toContain("String(i) + choice().split('').map((c, i) => c + i)")
|
|
421
|
+
expect(js).not.toContain('(c, i())')
|
|
422
|
+
})
|
|
423
|
+
|
|
424
|
+
test('a named class expression binds its own name inside the class body', () => {
|
|
425
|
+
const source = `
|
|
426
|
+
'use client'
|
|
427
|
+
import { createSignal } from '@barefootjs/client'
|
|
428
|
+
export function App() {
|
|
429
|
+
const [items] = createSignal(['a', 'b'])
|
|
430
|
+
return (
|
|
431
|
+
<div>
|
|
432
|
+
{items().map(choice => (
|
|
433
|
+
<b key={choice}>{String((class choice { static n = choice.name }).n)}</b>
|
|
434
|
+
))}
|
|
435
|
+
</div>
|
|
436
|
+
)
|
|
437
|
+
}
|
|
438
|
+
`
|
|
439
|
+
const js = getClientJs(source)
|
|
440
|
+
assertParses(js)
|
|
441
|
+
expect(js).toContain('class choice { static n = choice.name }')
|
|
442
|
+
})
|
|
443
|
+
|
|
444
|
+
test('a computed method key reads the outer item; the method body reads its own param', () => {
|
|
445
|
+
// The key is evaluated in the enclosing scope, before the method's
|
|
446
|
+
// parameters exist, so only the body's `choice` is the inner binding.
|
|
447
|
+
const source = `
|
|
448
|
+
'use client'
|
|
449
|
+
import { createSignal } from '@barefootjs/client'
|
|
450
|
+
export function App() {
|
|
451
|
+
const [items] = createSignal(['a', 'b'])
|
|
452
|
+
return (
|
|
453
|
+
<div>
|
|
454
|
+
{items().map(choice => (
|
|
455
|
+
<b key={choice}>{Object.keys({ [choice](choice) { return choice } }).join('')}</b>
|
|
456
|
+
))}
|
|
457
|
+
</div>
|
|
458
|
+
)
|
|
459
|
+
}
|
|
460
|
+
`
|
|
461
|
+
const js = getClientJs(source)
|
|
462
|
+
assertParses(js)
|
|
463
|
+
expect(js).toContain('{ [choice()](choice) { return choice } }')
|
|
411
464
|
})
|
|
412
465
|
})
|
|
413
466
|
|
|
@@ -481,12 +534,18 @@ describe('destructured loop-param path (rewriteLoopBindingRefs) shares the plain
|
|
|
481
534
|
expect(js).toContain('(function color() { return __bfItem().label })()')
|
|
482
535
|
})
|
|
483
536
|
|
|
484
|
-
test('a nested function declaration shadowing a binding
|
|
485
|
-
//
|
|
486
|
-
//
|
|
487
|
-
//
|
|
488
|
-
// old destructured regex did (#3260 review).
|
|
537
|
+
test('a nested function declaration shadowing a binding is called as the local function', () => {
|
|
538
|
+
// `function color() {}` binds `color` for its block, so both the
|
|
539
|
+
// declaration and its call stay local (#3595); before, the rewrite had no
|
|
540
|
+
// scope tracking and broke the declaration name to fail loudly (#3260).
|
|
489
541
|
const js = compileRow("<b key={label}>{(() => { function color() { return 'blue' } return color() })()}</b>")
|
|
490
|
-
|
|
542
|
+
assertParses(js)
|
|
543
|
+
expect(js).toContain("function color() { return 'blue' } return color() })()")
|
|
544
|
+
})
|
|
545
|
+
|
|
546
|
+
test('a nested callback rebinding a destructured name reads the inner param (#3595)', () => {
|
|
547
|
+
const js = compileRow("<b key={label}>{[1, 2].map((label) => label * 2).join(label)}</b>")
|
|
548
|
+
assertParses(js)
|
|
549
|
+
expect(js).toContain('[1, 2].map((label) => label * 2).join(__bfItem().label)')
|
|
491
550
|
})
|
|
492
551
|
})
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { describe, test, expect } from 'bun:test'
|
|
7
7
|
import { analyzeComponent } from '../analyzer'
|
|
8
8
|
import { jsxToIR } from '../jsx-to-ir'
|
|
9
|
-
import { planLoop, type LoopPlanContext } from '../adapters/loop-plan'
|
|
9
|
+
import { loopHeaderTargets, planLoop, savedLoopBindings, spellDestructureLocals, spellFilteredRow, spellSortedSource, type LoopPlanContext, type LoopSortSpelling } from '../adapters/loop-plan'
|
|
10
10
|
import { BindingScope } from '../scope/binding-scope'
|
|
11
11
|
import type { IRLoop, IRNode } from '../types'
|
|
12
12
|
|
|
@@ -46,6 +46,10 @@ describe('planLoop', () => {
|
|
|
46
46
|
unresolvableConst: null,
|
|
47
47
|
sort: null,
|
|
48
48
|
captureAliases: [],
|
|
49
|
+
filter: null,
|
|
50
|
+
itemVar: 'x',
|
|
51
|
+
indexLocal: null,
|
|
52
|
+
destructureLocals: [],
|
|
49
53
|
})
|
|
50
54
|
})
|
|
51
55
|
|
|
@@ -94,6 +98,33 @@ describe('planLoop', () => {
|
|
|
94
98
|
expect(sorted.sort).toMatchObject({ kind: 'eval', helper: 'sort_eval' })
|
|
95
99
|
})
|
|
96
100
|
|
|
101
|
+
test('a comparator outside the evaluator falls back to the structured descriptor', () => {
|
|
102
|
+
const sorted = plan(`export function Test(props: { items: { name: string }[] }) { return <ul>{props.items.toSorted((a, b) => a.name.localeCompare(b.name)).map(x => <li key={x.name}>{x.name}</li>)}</ul> }`)
|
|
103
|
+
expect(sorted.sort).toMatchObject({ kind: 'structured-sort', comparator: { keys: [{ key: { kind: 'field', field: 'name' }, type: 'string', direction: 'asc' }] } })
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('the sorted source spells each arm of the sort plan', () => {
|
|
107
|
+
const refusals: string[] = []
|
|
108
|
+
const spelling: LoopSortSpelling = {
|
|
109
|
+
emit: (e) => (e.kind === 'identifier' ? `v.${e.name}` : '?'),
|
|
110
|
+
eval: {
|
|
111
|
+
call: (helper, operands) => `${helper}(${operands.join(', ')})`,
|
|
112
|
+
string: (value) => `'${value}'`,
|
|
113
|
+
boolean: (value) => String(value),
|
|
114
|
+
env: (entries) => `{${entries.map(([name, value]) => `${name}: ${value}`).join(', ')}}`,
|
|
115
|
+
},
|
|
116
|
+
structured: (recv, comparator) => `sort(${recv}, ${comparator.keys.map((k) => (k.key.kind === 'field' ? k.key.field : 'self')).join(',')})`,
|
|
117
|
+
refuse: (message) => refusals.push(message),
|
|
118
|
+
}
|
|
119
|
+
expect(spellSortedSource(null, 'xs', spelling)).toBe('xs')
|
|
120
|
+
const evaluated = spellSortedSource({ kind: 'eval', helper: 'sort_eval', operands: [{ kind: 'string', value: 'body' }], captured: ['k'] }, 'xs', spelling)
|
|
121
|
+
expect(evaluated).toBe(`sort_eval(xs, 'body', {k: v.k})`)
|
|
122
|
+
const structured = plan(`export function Test(props: { items: { name: string }[] }) { return <ul>{props.items.toSorted((a, b) => a.name.localeCompare(b.name)).map(x => <li key={x.name}>{x.name}</li>)}</ul> }`)
|
|
123
|
+
expect(spellSortedSource(structured.sort, 'xs', spelling)).toBe('sort(xs, name)')
|
|
124
|
+
expect(spellSortedSource({ kind: 'refuse', message: 'no form', suggestion: '' }, 'xs', spelling)).toBe('xs')
|
|
125
|
+
expect(refusals).toEqual(['no form'])
|
|
126
|
+
})
|
|
127
|
+
|
|
97
128
|
test('a filter capture a row binding would shadow gets a per-loop alias', () => {
|
|
98
129
|
const captured = plan(`export function Test(props: { items: { id: string }[]; id: string }) {
|
|
99
130
|
const id = props.id
|
|
@@ -101,4 +132,168 @@ describe('planLoop', () => {
|
|
|
101
132
|
}`)
|
|
102
133
|
expect(captured.captureAliases).toEqual([{ name: 'id', alias: expect.stringMatching(/^__bf_cap_\w+_id$/) }])
|
|
103
134
|
})
|
|
135
|
+
|
|
136
|
+
test('a filter predicate is renamed for the row: captures to their aliases, the filter param to the loop param', () => {
|
|
137
|
+
const filtered = plan(`export function Test(props: { items: { id: string; label: string }[]; id: string }) {
|
|
138
|
+
const id = props.id
|
|
139
|
+
return <ul>{props.items.filter(x => x.id !== id && x.label !== 'x').map(id => <li key={id.id}>{id.id}</li>)}</ul>
|
|
140
|
+
}`)
|
|
141
|
+
const alias = filtered.captureAliases[0]!.alias
|
|
142
|
+
expect(filtered.filter).toMatchObject({ kind: 'predicate', param: 'x', aliases: new Map([[alias, 'id']]) })
|
|
143
|
+
if (filtered.filter?.kind !== 'predicate') throw new Error('expected a predicate')
|
|
144
|
+
const names = (e: unknown): string[] => {
|
|
145
|
+
if (Array.isArray(e)) return e.flatMap(names)
|
|
146
|
+
if (e === null || typeof e !== 'object') return []
|
|
147
|
+
const node = e as Record<string, unknown>
|
|
148
|
+
if (node.kind === 'identifier') return [node.name as string]
|
|
149
|
+
return Object.values(node).flatMap(names)
|
|
150
|
+
}
|
|
151
|
+
expect(names(filtered.filter.predicate)).toEqual(['x', 'id', 'x'])
|
|
152
|
+
expect(names(filtered.filter.captured)).toEqual(['x', alias, 'x'])
|
|
153
|
+
expect(names(filtered.filter.onRow)).toEqual(['id', alias, 'id'])
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
test('a filter without a predicate is refused, and a loop without a filter plans none', () => {
|
|
157
|
+
const loop = (filterPredicate: IRLoop['filterPredicate']): IRLoop => {
|
|
158
|
+
const ctx = analyzeComponent(`export function Test(props: { items: string[] }) { return <ul>{props.items.map(x => <li key={x}>{x}</li>)}</ul> }`, 'Test.tsx')
|
|
159
|
+
return { ...firstLoop(jsxToIR(ctx)), filterPredicate }
|
|
160
|
+
}
|
|
161
|
+
const ctx: LoopPlanContext<unknown[]> = {
|
|
162
|
+
scope: BindingScope.EMPTY,
|
|
163
|
+
localConstants: [],
|
|
164
|
+
resolveLiteralConst: () => null,
|
|
165
|
+
callbackLowering: { adapterName: 'Test' },
|
|
166
|
+
spellStatic: (items) => items,
|
|
167
|
+
}
|
|
168
|
+
expect(planLoop(loop(undefined), ctx).filter).toBeNull()
|
|
169
|
+
expect(planLoop(loop({ param: 'x', raw: 'x => { return x }' }), ctx).filter).toMatchObject({
|
|
170
|
+
kind: 'refuse',
|
|
171
|
+
message: expect.stringContaining('no Test loop-filter form'),
|
|
172
|
+
})
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
test('a filtered row spells inside its guard, a refused one under the stand-in condition', () => {
|
|
176
|
+
const refusals: string[] = []
|
|
177
|
+
const spelling = {
|
|
178
|
+
condition: (f: { param: string }) => `ok(${f.param})`,
|
|
179
|
+
refuse: (message: string) => refusals.push(message),
|
|
180
|
+
refused: 'true',
|
|
181
|
+
open: (condition: string) => `if ${condition}`,
|
|
182
|
+
close: 'end',
|
|
183
|
+
}
|
|
184
|
+
expect(spellFilteredRow(null, ['a', 'b'], spelling)).toEqual(['a', 'b'])
|
|
185
|
+
expect(spellFilteredRow({ kind: 'refuse', message: 'no form', suggestion: '' }, ['a'], spelling)).toEqual(['if true', 'a', 'end'])
|
|
186
|
+
expect(refusals).toEqual(['no form'])
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
test('the row binds its index through a local, except in an object iteration', () => {
|
|
190
|
+
expect(plan(`export function Test(props: { items: string[] }) { return <ul>{props.items.map((x, i) => <li key={x}>{i}</li>)}</ul> }`)).toMatchObject({
|
|
191
|
+
itemVar: 'x',
|
|
192
|
+
indexLocal: 'i',
|
|
193
|
+
})
|
|
194
|
+
expect(plan(`export function Test(props: { items: string[] }) { return <ul>{props.items.keys().map(k => <li key={k}>{k}</li>)}</ul> }`)).toMatchObject({
|
|
195
|
+
itemVar: '__bf_item',
|
|
196
|
+
indexLocal: 'k',
|
|
197
|
+
})
|
|
198
|
+
expect(plan(`export function Test(props: { m: Record<string, string> }) { return <ul>{Object.entries(props.m).map(([k, v]) => <li key={k}>{v}</li>)}</ul> }`)).toMatchObject({
|
|
199
|
+
indexLocal: null,
|
|
200
|
+
destructureLocals: [],
|
|
201
|
+
})
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
test('a lowered destructure binds one local per binding off the item', () => {
|
|
205
|
+
const lowered = plan(`export function Test(props: { rows: { id: string; user: { name: string }; tags: string[]; on: boolean }[] }) {
|
|
206
|
+
return <ul>{props.rows.map(({ id, user: { name }, ...rest }) => <li key={id} data-on={rest.on}>{name}</li>)}</ul>
|
|
207
|
+
}`)
|
|
208
|
+
expect(lowered.itemVar).toBe('__bf_item')
|
|
209
|
+
expect(lowered.destructureLocals).toEqual([
|
|
210
|
+
{ name: 'id', value: { kind: 'path', segments: [{ kind: 'field', key: 'id', isIdent: true }] } },
|
|
211
|
+
{ name: 'name', value: { kind: 'path', segments: [{ kind: 'field', key: 'user', isIdent: true }, { kind: 'field', key: 'name', isIdent: true }] } },
|
|
212
|
+
{ name: 'rest', value: { kind: 'omit', segments: [], exclude: ['id', 'user'] } },
|
|
213
|
+
])
|
|
214
|
+
const tuple = plan(`export function Test(props: { rows: string[][] }) { return <ul>{props.rows.map(([head, ...tail]) => <li key={head}>{tail.length}</li>)}</ul> }`)
|
|
215
|
+
expect(tuple.destructureLocals).toEqual([
|
|
216
|
+
{ name: 'head', value: { kind: 'path', segments: [{ kind: 'index', index: 0 }] } },
|
|
217
|
+
{ name: 'tail', value: { kind: 'slice', segments: [], from: 1 } },
|
|
218
|
+
])
|
|
219
|
+
const spelled = spellDestructureLocals(tuple, 'it', {
|
|
220
|
+
accessor: (item, segments) => item + segments.map((s) => (s.kind === 'index' ? `[${s.index}]` : `.${s.key}`)).join(''),
|
|
221
|
+
slice: (parent, from) => `slice(${parent}, ${from})`,
|
|
222
|
+
omit: (parent, keys) => `omit(${parent}, ${keys.join('|')})`,
|
|
223
|
+
})
|
|
224
|
+
expect(spelled).toEqual([
|
|
225
|
+
{ name: 'head', value: 'it[0]' },
|
|
226
|
+
{ name: 'tail', value: 'slice(it, 1)' },
|
|
227
|
+
])
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
test('an unlowerable destructure binds no locals', () => {
|
|
231
|
+
const refused = plan(`export function Test(props: { rows: { a: string; b: string }[] }) { return <ul>{props.rows.map(({ a, ...rest }) => <li key={a}>{String(rest)}</li>)}</ul> }`)
|
|
232
|
+
expect(refused).toMatchObject({ destructure: 'unlowerable', destructureLocals: [] })
|
|
233
|
+
})
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
function loopOf(source: string): IRLoop {
|
|
237
|
+
return firstLoop(jsxToIR(analyzeComponent(source, 'Test.tsx')))
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
describe('loopHeaderTargets', () => {
|
|
241
|
+
test('an array iteration binds its param, unless the param is not the item', () => {
|
|
242
|
+
const items = loopOf(`export function Test(props: { items: string[] }) { return <ul>{props.items.map((x, i) => <li key={x}>{i}</li>)}</ul> }`)
|
|
243
|
+
expect(loopHeaderTargets(items, { itemVar: 'x' })).toEqual(['x'])
|
|
244
|
+
const keys = loopOf(`export function Test(props: { items: string[] }) { return <ul>{props.items.keys().map(k => <li key={k}>{k}</li>)}</ul> }`)
|
|
245
|
+
expect(loopHeaderTargets(keys, { itemVar: '__bf_item' })).toEqual([])
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
test('an entries object iteration binds its key and value', () => {
|
|
249
|
+
const entries = loopOf(`export function Test(props: { m: Record<string, string> }) { return <ul>{Object.entries(props.m).map(([k, v]) => <li key={k}>{v}</li>)}</ul> }`)
|
|
250
|
+
expect(loopHeaderTargets(entries, { itemVar: 'v' })).toEqual(['k', 'v'])
|
|
251
|
+
})
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
describe('savedLoopBindings', () => {
|
|
255
|
+
const source = `export function Test(props: { items: string[] }) {
|
|
256
|
+
return <ul>{props.items.map((label, i) => { const n = label.length; return <li key={label}>{i}{n}</li> })}</ul>
|
|
257
|
+
}`
|
|
258
|
+
|
|
259
|
+
function saved(outside: BindingScope, templateLevelNames: string[], skip: string[] = []) {
|
|
260
|
+
const loop = loopOf(source)
|
|
261
|
+
return savedLoopBindings(
|
|
262
|
+
{ outside, row: outside.enterLoopRow(loop), templateLevelNames: new Set(templateLevelNames) },
|
|
263
|
+
skip,
|
|
264
|
+
(name, ordinal) => `held_${ordinal}_${name}`,
|
|
265
|
+
)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
test('saves each row binding a template-level name or an enclosing row also binds, in the order the template binds them', () => {
|
|
269
|
+
const outside = BindingScope.EMPTY.enterCallback(['n'])
|
|
270
|
+
expect(saved(outside, ['i', 'label', 'other'])).toEqual([
|
|
271
|
+
{ name: 'label', temporary: 'held_0_label' },
|
|
272
|
+
{ name: 'i', temporary: 'held_1_i' },
|
|
273
|
+
{ name: 'n', temporary: 'held_2_n' },
|
|
274
|
+
])
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
test('an index local binds before a destructure local', () => {
|
|
278
|
+
const loop = loopOf(`export function Test(props: { rows: { name: string }[] }) { return <ul>{props.rows.map(({ name }, count) => <li key={name}>{count}</li>)}</ul> }`)
|
|
279
|
+
const saved = savedLoopBindings(
|
|
280
|
+
{ outside: BindingScope.EMPTY, row: BindingScope.EMPTY.enterLoopRow(loop), templateLevelNames: new Set(['name', 'count']) },
|
|
281
|
+
[],
|
|
282
|
+
(_name, ordinal) => `held_${ordinal}`,
|
|
283
|
+
)
|
|
284
|
+
expect(saved.map((b) => b.name)).toEqual(['count', 'name'])
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
test('a row binding nothing outside shares is not saved', () => {
|
|
288
|
+
expect(saved(BindingScope.EMPTY, ['other'])).toEqual([])
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
test('a skipped name is restored by the backend itself', () => {
|
|
292
|
+
expect(saved(BindingScope.EMPTY, ['label', 'i'], ['label'])).toEqual([{ name: 'i', temporary: 'held_0_i' }])
|
|
293
|
+
})
|
|
294
|
+
|
|
295
|
+
test('a holder that collides with a row binding or a template-level name takes a suffix', () => {
|
|
296
|
+
expect(saved(BindingScope.EMPTY, ['i', 'held_0_i'])).toEqual([{ name: 'i', temporary: 'held_0_i_' }])
|
|
297
|
+
expect(saved(BindingScope.EMPTY.enterCallback(['held_0_i']), ['i'])).toEqual([{ name: 'i', temporary: 'held_0_i_' }])
|
|
298
|
+
})
|
|
104
299
|
})
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #3607: an event handler on an element forwarded as a loop-row child
|
|
3
|
+
* component's own `children` (`<Chip key={o.id}><button onClick={…}/></Chip>`).
|
|
4
|
+
* `collectLoopChildBindings` collects the event into the loop's
|
|
5
|
+
* `bindings.events`, but the component-root loop builder never read it, and
|
|
6
|
+
* a component-root loop has no delegation pass — so the handler reached no
|
|
7
|
+
* part of the client JS. The builder now resolves those events into
|
|
8
|
+
* `rowEvents`, and each row (hydrated and client-built) attaches them
|
|
9
|
+
* directly, the same per-row listener the composite row uses. The static
|
|
10
|
+
* array twin (`single-comp` child init) attaches them to each hydrated row.
|
|
11
|
+
*
|
|
12
|
+
* Conformance coverage: `loop-row-child-children-handler`.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { describe, test, expect } from 'bun:test'
|
|
16
|
+
import ts from 'typescript'
|
|
17
|
+
import { compileJSX } from '../compiler'
|
|
18
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
19
|
+
|
|
20
|
+
const adapter = new TestAdapter()
|
|
21
|
+
|
|
22
|
+
function clientJsOf(body: string): string {
|
|
23
|
+
const source = `
|
|
24
|
+
'use client'
|
|
25
|
+
import { createSignal } from '@barefootjs/client'
|
|
26
|
+
function Chip({ children }: { children?: any }) {
|
|
27
|
+
return <span class="chip">{children}</span>
|
|
28
|
+
}
|
|
29
|
+
export function Picker() {
|
|
30
|
+
const [rows, setRows] = createSignal([{ id: 'a', label: 'A' }])
|
|
31
|
+
const [picked, setPicked] = createSignal('')
|
|
32
|
+
const pick = (id: string) => setPicked(id)
|
|
33
|
+
return <div><p>{picked()}</p>${body}</div>
|
|
34
|
+
}
|
|
35
|
+
`
|
|
36
|
+
const result = compileJSX(source, 'Picker.tsx', { adapter })
|
|
37
|
+
expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
|
|
38
|
+
const js = result.files.find(f => f.type === 'clientJs')!.content
|
|
39
|
+
const diagnostics = ts.transpileModule(js, {
|
|
40
|
+
reportDiagnostics: true,
|
|
41
|
+
compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.Latest },
|
|
42
|
+
}).diagnostics ?? []
|
|
43
|
+
expect(diagnostics.filter(d => d.category === ts.DiagnosticCategory.Error)).toHaveLength(0)
|
|
44
|
+
return js
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The per-row listener block, as attached to the hydrated and the client-built row. */
|
|
48
|
+
function rowListener(elVar: '__existing' | '__csrEl', handler: string): string {
|
|
49
|
+
return `{ const __e = qsa(${elVar}, '[bf="^s3"]'); if (__e) __e.addEventListener('click', ${handler}) }`
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
describe('event handler in forwarded loop-row child children (#3607)', () => {
|
|
53
|
+
test('a handler reading the row item is attached to the hydrated and the client-built row', () => {
|
|
54
|
+
const js = clientJsOf(
|
|
55
|
+
'{rows().map(o => (<Chip key={o.id}><button onClick={() => pick(o.id)}>{o.label}</button></Chip>))}',
|
|
56
|
+
)
|
|
57
|
+
expect(js).toContain(rowListener('__existing', '() => pick(o().id)'))
|
|
58
|
+
expect(js).toContain(rowListener('__csrEl', '() => pick(o().id)'))
|
|
59
|
+
// Attached after the row component is initialised / created.
|
|
60
|
+
expect(js.indexOf(rowListener('__existing', '() => pick(o().id)')))
|
|
61
|
+
.toBeGreaterThan(js.indexOf(`initChild('Chip`))
|
|
62
|
+
expect(js.indexOf(rowListener('__csrEl', '() => pick(o().id)')))
|
|
63
|
+
.toBeGreaterThan(js.indexOf('const __csrEl = createComponent('))
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('a handler reading the row index reads it through the accessor', () => {
|
|
67
|
+
const js = clientJsOf(
|
|
68
|
+
'{rows().map((o, i) => (<Chip key={o.id}><button onClick={() => pick(o.id + i)}>{o.label}</button></Chip>))}',
|
|
69
|
+
)
|
|
70
|
+
expect(js).toContain(rowListener('__existing', '() => pick(o().id + i())'))
|
|
71
|
+
expect(js).toContain(rowListener('__csrEl', '() => pick(o().id + i())'))
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test('a handler reading destructured row bindings reads the row item accessor', () => {
|
|
75
|
+
const js = clientJsOf(
|
|
76
|
+
'{rows().map(({ id, label }, i) => (<Chip key={id}><button onClick={() => pick(id + i)}>{label}</button></Chip>))}',
|
|
77
|
+
)
|
|
78
|
+
expect(js).toContain(rowListener('__existing', '() => pick(__bfItem().id + i())'))
|
|
79
|
+
expect(js).toContain(rowListener('__csrEl', '() => pick(__bfItem().id + i())'))
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
test('a row with only a forwarded handler leaves the simple renderItem shape', () => {
|
|
83
|
+
// No reactive text: the handler alone must force the SSR/CSR split, since
|
|
84
|
+
// the simple shape returns straight from initChild/createComponent.
|
|
85
|
+
const js = clientJsOf(
|
|
86
|
+
`{rows().map(o => (<Chip key={o.id}><button onClick={() => pick(o.id)}>pick</button></Chip>))}`,
|
|
87
|
+
)
|
|
88
|
+
expect(js).not.toContain('if (__existing) { initChild(')
|
|
89
|
+
expect(js).toContain(`{ const __e = qsa(__existing, '[bf="^s2"]'); if (__e) __e.addEventListener('click', () => pick(o().id)) }`)
|
|
90
|
+
expect(js).toContain(`{ const __e = qsa(__csrEl, '[bf="^s2"]'); if (__e) __e.addEventListener('click', () => pick(o().id)) }`)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
test('a module-const array attaches the handler to each hydrated row, unwrapped', () => {
|
|
94
|
+
// A static array has no delegation pass for a component row either; the
|
|
95
|
+
// forEach binds the plain item and index, so the handler is not
|
|
96
|
+
// accessor-wrapped.
|
|
97
|
+
const source = `
|
|
98
|
+
'use client'
|
|
99
|
+
import { createSignal } from '@barefootjs/client'
|
|
100
|
+
function Chip({ children }: { children?: any }) {
|
|
101
|
+
return <span class="chip">{children}</span>
|
|
102
|
+
}
|
|
103
|
+
const OPTS = [{ id: 'a', label: 'A' }]
|
|
104
|
+
export function Picker() {
|
|
105
|
+
const [picked, setPicked] = createSignal('')
|
|
106
|
+
return <div><p>{picked()}</p>{OPTS.map(({ id, label }, i) => (<Chip key={id}><button onClick={() => setPicked(id + i)}>{label}</button></Chip>))}</div>
|
|
107
|
+
}
|
|
108
|
+
`
|
|
109
|
+
const result = compileJSX(source, 'Picker.tsx', { adapter })
|
|
110
|
+
expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
|
|
111
|
+
const js = result.files.find(f => f.type === 'clientJs')!.content
|
|
112
|
+
expect(ts.transpileModule(js, {
|
|
113
|
+
reportDiagnostics: true,
|
|
114
|
+
compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.Latest },
|
|
115
|
+
}).diagnostics?.filter(d => d.category === ts.DiagnosticCategory.Error) ?? []).toHaveLength(0)
|
|
116
|
+
expect(js).toContain(`__childScopes.forEach((childScope, i) => {
|
|
117
|
+
const { id, label } = OPTS[i]
|
|
118
|
+
initChild('Chip__`)
|
|
119
|
+
expect(js).toContain(`{ const __e = qsa(childScope, '[bf="^s3"]'); if (__e) __e.addEventListener('click', () => setPicked(id + i)) }`)
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
test('the direct-child row keeps its delegated dispatcher (control)', () => {
|
|
123
|
+
const js = clientJsOf(
|
|
124
|
+
'{rows().map(o => (<div key={o.id}><button onClick={() => pick(o.id)}>{o.label}</button></div>))}',
|
|
125
|
+
)
|
|
126
|
+
expect(js).toContain(`const s3El = target.closest('[bf="s3"]')`)
|
|
127
|
+
expect(js).toContain(';(() => pick(o.id))(__bfEvt)')
|
|
128
|
+
})
|
|
129
|
+
})
|