@barefootjs/jsx 0.40.2 → 0.41.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/callback-method.d.ts +8 -1
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/component-props.d.ts +14 -5
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +3 -12
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/loop-row-type.d.ts +7 -0
- package/dist/adapters/loop-row-type.d.ts.map +1 -1
- package/dist/adapters/nil-reading.d.ts +15 -20
- package/dist/adapters/nil-reading.d.ts.map +1 -1
- package/dist/adapters/non-primitive-equality.d.ts +1 -1
- package/dist/adapters/number-value.d.ts +2 -1
- package/dist/adapters/number-value.d.ts.map +1 -1
- package/dist/adapters/parsed-expr-emitter.d.ts +11 -1
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/adapters/primitive-plan.d.ts +40 -11
- package/dist/adapters/primitive-plan.d.ts.map +1 -1
- package/dist/adapters/prop-classes.d.ts +5 -0
- package/dist/adapters/prop-classes.d.ts.map +1 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/index.d.ts +6 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +566 -367
- package/dist/ir-to-client-js/child-props-object.d.ts +43 -1
- package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -1
- 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/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/phases/props-extraction.d.ts +3 -3
- package/dist/ir-to-client-js/plan/build-declaration-emit.d.ts.map +1 -1
- package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts +3 -1
- package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +4 -46
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/props-binding.d.ts +58 -13
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts +39 -3
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/types.d.ts +14 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +6 -6
- package/src/__tests__/body-destructured-prop-default-arrow.test.ts +2 -2
- package/src/__tests__/body-destructured-props-live.test.ts +5 -5
- package/src/__tests__/client-js-generation.test.ts +3 -3
- package/src/__tests__/destructure-default-reads-earlier-param.test.ts +7 -7
- package/src/__tests__/destructured-props-live.test.ts +1 -1
- package/src/__tests__/issue-3861-object-spread-child.test.ts +102 -0
- package/src/__tests__/memo-body-expr.test.ts +2 -2
- package/src/__tests__/nil-reading.test.ts +3 -2
- package/src/__tests__/nullable-signal-attr.test.ts +30 -103
- package/src/__tests__/primitive-plan.test.ts +90 -12
- package/src/__tests__/prop-default-undefined-only.test.ts +115 -0
- package/src/__tests__/prop-references.test.ts +2 -2
- package/src/__tests__/props-destructuring.test.ts +17 -0
- package/src/__tests__/prune-unused-prop-extractions.test.ts +3 -3
- package/src/__tests__/renamed-body-destructure-template.test.ts +7 -7
- package/src/__tests__/rewrite-destructured-props.test.ts +1 -1
- package/src/__tests__/ssr-defaults.test.ts +113 -25
- package/src/__tests__/static-const-child-prop.test.ts +85 -0
- package/src/__tests__/written-binding-substitution.test.ts +8 -8
- package/src/adapters/callback-method.ts +13 -5
- package/src/adapters/component-props.ts +20 -6
- package/src/adapters/dsl-template-adapter.ts +26 -28
- package/src/adapters/loop-row-type.ts +10 -0
- package/src/adapters/nil-reading.ts +23 -25
- package/src/adapters/non-primitive-equality.ts +1 -1
- package/src/adapters/number-value.ts +26 -2
- package/src/adapters/parsed-expr-emitter.ts +31 -2
- package/src/adapters/primitive-plan.ts +84 -15
- package/src/adapters/prop-classes.ts +16 -3
- package/src/analyzer.ts +27 -7
- package/src/index.ts +5 -12
- package/src/ir-to-client-js/child-props-object.ts +140 -6
- package/src/ir-to-client-js/collect-elements.ts +7 -14
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +4 -2
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +4 -2
- package/src/ir-to-client-js/html-template.ts +18 -9
- package/src/ir-to-client-js/imports.ts +1 -1
- package/src/ir-to-client-js/phases/props-extraction.ts +11 -7
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +40 -7
- package/src/ir-to-client-js/prune-unused-prop-extractions.ts +58 -5
- package/src/ir-to-client-js/rewrite-destructured-props.ts +31 -1
- package/src/jsx-to-ir.ts +113 -60
- package/src/nullable-signal-attr.ts +11 -138
- package/src/props-binding.ts +84 -19
- package/src/ssr-defaults.ts +100 -11
- package/src/types.ts +11 -0
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts +0 -42
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +0 -1
- package/src/adapters/ambiguous-nil-arithmetic.ts +0 -103
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import { describe, test, expect } from 'bun:test'
|
|
2
|
-
import { arithmeticCoercionOrRefuse, refuseAmbiguousNilArithmetic } from '../adapters/ambiguous-nil-arithmetic'
|
|
3
2
|
import { analyzeComponent } from '../analyzer'
|
|
4
|
-
import {
|
|
3
|
+
import { parseExpression } from '../expression-parser'
|
|
5
4
|
import {
|
|
6
|
-
ambiguousNilArithmeticOperands,
|
|
7
5
|
arithmeticOperandCoercion,
|
|
8
6
|
arithmeticOperandIsNull,
|
|
9
7
|
arithmeticOperandIsUndefined,
|
|
10
|
-
arithmeticOperandNilIsAmbiguous,
|
|
11
8
|
attrValueMayBeNullish,
|
|
12
9
|
collectAbsentPropSeedReads,
|
|
13
10
|
collectNullableSignalGetters,
|
|
@@ -16,7 +13,6 @@ import {
|
|
|
16
13
|
nullablePropsYieldedBy,
|
|
17
14
|
nullableSignalAttrGetter,
|
|
18
15
|
} from '../nullable-signal-attr'
|
|
19
|
-
import { BindingScope } from '../scope/binding-scope'
|
|
20
16
|
import type { ComponentIR } from '../types'
|
|
21
17
|
|
|
22
18
|
function nullableGetters(decls: string): Set<string> {
|
|
@@ -218,9 +214,9 @@ describe('arithmeticOperandCoercion (#3474)', () => {
|
|
|
218
214
|
for (const op of ['-', '*', '/', '%', '**']) expect(coercion(ctx, op, 'props.b', 'x')).toBe('undefined')
|
|
219
215
|
expect(coercion(ctx, '+', 'props.b', '-2')).toBe('undefined')
|
|
220
216
|
expect(coercion(ctx, '*', 'props.u', '2')).toBe('undefined')
|
|
221
|
-
// A type that admits both
|
|
222
|
-
//
|
|
223
|
-
expect(coercion(ctx, '*', 'props.both', '2')).
|
|
217
|
+
// A type that admits both is left to the runtime's helper, which reads
|
|
218
|
+
// `null` and `UNDEF` apart (spec/js-undefined-value.md).
|
|
219
|
+
expect(coercion(ctx, '*', 'props.both', '2')).toBeNull()
|
|
224
220
|
})
|
|
225
221
|
|
|
226
222
|
test('unary minus passes no other operand and coerces like binary minus (#3533)', () => {
|
|
@@ -279,7 +275,7 @@ describe('arithmeticOperandCoercion (#3474)', () => {
|
|
|
279
275
|
})
|
|
280
276
|
})
|
|
281
277
|
|
|
282
|
-
describe('
|
|
278
|
+
describe("an operand whose nil may be either is left to the runtime's helper", () => {
|
|
283
279
|
function ctxOf(signature: string, decls = ''): ReturnType<typeof collectNullishAttrContext> {
|
|
284
280
|
const source = `
|
|
285
281
|
'use client'
|
|
@@ -291,106 +287,58 @@ describe('arithmeticOperandNilIsAmbiguous (#3700)', () => {
|
|
|
291
287
|
`
|
|
292
288
|
return collectNullishAttrContext({ metadata: analyzeComponent(source, 'C.tsx') } as unknown as ComponentIR)
|
|
293
289
|
}
|
|
294
|
-
const
|
|
295
|
-
|
|
290
|
+
const coercion = (ctx: ReturnType<typeof collectNullishAttrContext>, op: string, operand: string, other?: string) =>
|
|
291
|
+
arithmeticOperandCoercion(op, parseExpression(operand), other === undefined ? undefined : parseExpression(other), ctx)
|
|
296
292
|
|
|
297
293
|
test('a number prop that admits both null and absence, in either props form', () => {
|
|
298
294
|
for (const signature of ['props: { n?: number | null }', 'props: { n: number | null | undefined }']) {
|
|
299
295
|
const ctx = ctxOf(signature)
|
|
300
|
-
for (const op of ['-', '*', '/', '%', '**']) expect(
|
|
301
|
-
expect(
|
|
302
|
-
expect(
|
|
303
|
-
expect(
|
|
304
|
-
// `+` against a non-number may concatenate: no `ToNumber`, no ambiguity.
|
|
305
|
-
expect(ambiguous(ctx, '+', 'props.n', "'px'")).toBe(false)
|
|
296
|
+
for (const op of ['-', '*', '/', '%', '**']) expect(coercion(ctx, op, 'props.n', 'x')).toBeNull()
|
|
297
|
+
expect(coercion(ctx, '+', 'props.n', '1')).toBeNull()
|
|
298
|
+
expect(coercion(ctx, '-', 'props.n')).toBeNull()
|
|
299
|
+
expect(coercion(ctx, '+', 'props.n')).toBeNull()
|
|
306
300
|
}
|
|
307
301
|
const destructured = ctxOf('{ n, m: alias }: { n?: number | null; m?: null | number }')
|
|
308
|
-
expect(
|
|
309
|
-
expect(
|
|
302
|
+
expect(coercion(destructured, '*', 'n', '2')).toBeNull()
|
|
303
|
+
expect(coercion(destructured, '*', 'alias', '2')).toBeNull()
|
|
310
304
|
})
|
|
311
305
|
|
|
312
|
-
test('a relational operand
|
|
306
|
+
test('a relational operand: only the one nil reading is spelled (#3824 step 3)', () => {
|
|
313
307
|
const ctx = ctxOf('props: { n?: number | null; a: number | null; b?: number }')
|
|
314
308
|
for (const op of ['<', '<=', '>', '>=']) {
|
|
315
|
-
expect(
|
|
316
|
-
expect(
|
|
317
|
-
expect(
|
|
318
|
-
expect(ambiguous(ctx, op, 'props.b', '1')).toBe(false)
|
|
309
|
+
expect(coercion(ctx, op, 'props.n', '1')).toBeNull()
|
|
310
|
+
expect(coercion(ctx, op, 'props.a', '1')).toBeNull()
|
|
311
|
+
expect(coercion(ctx, op, 'props.b', '1')).toBe('undefined')
|
|
319
312
|
}
|
|
320
|
-
// The spelling keeps the helper's null reading for the refused operand.
|
|
321
|
-
expect(arithmeticOperandCoercion('<', parseExpression('props.n'), parseExpression('1'), ctx)).toBeNull()
|
|
322
|
-
expect(arithmeticOperandCoercion('<', parseExpression('props.b'), parseExpression('1'), ctx)).toBe('undefined')
|
|
323
|
-
expect(arithmeticOperandCoercion('<', parseExpression('props.a'), parseExpression('1'), ctx)).toBeNull()
|
|
324
313
|
})
|
|
325
314
|
|
|
326
|
-
test('a string or boolean prop admitting both nils
|
|
315
|
+
test('a string or boolean prop admitting both nils under a comparison (#3824 step 3)', () => {
|
|
327
316
|
const ctx = ctxOf('props: { s?: string | null; b?: boolean | null; t?: string; m: string | null }', 'const id = createMemo(() => props.s)')
|
|
328
317
|
for (const op of ['<', '<=', '>', '>=']) {
|
|
329
|
-
expect(
|
|
330
|
-
expect(
|
|
331
|
-
expect(
|
|
332
|
-
expect(
|
|
333
|
-
expect(
|
|
334
|
-
expect(ambiguous(ctx, op, 'props.m', '1')).toBe(false)
|
|
318
|
+
expect(coercion(ctx, op, 'props.s', '1')).toBeNull()
|
|
319
|
+
expect(coercion(ctx, op, 'props.b', '1')).toBeNull()
|
|
320
|
+
expect(coercion(ctx, op, 'id()', '1')).toBeNull()
|
|
321
|
+
expect(coercion(ctx, op, 'props.t', '1')).toBe('undefined-keep')
|
|
322
|
+
expect(coercion(ctx, op, 'props.m', '1')).toBeNull()
|
|
335
323
|
}
|
|
336
|
-
expect(ambiguous(ctx, '*', 'props.s', '2')).toBe(false)
|
|
337
|
-
expect(ambiguous(ctx, '+', 'props.s')).toBe(false)
|
|
338
324
|
})
|
|
339
325
|
|
|
340
|
-
test('a
|
|
341
|
-
|
|
342
|
-
for (const operand of ['props.a', 'props.b', 'props.c']) expect(ambiguous(ctx, '*', operand, '2')).toBe(false)
|
|
343
|
-
expect(ambiguous(ctxOf('{ n = 1 }: { n?: number | null }'), '*', 'n', '2')).toBe(false)
|
|
326
|
+
test('a defaulted one and a shadowed one keep their own coercion', () => {
|
|
327
|
+
expect(coercion(ctxOf('{ n = 1 }: { n?: number | null }'), '*', 'n', '2')).toBeNull()
|
|
344
328
|
const destructured = ctxOf('{ n }: { n?: number | null }')
|
|
345
|
-
expect(
|
|
329
|
+
expect(arithmeticOperandCoercion('*', parseExpression('n'), parseExpression('2'), destructured, name => name === 'n')).toBeNull()
|
|
346
330
|
})
|
|
347
331
|
|
|
348
|
-
test('an identity memo over the prop is
|
|
332
|
+
test('an identity memo over the prop is left to the helper too', () => {
|
|
349
333
|
const ctx = ctxOf('props: { n?: number | null }', 'const m = createMemo(() => props.n)')
|
|
350
|
-
expect(
|
|
351
|
-
expect(arithmeticOperandCoercion('*', parseExpression('m()'), parseExpression('2'), ctx)).toBe('null')
|
|
334
|
+
expect(coercion(ctx, '*', 'm()', '2')).toBeNull()
|
|
352
335
|
expect(arithmeticOperandIsNull(parseExpression('m()'), ctx)).toBe(true)
|
|
353
336
|
})
|
|
354
337
|
|
|
355
338
|
test('a destructured union binding keeps its declared type for the classification', () => {
|
|
356
339
|
const ctx = ctxOf('{ a, u }: { a: number | null; u?: number | undefined }')
|
|
357
|
-
expect(
|
|
358
|
-
expect(
|
|
359
|
-
})
|
|
360
|
-
})
|
|
361
|
-
|
|
362
|
-
describe('ambiguousNilArithmeticOperands (#3700)', () => {
|
|
363
|
-
const ctx = collectNullishAttrContext({
|
|
364
|
-
metadata: analyzeComponent(
|
|
365
|
-
`export function C({ n, items }: { n?: number | null; items: number[] }) { return <p>x</p> }`,
|
|
366
|
-
'C.tsx',
|
|
367
|
-
),
|
|
368
|
-
} as unknown as ComponentIR)
|
|
369
|
-
const found = (source: string, scope?: BindingScope) =>
|
|
370
|
-
ambiguousNilArithmeticOperands(parseExpression(source), ctx, scope).map(exprToString)
|
|
371
|
-
|
|
372
|
-
test('finds the operands in nested positions', () => {
|
|
373
|
-
expect(found("items.filter(i => i > n * 2).length")).toEqual(['n'])
|
|
374
|
-
expect(found("(-n > 1 ? `a${n + 1}` : 'b')")).toEqual(['n', 'n'])
|
|
375
|
-
})
|
|
376
|
-
|
|
377
|
-
test('finds a unary plus operand (#3824 step 3)', () => {
|
|
378
|
-
expect(found('+n')).toEqual(['n'])
|
|
379
|
-
expect(found('+n > 1 ? 1 : 0')).toEqual(['n'])
|
|
380
|
-
})
|
|
381
|
-
|
|
382
|
-
test('finds a relational operand, in a predicate and at the top level', () => {
|
|
383
|
-
expect(found('items.filter(i => i < n).length')).toEqual(['n'])
|
|
384
|
-
expect(found('n >= 1')).toEqual(['n'])
|
|
385
|
-
expect(found('items.filter(n => n < 1).length')).toEqual([])
|
|
386
|
-
})
|
|
387
|
-
|
|
388
|
-
test('an arrow param or an enclosing binding of the same name shadows the prop', () => {
|
|
389
|
-
expect(found("items.filter(n => n * 2 > 1).length")).toEqual([])
|
|
390
|
-
expect(found("items.map(i => items.filter(n => n > i).length * n)")).toEqual(['n'])
|
|
391
|
-
expect(found('n * 2', BindingScope.EMPTY.enterCallback(['n']))).toEqual([])
|
|
392
|
-
// A sibling arrow's param does not leak out of its own body.
|
|
393
|
-
expect(found('items.filter(n => n > 1).length * n')).toEqual(['n'])
|
|
340
|
+
expect(coercion(ctx, '*', 'a', '2')).toBe('null')
|
|
341
|
+
expect(coercion(ctx, '*', 'u', '2')).toBe('undefined')
|
|
394
342
|
})
|
|
395
343
|
})
|
|
396
344
|
|
|
@@ -439,24 +387,3 @@ describe('a prop that can be absent, and an operand that is never nullish', () =
|
|
|
439
387
|
expect(isNeverNullishOperand(parseExpression('opt')!, destructured, () => false)).toBe(false)
|
|
440
388
|
})
|
|
441
389
|
})
|
|
442
|
-
|
|
443
|
-
describe('the BF101 of an ambiguous nil names the prop family (#3824 step 3)', () => {
|
|
444
|
-
const ctx = collectNullishAttrContext({
|
|
445
|
-
metadata: analyzeComponent(
|
|
446
|
-
`export function C(props: { n?: number | null; s?: string | null; b?: boolean | null }) { return <p>x</p> }`,
|
|
447
|
-
'C.tsx',
|
|
448
|
-
),
|
|
449
|
-
} as unknown as ComponentIR)
|
|
450
|
-
|
|
451
|
-
test('a numeric operand keeps the arithmetic message; a string or boolean one names its type', () => {
|
|
452
|
-
const sink = { errors: [] as import('../types').CompilerError[], componentName: 'C' }
|
|
453
|
-
arithmeticCoercionOrRefuse('*', parseExpression('props.n'), parseExpression('2'), ctx, () => false, sink)
|
|
454
|
-
arithmeticCoercionOrRefuse('<', parseExpression('props.s'), parseExpression('1'), ctx, () => false, sink)
|
|
455
|
-
refuseAmbiguousNilArithmetic(parseExpression('props.b >= 1'), ctx, BindingScope.EMPTY, sink)
|
|
456
|
-
expect(sink.errors.map(e => e.code)).toEqual(['BF101', 'BF101', 'BF101'])
|
|
457
|
-
expect(sink.errors[0]!.message).toStartWith('Arithmetic over `props.n` reads a number prop declared both optional and nullable (`?: number | null`).')
|
|
458
|
-
expect(sink.errors[1]!.message).toStartWith('A comparison over `props.s` reads a string prop declared both optional and nullable (`?: string | null`).')
|
|
459
|
-
expect(sink.errors[2]!.message).toStartWith('A comparison over `props.b` reads a boolean prop')
|
|
460
|
-
expect(sink.errors[1]!.suggestion?.message).toContain('`?: string`')
|
|
461
|
-
})
|
|
462
|
-
})
|
|
@@ -18,6 +18,11 @@ import {
|
|
|
18
18
|
foldNegatedNumberLiteral,
|
|
19
19
|
mayHoldObject,
|
|
20
20
|
numberLiteralSpelling,
|
|
21
|
+
collectNullableOptionalProps,
|
|
22
|
+
nilTestOperand,
|
|
23
|
+
exprToString,
|
|
24
|
+
refuseUnspelledNilTest,
|
|
25
|
+
strictEqSpelling,
|
|
21
26
|
type IRMetadata,
|
|
22
27
|
type NativeSafeTable,
|
|
23
28
|
type OperandFacts,
|
|
@@ -161,7 +166,12 @@ describe('operandFacts', () => {
|
|
|
161
166
|
})
|
|
162
167
|
|
|
163
168
|
const NONE: NativeSafeTable = {}
|
|
164
|
-
|
|
169
|
+
/**
|
|
170
|
+
* A table that claims `unproven` too, which the type forbids: the plan must
|
|
171
|
+
* still take the helper for an operand Phase 1 does not prove, since it may
|
|
172
|
+
* be `UNDEF` (spec/js-undefined-value.md).
|
|
173
|
+
*/
|
|
174
|
+
const CLAIMS_UNPROVEN = {
|
|
165
175
|
'to-boolean': ['boolean', 'unproven'],
|
|
166
176
|
'to-number': ['number-pair', 'number', 'unproven'],
|
|
167
177
|
plus: ['number-pair', 'string', 'unproven'],
|
|
@@ -171,7 +181,7 @@ const ALL: NativeSafeTable = {
|
|
|
171
181
|
'number-literal': ['safe-integer', 'unproven'],
|
|
172
182
|
nullish: ['non-nil', 'unproven'],
|
|
173
183
|
get: ['array-index', 'unproven'],
|
|
174
|
-
}
|
|
184
|
+
} as unknown as NativeSafeTable
|
|
175
185
|
const PROVEN_ONLY: NativeSafeTable = {
|
|
176
186
|
'to-boolean': ['boolean'],
|
|
177
187
|
'to-number': ['number-pair', 'number'],
|
|
@@ -198,7 +208,6 @@ describe('planPrimitive', () => {
|
|
|
198
208
|
test('ToBoolean (a test position) is native only for a proven boolean the target claims', () => {
|
|
199
209
|
expect(plan('!props.on', PROVEN_ONLY)).toBe('native to-boolean boolean')
|
|
200
210
|
expect(plan('!props.s', PROVEN_ONLY)).toBe('helper to-boolean truthy unproven')
|
|
201
|
-
expect(plan('!props.s', ALL)).toBe('native to-boolean unproven')
|
|
202
211
|
expect(plan('props.on ? 1 : 2', NONE)).toBe('helper to-boolean truthy boolean')
|
|
203
212
|
expect(plan('props.s && 1', PROVEN_ONLY)).toBe('helper to-boolean truthy unproven')
|
|
204
213
|
})
|
|
@@ -213,8 +222,8 @@ describe('planPrimitive', () => {
|
|
|
213
222
|
})
|
|
214
223
|
|
|
215
224
|
test('`/` and `%` always name their helper', () => {
|
|
216
|
-
expect(plan('props.n / 2',
|
|
217
|
-
expect(plan('props.n % 2',
|
|
225
|
+
expect(plan('props.n / 2', CLAIMS_UNPROVEN)).toBe('helper to-number div number-pair')
|
|
226
|
+
expect(plan('props.n % 2', CLAIMS_UNPROVEN)).toBe('helper to-number mod number-pair')
|
|
218
227
|
})
|
|
219
228
|
|
|
220
229
|
test('`+` adds two numbers, concatenates over a proven string, and is `plus` otherwise', () => {
|
|
@@ -228,16 +237,14 @@ describe('planPrimitive', () => {
|
|
|
228
237
|
expect(plan("props.s === 'a'", PROVEN_ONLY)).toBe('native strict-eq string-pair')
|
|
229
238
|
expect(plan('props.n === count()', PROVEN_ONLY)).toBe('native strict-eq number-pair')
|
|
230
239
|
expect(plan('props.maybe === 1', PROVEN_ONLY)).toBe('helper strict-eq strict_eq unproven')
|
|
231
|
-
expect(plan("props.n === 'a'",
|
|
232
|
-
expect(plan("props.n !== 'a'",
|
|
240
|
+
expect(plan("props.n === 'a'", CLAIMS_UNPROVEN)).toBe('const strict-eq false')
|
|
241
|
+
expect(plan("props.n !== 'a'", CLAIMS_UNPROVEN)).toBe('const strict-eq true')
|
|
233
242
|
})
|
|
234
243
|
|
|
235
244
|
test('unary minus over a non-zero number literal folds into the literal; any other operand takes `neg`', () => {
|
|
236
245
|
expect(plan('-1', PROVEN_ONLY)).toBe('native number-literal safe-integer')
|
|
237
246
|
expect(plan('-1.5', PROVEN_ONLY)).toBe('helper number-literal number unproven')
|
|
238
|
-
expect(plan('-1e21', ALL)).toBe('native number-literal unproven')
|
|
239
247
|
// `-0` does not fold: the zero operand is the case `neg` exists for.
|
|
240
|
-
expect(plan('-0', ALL)).toBe('native neg unproven')
|
|
241
248
|
expect(plan('-0', PROVEN_ONLY)).toBe('helper neg neg unproven')
|
|
242
249
|
expect(plan('-props.n', PROVEN_ONLY)).toBe('helper neg neg unproven')
|
|
243
250
|
expect(plan('+props.n', PROVEN_ONLY)).toBe('native to-number number')
|
|
@@ -289,9 +296,9 @@ describe('planPrimitive', () => {
|
|
|
289
296
|
})
|
|
290
297
|
|
|
291
298
|
test('loose equality and other operators are not planned', () => {
|
|
292
|
-
expect(plan('props.n == 1',
|
|
293
|
-
expect(plan("'a' in props",
|
|
294
|
-
expect(plan('typeof props.n',
|
|
299
|
+
expect(plan('props.n == 1', CLAIMS_UNPROVEN)).toBe('none')
|
|
300
|
+
expect(plan("'a' in props", CLAIMS_UNPROVEN)).toBe('none')
|
|
301
|
+
expect(plan('typeof props.n', CLAIMS_UNPROVEN)).toBe('none')
|
|
295
302
|
})
|
|
296
303
|
|
|
297
304
|
test('the empty typing proves only structure', () => {
|
|
@@ -301,6 +308,77 @@ describe('planPrimitive', () => {
|
|
|
301
308
|
})
|
|
302
309
|
})
|
|
303
310
|
|
|
311
|
+
describe('every target carries undefined (spec/js-undefined-value.md)', () => {
|
|
312
|
+
test('an operand Phase 1 does not prove takes the helper, even under a claim of `unproven`', () => {
|
|
313
|
+
for (const target of [PROVEN_ONLY, CLAIMS_UNPROVEN]) {
|
|
314
|
+
expect(plan('!props.s', target)).toBe('helper to-boolean truthy unproven')
|
|
315
|
+
expect(plan('props.n - props.maybe', target)).toBe('helper to-number sub unproven')
|
|
316
|
+
expect(plan('props.n <= props.maybe', target)).toBe('helper relational le unproven')
|
|
317
|
+
expect(plan('props.maybe ?? 0', target)).toBe('helper nullish coalesce unproven')
|
|
318
|
+
expect(plan('-props.n', target)).toBe('helper neg neg unproven')
|
|
319
|
+
expect(plan('-1e21', target)).toBe('helper number-literal number unproven')
|
|
320
|
+
expect(plan('props.rows[props.n]', target)).toBe('helper get get unproven')
|
|
321
|
+
// A dynamic `+` (neither side a proven number pair or a proven string).
|
|
322
|
+
expect(plan('props.maybe + props.n', target)).toBe('helper plus plus unproven')
|
|
323
|
+
}
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
test('a numeric either operand is planned as the helper, not refused', () => {
|
|
327
|
+
expect(plan('props.either * 2', PROVEN_ONLY)).toBe('helper to-number mul unproven')
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
test('`=== null` / `=== undefined` are strict and `== null` is nullish', () => {
|
|
331
|
+
const target = PROVEN_ONLY
|
|
332
|
+
expect(plan('props.maybe === null', target)).toBe('helper nil-test is_null unproven')
|
|
333
|
+
expect(plan('props.maybe !== null', target)).toBe('helper nil-test is_null unproven')
|
|
334
|
+
expect(plan('props.maybe === undefined', target)).toBe('helper nil-test is_undefined unproven')
|
|
335
|
+
expect(plan('undefined !== props.maybe', target)).toBe('helper nil-test is_undefined unproven')
|
|
336
|
+
expect(plan('null === props.maybe', target)).toBe('helper nil-test is_null unproven')
|
|
337
|
+
expect(plan('props.maybe == null', target)).toBe('helper nil-test is_nullish unproven')
|
|
338
|
+
expect(plan('props.maybe != undefined', target)).toBe('helper nil-test is_nullish unproven')
|
|
339
|
+
// Both sides nil literals: the right one names the test.
|
|
340
|
+
expect(plan('null === undefined', target)).toBe('helper nil-test is_undefined unproven')
|
|
341
|
+
// A bound `undefined` is a name, not the nil literal.
|
|
342
|
+
const shadowed = collectOperandTyping(COMPONENT).at(BindingScope.forComponent('props').enterCallback(['undefined']))
|
|
343
|
+
expect(plan('props.n === undefined', target, shadowed)).toBe('helper strict-eq strict_eq unproven')
|
|
344
|
+
// Loose equality against anything but a nil literal stays unplanned.
|
|
345
|
+
expect(plan('props.n == 1', target)).toBe('none')
|
|
346
|
+
})
|
|
347
|
+
|
|
348
|
+
test('an emitter that receives a nil test without a spelling refuses loudly', () => {
|
|
349
|
+
const p = planPrimitive(parseExpression('props.maybe === null'), typing, PROVEN_ONLY)
|
|
350
|
+
expect(() => refuseUnspelledNilTest(p)).toThrow('`is_null` nil test')
|
|
351
|
+
expect(() => strictEqSpelling('===', parseExpression('props.maybe'), parseExpression('null'), p)).toThrow('no spelling')
|
|
352
|
+
expect(() => refuseUnspelledNilTest(planPrimitive(parseExpression('props.n === 1'), typing, PROVEN_ONLY))).not.toThrow()
|
|
353
|
+
expect(strictEqSpelling('===', parseExpression('props.maybe'), parseExpression('null'), null)).toEqual({ kind: 'nullish' })
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
test('the operand of a nil test is the side that is not the nil literal', () => {
|
|
357
|
+
const operand = (src: string) => {
|
|
358
|
+
const e = parseExpression(src)
|
|
359
|
+
if (e.kind !== 'binary') throw new Error(src)
|
|
360
|
+
return exprToString(nilTestOperand(e.left, e.right, typing))
|
|
361
|
+
}
|
|
362
|
+
expect(operand('props.maybe === null')).toBe('props.maybe')
|
|
363
|
+
expect(operand('undefined !== props.maybe')).toBe('props.maybe')
|
|
364
|
+
expect(operand('null === undefined')).toBe('null')
|
|
365
|
+
})
|
|
366
|
+
|
|
367
|
+
test('a defaulted prop typed with `null` is presence-uncertain: the default does not apply to `null`', () => {
|
|
368
|
+
const ir = {
|
|
369
|
+
metadata: meta({
|
|
370
|
+
propsParams: [
|
|
371
|
+
param('v', union(prim('string'), prim('null')), true, "'D'"),
|
|
372
|
+
param('w', prim('string'), true, "'D'"),
|
|
373
|
+
param('x', prim('string'), true),
|
|
374
|
+
],
|
|
375
|
+
}),
|
|
376
|
+
} as unknown as Parameters<typeof collectNullableOptionalProps>[0]
|
|
377
|
+
const withMeta = { ...ir, metadata: { ...ir.metadata, restPropsExpandedKeys: [] } } as typeof ir
|
|
378
|
+
expect([...collectNullableOptionalProps(withMeta)].sort()).toEqual(['v', 'x'])
|
|
379
|
+
})
|
|
380
|
+
})
|
|
381
|
+
|
|
304
382
|
/** The typing of the one component in `src`, at component scope, from the analyzer's metadata. */
|
|
305
383
|
function analyzed(src: string): ScopedOperandTyping {
|
|
306
384
|
const metadata = buildMetadata(analyzeComponent(src, 'foo.tsx'))
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A destructure default applies to an absent or `undefined` prop only; an
|
|
3
|
+
* explicit `null` keeps `null` (`spec/js-undefined-value.md`, "Decisions"
|
|
4
|
+
* (6)). Every client-JS read of a defaulted prop is
|
|
5
|
+
* `_p.x === undefined ? d : _p.x`: the live reads in effects, the CSR
|
|
6
|
+
* template, a signal seed, a controlled signal's seed, a written prop's
|
|
7
|
+
* local, the `children` extraction and a body destructure of the props
|
|
8
|
+
* object. A compiler-supplied guard of a defaultless prop (`?? []` for a
|
|
9
|
+
* loop array) keeps replacing either nil.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { describe, test, expect } from 'bun:test'
|
|
13
|
+
import { compileJSX } from '../compiler'
|
|
14
|
+
import { TestAdapter } from '../adapters/test-adapter'
|
|
15
|
+
|
|
16
|
+
function clientJs(source: string): string {
|
|
17
|
+
const result = compileJSX(source, 'Demo.tsx', { adapter: new TestAdapter() })
|
|
18
|
+
expect(result.errors.filter(e => e.severity === 'error')).toEqual([])
|
|
19
|
+
return result.files.find(f => f.type === 'clientJs')!.content
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
describe('a destructure default in client JS applies to undefined only', () => {
|
|
23
|
+
test('text, attribute, CSR template and signal seeds', () => {
|
|
24
|
+
const js = clientJs(`
|
|
25
|
+
'use client'
|
|
26
|
+
import { createSignal } from '@barefootjs/client'
|
|
27
|
+
export function Demo({ label = 'D', start = 0 }: { label?: string | null; start?: number | null }) {
|
|
28
|
+
const [copy] = createSignal(label)
|
|
29
|
+
return <p title={label} data-s={start}>{label}{copy()}</p>
|
|
30
|
+
}
|
|
31
|
+
`)
|
|
32
|
+
expect(js).toContain("(_p.label === undefined ? 'D' : _p.label)")
|
|
33
|
+
expect(js).toContain("createSignal(_p.label === undefined ? 'D' : _p.label)")
|
|
34
|
+
expect(js).toContain('(_p.start === undefined ? 0 : _p.start)')
|
|
35
|
+
expect(js).not.toContain('_p.label ??')
|
|
36
|
+
expect(js).not.toContain('_p.start ??')
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('a body destructure of the props object and a written prop', () => {
|
|
40
|
+
const js = clientJs(`
|
|
41
|
+
'use client'
|
|
42
|
+
export function Demo(props: { label?: string | null; n?: number | null }) {
|
|
43
|
+
const { label = 'D' } = props
|
|
44
|
+
return <p>{label}</p>
|
|
45
|
+
}
|
|
46
|
+
export function Counter({ n = 1 }: { n?: number | null }) {
|
|
47
|
+
n = n + 1
|
|
48
|
+
return <p>{n}</p>
|
|
49
|
+
}
|
|
50
|
+
`)
|
|
51
|
+
expect(js).toContain("(_p.label === undefined ? 'D' : _p.label)")
|
|
52
|
+
expect(js).toContain('let n = _p.n === undefined ? 1 : _p.n')
|
|
53
|
+
expect(js).not.toContain('_p.label ??')
|
|
54
|
+
expect(js).not.toContain('_p.n ??')
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('a default reading an earlier binding kept as a local reads that local', () => {
|
|
58
|
+
const js = clientJs(`
|
|
59
|
+
'use client'
|
|
60
|
+
export function Demo(props: { a?: string[]; b?: string[] }) {
|
|
61
|
+
let { a = [], b = a } = props
|
|
62
|
+
a.push('x')
|
|
63
|
+
return <p>{b.length}</p>
|
|
64
|
+
}
|
|
65
|
+
export function Assigned(props: { a?: number; b?: number; c?: number }) {
|
|
66
|
+
let { a = 1, b = (a = 2), c = a } = props
|
|
67
|
+
return <p>{c}</p>
|
|
68
|
+
}
|
|
69
|
+
export function Kept(props: { a?: string[]; b?: string[] }) {
|
|
70
|
+
const { a = [], b = a } = props
|
|
71
|
+
a.push('x')
|
|
72
|
+
return <p>{b.length}</p>
|
|
73
|
+
}
|
|
74
|
+
`)
|
|
75
|
+
expect(js).toContain('let b = _p.b === undefined ? a : _p.b')
|
|
76
|
+
// An unread binding whose default assigns stays, so the assignment runs.
|
|
77
|
+
expect(js).toContain('let b = _p.b === undefined ? (a = 2) : _p.b')
|
|
78
|
+
expect(js).toContain('let c = _p.c === undefined ? a : _p.c')
|
|
79
|
+
expect(js).toContain('const b = _p.b === undefined ? a : _p.b')
|
|
80
|
+
expect(js).not.toContain('let b = _p.b === undefined ? (_p.a')
|
|
81
|
+
expect(js).not.toContain('const b = _p.b === undefined ? (_p.a')
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('an arrow default keeps its own parens', () => {
|
|
85
|
+
const js = clientJs(`
|
|
86
|
+
'use client'
|
|
87
|
+
export function Demo({ fmt = (v: string) => v + '!' }: { fmt?: (v: string) => string }) {
|
|
88
|
+
return <p>{fmt('x')}</p>
|
|
89
|
+
}
|
|
90
|
+
`)
|
|
91
|
+
expect(js).toContain("(_p.fmt === undefined ? ((v) => v + '!') : _p.fmt)('x')")
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test("an author's `??` over a prop keeps replacing null", () => {
|
|
95
|
+
const js = clientJs(`
|
|
96
|
+
'use client'
|
|
97
|
+
export function Demo(props: { label?: string | null }) {
|
|
98
|
+
const label = props.label ?? 'x'
|
|
99
|
+
return <p>{label.toUpperCase()}</p>
|
|
100
|
+
}
|
|
101
|
+
`)
|
|
102
|
+
expect(js).toContain("(_p.label ?? 'x')")
|
|
103
|
+
expect(js).not.toContain('_p.label === undefined')
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('a defaultless loop array keeps its nil guard', () => {
|
|
107
|
+
const js = clientJs(`
|
|
108
|
+
'use client'
|
|
109
|
+
export function Demo({ items }: { items?: string[] | null }) {
|
|
110
|
+
return <ul>{items.map(i => <li key={i}>{i}</li>)}</ul>
|
|
111
|
+
}
|
|
112
|
+
`)
|
|
113
|
+
expect(js).toContain('(_p.items ?? [])')
|
|
114
|
+
})
|
|
115
|
+
})
|
|
@@ -307,9 +307,9 @@ describe('ClientJS generation with semantic prop refs', () => {
|
|
|
307
307
|
const clientJs = result.files.find((f) => f.type === 'clientJs')
|
|
308
308
|
expect(clientJs).toBeDefined()
|
|
309
309
|
// No captured-once local — the live read carries the default at the
|
|
310
|
-
// read site itself: `(_p.open
|
|
310
|
+
// read site itself: `(_p.open === undefined ? false : _p.open)`.
|
|
311
311
|
expect(clientJs?.content).not.toContain('const open = ')
|
|
312
|
-
expect(clientJs?.content).toMatch(/\(_p\.open
|
|
312
|
+
expect(clientJs?.content).toMatch(/\(_p\.open === undefined \? false : _p\.open\)/)
|
|
313
313
|
})
|
|
314
314
|
})
|
|
315
315
|
|
|
@@ -210,6 +210,23 @@ describe('Destructured props keep their declared types (#2150)', () => {
|
|
|
210
210
|
expect(typeOf(ctx, 'configs')).toEqual({ kind: 'unknown', raw: 'unknown' })
|
|
211
211
|
})
|
|
212
212
|
|
|
213
|
+
test('resolves a structural member whose leaf is a nil union of a resolvable type; a top-level union still declines', () => {
|
|
214
|
+
const source = `
|
|
215
|
+
type Init = { label?: string | null; n: number | undefined }
|
|
216
|
+
type Props = { initial: Init; rows: { tag: string | null }[]; top: string | null }
|
|
217
|
+
|
|
218
|
+
export function Component({ initial, rows, top }: Props) {
|
|
219
|
+
return <div>{initial.label}{rows.length}{top}</div>
|
|
220
|
+
}
|
|
221
|
+
`
|
|
222
|
+
|
|
223
|
+
const ctx = analyzeComponent(source, 'Component.tsx')
|
|
224
|
+
|
|
225
|
+
expect(typeOf(ctx, 'initial')?.kind).toBe('interface')
|
|
226
|
+
expect(typeOf(ctx, 'rows')?.kind).toBe('array')
|
|
227
|
+
expect(typeOf(ctx, 'top')).toEqual({ kind: 'unknown', raw: 'unknown' })
|
|
228
|
+
})
|
|
229
|
+
|
|
213
230
|
// A same-file object type referenced BY NAME resolves like its inline
|
|
214
231
|
// spelling — the `props`-object form (`props: { initial: State }`) always
|
|
215
232
|
// kept it; the destructured form declined it to `unknown`.
|
|
@@ -77,8 +77,8 @@ export { Widget }
|
|
|
77
77
|
)
|
|
78
78
|
expect(js).not.toContain('const config = ')
|
|
79
79
|
expect(js).not.toContain('const items = ')
|
|
80
|
-
expect(js).toContain('(_p.config
|
|
81
|
-
expect(js).toContain('(_p.items
|
|
82
|
-
expect(js).toContain('(_p.items
|
|
80
|
+
expect(js).toContain('(_p.config === undefined ? {} : _p.config).startOpen')
|
|
81
|
+
expect(js).toContain('(_p.items === undefined ? [] : _p.items).length')
|
|
82
|
+
expect(js).toContain('(_p.items === undefined ? [] : _p.items)[0]')
|
|
83
83
|
})
|
|
84
84
|
})
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* like the parameter form (#3579). A binding that no longer holds the prop
|
|
5
5
|
* (a `let` reassigned before the return, a folded reassignment, a value
|
|
6
6
|
* mutated in place) stays a local: the template leaves its slot empty and
|
|
7
|
-
* defers a child that receives it, instead of reading `_p.b
|
|
7
|
+
* defers a child that receives it, instead of reading `_p.b` with its default.
|
|
8
8
|
*
|
|
9
9
|
* The reassigned shapes have no conformance fixture: the reference adapter
|
|
10
10
|
* itself drops the reassignment of a destructured binding, so these pin the
|
|
@@ -42,7 +42,7 @@ function templateOf(body: string, jsx: string, propsType = '{ b?: string[] | nul
|
|
|
42
42
|
describe('renamed body destructure binding in the client template (#3579)', () => {
|
|
43
43
|
test('a default holding a callback reads the prop with its default', () => {
|
|
44
44
|
const t = templateOf("const { b: local = ['y', ''].filter((x) => x !== '') } = props", '<p>{local.length}</p>')
|
|
45
|
-
expect(t).toContain("_p.b
|
|
45
|
+
expect(t).toContain("_p.b === undefined ? (['y', ''].filter((x) => x !== '')) : _p.b")
|
|
46
46
|
expect(t).not.toContain("${''}")
|
|
47
47
|
})
|
|
48
48
|
|
|
@@ -55,23 +55,23 @@ describe('renamed body destructure binding in the client template (#3579)', () =
|
|
|
55
55
|
test('a let reassigned before the return keeps the child deferred', () => {
|
|
56
56
|
const t = templateOf("let { b: local = null } = props\n local = ['changed']", '<p><Child xs={local} /></p>')
|
|
57
57
|
expect(t).toContain('data-bf-ph')
|
|
58
|
-
expect(t).not.toContain('_p.b
|
|
58
|
+
expect(t).not.toContain('_p.b === undefined ? ')
|
|
59
59
|
})
|
|
60
60
|
|
|
61
61
|
test('a let reassigned before the return is not read as the prop in text or an attribute', () => {
|
|
62
62
|
const t = templateOf("let { b: local = 'x' } = props\n local = local + '!'", '<p data-v={local}>{local}</p>', '{ b?: string }')
|
|
63
|
-
expect(t).not.toContain(
|
|
63
|
+
expect(t).not.toContain('_p.b === undefined ? ')
|
|
64
64
|
})
|
|
65
65
|
|
|
66
66
|
test('a folded conditional reassignment is not read as the prop', () => {
|
|
67
67
|
const t = templateOf("let { b: local = 'x' } = props\n if (!local) local = 'y'", '<p>{local}</p>', '{ b?: string }')
|
|
68
|
-
expect(t).not.toContain(
|
|
68
|
+
expect(t).not.toContain('_p.b === undefined ? ')
|
|
69
69
|
})
|
|
70
70
|
|
|
71
71
|
test('an un-renamed let reassigned before the return keeps the child deferred too', () => {
|
|
72
72
|
const t = templateOf("let { b = null } = props\n b = ['changed']", '<p><Child xs={b} /></p>')
|
|
73
73
|
expect(t).toContain('data-bf-ph')
|
|
74
|
-
expect(t).not.toContain('_p.b
|
|
74
|
+
expect(t).not.toContain('_p.b === undefined ? ')
|
|
75
75
|
})
|
|
76
76
|
|
|
77
77
|
test('an un-renamed binding with a callback default renders the child in place', () => {
|
|
@@ -81,6 +81,6 @@ describe('renamed body destructure binding in the client template (#3579)', () =
|
|
|
81
81
|
|
|
82
82
|
test('a value mutated in place is not read as the prop', () => {
|
|
83
83
|
const t = templateOf("const { b: local = ['x'] } = props\n local.push('y')", '<p>{local.length}</p>', '{ b?: string[] }')
|
|
84
|
-
expect(t).not.toContain(
|
|
84
|
+
expect(t).not.toContain('_p.b === undefined ? ')
|
|
85
85
|
})
|
|
86
86
|
})
|
|
@@ -107,7 +107,7 @@ function Comp({ base, tag = 'all', page }: { base: string; tag?: string; page: s
|
|
|
107
107
|
export { Comp }
|
|
108
108
|
`
|
|
109
109
|
const js = compileClientJs(source)
|
|
110
|
-
expect(js).toContain("{ tag: (_p.tag
|
|
110
|
+
expect(js).toContain("{ tag: (_p.tag === undefined ? 'all' : _p.tag), page: _p.page ? _p.page : '' }")
|
|
111
111
|
expect(js).not.toContain("(_p.tag ?? 'all'):")
|
|
112
112
|
})
|
|
113
113
|
|