@barefootjs/jsx 0.40.1 → 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 +4 -3
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -1
- 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/computed-const.d.ts +11 -1
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +21 -0
- 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 +5 -3
- package/dist/adapters/number-value.d.ts.map +1 -1
- 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 +4 -0
- package/dist/analyzer-context.d.ts.map +1 -1
- 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 +19 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4628 -2198
- 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-inner-loop.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/shared.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +4 -2
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
- 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 +30 -18
- 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/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/jsx-to-ir.d.ts +1 -0
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +68 -9
- 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-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 +85 -10
- 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 +10 -7
- package/dist/scope/root-prop-alias.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-position-walk.d.ts +21 -3
- package/dist/template-position-walk.d.ts.map +1 -1
- package/dist/types.d.ts +90 -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.test.ts +89 -1
- package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -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__/computed-const.test.ts +103 -0
- package/src/__tests__/emit-to-string-call.test.ts +2 -1
- 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 +2 -1
- 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-type.test.ts +64 -0
- package/src/__tests__/lowering-value-position.test.ts +4 -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 +134 -0
- package/src/__tests__/primitive-plan.test.ts +416 -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 +53 -8
- 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/adapters/ambiguous-nil-arithmetic.ts +22 -13
- 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/computed-const.ts +238 -4
- package/src/adapters/dsl-template-adapter.ts +40 -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 +17 -4
- 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 +6 -0
- package/src/analyzer.ts +505 -19
- 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 +70 -1
- package/src/index.ts +33 -7
- 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 +30 -55
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +6 -1
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +29 -28
- package/src/ir-to-client-js/control-flow/shared.ts +20 -22
- package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +5 -3
- package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +2 -2
- package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +1 -1
- package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +2 -2
- package/src/ir-to-client-js/control-flow/stringify/loop.ts +1 -1
- package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +3 -3
- package/src/ir-to-client-js/html-template.ts +25 -8
- 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 +31 -14
- package/src/ir-to-client-js/plan/static-array-child-init.ts +27 -18
- package/src/ir-to-client-js/prop-handling.ts +22 -3
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +26 -10
- package/src/jsx-to-ir.ts +446 -37
- package/src/nullable-signal-attr.ts +246 -29
- package/src/prop-rewrite.ts +30 -6
- package/src/props-object-whole-read.ts +87 -0
- package/src/rich-type-refusal.ts +2 -1
- package/src/scope/binding-scope.ts +182 -18
- package/src/scope/lexical-scope-walk.ts +4 -3
- package/src/scope/root-prop-alias.ts +15 -8
- package/src/strip-types.ts +31 -3
- package/src/template-position-walk.ts +57 -28
- package/src/types.ts +87 -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
|
+
})
|
|
@@ -67,19 +67,19 @@ describe('the props object a binding named props shadows (#3693)', () => {
|
|
|
67
67
|
const isBoundIn = (scope: BindingScope) => (name: string) => scope.isBound(name)
|
|
68
68
|
|
|
69
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)
|
|
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
73
|
const destructured = BindingScope.EMPTY.enterLoopRow({ param: '{ props }', paramBindings: [{ name: 'props' }] })
|
|
74
|
-
expect(isRootPropsObject(props, isBoundIn(destructured))).toBe(false)
|
|
74
|
+
expect(isRootPropsObject(props, isBoundIn(destructured), 'props')).toBe(false)
|
|
75
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)
|
|
76
|
+
expect(isRootPropsObject(props, isBoundIn(preamble), 'props')).toBe(false)
|
|
77
|
+
expect(isRootPropsObject(props, isBoundIn(BindingScope.EMPTY.enterCallback(['props'])), 'props')).toBe(false)
|
|
78
78
|
})
|
|
79
79
|
|
|
80
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)
|
|
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
83
|
})
|
|
84
84
|
|
|
85
85
|
test('a props.<name> read has no component typing while props is shadowed', () => {
|
|
@@ -93,3 +93,48 @@ describe('the props object a binding named props shadows (#3693)', () => {
|
|
|
93
93
|
expect(row.isStringValueName('label', () => true)).toBe(true)
|
|
94
94
|
})
|
|
95
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
|
+
})
|
|
@@ -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
|
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { lookupStaticArrayElement, lookupStaticRecordLiteral } from '../augment-inherited-props.ts'
|
|
3
|
+
import { parseExpression } from '../expression-parser.ts'
|
|
4
|
+
import { analyzeComponent } from '../analyzer.ts'
|
|
5
|
+
import { buildMetadata } from '../compiler.ts'
|
|
6
|
+
import type { ConstantInfo } from '../types.ts'
|
|
7
|
+
|
|
8
|
+
const loc = { file: 'c.tsx', start: { line: 1, column: 0 }, end: { line: 1, column: 0 } }
|
|
9
|
+
|
|
10
|
+
function moduleConst(name: string, value: string, extra: Partial<ConstantInfo> = {}): ConstantInfo {
|
|
11
|
+
return {
|
|
12
|
+
name,
|
|
13
|
+
value: value as ConstantInfo['value'],
|
|
14
|
+
parsed: parseExpression(value),
|
|
15
|
+
declarationKind: 'const',
|
|
16
|
+
isModule: true,
|
|
17
|
+
type: null,
|
|
18
|
+
loc,
|
|
19
|
+
...extra,
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const notShadowed = () => false
|
|
24
|
+
const num = (value: number) => ({ kind: 'literal', value, literalType: 'number', raw: String(value) })
|
|
25
|
+
const str = (value: string) => ({ kind: 'literal', value, literalType: 'string' })
|
|
26
|
+
|
|
27
|
+
describe('lookupStaticArrayElement', () => {
|
|
28
|
+
const QS = moduleConst('QS', `['a1', "b\\"c", \`m1\nm2\`, 1e3, 0x10, .5, -0, -1.5]`)
|
|
29
|
+
const read = (key: string) => lookupStaticArrayElement('QS', key, [QS], notShadowed)
|
|
30
|
+
|
|
31
|
+
test('a canonical index reads the element as a typed node', () => {
|
|
32
|
+
expect(read('0')).toEqual(str('a1'))
|
|
33
|
+
expect(read('1')).toEqual(str('b"c'))
|
|
34
|
+
expect(read('2')).toEqual(str('m1\nm2'))
|
|
35
|
+
expect(read('3')).toEqual(num(1000))
|
|
36
|
+
expect(read('4')).toEqual(num(16))
|
|
37
|
+
expect(read('5')).toEqual(num(0.5))
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
test('a negated element stays a unary minus, so -0 keeps its sign', () => {
|
|
41
|
+
expect(read('6')).toEqual({ kind: 'unary', op: '-', argument: num(0) })
|
|
42
|
+
expect(read('7')).toEqual({ kind: 'unary', op: '-', argument: num(1.5) })
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
test('length is the element count', () => {
|
|
46
|
+
expect(read('length')).toEqual(num(8))
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test('a key that names no element falls back to the generic lowering', () => {
|
|
50
|
+
expect(read('01')).toBeNull()
|
|
51
|
+
expect(read('8')).toBeNull()
|
|
52
|
+
expect(read('-1')).toBeNull()
|
|
53
|
+
expect(read('x')).toBeNull()
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('a hole, a spread or a non-literal element is not resolved', () => {
|
|
57
|
+
const holes = moduleConst('H', `['a', , 'c']`)
|
|
58
|
+
expect(lookupStaticArrayElement('H', '0', [holes], notShadowed)).toEqual(str('a'))
|
|
59
|
+
expect(lookupStaticArrayElement('H', '2', [holes], notShadowed)).toBeNull()
|
|
60
|
+
expect(lookupStaticArrayElement('H', 'length', [holes], notShadowed)).toBeNull()
|
|
61
|
+
const spread = moduleConst('S', `[...xs, 'b']`)
|
|
62
|
+
expect(lookupStaticArrayElement('S', '1', [spread], notShadowed)).toBeNull()
|
|
63
|
+
const ref = moduleConst('R', `[other, 'b']`)
|
|
64
|
+
expect(lookupStaticArrayElement('R', '0', [ref], notShadowed)).toBeNull()
|
|
65
|
+
expect(lookupStaticArrayElement('R', '1', [ref], notShadowed)).toEqual(str('b'))
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test('a loop binding, a component binding or a later mutation keeps the read live', () => {
|
|
69
|
+
expect(lookupStaticArrayElement('QS', '0', [QS], name => name === 'QS')).toBeNull()
|
|
70
|
+
const shadowed = moduleConst('QS', `['a1']`, { shadowedInComponent: true })
|
|
71
|
+
expect(lookupStaticArrayElement('QS', '0', [shadowed], notShadowed)).toBeNull()
|
|
72
|
+
const mutated = moduleConst('QS', `['a1']`, { mutatedAfterDeclaration: true })
|
|
73
|
+
expect(lookupStaticArrayElement('QS', 'length', [mutated], notShadowed)).toBeNull()
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test('a record const is not an array, and the record lookup no longer reads arrays', () => {
|
|
77
|
+
const rec = moduleConst('REC', `{ lf: 'r1' }`)
|
|
78
|
+
expect(lookupStaticArrayElement('REC', 'lf', [rec], notShadowed)).toBeNull()
|
|
79
|
+
expect(lookupStaticRecordLiteral('QS', '0', [QS], notShadowed)).toBeNull()
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
describe('resolveStaticConstBinding: a component-local literal const', () => {
|
|
84
|
+
const constantsOf = (source: string) => buildMetadata(analyzeComponent(source, 'c.tsx')).localConstants
|
|
85
|
+
const element = (source: string, key = '0') => lookupStaticArrayElement('QS', key, constantsOf(source), notShadowed)
|
|
86
|
+
const member = (source: string, key = 'k') => lookupStaticRecordLiteral('REC', key, constantsOf(source), notShadowed)
|
|
87
|
+
|
|
88
|
+
test('a component-local array or record const resolves like a module const', () => {
|
|
89
|
+
const source = `export function C() {
|
|
90
|
+
const QS = ['a', 'b']
|
|
91
|
+
const REC = { k: 'v', n: 3 }
|
|
92
|
+
return <p>{QS[0]}{REC.k}</p>
|
|
93
|
+
}`
|
|
94
|
+
expect(element(source, '1')).toEqual(str('b'))
|
|
95
|
+
expect(element(source, 'length')).toEqual(num(2))
|
|
96
|
+
expect(member(source)).toEqual({ kind: 'string', text: 'v' })
|
|
97
|
+
expect(member(source, 'n')).toEqual({ kind: 'number', text: '3' })
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
test('a component-local const shadows the module const of the same name', () => {
|
|
101
|
+
const source = `const QS = ['module']
|
|
102
|
+
const REC = { k: 'module' }
|
|
103
|
+
export function C() {
|
|
104
|
+
const QS = ['local']
|
|
105
|
+
const REC = { k: 'local' }
|
|
106
|
+
return <p>{QS[0]}{REC.k}</p>
|
|
107
|
+
}`
|
|
108
|
+
expect(element(source)).toEqual(str('local'))
|
|
109
|
+
expect(member(source)).toEqual({ kind: 'string', text: 'local' })
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
test('a loop binding of the name keeps the read live', () => {
|
|
113
|
+
const constants = constantsOf(`export function C() {
|
|
114
|
+
const QS = ['a']
|
|
115
|
+
return <p>{QS[0]}</p>
|
|
116
|
+
}`)
|
|
117
|
+
expect(lookupStaticArrayElement('QS', '0', constants, name => name === 'QS')).toBeNull()
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
test('a const the component changes during render is not resolved, local or module', () => {
|
|
121
|
+
expect(element(`export function C() {
|
|
122
|
+
const QS = ['a']
|
|
123
|
+
QS.push('b')
|
|
124
|
+
return <p>{QS[1]}</p>
|
|
125
|
+
}`, '1')).toBeNull()
|
|
126
|
+
expect(member(`export function C() {
|
|
127
|
+
const REC = { k: 'v' }
|
|
128
|
+
REC.k = 'w'
|
|
129
|
+
return <p>{REC.k}</p>
|
|
130
|
+
}`)).toBeNull()
|
|
131
|
+
expect(member(`const REC = { k: 'v' }
|
|
132
|
+
export function C() {
|
|
133
|
+
REC.k = 'w'
|
|
134
|
+
return <p>{REC.k}</p>
|
|
135
|
+
}`)).toBeNull()
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('a let is not resolved', () => {
|
|
139
|
+
expect(element(`export function C() {
|
|
140
|
+
let QS = ['a']
|
|
141
|
+
return <p>{QS[0]}</p>
|
|
142
|
+
}`)).toBeNull()
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
test("an early-return branch's own const does not leak into the main return", () => {
|
|
146
|
+
const source = `export function C({ on }: { on: boolean }) {
|
|
147
|
+
if (on) {
|
|
148
|
+
const QS = ['inner']
|
|
149
|
+
return <p>{QS[0]}</p>
|
|
150
|
+
}
|
|
151
|
+
const QS = ['outer']
|
|
152
|
+
return <p>{QS[0]}</p>
|
|
153
|
+
}`
|
|
154
|
+
expect(element(source)).toEqual(str('outer'))
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
test('each component of a file reads its own declaration', () => {
|
|
158
|
+
const source = `export function A() {
|
|
159
|
+
const QS = ['a']
|
|
160
|
+
return <p>{QS[0]}</p>
|
|
161
|
+
}
|
|
162
|
+
export function B() {
|
|
163
|
+
const QS = ['b']
|
|
164
|
+
return <p>{QS[0]}</p>
|
|
165
|
+
}`
|
|
166
|
+
const read = (name: string) =>
|
|
167
|
+
lookupStaticArrayElement('QS', '0', buildMetadata(analyzeComponent(source, 'c.tsx', name)).localConstants, notShadowed)
|
|
168
|
+
expect(read('A')).toEqual(str('a'))
|
|
169
|
+
expect(read('B')).toEqual(str('b'))
|
|
170
|
+
})
|
|
171
|
+
})
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { describe, test, expect } from 'bun:test'
|
|
2
|
-
import { isStringTypedOperand, isStringConcatBinary } from '../adapters/parsed-expr-emitter'
|
|
2
|
+
import { isStringTypedOperand, isStringConcatBinary, type StringNameTest } from '../adapters/parsed-expr-emitter'
|
|
3
|
+
import { BindingScope } from '../scope/binding-scope'
|
|
3
4
|
import { parseExpression } from '../expression-parser'
|
|
4
5
|
|
|
5
6
|
// #2212: `isStringTypedOperand` gained a bare-`identifier` arm — previously
|
|
@@ -53,3 +54,41 @@ describe('isStringTypedOperand identifier arm (#2212)', () => {
|
|
|
53
54
|
expect(isStringConcatBinary(parsed.op, parsed.left, parsed.right, isStr)).toBe(false)
|
|
54
55
|
})
|
|
55
56
|
})
|
|
57
|
+
|
|
58
|
+
// #3762: a field read (`row.id`) is string-typed when the binding's declared
|
|
59
|
+
// row type says the field is; a member of a string-typed name is not.
|
|
60
|
+
describe('isStringTypedOperand member arm (#3762)', () => {
|
|
61
|
+
const rowType = {
|
|
62
|
+
raw: 'Row',
|
|
63
|
+
properties: [
|
|
64
|
+
{ name: 'id', type: { primitive: 'string' } },
|
|
65
|
+
{ name: 'n', type: { primitive: 'number' } },
|
|
66
|
+
{ name: 'meta', type: { properties: [{ name: 'label', type: { primitive: 'string' } }] } },
|
|
67
|
+
],
|
|
68
|
+
}
|
|
69
|
+
const scope = BindingScope.EMPTY.enterLoopRow({ param: 'r', index: 'i', rowType })
|
|
70
|
+
const isStr: StringNameTest = (n, property, path) =>
|
|
71
|
+
scope.isStringValueName(n, name => name === 'label', property, path)
|
|
72
|
+
const concat = (source: string) => {
|
|
73
|
+
const parsed = parseExpression(source)
|
|
74
|
+
if (parsed.kind !== 'binary') throw new Error('expected binary')
|
|
75
|
+
return isStringConcatBinary(parsed.op, parsed.left, parsed.right, isStr)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
test('a string row field, alone or nested, makes + a concatenation', () => {
|
|
79
|
+
expect(concat('r.id + i')).toBe(true)
|
|
80
|
+
expect(concat('r.n + r.id')).toBe(true)
|
|
81
|
+
expect(concat('r.meta.label + r.n')).toBe(true)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('a numeric field, an unknown field and a member of a string name stay numeric', () => {
|
|
85
|
+
expect(concat('r.n + i')).toBe(false)
|
|
86
|
+
expect(concat('r.missing + 1')).toBe(false)
|
|
87
|
+
expect(concat('label.length + 1')).toBe(false)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('a test that ignores the path never types a member as a string', () => {
|
|
91
|
+
const nameOnly: StringNameTest = n => n === 'label'
|
|
92
|
+
expect(isStringTypedOperand(parseExpression('label.length'), nameOnly)).toBe(false)
|
|
93
|
+
})
|
|
94
|
+
})
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { exprToString, type ParsedExpr } from '../expression-parser.ts'
|
|
2
2
|
import {
|
|
3
|
-
|
|
3
|
+
ambiguousNilFamily,
|
|
4
|
+
ambiguousNilOperands,
|
|
4
5
|
arithmeticOperandCoercion,
|
|
5
|
-
|
|
6
|
+
type AmbiguousNilFamily,
|
|
6
7
|
type ArithmeticCoercion,
|
|
7
8
|
type NullishAttrContext,
|
|
8
9
|
} from '../nullable-signal-attr.ts'
|
|
@@ -25,9 +26,10 @@ export interface AmbiguousNilArithmeticSink {
|
|
|
25
26
|
* optional and nullable (`n?: number | null`, `arithmeticOperandNilIsAmbiguous`).
|
|
26
27
|
* JS reads that prop as `undefined` when omitted (`NaN` in arithmetic) and
|
|
27
28
|
* as `0` when `null`; the engine holds both as one nil, so any spelling
|
|
28
|
-
* renders one of them wrong.
|
|
29
|
-
*
|
|
30
|
-
* operand
|
|
29
|
+
* renders one of them wrong. Under a comparison the same holds for a string
|
|
30
|
+
* or boolean prop declared `?: string | null` / `?: boolean | null` (#3824
|
|
31
|
+
* step 3). Asked at the emission site so the operand is classified in the
|
|
32
|
+
* scope it is read in (`isShadowed`). One error per operand text per compile.
|
|
31
33
|
*/
|
|
32
34
|
export function arithmeticCoercionOrRefuse(
|
|
33
35
|
op: string,
|
|
@@ -37,9 +39,8 @@ export function arithmeticCoercionOrRefuse(
|
|
|
37
39
|
isShadowed: (name: string) => boolean,
|
|
38
40
|
sink: AmbiguousNilArithmeticSink,
|
|
39
41
|
): ArithmeticCoercion {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
42
|
+
const family = ambiguousNilFamily(op, operand, other, ctx, isShadowed)
|
|
43
|
+
if (family !== null) recordAmbiguousNilArithmetic(exprToString(operand), family, sink)
|
|
43
44
|
return arithmeticOperandCoercion(op, operand, other, ctx, isShadowed)
|
|
44
45
|
}
|
|
45
46
|
|
|
@@ -56,8 +57,8 @@ export function refuseAmbiguousNilArithmetic(
|
|
|
56
57
|
scope: BindingScope,
|
|
57
58
|
sink: AmbiguousNilArithmeticSink,
|
|
58
59
|
): void {
|
|
59
|
-
for (const operand of
|
|
60
|
-
recordAmbiguousNilArithmetic(exprToString(operand), sink)
|
|
60
|
+
for (const { operand, family } of ambiguousNilOperands(expr, ctx, scope)) {
|
|
61
|
+
recordAmbiguousNilArithmetic(exprToString(operand), family, sink)
|
|
61
62
|
}
|
|
62
63
|
}
|
|
63
64
|
|
|
@@ -78,8 +79,8 @@ export function refuseAmbiguousNilMemoArithmetic(
|
|
|
78
79
|
}
|
|
79
80
|
}
|
|
80
81
|
|
|
81
|
-
function recordAmbiguousNilArithmetic(operandText: string, sink: AmbiguousNilArithmeticSink): void {
|
|
82
|
-
const message =
|
|
82
|
+
function recordAmbiguousNilArithmetic(operandText: string, family: AmbiguousNilFamily, sink: AmbiguousNilArithmeticSink): void {
|
|
83
|
+
const message = ambiguousNilMessage(operandText, family)
|
|
83
84
|
if (sink.errors.some(e => e.code === 'BF101' && e.message === message)) return
|
|
84
85
|
sink.errors.push({
|
|
85
86
|
code: 'BF101',
|
|
@@ -87,8 +88,16 @@ function recordAmbiguousNilArithmetic(operandText: string, sink: AmbiguousNilAri
|
|
|
87
88
|
message,
|
|
88
89
|
loc: { file: `${sink.componentName}.tsx`, start: { line: 1, column: 0 }, end: { line: 1, column: 0 } },
|
|
89
90
|
suggestion: {
|
|
90
|
-
message:
|
|
91
|
+
message: `Declare the prop as only optional (\`?: ${family}\`) or only nullable (\`: ${family} | null\`), or give it a default value.`,
|
|
91
92
|
escape: [{ kind: 'rewrite' }],
|
|
92
93
|
},
|
|
93
94
|
})
|
|
94
95
|
}
|
|
96
|
+
|
|
97
|
+
/** The BF101 text for an operand {@link ambiguousNilFamily} matched; the numeric one predates #3824 step 3. */
|
|
98
|
+
function ambiguousNilMessage(operandText: string, family: AmbiguousNilFamily): string {
|
|
99
|
+
if (family === 'number') {
|
|
100
|
+
return `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.`
|
|
101
|
+
}
|
|
102
|
+
return `A comparison over \`${operandText}\` reads a ${family} prop declared both optional and nullable (\`?: ${family} | null\`). JS reads it as \`undefined\` when the caller omits it (\`NaN\`, so the comparison is false) and as \`null\` when it is \`null\` (\`0\`), but a template engine holds both as the same nil, so the server render cannot tell which one it is.`
|
|
103
|
+
}
|