@barefootjs/jsx 0.39.7 → 0.39.9
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/callback-method.d.ts +36 -7
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/component-props.d.ts +80 -13
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +153 -0
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -0
- package/dist/adapters/jsx-adapter.d.ts +4 -0
- package/dist/adapters/jsx-adapter.d.ts.map +1 -1
- package/dist/adapters/literal-value.d.ts +2 -2
- package/dist/adapters/literal-value.d.ts.map +1 -1
- package/dist/adapters/loop-plan.d.ts +133 -9
- package/dist/adapters/loop-plan.d.ts.map +1 -1
- package/dist/adapters/parsed-expr-emitter.d.ts +44 -0
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/adapters/style-object.d.ts +33 -0
- package/dist/adapters/style-object.d.ts.map +1 -0
- 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/children-nullish.d.ts +22 -0
- package/dist/children-nullish.d.ts.map +1 -0
- package/dist/errors.d.ts +2 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/expression-parser.d.ts +9 -19
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/free-refs.d.ts +6 -0
- package/dist/free-refs.d.ts.map +1 -1
- package/dist/index.d.ts +17 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15040 -13899
- 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/event-collision.d.ts +2 -1
- package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/insert.d.ts +2 -2
- package/dist/ir-to-client-js/control-flow/plan/insert.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/control-flow/stringify/event-delegation.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts +10 -9
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.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-reactive.d.ts +1 -1
- package/dist/ir-to-client-js/emit-reactive.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-registration.d.ts +10 -8
- package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
- package/dist/ir-to-client-js/generate-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts +0 -9
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/identifiers.d.ts +6 -8
- package/dist/ir-to-client-js/identifiers.d.ts.map +1 -1
- package/dist/ir-to-client-js/imports.d.ts +23 -2
- package/dist/ir-to-client-js/imports.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases/props-extraction.d.ts +6 -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/prune-unused-prop-extractions.d.ts +1 -1
- package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
- package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
- package/dist/ir-to-client-js/rewrite-destructured-props.d.ts +1 -1
- package/dist/ir-to-client-js/rewrite-destructured-props.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/safe-html.d.ts +20 -2
- 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 +1 -0
- 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 +3 -3
- package/dist/ir-to-client-js/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +2 -11
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +40 -10
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/prop-rewrite.d.ts +35 -8
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-binding.d.ts +66 -1
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/scanner/js-scanner.d.ts.map +1 -1
- package/dist/scanner/js-scanner.js +30 -2
- package/dist/scope/binding-scope.d.ts +30 -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/signal-prop-name-collision.d.ts +6 -5
- package/dist/signal-prop-name-collision.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts +28 -0
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +39 -14
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/types.d.ts +133 -19
- package/dist/types.d.ts.map +1 -1
- package/dist/value-references.d.ts +21 -0
- package/dist/value-references.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +18 -18
- package/src/__tests__/adapter-output.test.ts +5 -3
- package/src/__tests__/analyzer.test.ts +33 -0
- package/src/__tests__/arithmetic-lowering.test.ts +48 -0
- package/src/__tests__/binding-scope.test.ts +29 -0
- package/src/__tests__/children-read.test.ts +226 -0
- package/src/__tests__/client-template-escape-soundness.test.ts +31 -1
- package/src/__tests__/component-props.test.ts +68 -1
- package/src/__tests__/constant-inlining-tree.test.ts +136 -0
- package/src/__tests__/create-query.test.ts +1 -1
- package/src/__tests__/destructure-default-reads-earlier-param.test.ts +138 -0
- package/src/__tests__/dsl-template-adapter.test.ts +173 -0
- package/src/__tests__/emit-to-string-call.test.ts +58 -0
- package/src/__tests__/event-handler-verbatim.test.ts +73 -0
- package/src/__tests__/expression-parser.test.ts +1 -44
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +90 -31
- package/src/__tests__/loop-plan.test.ts +131 -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__/nullable-signal-attr.test.ts +67 -3
- package/src/__tests__/props-alias-const.test.ts +60 -1
- package/src/__tests__/props-named-callback-param.test.ts +256 -0
- package/src/__tests__/reference-graph-identifiers.test.ts +161 -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__/runtime-import-detection.test.ts +134 -0
- package/src/__tests__/signal-prop-name-collision.test.ts +30 -0
- package/src/__tests__/ssr-defaults.test.ts +19 -1
- package/src/__tests__/ssr-seed-plan.test.ts +119 -0
- package/src/__tests__/style-object-plan.test.ts +54 -0
- package/src/__tests__/written-binding-substitution.test.ts +430 -0
- package/src/__tests__/zero-param-helper-inline.test.ts +57 -0
- package/src/adapters/callback-method.ts +48 -10
- package/src/adapters/component-props.ts +130 -12
- package/src/adapters/dsl-template-adapter.ts +259 -0
- package/src/adapters/jsx-adapter.ts +12 -6
- package/src/adapters/literal-value.ts +2 -2
- package/src/adapters/loop-plan.ts +201 -13
- package/src/adapters/parsed-expr-emitter.ts +67 -0
- package/src/adapters/style-object.ts +61 -0
- package/src/analyzer.ts +396 -16
- package/src/assignment-target.ts +79 -0
- package/src/children-nullish.ts +33 -0
- package/src/debug.ts +2 -2
- package/src/errors.ts +23 -0
- package/src/expression-parser.ts +56 -53
- package/src/free-refs.ts +28 -17
- package/src/index.ts +31 -10
- package/src/ir-to-client-js/compute-inlinability.ts +35 -5
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +21 -5
- package/src/ir-to-client-js/control-flow/plan/event-collision.ts +2 -1
- package/src/ir-to-client-js/control-flow/plan/insert.ts +2 -2
- 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/control-flow/stringify/event-delegation.ts +3 -2
- package/src/ir-to-client-js/control-flow/stringify/event-listener.ts +13 -12
- package/src/ir-to-client-js/csr-substitute.ts +82 -24
- package/src/ir-to-client-js/emit-reactive.ts +3 -3
- package/src/ir-to-client-js/emit-registration.ts +34 -58
- package/src/ir-to-client-js/generate-init.ts +17 -1
- package/src/ir-to-client-js/html-template.ts +48 -57
- package/src/ir-to-client-js/identifiers.ts +75 -24
- package/src/ir-to-client-js/imports.ts +56 -19
- package/src/ir-to-client-js/index.ts +2 -1
- package/src/ir-to-client-js/phases/event-handlers.ts +2 -2
- package/src/ir-to-client-js/phases/props-extraction.ts +39 -17
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +2 -2
- 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/prune-unused-prop-extractions.ts +7 -4
- package/src/ir-to-client-js/reactivity.ts +2 -2
- package/src/ir-to-client-js/rewrite-destructured-props.ts +22 -5
- package/src/ir-to-client-js/rewrite-props-object.ts +14 -8
- package/src/ir-to-client-js/safe-html.ts +29 -35
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +4 -1
- package/src/ir-to-client-js/types.ts +3 -2
- package/src/ir-to-client-js/utils.ts +42 -122
- package/src/jsx-to-ir.ts +310 -90
- package/src/nullable-signal-attr.ts +149 -33
- package/src/prop-rewrite.ts +72 -72
- package/src/props-binding.ts +90 -3
- package/src/scanner/__tests__/js-scanner.test.ts +37 -0
- package/src/scanner/js-scanner.ts +64 -4
- package/src/scope/binding-scope.ts +34 -3
- package/src/scope/lexical-scope-walk.ts +167 -0
- package/src/signal-prop-name-collision.ts +50 -24
- package/src/ssr-defaults.ts +47 -20
- package/src/ssr-seed-plan.ts +103 -23
- package/src/types.ts +140 -20
- package/src/value-references.ts +132 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Callback parameters spelled like the props object in the CSR client JS.
|
|
3
|
+
*
|
|
4
|
+
* The init body reads the component's props through `_p`, and a
|
|
5
|
+
* destructured prop read compiles to `_p.<prop>`. The props-object rename
|
|
6
|
+
* (`rewritePropsObjectRef`) used to rename EVERY `props` identifier in the
|
|
7
|
+
* init body, binders included, so a `.map((props) => …)` row, a handler
|
|
8
|
+
* `(props) => …` arrow, a `.filter((props) => …)` callback or a nested
|
|
9
|
+
* `function f(props)` became a `_p` binding that captured the outer
|
|
10
|
+
* `_p.<prop>` reads inside it: a client-created row lost its `suffix`, a
|
|
11
|
+
* forwarded child prop read the row accessor, and `props.by * step` became
|
|
12
|
+
* `NaN`. Fixture: `loop-param-named-props`.
|
|
13
|
+
*/
|
|
14
|
+
import { describe, test, expect } from 'bun:test'
|
|
15
|
+
import { compileJSX } from '../compiler'
|
|
16
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
17
|
+
import { rewriteScopedValueRefs } from '../prop-rewrite'
|
|
18
|
+
|
|
19
|
+
const adapter = new TestAdapter()
|
|
20
|
+
|
|
21
|
+
function clientJs(source: string): string {
|
|
22
|
+
const result = compileJSX(source, 'Repro.tsx', { adapter })
|
|
23
|
+
expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
|
|
24
|
+
return result.files.find(f => f.type === 'clientJs')!.content
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** An `init<Name>` function body (excludes the module-level template lambda). */
|
|
28
|
+
function initBody(content: string, name = ''): string {
|
|
29
|
+
const start = content.indexOf(`export function init${name}`)
|
|
30
|
+
const end = content.indexOf('\nhydrate(', start)
|
|
31
|
+
return content.slice(start, end)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
describe('a callback param named `props` keeps its binding in the client JS', () => {
|
|
35
|
+
test('destructured props: a reactive loop row reads the outer prop through `_p`', () => {
|
|
36
|
+
const body = initBody(clientJs(`
|
|
37
|
+
"use client";
|
|
38
|
+
import { createSignal } from '@barefootjs/client'
|
|
39
|
+
export function Rows({ suffix }: { suffix: string }) {
|
|
40
|
+
const [items, setItems] = createSignal([{ id: 1, label: 'a' }])
|
|
41
|
+
return (
|
|
42
|
+
<div>
|
|
43
|
+
<ul>{items().map((props) => <li key={props.id}>{props.label}{suffix}</li>)}</ul>
|
|
44
|
+
<button onClick={() => setItems([...items(), { id: 2, label: 'b' }])}>add</button>
|
|
45
|
+
</div>
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
`))
|
|
49
|
+
expect(body).toContain('const props = () => __e.item')
|
|
50
|
+
expect(body).toContain('props().label')
|
|
51
|
+
expect(body).toContain('_p.suffix')
|
|
52
|
+
expect(body).not.toMatch(/const _p = /)
|
|
53
|
+
expect(body).not.toContain('_p().')
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('a child component forwarded inside the row reads the outer prop, not the row', () => {
|
|
57
|
+
const body = initBody(clientJs(`
|
|
58
|
+
"use client";
|
|
59
|
+
import { createSignal } from '@barefootjs/client'
|
|
60
|
+
function Item(props: { label: string; mark: string }) { return <li>{props.label}{props.mark}</li> }
|
|
61
|
+
export function List({ mark }: { mark: string }) {
|
|
62
|
+
const [items, setItems] = createSignal([{ label: 'a' }])
|
|
63
|
+
return (
|
|
64
|
+
<div>
|
|
65
|
+
<ul>{items().map((props) => <Item key={props.label} label={props.label} mark={mark} />)}</ul>
|
|
66
|
+
<button onClick={() => setItems([...items(), { label: 'b' }])}>add</button>
|
|
67
|
+
</div>
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
`), 'List')
|
|
71
|
+
expect(body).toContain('get mark() { return _p.mark }')
|
|
72
|
+
expect(body).toContain('get label() { return props().label }')
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('props object: an inlined outer read inside a `props` row stays on `_p`', () => {
|
|
76
|
+
const body = initBody(clientJs(`
|
|
77
|
+
"use client";
|
|
78
|
+
import { createSignal } from '@barefootjs/client'
|
|
79
|
+
export function Rows(props: { title: string }) {
|
|
80
|
+
const t = props.title
|
|
81
|
+
const [items, setItems] = createSignal([{ id: 1 }])
|
|
82
|
+
return (
|
|
83
|
+
<div>
|
|
84
|
+
<ul>{items().map((props) => <li key={props.id}>{props.id}{t}</li>)}</ul>
|
|
85
|
+
<button onClick={() => setItems([...items(), { id: 2 }])}>add</button>
|
|
86
|
+
</div>
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
`))
|
|
90
|
+
expect(body).toContain('const props = () => __e.item')
|
|
91
|
+
expect(body).toContain('_p.title')
|
|
92
|
+
expect(body).not.toMatch(/const _p = /)
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test('a handler arrow param and a filter callback param keep their names', () => {
|
|
96
|
+
const body = initBody(clientJs(`
|
|
97
|
+
"use client";
|
|
98
|
+
import { createSignal } from '@barefootjs/client'
|
|
99
|
+
export function Counter({ step }: { step: number }) {
|
|
100
|
+
const [n, setN] = createSignal(0)
|
|
101
|
+
const [items, setItems] = createSignal([{ id: 1 }, { id: 2 }])
|
|
102
|
+
const bump = (props: { by: number }) => setN(n() + props.by * step)
|
|
103
|
+
return (
|
|
104
|
+
<div>
|
|
105
|
+
<button onClick={() => bump({ by: 2 })}>{n()}</button>
|
|
106
|
+
<button onClick={() => setItems(items().filter((props) => props.id !== step))}>{items().length}</button>
|
|
107
|
+
</div>
|
|
108
|
+
)
|
|
109
|
+
}
|
|
110
|
+
`))
|
|
111
|
+
expect(body).toContain('const bump = (props) => setN(n() + props.by * _p.step)')
|
|
112
|
+
expect(body).toContain('.filter((props) => props.id !== _p.step)')
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
test('a nested function param keeps its name', () => {
|
|
116
|
+
const body = initBody(clientJs(`
|
|
117
|
+
"use client";
|
|
118
|
+
import { createSignal } from '@barefootjs/client'
|
|
119
|
+
export function Say({ suffix }: { suffix: string }) {
|
|
120
|
+
const [last, setLast] = createSignal('')
|
|
121
|
+
function describe(props: { label: string }) { return props.label + suffix }
|
|
122
|
+
return <button onClick={() => setLast(describe({ label: 'x' }))}>{last()}</button>
|
|
123
|
+
}
|
|
124
|
+
`))
|
|
125
|
+
expect(body).toContain('props.label + _p.suffix')
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
test('a free `props` read in a destructured component is still renamed', () => {
|
|
129
|
+
const body = initBody(clientJs(`
|
|
130
|
+
"use client";
|
|
131
|
+
import { createSignal } from '@barefootjs/client'
|
|
132
|
+
export function Free({ id }: { id: string }) {
|
|
133
|
+
const [n, setN] = createSignal(0)
|
|
134
|
+
return <button data-id={id} onClick={() => setN(n() + (props as any).delta)}>{n()}</button>
|
|
135
|
+
}
|
|
136
|
+
`))
|
|
137
|
+
expect(body).toContain('(_p).delta')
|
|
138
|
+
})
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
describe('a `var` binds in its whole function, not just its block', () => {
|
|
142
|
+
test('a `var props` in a nested block shadows the props rename for the whole handler', () => {
|
|
143
|
+
const body = initBody(clientJs(`
|
|
144
|
+
"use client";
|
|
145
|
+
import { createSignal } from '@barefootjs/client'
|
|
146
|
+
export function A({ step }: { step: number }) {
|
|
147
|
+
const [n, setN] = createSignal(0)
|
|
148
|
+
return <button onClick={() => { if (step > 0) { var props = { by: 2 } } setN(n() + props.by * step) }}>{n()}</button>
|
|
149
|
+
}
|
|
150
|
+
`))
|
|
151
|
+
expect(body).toContain('{ var props = { by: 2 } } setN(n() + props.by * _p.step)')
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
test('a `for (var props …)` head shadows the props rename after the loop', () => {
|
|
155
|
+
const body = initBody(clientJs(`
|
|
156
|
+
"use client";
|
|
157
|
+
import { createSignal } from '@barefootjs/client'
|
|
158
|
+
export function B({ step }: { step: number }) {
|
|
159
|
+
const [n, setN] = createSignal(0)
|
|
160
|
+
return <button onClick={() => { for (var props = 0; props < step; props++) {} setN(n() + props) }}>{n()}</button>
|
|
161
|
+
}
|
|
162
|
+
`))
|
|
163
|
+
expect(body).toContain('for (var props = 0; props < _p.step; props++) {} setN(n() + props)')
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
test('a block-nested `var` named like a destructured prop is not rewritten to the prop', () => {
|
|
167
|
+
const body = initBody(clientJs(`
|
|
168
|
+
"use client";
|
|
169
|
+
import { createSignal } from '@barefootjs/client'
|
|
170
|
+
export function C({ step }: { step: number }) {
|
|
171
|
+
const [n, setN] = createSignal(0)
|
|
172
|
+
return <button data-step={step} onClick={() => { if (n() >= 0) { var step = 5 } setN(n() + step) }}>{n()}</button>
|
|
173
|
+
}
|
|
174
|
+
`))
|
|
175
|
+
expect(body).toContain('{ var step = 5 } setN(n() + step)')
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
test('a `let` in a nested block still binds only inside that block', () => {
|
|
179
|
+
const body = initBody(clientJs(`
|
|
180
|
+
"use client";
|
|
181
|
+
import { createSignal } from '@barefootjs/client'
|
|
182
|
+
export function D({ step }: { step: number }) {
|
|
183
|
+
const [n, setN] = createSignal(0)
|
|
184
|
+
return <button onClick={() => { if (n() >= 0) { let step = 5; setN(step) } setN(n() + step) }}>{n()}</button>
|
|
185
|
+
}
|
|
186
|
+
`))
|
|
187
|
+
expect(body).toContain('{ let step = 5; setN(step) } setN(n() + _p.step)')
|
|
188
|
+
})
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
describe('the Phase-1 prop rewrite shares the same `var` scoping', () => {
|
|
192
|
+
const toProp = (name: string) => `_p.${name}`
|
|
193
|
+
test('a block-nested `var` binds for the whole arrow', () => {
|
|
194
|
+
const out = rewriteScopedValueRefs('() => { if (c) { var step = 5 } return step }', new Set(['step']), toProp)
|
|
195
|
+
expect(out).toBe('() => { if (c) { var step = 5 } return step }')
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
test('a `for (var …)` head binds after the loop', () => {
|
|
199
|
+
const out = rewriteScopedValueRefs('() => { for (var step = 0; step < 3; step++) {} return step }', new Set(['step']), toProp)
|
|
200
|
+
expect(out).toBe('() => { for (var step = 0; step < 3; step++) {} return step }')
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
test('a `let` stays block-scoped', () => {
|
|
204
|
+
const out = rewriteScopedValueRefs('() => { if (c) { let step = 5 } return step }', new Set(['step']), toProp)
|
|
205
|
+
expect(out).toBe('() => { if (c) { let step = 5 } return _p.step }')
|
|
206
|
+
})
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
describe('a parameter default does not see the body\'s declarations', () => {
|
|
210
|
+
const toProp = (name: string) => `_p.${name}`
|
|
211
|
+
test('Phase-1 rewrite: a default reads the prop despite a same-named body `var`', () => {
|
|
212
|
+
const out = rewriteScopedValueRefs('(value = step) => { var step = 0; return value }', new Set(['step']), toProp)
|
|
213
|
+
expect(out).toBe('(value = _p.step) => { var step = 0; return value }')
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
test('Phase-1 rewrite: a default reads the prop despite a same-named body `let`', () => {
|
|
217
|
+
const out = rewriteScopedValueRefs('(value = step) => { let step = 0; return value + step }', new Set(['step']), toProp)
|
|
218
|
+
expect(out).toBe('(value = _p.step) => { let step = 0; return value + step }')
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
test('a handler-local arrow whose default reads a destructured prop shadowed in its body', () => {
|
|
222
|
+
const body = initBody(clientJs(`
|
|
223
|
+
"use client";
|
|
224
|
+
import { createSignal } from '@barefootjs/client'
|
|
225
|
+
export function E({ step }: { step: number }) {
|
|
226
|
+
const [n, setN] = createSignal(0)
|
|
227
|
+
return <button onClick={() => { const f = (value = step) => { var step = 0; return value + step }; setN(f()) }}>{n()}</button>
|
|
228
|
+
}
|
|
229
|
+
`))
|
|
230
|
+
expect(body).toContain('const f = (value = _p.step) => { var step = 0; return value + step }')
|
|
231
|
+
})
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
describe('a class static block owns its `var`s', () => {
|
|
235
|
+
const toProp = (name: string) => `_p.${name}`
|
|
236
|
+
test('Phase-1 rewrite: a read inside the static block is local, a read after the class is the prop', () => {
|
|
237
|
+
const out = rewriteScopedValueRefs(
|
|
238
|
+
'() => { class C { static { var step = 0; C.s = step } }; return step }',
|
|
239
|
+
new Set(['step']),
|
|
240
|
+
toProp,
|
|
241
|
+
)
|
|
242
|
+
expect(out).toBe('() => { class C { static { var step = 0; C.s = step } }; return _p.step }')
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
test('a handler whose class static block declares a `var` named like a destructured prop', () => {
|
|
246
|
+
const body = initBody(clientJs(`
|
|
247
|
+
"use client";
|
|
248
|
+
import { createSignal } from '@barefootjs/client'
|
|
249
|
+
export function F({ step }: { step: number }) {
|
|
250
|
+
const [n, setN] = createSignal(0)
|
|
251
|
+
return <button onClick={() => { class C { static { var step = 0; C.s = step } }; setN(n() + step) }}>{n()}</button>
|
|
252
|
+
}
|
|
253
|
+
`))
|
|
254
|
+
expect(body).toContain('class C { static { var step = 0; C.s = step } }; setN(n() + _p.step)')
|
|
255
|
+
})
|
|
256
|
+
})
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The client-JS reference graph finds identifiers with TypeScript's scanner
|
|
3
|
+
* (#3536).
|
|
4
|
+
*
|
|
5
|
+
* The graph decides which locals the emitted `init` keeps. Its identifier
|
|
6
|
+
* scan used to be the regex `\b[a-zA-Z_][a-zA-Z0-9_]*\b`: `$` is not a word
|
|
7
|
+
* character, so a `$`-named local reached only through a helper was dropped
|
|
8
|
+
* and the handler threw ReferenceError. Words inside strings, comments and
|
|
9
|
+
* template static text were collected too.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { describe, test, expect } from 'bun:test'
|
|
13
|
+
import { compileJSX } from '../compiler'
|
|
14
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
15
|
+
import { HonoAdapter } from '../../../adapter-hono/src/adapter/index'
|
|
16
|
+
import {
|
|
17
|
+
extractIdentifiers,
|
|
18
|
+
extractTemplateExpressions,
|
|
19
|
+
extractTemplateIdentifiers,
|
|
20
|
+
} from '../ir-to-client-js/identifiers'
|
|
21
|
+
|
|
22
|
+
const adapter = new TestAdapter()
|
|
23
|
+
|
|
24
|
+
function clientJsFor(locals: string, handlerBody: string): string {
|
|
25
|
+
const source = `
|
|
26
|
+
'use client'
|
|
27
|
+
import { createSignal } from '@barefootjs/client'
|
|
28
|
+
export function T(props: { n: number }) {
|
|
29
|
+
const [count, setCount] = createSignal(0)
|
|
30
|
+
${locals}
|
|
31
|
+
return <button onClick={() => setCount(${handlerBody})}>{count()}</button>
|
|
32
|
+
}
|
|
33
|
+
`
|
|
34
|
+
const result = compileJSX(source, 'T.tsx', { adapter })
|
|
35
|
+
expect(result.errors).toHaveLength(0)
|
|
36
|
+
return result.files.find(f => f.type === 'clientJs')!.content
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
describe('reference graph keeps $-named locals (#3536)', () => {
|
|
40
|
+
for (const name of ['step$', '$step', '$', 'a$b']) {
|
|
41
|
+
test(`a local \`${name}\` reached only through a helper is emitted`, () => {
|
|
42
|
+
const js = clientJsFor(
|
|
43
|
+
`const ${name} = props.n * 2\n const _helper = (x: number) => x + ${name}`,
|
|
44
|
+
'_helper(count())',
|
|
45
|
+
)
|
|
46
|
+
expect(js).toContain(`const ${name} = `)
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
test('a contextual-keyword local (`type`) reached through a helper is emitted', () => {
|
|
51
|
+
const js = clientJsFor('const type = props.n + 1\n const _helper = (x: number) => x + type', '_helper(count())')
|
|
52
|
+
expect(js).toContain('const type = ')
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
describe('extractIdentifiers', () => {
|
|
57
|
+
const ids = (expr: string) => {
|
|
58
|
+
const set = new Set<string>()
|
|
59
|
+
extractIdentifiers(expr, set)
|
|
60
|
+
return [...set].sort()
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
test('skips string contents, comments and template static text; keeps member names', () => {
|
|
64
|
+
// A member name stays in: the graph reads `props.value` as a use of the
|
|
65
|
+
// `value` prop in props-object mode.
|
|
66
|
+
expect(ids("obj.member + other?.deep + 'word in a string' /* note */ + `tpl ${inner}`")).toEqual([
|
|
67
|
+
'deep',
|
|
68
|
+
'inner',
|
|
69
|
+
'member',
|
|
70
|
+
'obj',
|
|
71
|
+
'other',
|
|
72
|
+
])
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('reports $ names and contextual keywords, never reserved words', () => {
|
|
76
|
+
expect(ids('(x) => x + step$ + $ + type + of')).toEqual(['$', 'of', 'step$', 'type', 'x'])
|
|
77
|
+
})
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
describe('template substitutions', () => {
|
|
81
|
+
test('a body with an object literal or a nested template stays whole', () => {
|
|
82
|
+
expect(extractTemplateExpressions('<p>${a ? { x: 1 }.x : b}</p>${`n${deep}`}')).toEqual([
|
|
83
|
+
'a ? { x: 1 }.x : b',
|
|
84
|
+
'`n${deep}`',
|
|
85
|
+
])
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('static text is not scanned for identifiers', () => {
|
|
89
|
+
const set = new Set<string>()
|
|
90
|
+
extractTemplateIdentifiers('<div class="btn size-lg ${cls}">${label$}</div>', set)
|
|
91
|
+
expect([...set].sort()).toEqual(['cls', 'label$'])
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
describe('regex literals and keyword-named members (#3536 review)', () => {
|
|
96
|
+
test('a quote inside a regex literal does not hide a later dependency', () => {
|
|
97
|
+
const js = clientJsFor('const step = props.n * 2\n const _helper = (x: string) => (/"/.test(x) ? step : 0)', "_helper('a')")
|
|
98
|
+
expect(js).toContain('const step = ')
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
test('division after a contextual-keyword local does not hide a later dependency', () => {
|
|
102
|
+
const js = clientJsFor('const type = props.n\n const step = props.n * 2\n const _helper = () => type / 2 + step / 3', '_helper()')
|
|
103
|
+
expect(js).toContain('const step = ')
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('division after a keyword-named member does not hide a later dependency', () => {
|
|
107
|
+
const js = clientJsFor('const step = props.n * 2\n const _helper = (o: { default: number }) => o.default / 2 + step / 3', '_helper({ default: 1 })')
|
|
108
|
+
expect(js).toContain('const step = ')
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
test('a regex literal after the for-of separator does not hide a later dependency', () => {
|
|
112
|
+
const js = clientJsFor(
|
|
113
|
+
'const step = props.n * 2\n const _helper = (x: string) => { for (const hit of /"/.exec(x) || []) {} return step }',
|
|
114
|
+
"_helper('a')",
|
|
115
|
+
)
|
|
116
|
+
expect(js).toContain('const step = ')
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
test('a regex literal after a contextual-keyword for-of binding does not hide a later dependency', () => {
|
|
120
|
+
const js = clientJsFor(
|
|
121
|
+
'const step = props.n * 2\n const _helper = (x: string) => { for (const type of /"/.exec(x) || []) {} return step }',
|
|
122
|
+
"_helper('a')",
|
|
123
|
+
)
|
|
124
|
+
expect(js).toContain('const step = ')
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
test('division on an `of` binding after a line break does not hide a later dependency', () => {
|
|
128
|
+
const js = clientJsFor(
|
|
129
|
+
'const step = props.n * 2\n const _helper = (x: number) => {\n let of = x\n of /= step\n return of\n }',
|
|
130
|
+
'_helper(12)',
|
|
131
|
+
)
|
|
132
|
+
expect(js).toContain('const step = ')
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
test('a brace inside a regex literal does not close a template substitution', () => {
|
|
136
|
+
expect(extractTemplateExpressions('<p>${/}/.test(dep) ? dep : fallback}</p>')).toEqual([
|
|
137
|
+
'/}/.test(dep) ? dep : fallback',
|
|
138
|
+
])
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
test('a reserved word read as a member name is reported; as a keyword it is not', () => {
|
|
142
|
+
const set = new Set<string>()
|
|
143
|
+
extractIdentifiers('props.default + obj?.enum + (x instanceof Y)', set)
|
|
144
|
+
expect([...set].sort()).toEqual(['Y', 'default', 'enum', 'obj', 'props', 'x'])
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('a keyword-named prop read only by a handler is still hydrated', () => {
|
|
148
|
+
const source = `
|
|
149
|
+
'use client'
|
|
150
|
+
import { createSignal } from '@barefootjs/client'
|
|
151
|
+
export function T(props: { default: number }) {
|
|
152
|
+
const [count, setCount] = createSignal(0)
|
|
153
|
+
return <button onClick={() => setCount(props.default)}>{count()}</button>
|
|
154
|
+
}
|
|
155
|
+
`
|
|
156
|
+
const hono = compileJSX(source, 'T.tsx', { adapter: new HonoAdapter() })
|
|
157
|
+
expect(hono.errors).toHaveLength(0)
|
|
158
|
+
const template = hono.files.find(f => f.type === 'markedTemplate')!.content
|
|
159
|
+
expect(template).toContain("__hydrateProps['default']")
|
|
160
|
+
})
|
|
161
|
+
})
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The client template reads a renamed body destructure binding that holds
|
|
3
|
+
* its prop (`const { b: local = d } = props`) as that prop with its default,
|
|
4
|
+
* like the parameter form (#3579). A binding that no longer holds the prop
|
|
5
|
+
* (a `let` reassigned before the return, a folded reassignment, a value
|
|
6
|
+
* mutated in place) stays a local: the template leaves its slot empty and
|
|
7
|
+
* defers a child that receives it, instead of reading `_p.b ?? d`.
|
|
8
|
+
*
|
|
9
|
+
* The reassigned shapes have no conformance fixture: the reference adapter
|
|
10
|
+
* itself drops the reassignment of a destructured binding, so these pin the
|
|
11
|
+
* client template only.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import { describe, test, expect } from 'bun:test'
|
|
15
|
+
import { compileJSX } from '../compiler'
|
|
16
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
17
|
+
|
|
18
|
+
const adapter = new TestAdapter()
|
|
19
|
+
const transpiler = new Bun.Transpiler({ loader: 'js' })
|
|
20
|
+
|
|
21
|
+
const CHILD = 'function Child(props: { xs: string[] }) { return <i>{props.xs.length}</i> }'
|
|
22
|
+
|
|
23
|
+
/** The `template: (_p) => …` lambda the client registers for `C`. */
|
|
24
|
+
function templateOf(body: string, jsx: string, propsType = '{ b?: string[] | null }'): string {
|
|
25
|
+
const source = `
|
|
26
|
+
'use client'
|
|
27
|
+
${CHILD}
|
|
28
|
+
export function C(props: ${propsType}) {
|
|
29
|
+
${body}
|
|
30
|
+
return ${jsx}
|
|
31
|
+
}
|
|
32
|
+
`
|
|
33
|
+
const result = compileJSX(source, 'C.tsx', { adapter })
|
|
34
|
+
expect(result.errors.filter(e => e.severity === 'error')).toHaveLength(0)
|
|
35
|
+
const js = result.files.find(f => f.type === 'clientJs')!.content
|
|
36
|
+
expect(() => transpiler.transformSync(js)).not.toThrow()
|
|
37
|
+
const line = js.split('\n').find(l => l.includes("hydrate('C'") && l.includes('template: (_p) =>'))
|
|
38
|
+
expect(line).toBeDefined()
|
|
39
|
+
return line!
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
describe('renamed body destructure binding in the client template (#3579)', () => {
|
|
43
|
+
test('a default holding a callback reads the prop with its default', () => {
|
|
44
|
+
const t = templateOf("const { b: local = ['y', ''].filter((x) => x !== '') } = props", '<p>{local.length}</p>')
|
|
45
|
+
expect(t).toContain("_p.b ?? (['y', ''].filter((x) => x !== ''))")
|
|
46
|
+
expect(t).not.toContain("${''}")
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test('a child prop holding it renders the child in place', () => {
|
|
50
|
+
const t = templateOf("const { b: local = ['y', ''].filter((x) => x !== '') } = props", '<p><Child xs={local} /></p>')
|
|
51
|
+
expect(t).toContain("renderChild('Child")
|
|
52
|
+
expect(t).not.toContain('data-bf-ph')
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test('a let reassigned before the return keeps the child deferred', () => {
|
|
56
|
+
const t = templateOf("let { b: local = null } = props\n local = ['changed']", '<p><Child xs={local} /></p>')
|
|
57
|
+
expect(t).toContain('data-bf-ph')
|
|
58
|
+
expect(t).not.toContain('_p.b ?? null')
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('a let reassigned before the return is not read as the prop in text or an attribute', () => {
|
|
62
|
+
const t = templateOf("let { b: local = 'x' } = props\n local = local + '!'", '<p data-v={local}>{local}</p>', '{ b?: string }')
|
|
63
|
+
expect(t).not.toContain("_p.b ?? 'x'")
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('a folded conditional reassignment is not read as the prop', () => {
|
|
67
|
+
const t = templateOf("let { b: local = 'x' } = props\n if (!local) local = 'y'", '<p>{local}</p>', '{ b?: string }')
|
|
68
|
+
expect(t).not.toContain("_p.b ?? 'x'")
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test('an un-renamed let reassigned before the return keeps the child deferred too', () => {
|
|
72
|
+
const t = templateOf("let { b = null } = props\n b = ['changed']", '<p><Child xs={b} /></p>')
|
|
73
|
+
expect(t).toContain('data-bf-ph')
|
|
74
|
+
expect(t).not.toContain('_p.b ?? null')
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('an un-renamed binding with a callback default renders the child in place', () => {
|
|
78
|
+
const t = templateOf("const { b = ['y', ''].filter((x) => x !== '') } = props", '<p><Child xs={b} /></p>')
|
|
79
|
+
expect(t).toContain("renderChild('Child")
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
test('a value mutated in place is not read as the prop', () => {
|
|
83
|
+
const t = templateOf("const { b: local = ['x'] } = props\n local.push('y')", '<p>{local.length}</p>', '{ b?: string[] }')
|
|
84
|
+
expect(t).not.toContain("_p.b ?? ['x']")
|
|
85
|
+
})
|
|
86
|
+
})
|
|
@@ -120,4 +120,94 @@ describe('rewritePropsObjectRef', () => {
|
|
|
120
120
|
const out = rewritePropsObjectRef('const x = propsX.name', 'props')
|
|
121
121
|
expect(out).toBe('const x = propsX.name')
|
|
122
122
|
})
|
|
123
|
+
|
|
124
|
+
describe('an inner binding of the same name shadows the props object', () => {
|
|
125
|
+
// The client JS reads the component's props through `_p`. A callback
|
|
126
|
+
// parameter or local spelled like the props object belongs to its own
|
|
127
|
+
// binding inside its scope: renaming it too would make every `_p.<prop>`
|
|
128
|
+
// read the compiler emits inside that scope (a destructured prop, a
|
|
129
|
+
// forwarded child prop) read the parameter instead.
|
|
130
|
+
test('a loop row callback param keeps its name; the outer `_p` read stays outer', () => {
|
|
131
|
+
const code = 'mapArray(() => items(), el, (props) => String(props.id), (props) => props().label + _p.suffix)'
|
|
132
|
+
expect(rewritePropsObjectRef(code, null)).toBe(code)
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
test('a row accessor `const props = () => __e.item` keeps its name', () => {
|
|
136
|
+
const code = 'const row = (__e) => {\n const props = () => __e.item\n return props().label + _p.suffix\n}'
|
|
137
|
+
expect(rewritePropsObjectRef(code, null)).toBe(code)
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
test('a handler arrow param and a nested function param keep their names', () => {
|
|
141
|
+
const code = 'const bump = (props) => set(props.by * _p.step)\nfunction describe(props) { return props.label + _p.suffix }'
|
|
142
|
+
expect(rewritePropsObjectRef(code, null)).toBe(code)
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
test('references outside the shadowing scope are still rewritten', () => {
|
|
146
|
+
const out = rewritePropsObjectRef('const a = props.x\nconst f = (props) => props.y\nconst b = props.z', 'props')
|
|
147
|
+
expect(out).toBe('const a = _p.x\nconst f = (props) => props.y\nconst b = _p.z')
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
test('a rest binding name is shadowed the same way', () => {
|
|
151
|
+
const out = rewritePropsObjectRef('const a = rest\nconst f = (rest) => rest.y', null, 'rest')
|
|
152
|
+
expect(out).toBe('const a = _p\nconst f = (rest) => rest.y')
|
|
153
|
+
})
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
describe('`var` is function-scoped', () => {
|
|
157
|
+
test('a `var props` in a nested block binds for the whole enclosing function', () => {
|
|
158
|
+
const code = 'const f = () => { if (c) { var props = { by: 2 } } return props.by }\nconst g = props.x'
|
|
159
|
+
expect(rewritePropsObjectRef(code, null)).toBe('const f = () => { if (c) { var props = { by: 2 } } return props.by }\nconst g = _p.x')
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
test('a `for (var props …)` head binds after the loop, but not outside its function', () => {
|
|
163
|
+
const code = 'function f() { for (var props = 0; props < 3; props++) {} return props }\nconst g = props.x'
|
|
164
|
+
expect(rewritePropsObjectRef(code, null)).toBe('function f() { for (var props = 0; props < 3; props++) {} return props }\nconst g = _p.x')
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
test('a `var` inside a nested function does not leak into the outer one', () => {
|
|
168
|
+
const code = 'const f = () => { const h = () => { var props = 1; return props }; return props.x }'
|
|
169
|
+
expect(rewritePropsObjectRef(code, null)).toBe('const f = () => { const h = () => { var props = 1; return props }; return _p.x }')
|
|
170
|
+
})
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
describe('a parameter default does not see the body\'s declarations', () => {
|
|
174
|
+
test('a default reads the props object despite a same-named body `var`', () => {
|
|
175
|
+
const code = 'const f = (v = props.x) => { var props = 1; return v + props }'
|
|
176
|
+
expect(rewritePropsObjectRef(code, null)).toBe('const f = (v = _p.x) => { var props = 1; return v + props }')
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
test('a default reads the props object despite a same-named body `let`', () => {
|
|
180
|
+
const code = 'function f(v = props.x) { let props = 1; return v + props }'
|
|
181
|
+
expect(rewritePropsObjectRef(code, null)).toBe('function f(v = _p.x) { let props = 1; return v + props }')
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
test('a default sees an earlier parameter of the same name, not the props object', () => {
|
|
185
|
+
const code = 'const f = (props, v = props.x) => v'
|
|
186
|
+
expect(rewritePropsObjectRef(code, null)).toBe(code)
|
|
187
|
+
})
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
describe('a class static block owns its `var`s', () => {
|
|
191
|
+
test('a read inside the block is local; a read after the class is the props object', () => {
|
|
192
|
+
const code = 'const f = () => { class C { static { var props = 1; C.p = props } }; return props.x }'
|
|
193
|
+
expect(rewritePropsObjectRef(code, null)).toBe('const f = () => { class C { static { var props = 1; C.p = props } }; return _p.x }')
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
test('a `var` in a method or a field-initializer arrow does not leak into the enclosing function', () => {
|
|
197
|
+
const code = 'const f = () => { class C { m() { var props = 1; return props } g = () => { var props = 2; return props } }; return props.x }'
|
|
198
|
+
expect(rewritePropsObjectRef(code, null)).toBe('const f = () => { class C { m() { var props = 1; return props } g = () => { var props = 2; return props } }; return _p.x }')
|
|
199
|
+
})
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
describe('a computed method name is read in the enclosing scope', () => {
|
|
203
|
+
test('the key reads the props object; the body reads the parameter', () => {
|
|
204
|
+
const code = 'const o = { [props.k](props) { return props } }'
|
|
205
|
+
expect(rewritePropsObjectRef(code, null)).toBe('const o = { [_p.k](props) { return props } }')
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
test('the same holds for a class method and an accessor', () => {
|
|
209
|
+
const code = 'class C { [props.a](props) { return props } get [props.b]() { const props = 1; return props } }'
|
|
210
|
+
expect(rewritePropsObjectRef(code, null)).toBe('class C { [_p.a](props) { return props } get [_p.b]() { const props = 1; return props } }')
|
|
211
|
+
})
|
|
212
|
+
})
|
|
123
213
|
})
|