@barefootjs/jsx 0.40.0 → 0.40.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts +42 -0
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -0
- package/dist/adapters/attr-plan.d.ts.map +1 -1
- package/dist/adapters/boolean-carrier.d.ts +31 -0
- package/dist/adapters/boolean-carrier.d.ts.map +1 -0
- package/dist/adapters/boolean-result.d.ts +32 -10
- package/dist/adapters/boolean-result.d.ts.map +1 -1
- package/dist/adapters/brace-delimiter-text.d.ts +18 -0
- package/dist/adapters/brace-delimiter-text.d.ts.map +1 -0
- package/dist/adapters/callback-method.d.ts +38 -5
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/component-props.d.ts +22 -7
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +28 -1
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +39 -1
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/loop-bound-names.d.ts +9 -1
- package/dist/adapters/loop-bound-names.d.ts.map +1 -1
- package/dist/adapters/loop-row-type.d.ts +52 -0
- package/dist/adapters/loop-row-type.d.ts.map +1 -0
- package/dist/adapters/nil-reading.d.ts +68 -0
- package/dist/adapters/nil-reading.d.ts.map +1 -0
- package/dist/adapters/non-primitive-equality.d.ts +33 -0
- package/dist/adapters/non-primitive-equality.d.ts.map +1 -0
- package/dist/adapters/number-value.d.ts +45 -0
- package/dist/adapters/number-value.d.ts.map +1 -0
- package/dist/adapters/operand-typing.d.ts +126 -0
- package/dist/adapters/operand-typing.d.ts.map +1 -0
- package/dist/adapters/parsed-expr-emitter.d.ts +70 -12
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/adapters/primitive-plan.d.ts +291 -0
- package/dist/adapters/primitive-plan.d.ts.map +1 -0
- package/dist/analyzer-context.d.ts +11 -0
- package/dist/analyzer-context.d.ts.map +1 -1
- package/dist/analyzer.d.ts +34 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +63 -8
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/compiler.d.ts.map +1 -1
- package/dist/event-handler-name.d.ts +12 -0
- package/dist/event-handler-name.d.ts.map +1 -1
- package/dist/expression-parser.d.ts +8 -0
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/index.d.ts +24 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5671 -2586
- package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
- package/dist/ir-to-client-js/child-props-object.d.ts +64 -0
- package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -0
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts +29 -0
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-lazy-row.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/build-lazy-row.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-reactive-effects.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/inner-loop.d.ts +5 -4
- package/dist/ir-to-client-js/control-flow/plan/inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +15 -0
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/reactive-effects.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/reactive-effects.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +22 -0
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/lazy-row.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts +3 -2
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/imports.d.ts +2 -2
- package/dist/ir-to-client-js/imports.d.ts.map +1 -1
- package/dist/ir-to-client-js/index.d.ts +2 -6
- package/dist/ir-to-client-js/index.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +37 -19
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
- package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
- package/dist/ir-to-client-js/safe-html.d.ts +9 -3
- package/dist/ir-to-client-js/safe-html.d.ts.map +1 -1
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +3 -2
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/types.d.ts +30 -0
- package/dist/ir-to-client-js/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +8 -0
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts +1 -0
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +95 -10
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/prop-rewrite.d.ts +7 -0
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-binding.d.ts +27 -3
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/props-object-whole-read.d.ts +15 -0
- package/dist/props-object-whole-read.d.ts.map +1 -0
- package/dist/rich-type-refusal.d.ts.map +1 -1
- package/dist/scope/binding-scope.d.ts +100 -4
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/scope/lexical-scope-walk.d.ts +3 -2
- package/dist/scope/lexical-scope-walk.d.ts.map +1 -1
- package/dist/scope/root-prop-alias.d.ts +16 -0
- package/dist/scope/root-prop-alias.d.ts.map +1 -1
- package/dist/signal-prop-name-collision.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts +4 -2
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +14 -0
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/strip-types.d.ts +13 -0
- package/dist/strip-types.d.ts.map +1 -1
- package/dist/template-parts.d.ts.map +1 -1
- package/dist/template-position-walk.d.ts +21 -3
- package/dist/template-position-walk.d.ts.map +1 -1
- package/dist/types.d.ts +120 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +3 -3
- package/src/__tests__/binding-scope-ratchet.test.ts +12 -2
- package/src/__tests__/binding-scope.test.ts +89 -1
- package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -0
- package/src/__tests__/boolean-carrier.test.ts +74 -0
- package/src/__tests__/boolean-result.test.ts +22 -5
- package/src/__tests__/brace-delimiter-text.test.ts +26 -0
- package/src/__tests__/callback-method.test.ts +62 -2
- package/src/__tests__/child-components-in-map.test.ts +1 -1
- package/src/__tests__/component-props.test.ts +14 -0
- package/src/__tests__/computed-const.test.ts +188 -0
- package/src/__tests__/emit-to-string-call.test.ts +2 -1
- package/src/__tests__/expression-parser.test.ts +24 -0
- package/src/__tests__/hydration-payload-analysis.test.ts +166 -0
- package/src/__tests__/issue-2724-prop-alias-loop-not-static.test.ts +7 -7
- package/src/__tests__/issue-2754-rest-spread-needs-slot.test.ts +5 -2
- package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +1 -1
- package/src/__tests__/issue-2835-props-type-member-name-collision.test.ts +4 -4
- package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +1 -1
- package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +143 -0
- package/src/__tests__/issue-3713-props-spread-loop-row.test.ts +65 -0
- package/src/__tests__/lookup-literal-const.test.ts +9 -3
- package/src/__tests__/loop-index-reactivity.test.ts +2 -2
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +1 -1
- package/src/__tests__/loop-plan.test.ts +1 -1
- package/src/__tests__/loop-row-const-initializer-shadow.test.ts +146 -0
- package/src/__tests__/loop-row-prop-read.test.ts +111 -0
- package/src/__tests__/loop-row-type.test.ts +64 -0
- package/src/__tests__/lowering-value-position.test.ts +4 -0
- package/src/__tests__/memo-body-expr.test.ts +137 -0
- package/src/__tests__/module-const-destructure-default.test.ts +156 -0
- package/src/__tests__/nested-loop-index-param.test.ts +1 -1
- package/src/__tests__/nested-loop-reactive-attrs.test.ts +2 -2
- package/src/__tests__/nested-row-shadowing.test.ts +2 -2
- package/src/__tests__/nil-reading.test.ts +77 -0
- package/src/__tests__/nullable-signal-attr.test.ts +231 -2
- package/src/__tests__/primitive-plan.test.ts +416 -0
- package/src/__tests__/profile-binding-ids.test.ts +14 -0
- package/src/__tests__/profile-loop-binding-ids.test.ts +2 -2
- package/src/__tests__/profile-nested-binding-ids.test.ts +2 -2
- package/src/__tests__/props-object-whole-read.test.ts +40 -0
- package/src/__tests__/reactive-attrs-in-map.test.ts +1 -1
- package/src/__tests__/reactive-factory-inlining.test.ts +4 -1
- package/src/__tests__/rest-binding-gaps.test.ts +192 -0
- package/src/__tests__/root-prop-alias.test.ts +79 -1
- package/src/__tests__/signal-prop-name-collision.test.ts +102 -2
- package/src/__tests__/source-file-cache.test.ts +105 -0
- package/src/__tests__/ssr-seed-plan.test.ts +43 -0
- package/src/__tests__/static-loop-csr-materialize.test.ts +2 -2
- package/src/__tests__/static-record-literal-array.test.ts +171 -0
- package/src/__tests__/string-concat-identifier.test.ts +40 -1
- package/src/__tests__/template-parts-literal-boundary.test.ts +19 -0
- package/src/adapters/ambiguous-nil-arithmetic.ts +103 -0
- package/src/adapters/attr-plan.ts +6 -2
- package/src/adapters/boolean-carrier.ts +76 -0
- package/src/adapters/boolean-result.ts +72 -66
- package/src/adapters/brace-delimiter-text.ts +25 -0
- package/src/adapters/callback-method.ts +151 -27
- package/src/adapters/component-props.ts +28 -8
- package/src/adapters/computed-const.ts +443 -10
- package/src/adapters/dsl-template-adapter.ts +79 -2
- package/src/adapters/loop-bound-names.ts +9 -1
- package/src/adapters/loop-plan.ts +1 -1
- package/src/adapters/loop-row-type.ts +139 -0
- package/src/adapters/nil-reading.ts +297 -0
- package/src/adapters/non-primitive-equality.ts +92 -0
- package/src/adapters/number-value.ts +108 -0
- package/src/adapters/operand-typing.ts +522 -0
- package/src/adapters/parsed-expr-emitter.ts +154 -19
- package/src/adapters/primitive-plan.ts +696 -0
- package/src/analyzer-context.ts +14 -0
- package/src/analyzer.ts +692 -85
- package/src/augment-inherited-props.ts +108 -10
- package/src/compiler.ts +17 -0
- package/src/event-handler-name.ts +15 -0
- package/src/expression-parser.ts +74 -2
- package/src/index.ts +45 -9
- package/src/ir-to-client-js/build-references.ts +69 -0
- package/src/ir-to-client-js/child-props-object.ts +106 -0
- package/src/ir-to-client-js/collect-elements.ts +100 -67
- package/src/ir-to-client-js/compute-inlinability.ts +1 -1
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +15 -1
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +85 -5
- package/src/ir-to-client-js/control-flow/plan/build-lazy-row.ts +3 -0
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +30 -28
- package/src/ir-to-client-js/control-flow/plan/build-reactive-effects.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/inner-loop.ts +5 -4
- package/src/ir-to-client-js/control-flow/plan/loop-child-arm.ts +2 -0
- package/src/ir-to-client-js/control-flow/plan/loop.ts +12 -0
- package/src/ir-to-client-js/control-flow/plan/reactive-effects.ts +2 -0
- package/src/ir-to-client-js/control-flow/shared.ts +23 -24
- package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +29 -0
- package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +8 -2
- package/src/ir-to-client-js/control-flow/stringify/event-listener.ts +8 -1
- package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +8 -7
- package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +8 -7
- package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +4 -4
- package/src/ir-to-client-js/control-flow/stringify/loop.ts +3 -3
- package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +6 -6
- package/src/ir-to-client-js/emit-reactive.ts +1 -1
- package/src/ir-to-client-js/html-template.ts +95 -38
- package/src/ir-to-client-js/imports.ts +1 -1
- package/src/ir-to-client-js/index.ts +53 -11
- package/src/ir-to-client-js/plan/build-static-array-child-init.ts +42 -15
- package/src/ir-to-client-js/plan/static-array-child-init.ts +34 -19
- package/src/ir-to-client-js/prop-handling.ts +22 -3
- package/src/ir-to-client-js/reactivity.ts +14 -0
- package/src/ir-to-client-js/safe-html.ts +11 -4
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +30 -12
- package/src/ir-to-client-js/types.ts +23 -0
- package/src/ir-to-client-js/utils.ts +15 -1
- package/src/jsx-to-ir.ts +510 -58
- package/src/nullable-signal-attr.ts +366 -28
- package/src/prop-rewrite.ts +30 -6
- package/src/props-binding.ts +54 -3
- package/src/props-object-whole-read.ts +87 -0
- package/src/rich-type-refusal.ts +2 -1
- package/src/scope/binding-scope.ts +203 -11
- package/src/scope/lexical-scope-walk.ts +4 -3
- package/src/scope/root-prop-alias.ts +23 -0
- package/src/signal-prop-name-collision.ts +113 -22
- package/src/ssr-defaults.ts +10 -4
- package/src/ssr-seed-plan.ts +82 -17
- package/src/strip-types.ts +31 -3
- package/src/template-parts.ts +21 -1
- package/src/template-position-walk.ts +57 -28
- package/src/types.ts +117 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rest-binding and body-destructure shapes that used to compile without a
|
|
3
|
+
* diagnostic and render wrong (#3851, #3854). Each is either decided in
|
|
4
|
+
* Phase 1 so every backend renders it as JS does, or refused with a
|
|
5
|
+
* diagnostic. The rendered output is pinned by the conformance fixtures
|
|
6
|
+
* (`rest-spread-explicit-before-closed-rest`,
|
|
7
|
+
* `rest-binding-shadowed-by-loop-row`, `body-rest-member-read`, and the
|
|
8
|
+
* refused `body-destructure-*` / `body-rest-taken-key-read`); these tests
|
|
9
|
+
* pin the Phase 1 decisions and the refusals of the shapes no fixture
|
|
10
|
+
* renders.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { describe, test, expect } from 'bun:test'
|
|
14
|
+
import { compileJSX } from '../compiler'
|
|
15
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
16
|
+
import type { ComponentIR, IRNode } from '../types'
|
|
17
|
+
|
|
18
|
+
function compile(source: string) {
|
|
19
|
+
return compileJSX(source, 'Component.tsx', { adapter: new TestAdapter(), outputIR: true })
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function codes(source: string): string[] {
|
|
23
|
+
return compile(source).errors.filter(e => e.severity === 'error').map(e => e.code)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function irOf(source: string): ComponentIR {
|
|
27
|
+
const file = compile(source).files.find(f => f.type === 'ir')
|
|
28
|
+
if (!file) throw new Error('no IR')
|
|
29
|
+
return JSON.parse(file.content) as ComponentIR
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Every element of `node`'s tree with tag `tag`. */
|
|
33
|
+
function elements(node: IRNode, tag: string): Array<Extract<IRNode, { type: 'element' }>> {
|
|
34
|
+
const out: Array<Extract<IRNode, { type: 'element' }>> = []
|
|
35
|
+
const visit = (n: unknown): void => {
|
|
36
|
+
if (Array.isArray(n)) return n.forEach(visit)
|
|
37
|
+
if (n === null || typeof n !== 'object') return
|
|
38
|
+
const rec = n as { type?: string; tag?: string }
|
|
39
|
+
if (rec.type === 'element' && rec.tag === tag) out.push(n as Extract<IRNode, { type: 'element' }>)
|
|
40
|
+
for (const v of Object.values(n)) visit(v)
|
|
41
|
+
}
|
|
42
|
+
visit(node)
|
|
43
|
+
return out
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function attrExpr(el: Extract<IRNode, { type: 'element' }>, name: string): string[] {
|
|
47
|
+
return el.attrs.filter(a => a.name === name).map(a => (a.value.kind === 'expression' ? a.value.expr : a.value.kind))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
describe('an attribute before a closed rest spread (#3851)', () => {
|
|
51
|
+
test('the spread key falls back to the earlier value, which is dropped', () => {
|
|
52
|
+
const ir = irOf(`
|
|
53
|
+
export function P({ label, ...rest }: { label: string; title?: string }) {
|
|
54
|
+
return <p title="fixed" {...rest}>{label}</p>
|
|
55
|
+
}
|
|
56
|
+
`)
|
|
57
|
+
const [p] = elements(ir.root, 'p')
|
|
58
|
+
expect(attrExpr(p, 'title')).toEqual(['rest.title ?? ("fixed")'])
|
|
59
|
+
expect(p.attrs.find(a => a.name === 'title')?.restFallback).toBe('"fixed"')
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
test('a later attribute still wins over the spread, with no fallback', () => {
|
|
63
|
+
const ir = irOf(`
|
|
64
|
+
export function P({ label, ...rest }: { label: string; title?: string }) {
|
|
65
|
+
return <p {...rest} title="fixed">{label}</p>
|
|
66
|
+
}
|
|
67
|
+
`)
|
|
68
|
+
const [p] = elements(ir.root, 'p')
|
|
69
|
+
expect(attrExpr(p, 'title')).toEqual(['rest.title', 'literal'])
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test('a member read before the spread is a fallback', () => {
|
|
73
|
+
expect(codes(`
|
|
74
|
+
export function P({ label, row, ...rest }: { label: string; row: { t: string }; title?: string }) {
|
|
75
|
+
return <p title={row.t} {...rest}>{label}</p>
|
|
76
|
+
}
|
|
77
|
+
`)).toEqual([])
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('a value that may have an effect is refused (BF021)', () => {
|
|
81
|
+
expect(codes(`
|
|
82
|
+
function mark(): string { return 'm' }
|
|
83
|
+
export function P({ label, ...rest }: { label: string; title?: string }) {
|
|
84
|
+
return <p title={mark()} {...rest}>{label}</p>
|
|
85
|
+
}
|
|
86
|
+
`)).toEqual(['BF021'])
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
test('a JSX element value is refused (BF021)', () => {
|
|
90
|
+
expect(codes(`
|
|
91
|
+
import { Card } from './card'
|
|
92
|
+
export function P({ ...rest }: { header?: unknown }) {
|
|
93
|
+
return <Card header={<b>h</b>} {...rest} />
|
|
94
|
+
}
|
|
95
|
+
`)).toEqual(['BF021'])
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
describe('a loop row named like the rest binding (#3851)', () => {
|
|
100
|
+
test('a spread of the row stays a spread of the row', () => {
|
|
101
|
+
const ir = irOf(`
|
|
102
|
+
export function P({ rows, ...rest }: { rows: { id: string }[]; tone?: string }) {
|
|
103
|
+
return <ul data-tone={rest.tone}>{rows.map(rest => <li key={rest.id} {...rest}>{rest.id}</li>)}</ul>
|
|
104
|
+
}
|
|
105
|
+
`)
|
|
106
|
+
const [li] = elements(ir.root, 'li')
|
|
107
|
+
const spread = li.attrs.find(a => a.name === '...')
|
|
108
|
+
expect(spread?.value.kind).toBe('spread')
|
|
109
|
+
expect(spread?.value.kind === 'spread' && spread.value.callerBag).toBeFalsy()
|
|
110
|
+
expect(li.attrs.some(a => a.name === 'tone')).toBe(false)
|
|
111
|
+
})
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
describe('a member read of a props-object body rest (#3854)', () => {
|
|
115
|
+
test('reads the props object', () => {
|
|
116
|
+
const ir = irOf(`
|
|
117
|
+
export function C(props: { label: string; id?: string }) {
|
|
118
|
+
const { label, ...rest } = props
|
|
119
|
+
return <i data-x={rest.id}>{label}</i>
|
|
120
|
+
}
|
|
121
|
+
`)
|
|
122
|
+
const [i] = elements(ir.root, 'i')
|
|
123
|
+
expect(attrExpr(i, 'data-x')).toEqual(['props.id'])
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
test('a row shadowing the rest keeps its own read', () => {
|
|
127
|
+
const ir = irOf(`
|
|
128
|
+
export function C(props: { label: string; id?: string; rows: { id: string }[] }) {
|
|
129
|
+
const { label, ...rest } = props
|
|
130
|
+
return <ul>{props.rows.map(rest => <li key={rest.id} data-x={rest.id}>{label}</li>)}</ul>
|
|
131
|
+
}
|
|
132
|
+
`)
|
|
133
|
+
const [li] = elements(ir.root, 'li')
|
|
134
|
+
expect(attrExpr(li, 'data-x')).toEqual(['rest.id'])
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
test('a key the props type does not declare stays a read of the rest', () => {
|
|
138
|
+
const ir = irOf(`
|
|
139
|
+
export function C(props: { label: string; [key: string]: unknown }) {
|
|
140
|
+
const { label, ...rest } = props
|
|
141
|
+
return <i data-x={rest.other}>{label}</i>
|
|
142
|
+
}
|
|
143
|
+
`)
|
|
144
|
+
const [i] = elements(ir.root, 'i')
|
|
145
|
+
expect(attrExpr(i, 'data-x')).toEqual(['rest.other'])
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
test('a read or spread under a row that shadows the props name is refused (BF101)', () => {
|
|
149
|
+
const src = (use: string) => `
|
|
150
|
+
export function C(props: { id?: string; rows: { key: string }[] }) {
|
|
151
|
+
const { rows, ...rest } = props
|
|
152
|
+
return <ul>{rows.map(props => <li key={props.key} ${use} />)}</ul>
|
|
153
|
+
}
|
|
154
|
+
`
|
|
155
|
+
expect(codes(src('data-x={rest.id}'))).toEqual(['BF101'])
|
|
156
|
+
expect(codes(src('{...rest}'))).toEqual(['BF101'])
|
|
157
|
+
})
|
|
158
|
+
|
|
159
|
+
test('a read of a taken key is refused (BF101)', () => {
|
|
160
|
+
expect(codes(`
|
|
161
|
+
export function C(props: { label: string; id?: string }) {
|
|
162
|
+
const { label, ...rest } = props
|
|
163
|
+
return <i>{label}{rest.label}</i>
|
|
164
|
+
}
|
|
165
|
+
`)).toEqual(['BF101'])
|
|
166
|
+
})
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
describe('a component-body destructure with no const form (#3854)', () => {
|
|
170
|
+
const wrap = (stmt: string, use: string) => `
|
|
171
|
+
const conf = { a: 'A', b: 'B' }
|
|
172
|
+
function getConf() { return conf }
|
|
173
|
+
export function C(props: { b: string; nested: { a: string } }) {
|
|
174
|
+
${stmt}
|
|
175
|
+
return <i>{${use}}</i>
|
|
176
|
+
}
|
|
177
|
+
`
|
|
178
|
+
|
|
179
|
+
test.each([
|
|
180
|
+
['a rest element', 'const { a, ...more } = conf', 'more.b'],
|
|
181
|
+
['a non-literal default', 'const { a = props.b } = conf', 'a'],
|
|
182
|
+
['a call source', 'const { a } = getConf()', 'a'],
|
|
183
|
+
['a member source', 'const { a } = props.nested', 'a'],
|
|
184
|
+
['a let destructure', 'let { a } = conf', 'a'],
|
|
185
|
+
])('%s is refused (BF101)', (_name, stmt, use) => {
|
|
186
|
+
expect(codes(wrap(stmt, use))).toEqual(['BF101'])
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
test('a const destructure of a const object still compiles', () => {
|
|
190
|
+
expect(codes(wrap(`const { a, b: bee = 'x' } = conf`, 'a + bee'))).toEqual([])
|
|
191
|
+
})
|
|
192
|
+
})
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { describe, test, expect } from 'bun:test'
|
|
2
2
|
import { BindingScope } from '../scope/binding-scope'
|
|
3
|
-
import { rootPropAliasNames, rootPropAliasesForLoop, rootPropReadName } from '../scope/root-prop-alias'
|
|
3
|
+
import { isRootPropsObject, rootPropAliasNames, rootPropAliasesForLoop, rootPropReadName } from '../scope/root-prop-alias'
|
|
4
|
+
import type { ParsedExpr } from '../expression-parser'
|
|
4
5
|
import type { ComponentIR } from '../types'
|
|
5
6
|
|
|
6
7
|
function irWith(opts: { localConstants?: string[]; loopParam?: string } = {}): ComponentIR {
|
|
@@ -60,3 +61,80 @@ describe('root-prop aliases (#3314)', () => {
|
|
|
60
61
|
expect(new Set(both.values()).size).toBe(3)
|
|
61
62
|
})
|
|
62
63
|
})
|
|
64
|
+
|
|
65
|
+
describe('the props object a binding named props shadows (#3693)', () => {
|
|
66
|
+
const props: ParsedExpr = { kind: 'identifier', name: 'props' }
|
|
67
|
+
const isBoundIn = (scope: BindingScope) => (name: string) => scope.isBound(name)
|
|
68
|
+
|
|
69
|
+
test('props is the component props object until a binding of that name shadows it', () => {
|
|
70
|
+
expect(isRootPropsObject(props, isBoundIn(BindingScope.EMPTY), 'props')).toBe(true)
|
|
71
|
+
expect(isRootPropsObject(props, isBoundIn(BindingScope.EMPTY.enterLoopRow({ param: 'row' })), 'props')).toBe(true)
|
|
72
|
+
expect(isRootPropsObject(props, isBoundIn(BindingScope.EMPTY.enterLoopRow({ param: 'props' })), 'props')).toBe(false)
|
|
73
|
+
const destructured = BindingScope.EMPTY.enterLoopRow({ param: '{ props }', paramBindings: [{ name: 'props' }] })
|
|
74
|
+
expect(isRootPropsObject(props, isBoundIn(destructured), 'props')).toBe(false)
|
|
75
|
+
const preamble = BindingScope.EMPTY.enterLoopRow({ param: 'row', preamble: { declaredNames: ['props'] } })
|
|
76
|
+
expect(isRootPropsObject(props, isBoundIn(preamble), 'props')).toBe(false)
|
|
77
|
+
expect(isRootPropsObject(props, isBoundIn(BindingScope.EMPTY.enterCallback(['props'])), 'props')).toBe(false)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('only the bare identifier props reads the props object', () => {
|
|
81
|
+
expect(isRootPropsObject({ kind: 'identifier', name: 'row' }, () => false, 'props')).toBe(false)
|
|
82
|
+
expect(isRootPropsObject({ kind: 'member', object: props, property: 'a', computed: false, optional: false }, () => false, 'props')).toBe(false)
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test('a props.<name> read has no component typing while props is shadowed', () => {
|
|
86
|
+
const outer = BindingScope.EMPTY
|
|
87
|
+
const row = outer.enterLoopRow({ param: 'props' })
|
|
88
|
+
expect(outer.shadowsPropsObject()).toBe(false)
|
|
89
|
+
expect(row.shadowsPropsObject()).toBe(true)
|
|
90
|
+
expect(outer.isStringValueName('label', () => true, true)).toBe(true)
|
|
91
|
+
expect(row.isStringValueName('label', () => true, true)).toBe(false)
|
|
92
|
+
// A bare `label` is untouched: only the `props.<name>` form changes.
|
|
93
|
+
expect(row.isStringValueName('label', () => true)).toBe(true)
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
describe('a props object parameter not named props (#3729)', () => {
|
|
98
|
+
const p: ParsedExpr = { kind: 'identifier', name: 'p' }
|
|
99
|
+
const props: ParsedExpr = { kind: 'identifier', name: 'props' }
|
|
100
|
+
const component = BindingScope.forComponent('p')
|
|
101
|
+
const isBoundIn = (scope: BindingScope) => (name: string) => scope.isBound(name)
|
|
102
|
+
|
|
103
|
+
test('the props parameter name is the props object; props is not', () => {
|
|
104
|
+
expect(component.propsObject).toBe('p')
|
|
105
|
+
expect(isRootPropsObject(p, isBoundIn(component), component.propsObject)).toBe(true)
|
|
106
|
+
expect(isRootPropsObject(props, isBoundIn(component), component.propsObject)).toBe(false)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('a destructured or props-named component keeps props', () => {
|
|
110
|
+
expect(BindingScope.forComponent(null).propsObject).toBe('props')
|
|
111
|
+
expect(BindingScope.forComponent('props').propsObject).toBe('props')
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
test('child scopes keep the name, and a binding of it shadows the props object', () => {
|
|
115
|
+
const row = component.enterLoopRow({ param: 'p' })
|
|
116
|
+
expect(row.propsObject).toBe('p')
|
|
117
|
+
expect(isRootPropsObject(p, isBoundIn(row), row.propsObject)).toBe(false)
|
|
118
|
+
expect(row.shadowsPropsObject()).toBe(true)
|
|
119
|
+
expect(component.enterLoopRow({ param: 'props' }).shadowsPropsObject()).toBe(false)
|
|
120
|
+
expect(component.enterCallback(['x']).propsObject).toBe('p')
|
|
121
|
+
// The typing callers flag only the literal receiver `props.<name>`: in a
|
|
122
|
+
// component whose props object is `p`, an unbound `props` is not it.
|
|
123
|
+
expect(component.isStringValueName('label', () => true, true)).toBe(false)
|
|
124
|
+
expect(row.isStringValueName('label', () => true, true)).toBe(false)
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
test('a row binding named props keeps its own member types when the props object is named p', () => {
|
|
128
|
+
const fields = {
|
|
129
|
+
Row: [
|
|
130
|
+
{ name: 'value', type: { primitive: 'number' }, optional: false },
|
|
131
|
+
{ name: 'label', type: { primitive: 'string' }, optional: false },
|
|
132
|
+
],
|
|
133
|
+
}
|
|
134
|
+
const named = (name: string) => fields[name as keyof typeof fields]
|
|
135
|
+
const row = component.enterLoopRow({ param: 'props', rowType: { raw: 'Row' } })
|
|
136
|
+
// `outer` would type every component name as a string; the row binding wins.
|
|
137
|
+
expect(row.isStringValueName('value', () => true, true, [], named)).toBe(false)
|
|
138
|
+
expect(row.isStringValueName('label', () => false, true, [], named)).toBe(true)
|
|
139
|
+
})
|
|
140
|
+
})
|
|
@@ -73,7 +73,7 @@ test('a signal seeded from another prop, a memo, and a case-folded name are sepa
|
|
|
73
73
|
expect(ctx.sourceFile.text).toContain('{props.label}{props.count}{props.title}')
|
|
74
74
|
})
|
|
75
75
|
|
|
76
|
-
test('a memo derived from its own
|
|
76
|
+
test('a memo derived from its own prop through a const is separated: the const reads the raw prop', () => {
|
|
77
77
|
const ctx = analyzeComponent(`
|
|
78
78
|
'use client'
|
|
79
79
|
import { createMemo } from '@barefootjs/client'
|
|
@@ -83,7 +83,107 @@ test('a memo derived from its own same-named prop keeps its name', () => {
|
|
|
83
83
|
return <p>{label()}</p>
|
|
84
84
|
}
|
|
85
85
|
`, 'test.tsx')
|
|
86
|
-
expect(ctx.memos.map(m => m.name)).toEqual(['
|
|
86
|
+
expect(ctx.memos.map(m => m.name)).toEqual(['bfMemo_label'])
|
|
87
|
+
expect(ctx.sourceFile.text).toContain('const fallback = props.label')
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('a binding derived from its own prop is separated when the raw prop is read elsewhere', () => {
|
|
91
|
+
const ctx = analyzeComponent(`
|
|
92
|
+
'use client'
|
|
93
|
+
import { createSignal, createMemo } from '@barefootjs/client'
|
|
94
|
+
export function App(props: { label?: string; note?: string; other?: string }) {
|
|
95
|
+
const [label] = createSignal(props.label ?? 'D')
|
|
96
|
+
const note = createMemo(() => (props.note ?? 'N') + '!')
|
|
97
|
+
const [other] = createSignal(props.other ?? 'O')
|
|
98
|
+
const shout = () => props.note
|
|
99
|
+
return <p title={props.label}>{label()}{note()}{other()}{shout()}</p>
|
|
100
|
+
}
|
|
101
|
+
`, 'test.tsx')
|
|
102
|
+
// `other` is read raw only inside its own initializer.
|
|
103
|
+
expect(ctx.signals.map(s => s.getter)).toEqual(['bfSignal_label', 'other'])
|
|
104
|
+
expect(ctx.memos.map(m => m.name)).toEqual(['bfMemo_note'])
|
|
105
|
+
expect(ctx.sourceFile.text).toContain('createSignal(props.label ?? \'D\')')
|
|
106
|
+
expect(ctx.sourceFile.text).toContain('<p title={props.label}>{bfSignal_label()}{bfMemo_note()}{other()}{shout()}</p>')
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('an untyped props object names its props by the reads off it', () => {
|
|
110
|
+
const ctx = analyzeComponent(`
|
|
111
|
+
'use client'
|
|
112
|
+
import { createSignal, createMemo } from '@barefootjs/client'
|
|
113
|
+
export function App(props) {
|
|
114
|
+
const [label] = createSignal(props.label ?? 'D')
|
|
115
|
+
const note = createMemo(() => label() + '!')
|
|
116
|
+
return <p>{label()}{note()}{props.label}</p>
|
|
117
|
+
}
|
|
118
|
+
`, 'test.tsx')
|
|
119
|
+
expect(ctx.signals.map(s => s.getter)).toEqual(['bfSignal_label'])
|
|
120
|
+
expect(ctx.memos.map(m => m.name)).toEqual(['note'])
|
|
121
|
+
expect(ctx.sourceFile.text).toContain('{bfSignal_label()}{note()}{props.label}')
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
test('a read off a callback parameter that shadows props is no raw prop read', () => {
|
|
125
|
+
const ctx = analyzeComponent(`
|
|
126
|
+
'use client'
|
|
127
|
+
import { createSignal } from '@barefootjs/client'
|
|
128
|
+
export function App(props: { label?: string; rows?: { label: string }[] }) {
|
|
129
|
+
const [label] = createSignal(props.label ?? 'D')
|
|
130
|
+
return <ul>{label()}{(props.rows ?? []).map(props => <li>{props.label}</li>)}</ul>
|
|
131
|
+
}
|
|
132
|
+
`, 'test.tsx')
|
|
133
|
+
expect(ctx.signals.map(s => s.getter)).toEqual(['label'])
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
test('a literal element-access read of the raw prop separates the binding', () => {
|
|
137
|
+
const ctx = analyzeComponent(`
|
|
138
|
+
'use client'
|
|
139
|
+
import { createSignal } from '@barefootjs/client'
|
|
140
|
+
export function App(props: { a?: string; b?: string; c?: string; d?: string }) {
|
|
141
|
+
const [a] = createSignal(props.a ?? 'A')
|
|
142
|
+
const [b] = createSignal(props.b ?? 'B')
|
|
143
|
+
const [c] = createSignal(props.c ?? 'C')
|
|
144
|
+
const [d] = createSignal(props.d ?? 'D')
|
|
145
|
+
return <p>{a()}{b()}{c()}{d()}{props['a']}{props?.['b']}{props[\`c\`]}{props[0]}</p>
|
|
146
|
+
}
|
|
147
|
+
`, 'test.tsx')
|
|
148
|
+
// A numeric key names no identifier prop, so `d` keeps its name.
|
|
149
|
+
expect(ctx.signals.map(s => s.getter)).toEqual(['bfSignal_a', 'bfSignal_b', 'bfSignal_c', 'd'])
|
|
150
|
+
expect(ctx.sourceFile.text).toContain("{props['a']}{props?.['b']}")
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
test('a computed-key read may read any prop, so it separates every self-derived binding', () => {
|
|
154
|
+
const ctx = analyzeComponent(`
|
|
155
|
+
'use client'
|
|
156
|
+
import { createSignal } from '@barefootjs/client'
|
|
157
|
+
export function App(props: { a?: string; k: 'a' | 'b' }) {
|
|
158
|
+
const [a] = createSignal(props.a ?? 'A')
|
|
159
|
+
return <p>{a()}{props[props.k]}</p>
|
|
160
|
+
}
|
|
161
|
+
`, 'test.tsx')
|
|
162
|
+
expect(ctx.signals.map(s => s.getter)).toEqual(['bfSignal_a'])
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
test('an untyped props object names its props by literal element-access reads too', () => {
|
|
166
|
+
const ctx = analyzeComponent(`
|
|
167
|
+
'use client'
|
|
168
|
+
import { createSignal } from '@barefootjs/client'
|
|
169
|
+
export function App(props) {
|
|
170
|
+
const [label] = createSignal(props.label ?? 'D')
|
|
171
|
+
return <p>{label()}{props['label']}</p>
|
|
172
|
+
}
|
|
173
|
+
`, 'test.tsx')
|
|
174
|
+
expect(ctx.signals.map(s => s.getter)).toEqual(['bfSignal_label'])
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
test('an element-access read off a callback parameter that shadows props is no raw prop read', () => {
|
|
178
|
+
const ctx = analyzeComponent(`
|
|
179
|
+
'use client'
|
|
180
|
+
import { createSignal } from '@barefootjs/client'
|
|
181
|
+
export function App(props: { label?: string; rows?: { label: string }[] }) {
|
|
182
|
+
const [label] = createSignal(props.label ?? 'D')
|
|
183
|
+
return <ul>{label()}{(props.rows ?? []).map(props => <li>{props['label']}{props[0]}</li>)}</ul>
|
|
184
|
+
}
|
|
185
|
+
`, 'test.tsx')
|
|
186
|
+
expect(ctx.signals.map(s => s.getter)).toEqual(['label'])
|
|
87
187
|
})
|
|
88
188
|
|
|
89
189
|
test('signed literal seeds are separated without a type-based program', () => {
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `createAnchoredProgram` shares parsed SourceFiles between the Programs it
|
|
3
|
+
* builds (#3666): the conformance harness builds one per compile, and
|
|
4
|
+
* re-parsing `lib.*.d.ts` and `@barefootjs/client`'s declarations for each
|
|
5
|
+
* was most of an adapter suite's run time.
|
|
6
|
+
*
|
|
7
|
+
* Pinned here: a declaration file is parsed once across Programs, a
|
|
8
|
+
* different source under the same virtual name is parsed again (never
|
|
9
|
+
* served stale) and then kept alongside the first, every Program is still
|
|
10
|
+
* new, and a Program built from cached files still types a
|
|
11
|
+
* `@barefootjs/client` import with its real declarations.
|
|
12
|
+
*/
|
|
13
|
+
import { describe, expect, test } from 'bun:test'
|
|
14
|
+
import { resolve } from 'node:path'
|
|
15
|
+
import { createAnchoredProgram, SourceFileCache, type SourceFileParse } from '../analyzer'
|
|
16
|
+
|
|
17
|
+
const ANCHOR = resolve(import.meta.dir, '__virtual__')
|
|
18
|
+
const DISK: SourceFileParse = { setParentNodes: false }
|
|
19
|
+
// `createSignal`'s getter type, as `@barefootjs/client`'s declarations spell it.
|
|
20
|
+
const GETTER_TYPE = 'Reactive<() => number>'
|
|
21
|
+
|
|
22
|
+
// The `.map(` puts the source behind `needsTypeBasedDetection`'s gate, so
|
|
23
|
+
// `createAnchoredProgram` builds a Program for it.
|
|
24
|
+
const counterSource = (initial: string) => `
|
|
25
|
+
'use client'
|
|
26
|
+
import { createSignal } from '@barefootjs/client'
|
|
27
|
+
export function Counter() {
|
|
28
|
+
const [count] = createSignal(${initial})
|
|
29
|
+
return <ul>{[count()].map((c) => <li>{c}</li>)}</ul>
|
|
30
|
+
}
|
|
31
|
+
`
|
|
32
|
+
|
|
33
|
+
function libFile(program: NonNullable<ReturnType<typeof createAnchoredProgram>>) {
|
|
34
|
+
const lib = program.getSourceFiles().find((f) => /lib\.es5\.d\.ts$/.test(f.fileName))
|
|
35
|
+
if (!lib) throw new Error('lib.es5.d.ts not in the Program')
|
|
36
|
+
return lib
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
describe('createAnchoredProgram SourceFile sharing', () => {
|
|
40
|
+
test('a declaration file is the same SourceFile object in consecutive Programs', () => {
|
|
41
|
+
const a = createAnchoredProgram(counterSource('0'), 'Counter.tsx', ANCHOR)!
|
|
42
|
+
const b = createAnchoredProgram(counterSource('1'), 'Other.tsx', ANCHOR)!
|
|
43
|
+
expect(libFile(b)).toBe(libFile(a))
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('a different source under the same virtual name is parsed again', () => {
|
|
47
|
+
const a = createAnchoredProgram(counterSource('0'), 'Counter.tsx', ANCHOR)!
|
|
48
|
+
const b = createAnchoredProgram(counterSource("'x'"), 'Counter.tsx', ANCHOR)!
|
|
49
|
+
expect(b.getSourceFile('Counter.tsx')!.text).toBe(counterSource("'x'"))
|
|
50
|
+
expect(b.getSourceFile('Counter.tsx')).not.toBe(a.getSourceFile('Counter.tsx'))
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
test('each source under one virtual name is parsed once, even when they alternate', () => {
|
|
54
|
+
const a = createAnchoredProgram(counterSource('0'), 'Counter.tsx', ANCHOR)!
|
|
55
|
+
createAnchoredProgram(counterSource("'x'"), 'Counter.tsx', ANCHOR)
|
|
56
|
+
const again = createAnchoredProgram(counterSource('0'), 'Counter.tsx', ANCHOR)!
|
|
57
|
+
expect(again.getSourceFile('Counter.tsx')).toBe(a.getSourceFile('Counter.tsx'))
|
|
58
|
+
// A new Program each time, though: only parsed files are shared.
|
|
59
|
+
expect(again).not.toBe(a)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
test('a Program built from cached files still resolves @barefootjs/client types', () => {
|
|
63
|
+
createAnchoredProgram(counterSource('0'), 'Counter.tsx', ANCHOR)
|
|
64
|
+
const program = createAnchoredProgram(counterSource('0'), 'Counter.tsx', ANCHOR)!
|
|
65
|
+
const checker = program.getTypeChecker()
|
|
66
|
+
const file = program.getSourceFile('Counter.tsx')!
|
|
67
|
+
const text = file.text
|
|
68
|
+
const pos = text.indexOf('count()')
|
|
69
|
+
// The token at `count` is the signal getter, typed by the client's
|
|
70
|
+
// declarations. Unresolved, it would be `any`.
|
|
71
|
+
const node = findToken(file, pos)
|
|
72
|
+
expect(checker.typeToString(checker.getTypeAtLocation(node))).toBe(GETTER_TYPE)
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
describe('SourceFileCache', () => {
|
|
77
|
+
test('keys on how a file is parsed, not only its name and text', () => {
|
|
78
|
+
const cache = new SourceFileCache()
|
|
79
|
+
const plain = cache.get('/x/a.d.ts', 99, 'export declare const a: number', DISK)
|
|
80
|
+
const withParents = cache.get('/x/a.d.ts', 99, 'export declare const a: number', { setParentNodes: true })
|
|
81
|
+
expect(withParents).not.toBe(plain)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('parses each text of a file once', () => {
|
|
85
|
+
const cache = new SourceFileCache()
|
|
86
|
+
const first = cache.get('/x/a.d.ts', 99, 'export declare const a: number', DISK)
|
|
87
|
+
expect(cache.get('/x/a.d.ts', 99, 'export declare const a: number', DISK)).toBe(first)
|
|
88
|
+
const changed = cache.get('/x/a.d.ts', 99, 'export declare const a: string', DISK)
|
|
89
|
+
expect(changed).not.toBe(first)
|
|
90
|
+
expect(changed.text).toBe('export declare const a: string')
|
|
91
|
+
expect(cache.get('/x/a.d.ts', 99, 'export declare const a: number', DISK)).toBe(first)
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
function findToken(file: import('typescript').SourceFile, pos: number): import('typescript').Node {
|
|
96
|
+
let found: import('typescript').Node = file
|
|
97
|
+
const visit = (node: import('typescript').Node) => {
|
|
98
|
+
if (node.getStart(file) <= pos && pos < node.getEnd()) {
|
|
99
|
+
found = node
|
|
100
|
+
node.forEachChild(visit)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
file.forEachChild(visit)
|
|
104
|
+
return found
|
|
105
|
+
}
|
|
@@ -486,4 +486,47 @@ describe('computeSsrSeedPlan', () => {
|
|
|
486
486
|
`)
|
|
487
487
|
expect(plan.steps.map(s => s.name)).toEqual(['b', 'c'])
|
|
488
488
|
})
|
|
489
|
+
|
|
490
|
+
test('a computed default reading no earlier prop re-binds the prop; literal data does not', () => {
|
|
491
|
+
const plan = planFor(`
|
|
492
|
+
'use client'
|
|
493
|
+
const DEFAULT = 'm'
|
|
494
|
+
function C({
|
|
495
|
+
rows = ['y', ''].filter((rows) => rows !== ''),
|
|
496
|
+
n = 'abc'.length,
|
|
497
|
+
m = DEFAULT,
|
|
498
|
+
lit = 'x',
|
|
499
|
+
neg = -1,
|
|
500
|
+
list = ['p', { q: 1 }],
|
|
501
|
+
tpl = \`t\`,
|
|
502
|
+
fn = () => 1,
|
|
503
|
+
}: { rows?: string[]; n?: number; m?: string; lit?: string; neg?: number; list?: unknown[]; tpl?: string; fn?: () => number }) {
|
|
504
|
+
return <p>{rows.length}{n}{m}{lit}{neg}{list.length}{tpl}{fn()}</p>
|
|
505
|
+
}
|
|
506
|
+
`)
|
|
507
|
+
expect(plan.steps.map(s => s.name)).toEqual(['rows', 'n', 'm'])
|
|
508
|
+
const rows = step(plan, 'rows')
|
|
509
|
+
expect(rows.kind).toBe('derived')
|
|
510
|
+
if (rows.kind === 'derived') {
|
|
511
|
+
expect(rows.origin).toBe('prop')
|
|
512
|
+
// The callback's own `rows` parameter is not a read of the prop.
|
|
513
|
+
expect(rows.frees).toEqual(['rows'])
|
|
514
|
+
expect(rows.constant).toBe(false)
|
|
515
|
+
}
|
|
516
|
+
})
|
|
517
|
+
|
|
518
|
+
test('a computed default reading a module const that a component local shadows gets no step', () => {
|
|
519
|
+
const plan = planFor(`
|
|
520
|
+
const DEF = 'm'
|
|
521
|
+
const OTHER = 'o'
|
|
522
|
+
function C(props: { b?: string; c?: string }) {
|
|
523
|
+
const { b = DEF.toUpperCase(), c = OTHER.toUpperCase() } = props
|
|
524
|
+
const DEF = 'z'
|
|
525
|
+
return <p>{b}{c}</p>
|
|
526
|
+
}
|
|
527
|
+
`)
|
|
528
|
+
// \`DEF\` in the default is the component's own const (declared after
|
|
529
|
+
// the destructure, so a TDZ read in JS), never the module one.
|
|
530
|
+
expect(plan.steps.map(s => s.name)).toEqual(['c'])
|
|
531
|
+
})
|
|
489
532
|
})
|
|
@@ -250,10 +250,10 @@ describe('#1247 — static-loop CSR self-heal', () => {
|
|
|
250
250
|
expect(clientJs).not.toMatch(/__bfItem\(\)/)
|
|
251
251
|
// The `static-array-child-inits` phase still emits its `initChild`
|
|
252
252
|
// pass — it runs after materialize, finds the cloned children via
|
|
253
|
-
// `
|
|
253
|
+
// `childScopesBySlot`, and wires them with the same prop getters as
|
|
254
254
|
// before. Pinning this here is a guard against an accidental
|
|
255
255
|
// double-init by emitting renderChild AND createComponent.
|
|
256
|
-
expect(clientJs).toMatch(/
|
|
256
|
+
expect(clientJs).toMatch(/childScopesBySlot\(/)
|
|
257
257
|
expect(clientJs).toMatch(/initChild\('Tag',/)
|
|
258
258
|
})
|
|
259
259
|
|