@barefootjs/jsx 0.39.9 → 0.40.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts +41 -0
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -0
- package/dist/adapters/attr-plan.d.ts +1 -20
- 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/component-props.d.ts +22 -7
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +17 -0
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +95 -2
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/loop-plan.d.ts +41 -1
- package/dist/adapters/loop-plan.d.ts.map +1 -1
- package/dist/adapters/number-value.d.ts +43 -0
- package/dist/adapters/number-value.d.ts.map +1 -0
- package/dist/analyzer-context.d.ts +7 -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.map +1 -1
- package/dist/css-layer-prefixer.d.ts.map +1 -1
- package/dist/debug.d.ts.map +1 -1
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/index.d.ts +10 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2102 -1344
- 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 +20 -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/plan/build-static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +7 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.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.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 +9 -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/nullable-signal-attr.d.ts +29 -3
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/props-binding.d.ts +27 -3
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/scope/binding-scope.d.ts +24 -1
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/scope/root-prop-alias.d.ts +14 -1
- 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/template-parts.d.ts +1 -1
- package/dist/template-parts.d.ts.map +1 -1
- package/dist/types.d.ts +35 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/attr-plan.test.ts +1 -14
- package/src/__tests__/binding-scope-ratchet.test.ts +12 -2
- package/src/__tests__/boolean-carrier.test.ts +74 -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__/component-props.test.ts +14 -0
- package/src/__tests__/computed-const.test.ts +85 -0
- package/src/__tests__/dsl-template-adapter.test.ts +70 -0
- package/src/__tests__/expression-parser.test.ts +24 -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__/issue-3688-props-spread-child-csr.test.ts +142 -0
- package/src/__tests__/jsx-text-linebreak-whitespace.test.ts +11 -0
- package/src/__tests__/loop-plan.test.ts +66 -1
- package/src/__tests__/loop-row-prop-read.test.ts +111 -0
- package/src/__tests__/memo-body-expr.test.ts +137 -0
- package/src/__tests__/nullable-signal-attr.test.ts +97 -2
- package/src/__tests__/profile-binding-ids.test.ts +14 -0
- package/src/__tests__/root-prop-alias.test.ts +34 -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__/template-parts-literal-boundary.test.ts +41 -0
- package/src/adapters/ambiguous-nil-arithmetic.ts +94 -0
- package/src/adapters/attr-plan.ts +8 -56
- package/src/adapters/boolean-carrier.ts +76 -0
- package/src/adapters/component-props.ts +28 -8
- package/src/adapters/computed-const.ts +206 -7
- package/src/adapters/dsl-template-adapter.ts +183 -2
- package/src/adapters/loop-plan.ts +67 -2
- package/src/adapters/number-value.ts +95 -0
- package/src/analyzer-context.ts +8 -0
- package/src/analyzer.ts +187 -66
- package/src/augment-inherited-props.ts +5 -9
- package/src/css-layer-prefixer.ts +4 -5
- package/src/debug.ts +1 -0
- package/src/expression-parser.ts +4 -1
- package/src/index.ts +23 -5
- package/src/ir-to-client-js/collect-elements.ts +104 -46
- package/src/ir-to-client-js/compute-inlinability.ts +2 -2
- 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 +79 -4
- 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 +1 -0
- 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 +3 -2
- package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +27 -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 +122 -75
- package/src/ir-to-client-js/plan/build-static-array-child-init.ts +11 -1
- package/src/ir-to-client-js/plan/static-array-child-init.ts +7 -1
- 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 +6 -4
- package/src/ir-to-client-js/types.ts +23 -0
- package/src/ir-to-client-js/utils.ts +16 -2
- package/src/jsx-to-ir.ts +112 -55
- package/src/loop-destructure.ts +4 -1
- package/src/module-exports.ts +13 -21
- package/src/nullable-signal-attr.ts +134 -13
- package/src/props-binding.ts +54 -3
- package/src/rich-type-refusal.ts +3 -1
- package/src/scope/binding-scope.ts +35 -2
- package/src/scope/root-prop-alias.ts +17 -1
- 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/template-parts.ts +25 -3
- package/src/template-position-walk.ts +3 -1
- package/src/types.ts +32 -1
|
@@ -13,6 +13,7 @@ import { TestAdapter } from '../adapters/test-adapter'
|
|
|
13
13
|
import { buildIdIndex } from '../profiler'
|
|
14
14
|
import { analyzeComponent } from '../analyzer'
|
|
15
15
|
import { buildComponentAnalysis } from '../debug'
|
|
16
|
+
import { codeSafeStringLiteral } from '../ir-to-client-js/utils'
|
|
16
17
|
|
|
17
18
|
const adapter = new TestAdapter()
|
|
18
19
|
|
|
@@ -223,3 +224,16 @@ describe('the analyzer records `undefined` for a zero-arg createSignal() (#3215)
|
|
|
223
224
|
expect(signal.type.raw).toBe('string')
|
|
224
225
|
})
|
|
225
226
|
})
|
|
227
|
+
|
|
228
|
+
describe('codeSafeStringLiteral (profile binding id spelling)', () => {
|
|
229
|
+
test('prints a plain id exactly as JSON.stringify does', () => {
|
|
230
|
+
expect(codeSafeStringLiteral('Counter#binding:s0')).toBe(JSON.stringify('Counter#binding:s0'))
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
test('escapes <, >, / and the line separators, and still evaluates to the same string', () => {
|
|
234
|
+
const value = 'a</script>b\u2028c\u2029d'
|
|
235
|
+
const literal = codeSafeStringLiteral(value)
|
|
236
|
+
expect(literal).not.toMatch(/[<>/\u2028\u2029]/)
|
|
237
|
+
expect(new Function(`return ${literal}`)()).toBe(value)
|
|
238
|
+
})
|
|
239
|
+
})
|
|
@@ -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,35 @@ 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))).toBe(true)
|
|
71
|
+
expect(isRootPropsObject(props, isBoundIn(BindingScope.EMPTY.enterLoopRow({ param: 'row' })))).toBe(true)
|
|
72
|
+
expect(isRootPropsObject(props, isBoundIn(BindingScope.EMPTY.enterLoopRow({ param: 'props' })))).toBe(false)
|
|
73
|
+
const destructured = BindingScope.EMPTY.enterLoopRow({ param: '{ props }', paramBindings: [{ name: 'props' }] })
|
|
74
|
+
expect(isRootPropsObject(props, isBoundIn(destructured))).toBe(false)
|
|
75
|
+
const preamble = BindingScope.EMPTY.enterLoopRow({ param: 'row', preamble: { declaredNames: ['props'] } })
|
|
76
|
+
expect(isRootPropsObject(props, isBoundIn(preamble))).toBe(false)
|
|
77
|
+
expect(isRootPropsObject(props, isBoundIn(BindingScope.EMPTY.enterCallback(['props'])))).toBe(false)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('only the bare identifier props reads the props object', () => {
|
|
81
|
+
expect(isRootPropsObject({ kind: 'identifier', name: 'row' }, () => false)).toBe(false)
|
|
82
|
+
expect(isRootPropsObject({ kind: 'member', object: props, property: 'a', computed: false, optional: false }, () => false)).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
|
+
})
|
|
@@ -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
|
})
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { expect, test } from 'bun:test'
|
|
2
|
+
import { templatePartsToJsExpr } from '../template-parts'
|
|
3
|
+
import { parseExpression } from '../expression-parser'
|
|
4
|
+
|
|
5
|
+
for (const text of ['${notCode}', '`', '\\', '\\${notCode}', '"\'\n\r\t', '']) {
|
|
6
|
+
test(`template text stays literal: ${JSON.stringify(text)}`, () => {
|
|
7
|
+
const source = templatePartsToJsExpr([
|
|
8
|
+
{ type: 'string', value: text },
|
|
9
|
+
{ type: 'expression', expr: 'value', templateExpr: '_p.value', parsed: parseExpression('value') },
|
|
10
|
+
{ type: 'string', value: text },
|
|
11
|
+
])
|
|
12
|
+
expect(new Function('value', `return ${source}`)('}')).toBe(`${text}}${text}`)
|
|
13
|
+
})
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
test('template expression projection rewrites only the expression part', () => {
|
|
17
|
+
const source = templatePartsToJsExpr([
|
|
18
|
+
{ type: 'string', value: '${value} ' },
|
|
19
|
+
{ type: 'expression', expr: 'value', templateExpr: '_p.value', parsed: parseExpression('value') },
|
|
20
|
+
], { useTemplate: true })
|
|
21
|
+
expect(new Function('_p', `return ${source}`)({ value: 'tail' })).toBe('${value} tail')
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
// A `ternary` part carries each branch's COOKED value (`'a\nb'` holds a real
|
|
25
|
+
// newline), so the spelling must re-escape it into a valid string literal.
|
|
26
|
+
for (const text of ['a\nb', "it's", 'back\\slash', 'tab\tx', 'cr\rlf', 'ls\u2028ps\u2029', '"', '${x}', '`', '']) {
|
|
27
|
+
test(`ternary branch stays a valid literal: ${JSON.stringify(text)}`, () => {
|
|
28
|
+
const source = templatePartsToJsExpr([
|
|
29
|
+
{ type: 'ternary', condition: 'on', whenTrue: text, whenFalse: `${text}!` },
|
|
30
|
+
])
|
|
31
|
+
const evaluate = new Function('on', `return ${source}`)
|
|
32
|
+
expect(evaluate(true)).toBe(text)
|
|
33
|
+
expect(evaluate(false)).toBe(`${text}!`)
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
test('ternary branch without escapes prints unchanged', () => {
|
|
38
|
+
expect(templatePartsToJsExpr([
|
|
39
|
+
{ type: 'ternary', condition: 'on', whenTrue: 'a b', whenFalse: 'c' },
|
|
40
|
+
])).toBe("`${on ? 'a b' : 'c'}`")
|
|
41
|
+
})
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { exprToString, type ParsedExpr } from '../expression-parser.ts'
|
|
2
|
+
import {
|
|
3
|
+
ambiguousNilArithmeticOperands,
|
|
4
|
+
arithmeticOperandCoercion,
|
|
5
|
+
arithmeticOperandNilIsAmbiguous,
|
|
6
|
+
type ArithmeticCoercion,
|
|
7
|
+
type NullishAttrContext,
|
|
8
|
+
} from '../nullable-signal-attr.ts'
|
|
9
|
+
import { BindingScope } from '../scope/binding-scope.ts'
|
|
10
|
+
import type { CompilerError, MemoInfo } from '../types.ts'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Where a template adapter records the refusal of an arithmetic operand
|
|
14
|
+
* whose nil the declared type cannot classify (#3700).
|
|
15
|
+
*/
|
|
16
|
+
export interface AmbiguousNilArithmeticSink {
|
|
17
|
+
readonly errors: CompilerError[]
|
|
18
|
+
readonly componentName: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The shared arithmetic-operand decision for a template adapter's emission
|
|
23
|
+
* site: the coercion the adapter spells (`arithmeticOperandCoercion`), after
|
|
24
|
+
* refusing (BF101) an operand that reads a number prop declared both
|
|
25
|
+
* optional and nullable (`n?: number | null`, `arithmeticOperandNilIsAmbiguous`).
|
|
26
|
+
* JS reads that prop as `undefined` when omitted (`NaN` in arithmetic) and
|
|
27
|
+
* as `0` when `null`; the engine holds both as one nil, so any spelling
|
|
28
|
+
* renders one of them wrong. Asked at the emission site so the operand is
|
|
29
|
+
* classified in the scope it is read in (`isShadowed`). One error per
|
|
30
|
+
* operand text per compile.
|
|
31
|
+
*/
|
|
32
|
+
export function arithmeticCoercionOrRefuse(
|
|
33
|
+
op: string,
|
|
34
|
+
operand: ParsedExpr,
|
|
35
|
+
other: ParsedExpr | undefined,
|
|
36
|
+
ctx: NullishAttrContext,
|
|
37
|
+
isShadowed: (name: string) => boolean,
|
|
38
|
+
sink: AmbiguousNilArithmeticSink,
|
|
39
|
+
): ArithmeticCoercion {
|
|
40
|
+
if (arithmeticOperandNilIsAmbiguous(op, operand, other, ctx, isShadowed)) {
|
|
41
|
+
recordAmbiguousNilArithmetic(exprToString(operand), sink)
|
|
42
|
+
}
|
|
43
|
+
return arithmeticOperandCoercion(op, operand, other, ctx, isShadowed)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Refuse (BF101) each arithmetic operand in a whole expression that a
|
|
48
|
+
* template adapter lowers without asking {@link arithmeticCoercionOrRefuse}
|
|
49
|
+
* per operator: a `.filter()` predicate, an expression the runtime evaluator
|
|
50
|
+
* runs, a condition lowered by its own renderer (`ambiguousNilArithmeticOperands`).
|
|
51
|
+
* `scope` is the scope the expression is read in.
|
|
52
|
+
*/
|
|
53
|
+
export function refuseAmbiguousNilArithmetic(
|
|
54
|
+
expr: ParsedExpr,
|
|
55
|
+
ctx: NullishAttrContext,
|
|
56
|
+
scope: BindingScope,
|
|
57
|
+
sink: AmbiguousNilArithmeticSink,
|
|
58
|
+
): void {
|
|
59
|
+
for (const operand of ambiguousNilArithmeticOperands(expr, ctx, scope)) {
|
|
60
|
+
recordAmbiguousNilArithmetic(exprToString(operand), sink)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Refuse the ambiguous arithmetic in the component's memo bodies. A template
|
|
66
|
+
* adapter seeds every memo at SSR, and some seed a memo through a path that
|
|
67
|
+
* never reaches its operator emitter (a static seed after a speculative
|
|
68
|
+
* lowering, a constructor-time evaluation), so the bodies are checked as a
|
|
69
|
+
* whole, in the component scope they are declared in.
|
|
70
|
+
*/
|
|
71
|
+
export function refuseAmbiguousNilMemoArithmetic(
|
|
72
|
+
memos: readonly MemoInfo[],
|
|
73
|
+
ctx: NullishAttrContext,
|
|
74
|
+
sink: AmbiguousNilArithmeticSink,
|
|
75
|
+
): void {
|
|
76
|
+
for (const memo of memos) {
|
|
77
|
+
if (memo.parsed) refuseAmbiguousNilArithmetic(memo.parsed, ctx, BindingScope.EMPTY, sink)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function recordAmbiguousNilArithmetic(operandText: string, sink: AmbiguousNilArithmeticSink): void {
|
|
82
|
+
const message = `Arithmetic over \`${operandText}\` reads a number prop declared both optional and nullable (\`?: number | null\`). JS reads it as \`undefined\` when the caller omits it (\`NaN\` in arithmetic) and as \`0\` when it is \`null\`, but a template engine holds both as the same nil, so the server render cannot tell which one it is.`
|
|
83
|
+
if (sink.errors.some(e => e.code === 'BF101' && e.message === message)) return
|
|
84
|
+
sink.errors.push({
|
|
85
|
+
code: 'BF101',
|
|
86
|
+
severity: 'error',
|
|
87
|
+
message,
|
|
88
|
+
loc: { file: `${sink.componentName}.tsx`, start: { line: 1, column: 0 }, end: { line: 1, column: 0 } },
|
|
89
|
+
suggestion: {
|
|
90
|
+
message: 'Declare the prop as only optional (`?: number`) or only nullable (`: number | null`), or give it a default value.',
|
|
91
|
+
escape: [{ kind: 'rewrite' }],
|
|
92
|
+
},
|
|
93
|
+
})
|
|
94
|
+
}
|
|
@@ -10,65 +10,13 @@
|
|
|
10
10
|
* hash literal.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import { exprToString,
|
|
14
|
-
import type { LoweringMatcher } from '../lowering-registry.ts'
|
|
13
|
+
import { exprToString, parseExpression, type ParsedExpr } from '../expression-parser.ts'
|
|
15
14
|
import { attrValueMayBeNullish, nullableSignalAttrGetter, type NullishAttrContext } from '../nullable-signal-attr.ts'
|
|
16
|
-
import type {
|
|
15
|
+
import type { ConstantInfo, ExpressionAttr, SpreadAttr } from '../types.ts'
|
|
17
16
|
import { isBooleanAttr } from '../html-constants.ts'
|
|
18
17
|
|
|
19
18
|
const IDENTIFIER = /^[A-Za-z_$][\w$]*$/
|
|
20
19
|
|
|
21
|
-
/**
|
|
22
|
-
* Whether an attribute value is a shape no template lowers: a JS object
|
|
23
|
-
* literal (`style={{…}}` aside, which the adapter lowers first) or a
|
|
24
|
-
* tagged template literal (`cn\`base ${tone()}\``). Returns the refusal, with
|
|
25
|
-
* the support gate's reason when it gives one, or `null` when the value
|
|
26
|
-
* goes on to {@link planAttrExpression}. The adapter reports BF101 in its own
|
|
27
|
-
* words. A value starting with neither shape is never parsed here.
|
|
28
|
-
*/
|
|
29
|
-
export function unsupportedAttrExpression(
|
|
30
|
-
expr: string,
|
|
31
|
-
loweringMatchers: readonly LoweringMatcher[],
|
|
32
|
-
): { reason: string | undefined } | null {
|
|
33
|
-
let probe = expr.trim()
|
|
34
|
-
while (probe.startsWith('(')) probe = probe.slice(1).trimStart()
|
|
35
|
-
const startsAsObjectLiteral = probe.startsWith('{')
|
|
36
|
-
const hasTaggedTemplate = /[A-Za-z_$][\w$]*\s*`/.test(probe)
|
|
37
|
-
if (!startsAsObjectLiteral && !hasTaggedTemplate) return null
|
|
38
|
-
const parsed = parseExpression(expr.trim())
|
|
39
|
-
const support = isSupported(parsed, { loweringMatchers })
|
|
40
|
-
if (parsed.kind !== 'unsupported' && support.supported) return null
|
|
41
|
-
return { reason: support.reason ?? (parsed.kind === 'unsupported' ? parsed.reason : undefined) }
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* The BF101 for an attribute value {@link unsupportedAttrExpression}
|
|
46
|
-
* refuses, worded for one adapter (`adapterLabel`, e.g. `Mojo`) and its
|
|
47
|
-
* template language (`languageLabel`, e.g. `Embedded Perl`); `null` when the
|
|
48
|
-
* value goes on to {@link planAttrExpression}.
|
|
49
|
-
*/
|
|
50
|
-
export function unsupportedAttrExpressionError(
|
|
51
|
-
expr: string,
|
|
52
|
-
attrName: string,
|
|
53
|
-
loweringMatchers: readonly LoweringMatcher[],
|
|
54
|
-
componentName: string,
|
|
55
|
-
adapterLabel: string,
|
|
56
|
-
languageLabel: string,
|
|
57
|
-
): CompilerError | null {
|
|
58
|
-
const refusal = unsupportedAttrExpression(expr, loweringMatchers)
|
|
59
|
-
if (!refusal) return null
|
|
60
|
-
const reasonLine = refusal.reason ? `\n${refusal.reason}` : ''
|
|
61
|
-
return {
|
|
62
|
-
code: 'BF101',
|
|
63
|
-
severity: 'error',
|
|
64
|
-
message: `Expression not supported on attribute '${attrName}': ${expr.trim()}${reasonLine}`,
|
|
65
|
-
loc: { file: componentName + '.tsx', start: { line: 1, column: 0 }, end: { line: 1, column: 0 } },
|
|
66
|
-
suggestion: {
|
|
67
|
-
message: `The ${adapterLabel} adapter cannot lower JS object literals or tagged-template-literal expressions into ${languageLabel}. Move the expression into a \`'use client'\` component (so hydration computes it), or expand it into discrete attributes whose values are values the adapter can lower.`,
|
|
68
|
-
},
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
20
|
/**
|
|
73
21
|
* The emission of an `{expr}` attribute value.
|
|
74
22
|
*
|
|
@@ -139,8 +87,12 @@ function nullableBareRead(value: ExpressionAttr, ctx: AttrPlanContext): { restKe
|
|
|
139
87
|
const bare = value.expr.trim()
|
|
140
88
|
const restKey = ctx.restPropsName !== null && bare.startsWith(`${ctx.restPropsName}.`)
|
|
141
89
|
let normalized = bare
|
|
142
|
-
if (ctx.propsObjectName && bare.startsWith(`${ctx.propsObjectName}.`))
|
|
143
|
-
|
|
90
|
+
if (ctx.propsObjectName && bare.startsWith(`${ctx.propsObjectName}.`)) {
|
|
91
|
+
// A binding of the props object's name shadows it (#3693): the read is
|
|
92
|
+
// that binding's member, no component prop.
|
|
93
|
+
if (ctx.isLoopBound(ctx.propsObjectName)) return null
|
|
94
|
+
normalized = bare.slice(ctx.propsObjectName.length + 1)
|
|
95
|
+
} else if (restKey) normalized = bare.slice(ctx.restPropsName!.length + 1)
|
|
144
96
|
const prop = IDENTIFIER.test(normalized) && ctx.nullableOptionalProps.has(normalized) && !ctx.isLoopBound(normalized)
|
|
145
97
|
const signal = nullableSignalAttrGetter(value.parsed, ctx.nullableSignalGetters)
|
|
146
98
|
if (prop || (signal !== null && !ctx.isLoopBound(signal))) return { restKey }
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which values may hold a JS boolean that the backend received as data, for
|
|
3
|
+
* backends whose own stringification of such a value differs from JS's
|
|
4
|
+
* `String(boolean)`: Perl decodes a JSON `false` to an object that prints
|
|
5
|
+
* `0` (and `true` prints `1`). Unlike `collectBooleanValueNames`, the value
|
|
6
|
+
* need not be a boolean every time — an optional prop, a `boolean | string`
|
|
7
|
+
* union or a `??` fallback may also be absent or another type — so the
|
|
8
|
+
* backend routes it through its JS `String` helper, which prints every value
|
|
9
|
+
* kind as JS does, rather than through `bool_str`.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { ParsedExpr } from '../expression-parser.ts'
|
|
13
|
+
import type { IRMetadata, TypeInfo } from '../types.ts'
|
|
14
|
+
|
|
15
|
+
/** Whether a declared type admits a boolean: `boolean`, a boolean literal, or a union with such a member. */
|
|
16
|
+
function admitsBoolean(type: TypeInfo | null | undefined): boolean {
|
|
17
|
+
if (!type) return false
|
|
18
|
+
if (type.kind === 'primitive') return type.primitive === 'boolean'
|
|
19
|
+
return type.kind === 'union' && (type.unionTypes ?? []).some(admitsBoolean)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Signals, memos and props whose declared type admits a boolean, optional or
|
|
24
|
+
* not, plus signals seeded with a boolean literal and no declared type.
|
|
25
|
+
* `names` holds the bare spellings (a getter, a memo, a destructured prop),
|
|
26
|
+
* `props` the caller-facing keys a `props.<key>` member reads. Whether a
|
|
27
|
+
* binding shadows a name is the caller's scope question.
|
|
28
|
+
*/
|
|
29
|
+
export function collectBooleanCarrierNames(
|
|
30
|
+
metadata: Pick<IRMetadata, 'signals' | 'memos' | 'propsParams' | 'propsObjectName'>,
|
|
31
|
+
): { names: Set<string>; props: Set<string> } {
|
|
32
|
+
const names = new Set<string>()
|
|
33
|
+
const props = new Set<string>()
|
|
34
|
+
for (const signal of metadata.signals) {
|
|
35
|
+
const untyped = !signal.type || signal.type.kind === 'unknown'
|
|
36
|
+
const booleanSeed = signal.parsed?.kind === 'literal' && signal.parsed.literalType === 'boolean'
|
|
37
|
+
if (admitsBoolean(signal.type) || (untyped && booleanSeed)) names.add(signal.getter)
|
|
38
|
+
}
|
|
39
|
+
for (const memo of metadata.memos ?? []) {
|
|
40
|
+
if (admitsBoolean(memo.type)) names.add(memo.name)
|
|
41
|
+
}
|
|
42
|
+
for (const param of metadata.propsParams) {
|
|
43
|
+
// A destructured union-typed prop keeps its written type in `declaredType`.
|
|
44
|
+
if (!admitsBoolean(param.declaredType ?? param.type)) continue
|
|
45
|
+
props.add(param.sourceName ?? param.name)
|
|
46
|
+
if (!metadata.propsObjectName) names.add(param.name)
|
|
47
|
+
}
|
|
48
|
+
return { names, props }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether `expr` may evaluate to the value of a name `isCarrierName`
|
|
53
|
+
* accepts — a zero-argument getter call, a `props.<key>` member or a bare
|
|
54
|
+
* identifier — read directly, as an operand of `??` / `||` / `&&` (JS
|
|
55
|
+
* returns an operand, not a coerced boolean), or as a ternary branch.
|
|
56
|
+
*/
|
|
57
|
+
export function mayYieldBooleanCarrier(
|
|
58
|
+
expr: ParsedExpr | null | undefined,
|
|
59
|
+
isCarrierName: (name: string, componentProperty?: boolean) => boolean,
|
|
60
|
+
): boolean {
|
|
61
|
+
if (!expr) return false
|
|
62
|
+
switch (expr.kind) {
|
|
63
|
+
case 'logical':
|
|
64
|
+
return mayYieldBooleanCarrier(expr.left, isCarrierName) || mayYieldBooleanCarrier(expr.right, isCarrierName)
|
|
65
|
+
case 'conditional':
|
|
66
|
+
return mayYieldBooleanCarrier(expr.consequent, isCarrierName) || mayYieldBooleanCarrier(expr.alternate, isCarrierName)
|
|
67
|
+
case 'call':
|
|
68
|
+
return expr.callee.kind === 'identifier' && expr.args.length === 0 && isCarrierName(expr.callee.name)
|
|
69
|
+
case 'member':
|
|
70
|
+
return !expr.computed && expr.object.kind === 'identifier' && expr.object.name === 'props' && isCarrierName(expr.property, true)
|
|
71
|
+
case 'identifier':
|
|
72
|
+
return isCarrierName(expr.name)
|
|
73
|
+
default:
|
|
74
|
+
return false
|
|
75
|
+
}
|
|
76
|
+
}
|