@barefootjs/jsx 0.40.0 → 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.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 +18 -1
- package/dist/adapters/dsl-template-adapter.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/expression-parser.d.ts.map +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1006 -351
- 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 +8 -0
- 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 +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 +22 -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.map +1 -1
- package/dist/types.d.ts +30 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/binding-scope-ratchet.test.ts +12 -2
- package/src/__tests__/boolean-carrier.test.ts +74 -0
- package/src/__tests__/component-props.test.ts +14 -0
- package/src/__tests__/computed-const.test.ts +85 -0
- package/src/__tests__/expression-parser.test.ts +24 -0
- package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +142 -0
- 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 +19 -0
- package/src/adapters/ambiguous-nil-arithmetic.ts +94 -0
- package/src/adapters/attr-plan.ts +6 -2
- 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 +39 -0
- package/src/adapters/number-value.ts +95 -0
- package/src/analyzer-context.ts +8 -0
- package/src/analyzer.ts +187 -66
- package/src/expression-parser.ts +4 -1
- package/src/index.ts +12 -2
- package/src/ir-to-client-js/collect-elements.ts +104 -46
- package/src/ir-to-client-js/compute-inlinability.ts +1 -1
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +15 -1
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +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 +70 -30
- 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 +15 -1
- package/src/jsx-to-ir.ts +78 -35
- package/src/nullable-signal-attr.ts +134 -13
- package/src/props-binding.ts +54 -3
- package/src/scope/binding-scope.ts +30 -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 +21 -1
- package/src/types.ts +30 -0
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import { describe, test, expect } from 'bun:test'
|
|
2
2
|
import { analyzeComponent } from '../analyzer'
|
|
3
|
-
import { parseExpression } from '../expression-parser'
|
|
3
|
+
import { exprToString, parseExpression } from '../expression-parser'
|
|
4
4
|
import {
|
|
5
|
+
ambiguousNilArithmeticOperands,
|
|
5
6
|
arithmeticOperandCoercion,
|
|
7
|
+
arithmeticOperandIsNull,
|
|
6
8
|
arithmeticOperandIsUndefined,
|
|
9
|
+
arithmeticOperandNilIsAmbiguous,
|
|
7
10
|
attrValueMayBeNullish,
|
|
8
11
|
collectAbsentPropSeedReads,
|
|
9
12
|
collectNullableSignalGetters,
|
|
10
13
|
collectNullishAttrContext,
|
|
11
14
|
nullableSignalAttrGetter,
|
|
12
15
|
} from '../nullable-signal-attr'
|
|
16
|
+
import { BindingScope } from '../scope/binding-scope'
|
|
13
17
|
import type { ComponentIR } from '../types'
|
|
14
18
|
|
|
15
19
|
function nullableGetters(decls: string): Set<string> {
|
|
@@ -120,6 +124,7 @@ describe('attrValueMayBeNullish (#3322)', () => {
|
|
|
120
124
|
const plain = createMemo(() => t())
|
|
121
125
|
const typed = createMemo<string | undefined>(() => t())
|
|
122
126
|
const loop = createMemo(() => loop())
|
|
127
|
+
const tail = createMemo(() => t().at(-1))
|
|
123
128
|
return <p>x</p>
|
|
124
129
|
}
|
|
125
130
|
`
|
|
@@ -157,6 +162,20 @@ describe('attrValueMayBeNullish (#3322)', () => {
|
|
|
157
162
|
expect(mayBe('0')).toBe(false)
|
|
158
163
|
})
|
|
159
164
|
|
|
165
|
+
test('`.at(i)` / `.find()` / `.findLast()` read undefined, directly or through a memo', () => {
|
|
166
|
+
expect(mayBe('t().at(-1)')).toBe(true)
|
|
167
|
+
expect(mayBe('tail()')).toBe(true)
|
|
168
|
+
expect(mayBe("t() ? t().at(9) : 'x'")).toBe(true)
|
|
169
|
+
expect(mayBe("t().at(-1) ?? 'x'")).toBe(false)
|
|
170
|
+
expect(mayBe('t().slice(-1)')).toBe(false)
|
|
171
|
+
expect(mayBe("t().indexOf('a')")).toBe(false)
|
|
172
|
+
expect(mayBe("xs.find(x => x === 'z')")).toBe(true)
|
|
173
|
+
expect(mayBe('xs.findLast(x => x)')).toBe(true)
|
|
174
|
+
expect(mayBe("xs.find(x => x === 'z').name")).toBe(true)
|
|
175
|
+
expect(mayBe('xs.findIndex(x => x)')).toBe(false)
|
|
176
|
+
expect(mayBe('xs.filter(x => x)')).toBe(false)
|
|
177
|
+
})
|
|
178
|
+
|
|
160
179
|
test('a getter a loop binding shadows is not the signal or memo', () => {
|
|
161
180
|
expect(mayBe('label()', ['label'])).toBe(false)
|
|
162
181
|
})
|
|
@@ -196,7 +215,8 @@ describe('arithmeticOperandCoercion (#3474)', () => {
|
|
|
196
215
|
for (const op of ['-', '*', '/', '%', '**']) expect(coercion(ctx, op, 'props.b', 'x')).toBe('undefined')
|
|
197
216
|
expect(coercion(ctx, '+', 'props.b', '-2')).toBe('undefined')
|
|
198
217
|
expect(coercion(ctx, '*', 'props.u', '2')).toBe('undefined')
|
|
199
|
-
// A type that admits both keeps the null
|
|
218
|
+
// A type that admits both keeps the null spelling; the arithmetic over it
|
|
219
|
+
// is refused (`arithmeticOperandNilIsAmbiguous`, #3700).
|
|
200
220
|
expect(coercion(ctx, '*', 'props.both', '2')).toBe('null')
|
|
201
221
|
})
|
|
202
222
|
|
|
@@ -231,3 +251,78 @@ describe('arithmeticOperandCoercion (#3474)', () => {
|
|
|
231
251
|
expect(coercion(ctxOf('', 'const [t] = createSignal<number>()'), '+', 't()', '1')).toBe('undefined')
|
|
232
252
|
})
|
|
233
253
|
})
|
|
254
|
+
|
|
255
|
+
describe('arithmeticOperandNilIsAmbiguous (#3700)', () => {
|
|
256
|
+
function ctxOf(signature: string, decls = ''): ReturnType<typeof collectNullishAttrContext> {
|
|
257
|
+
const source = `
|
|
258
|
+
'use client'
|
|
259
|
+
import { createMemo } from '@barefootjs/client'
|
|
260
|
+
export function C(${signature}) {
|
|
261
|
+
${decls}
|
|
262
|
+
return <p>x</p>
|
|
263
|
+
}
|
|
264
|
+
`
|
|
265
|
+
return collectNullishAttrContext({ metadata: analyzeComponent(source, 'C.tsx') } as unknown as ComponentIR)
|
|
266
|
+
}
|
|
267
|
+
const ambiguous = (ctx: ReturnType<typeof collectNullishAttrContext>, op: string, operand: string, other?: string) =>
|
|
268
|
+
arithmeticOperandNilIsAmbiguous(op, parseExpression(operand), other === undefined ? undefined : parseExpression(other), ctx)
|
|
269
|
+
|
|
270
|
+
test('a number prop that admits both null and absence, in either props form', () => {
|
|
271
|
+
for (const signature of ['props: { n?: number | null }', 'props: { n: number | null | undefined }']) {
|
|
272
|
+
const ctx = ctxOf(signature)
|
|
273
|
+
for (const op of ['-', '*', '/', '%', '**']) expect(ambiguous(ctx, op, 'props.n', 'x')).toBe(true)
|
|
274
|
+
expect(ambiguous(ctx, '+', 'props.n', '1')).toBe(true)
|
|
275
|
+
expect(ambiguous(ctx, '-', 'props.n')).toBe(true)
|
|
276
|
+
// `+` against a non-number may concatenate: no `ToNumber`, no ambiguity.
|
|
277
|
+
expect(ambiguous(ctx, '+', 'props.n', "'px'")).toBe(false)
|
|
278
|
+
}
|
|
279
|
+
const destructured = ctxOf('{ n, m: alias }: { n?: number | null; m?: null | number }')
|
|
280
|
+
expect(ambiguous(destructured, '*', 'n', '2')).toBe(true)
|
|
281
|
+
expect(ambiguous(destructured, '*', 'alias', '2')).toBe(true)
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
test('a prop with one nil reading, a defaulted one and a shadowed one are not ambiguous', () => {
|
|
285
|
+
const ctx = ctxOf('props: { a: number | null; b?: number; c: number }')
|
|
286
|
+
for (const operand of ['props.a', 'props.b', 'props.c']) expect(ambiguous(ctx, '*', operand, '2')).toBe(false)
|
|
287
|
+
expect(ambiguous(ctxOf('{ n = 1 }: { n?: number | null }'), '*', 'n', '2')).toBe(false)
|
|
288
|
+
const destructured = ctxOf('{ n }: { n?: number | null }')
|
|
289
|
+
expect(arithmeticOperandNilIsAmbiguous('*', parseExpression('n'), parseExpression('2'), destructured, name => name === 'n')).toBe(false)
|
|
290
|
+
})
|
|
291
|
+
|
|
292
|
+
test('an identity memo over the prop is ambiguous too; the null spelling is kept for it', () => {
|
|
293
|
+
const ctx = ctxOf('props: { n?: number | null }', 'const m = createMemo(() => props.n)')
|
|
294
|
+
expect(ambiguous(ctx, '*', 'm()', '2')).toBe(true)
|
|
295
|
+
expect(arithmeticOperandCoercion('*', parseExpression('m()'), parseExpression('2'), ctx)).toBe('null')
|
|
296
|
+
expect(arithmeticOperandIsNull(parseExpression('m()'), ctx)).toBe(true)
|
|
297
|
+
})
|
|
298
|
+
|
|
299
|
+
test('a destructured union binding keeps its declared type for the classification', () => {
|
|
300
|
+
const ctx = ctxOf('{ a, u }: { a: number | null; u?: number | undefined }')
|
|
301
|
+
expect(arithmeticOperandCoercion('*', parseExpression('a'), parseExpression('2'), ctx)).toBe('null')
|
|
302
|
+
expect(arithmeticOperandCoercion('*', parseExpression('u'), parseExpression('2'), ctx)).toBe('undefined')
|
|
303
|
+
})
|
|
304
|
+
})
|
|
305
|
+
|
|
306
|
+
describe('ambiguousNilArithmeticOperands (#3700)', () => {
|
|
307
|
+
const ctx = collectNullishAttrContext({
|
|
308
|
+
metadata: analyzeComponent(
|
|
309
|
+
`export function C({ n, items }: { n?: number | null; items: number[] }) { return <p>x</p> }`,
|
|
310
|
+
'C.tsx',
|
|
311
|
+
),
|
|
312
|
+
} as unknown as ComponentIR)
|
|
313
|
+
const found = (source: string, scope?: BindingScope) =>
|
|
314
|
+
ambiguousNilArithmeticOperands(parseExpression(source), ctx, scope).map(exprToString)
|
|
315
|
+
|
|
316
|
+
test('finds the operands in nested positions', () => {
|
|
317
|
+
expect(found("items.filter(i => i > n * 2).length")).toEqual(['n'])
|
|
318
|
+
expect(found("(-n > 1 ? `a${n + 1}` : 'b')")).toEqual(['n', 'n'])
|
|
319
|
+
})
|
|
320
|
+
|
|
321
|
+
test('an arrow param or an enclosing binding of the same name shadows the prop', () => {
|
|
322
|
+
expect(found("items.filter(n => n * 2 > 1).length")).toEqual([])
|
|
323
|
+
expect(found("items.map(i => items.filter(n => n > i).length * n)")).toEqual(['n'])
|
|
324
|
+
expect(found('n * 2', BindingScope.EMPTY.enterCallback(['n']))).toEqual([])
|
|
325
|
+
// A sibling arrow's param does not leak out of its own body.
|
|
326
|
+
expect(found('items.filter(n => n > 1).length * n')).toEqual(['n'])
|
|
327
|
+
})
|
|
328
|
+
})
|
|
@@ -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
|
})
|
|
@@ -20,3 +20,22 @@ test('template expression projection rewrites only the expression part', () => {
|
|
|
20
20
|
], { useTemplate: true })
|
|
21
21
|
expect(new Function('_p', `return ${source}`)({ value: 'tail' })).toBe('${value} tail')
|
|
22
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
|
+
}
|
|
@@ -87,8 +87,12 @@ function nullableBareRead(value: ExpressionAttr, ctx: AttrPlanContext): { restKe
|
|
|
87
87
|
const bare = value.expr.trim()
|
|
88
88
|
const restKey = ctx.restPropsName !== null && bare.startsWith(`${ctx.restPropsName}.`)
|
|
89
89
|
let normalized = bare
|
|
90
|
-
if (ctx.propsObjectName && bare.startsWith(`${ctx.propsObjectName}.`))
|
|
91
|
-
|
|
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)
|
|
92
96
|
const prop = IDENTIFIER.test(normalized) && ctx.nullableOptionalProps.has(normalized) && !ctx.isLoopBound(normalized)
|
|
93
97
|
const signal = nullableSignalAttrGetter(value.parsed, ctx.nullableSignalGetters)
|
|
94
98
|
if (prop || (signal !== null && !ctx.isLoopBound(signal))) return { restKey }
|