@barefootjs/jsx 0.40.0 → 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 +42 -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/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/component-props.d.ts +22 -7
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +28 -1
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +39 -1
- 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 +45 -0
- package/dist/adapters/number-value.d.ts.map +1 -0
- 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 +11 -0
- package/dist/analyzer-context.d.ts.map +1 -1
- package/dist/analyzer.d.ts +34 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +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 +24 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5671 -2586
- 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-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 +22 -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/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 +37 -19
- 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/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 +3 -2
- 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 +1 -0
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +95 -10
- 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-binding.d.ts +27 -3
- package/dist/props-binding.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 +100 -4
- 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 +16 -0
- 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/strip-types.d.ts +13 -0
- package/dist/strip-types.d.ts.map +1 -1
- package/dist/template-parts.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 +120 -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-ratchet.test.ts +12 -2
- 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-carrier.test.ts +74 -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__/component-props.test.ts +14 -0
- package/src/__tests__/computed-const.test.ts +188 -0
- package/src/__tests__/emit-to-string-call.test.ts +2 -1
- package/src/__tests__/expression-parser.test.ts +24 -0
- 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 +143 -0
- 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-prop-read.test.ts +111 -0
- package/src/__tests__/loop-row-type.test.ts +64 -0
- package/src/__tests__/lowering-value-position.test.ts +4 -0
- package/src/__tests__/memo-body-expr.test.ts +137 -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 +231 -2
- package/src/__tests__/primitive-plan.test.ts +416 -0
- package/src/__tests__/profile-binding-ids.test.ts +14 -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 +79 -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__/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/__tests__/template-parts-literal-boundary.test.ts +19 -0
- package/src/adapters/ambiguous-nil-arithmetic.ts +103 -0
- package/src/adapters/attr-plan.ts +6 -2
- package/src/adapters/boolean-carrier.ts +76 -0
- 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/component-props.ts +28 -8
- package/src/adapters/computed-const.ts +443 -10
- package/src/adapters/dsl-template-adapter.ts +79 -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 +108 -0
- 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 +14 -0
- package/src/analyzer.ts +692 -85
- 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 +74 -2
- package/src/index.ts +45 -9
- 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 +100 -67
- 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 +85 -5
- 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 +30 -28
- 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 +23 -24
- package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +29 -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 +95 -38
- 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 +42 -15
- package/src/ir-to-client-js/plan/static-array-child-init.ts +34 -19
- package/src/ir-to-client-js/prop-handling.ts +22 -3
- 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 +30 -12
- 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 +510 -58
- package/src/nullable-signal-attr.ts +366 -28
- package/src/prop-rewrite.ts +30 -6
- package/src/props-binding.ts +54 -3
- package/src/props-object-whole-read.ts +87 -0
- package/src/rich-type-refusal.ts +2 -1
- package/src/scope/binding-scope.ts +203 -11
- package/src/scope/lexical-scope-walk.ts +4 -3
- package/src/scope/root-prop-alias.ts +23 -0
- 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/strip-types.ts +31 -3
- package/src/template-parts.ts +21 -1
- package/src/template-position-walk.ts +57 -28
- package/src/types.ts +117 -0
|
@@ -30,7 +30,7 @@ export function N(props: { groups: { name: string; tags: string[] }[] }) {
|
|
|
30
30
|
expect(js).not.toContain('const __bfItem().name')
|
|
31
31
|
expect(js).toContain("const name = t() + '!'")
|
|
32
32
|
expect(js).toContain('String(name)')
|
|
33
|
-
expect(js).toContain('
|
|
33
|
+
expect(js).toContain('textOrNode(__bfItem().name)')
|
|
34
34
|
})
|
|
35
35
|
|
|
36
36
|
test('a row two loops deep still reads the outermost item through its accessor', () => {
|
|
@@ -51,7 +51,7 @@ export function Feed() {
|
|
|
51
51
|
</article>)}</main>
|
|
52
52
|
}
|
|
53
53
|
`)
|
|
54
|
-
expect(js).toContain('
|
|
54
|
+
expect(js).toContain('textOrNode(post().text + reply().text)')
|
|
55
55
|
expect(js).not.toContain('String(post.text + reply().text)')
|
|
56
56
|
})
|
|
57
57
|
})
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The nil-reading rewrite (#3824 step 4a, `spec/js-primitive-semantics.md`
|
|
3
|
+
* "Nil reading is a tree rewrite"): an operand whose nil Phase 1 can read is
|
|
4
|
+
* made non-nil before the plan, by its consumer's coercion.
|
|
5
|
+
*/
|
|
6
|
+
import { describe, test, expect } from 'bun:test'
|
|
7
|
+
import {
|
|
8
|
+
analyzeComponent,
|
|
9
|
+
buildMetadata,
|
|
10
|
+
collectOperandTyping,
|
|
11
|
+
rewriteNilReadings,
|
|
12
|
+
rewriteNilReadingsDeep,
|
|
13
|
+
parseExpression,
|
|
14
|
+
serializeParsedExpr,
|
|
15
|
+
stringifyParsedExpr,
|
|
16
|
+
BindingScope,
|
|
17
|
+
type ParsedExpr,
|
|
18
|
+
} from '../index'
|
|
19
|
+
|
|
20
|
+
const SRC = `export function C(props: { n?: number; nn: number | null; e?: number | null; s?: string; sn: string | null; c?: string | null; m: number; t: string; x: any }) { return <p>{props.m}</p> }`
|
|
21
|
+
|
|
22
|
+
function typing() {
|
|
23
|
+
const metadata = buildMetadata(analyzeComponent(SRC, 'c.tsx'))
|
|
24
|
+
return collectOperandTyping(metadata).at(BindingScope.forComponent(metadata.propsObjectName))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** `e` with every operator node parenthesised, so the tests read the tree's grouping. */
|
|
28
|
+
function show(e: ParsedExpr): string {
|
|
29
|
+
switch (e.kind) {
|
|
30
|
+
case 'binary':
|
|
31
|
+
case 'logical':
|
|
32
|
+
return `(${show(e.left)} ${e.op} ${show(e.right)})`
|
|
33
|
+
case 'unary':
|
|
34
|
+
return `${e.op}${show(e.argument)}`
|
|
35
|
+
case 'literal':
|
|
36
|
+
return e.literalType === 'string' ? `'${e.value}'` : String(e.value)
|
|
37
|
+
case 'template-literal':
|
|
38
|
+
return '`' + e.parts.map(p => (p.type === 'string' ? p.value : `\${${show(p.expr)}}`)).join('') + '`'
|
|
39
|
+
default:
|
|
40
|
+
return stringifyParsedExpr(e)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const rewrite = (src: string): string => show(rewriteNilReadings(parseExpression(src), typing()))
|
|
45
|
+
|
|
46
|
+
describe('rewriteNilReadings', () => {
|
|
47
|
+
test('a numeric consumer reads an undefined nil as NaN and a null one as 0', () => {
|
|
48
|
+
expect(rewrite('props.n * 2')).toBe('((props.n ?? NaN) * 2)')
|
|
49
|
+
expect(rewrite('props.nn - props.m')).toBe('((props.nn ?? 0) - props.m)')
|
|
50
|
+
expect(rewrite('-props.n')).toBe('-(props.n ?? NaN)')
|
|
51
|
+
expect(rewrite('props.s < props.t')).toBe('((props.s ?? NaN) < props.t)')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('a string consumer reads it as "undefined" / "null"', () => {
|
|
55
|
+
expect(rewrite("props.s + '!'")).toBe("((props.s ?? 'undefined') + '!')")
|
|
56
|
+
expect(rewrite("props.t + props.sn")).toBe("(props.t + (props.sn ?? 'null'))")
|
|
57
|
+
expect(rewrite('`${props.n}`')).toBe("`${(props.n ?? 'undefined')}`")
|
|
58
|
+
expect(rewrite("null + 'x'")).toBe("('null' + 'x')")
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('a string operand under + number is split on nil by the numeric row', () => {
|
|
62
|
+
expect(rewrite('props.s + 1')).toBe('((props.s ?? NaN) + 1)')
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test('an either nil keeps the display reading, and a dynamic + is left alone', () => {
|
|
66
|
+
expect(rewrite('(props.e * 2)')).toBe('(props.e * 2)')
|
|
67
|
+
expect(rewrite("'btn ' + props.c")).toBe("('btn ' + (props.c ?? ''))")
|
|
68
|
+
expect(rewrite('(props.n + props.x)')).toBe('(props.n + props.x)')
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test('the deep form enters callbacks and serializes NaN through Number', () => {
|
|
72
|
+
const deep = rewriteNilReadingsDeep(parseExpression('props.items.filter(i => i > props.n)'), typing())
|
|
73
|
+
const arrow = deep.kind === 'call' ? deep.args[0] : undefined
|
|
74
|
+
expect(arrow?.kind === 'arrow' ? show(arrow.body) : null).toBe('(i > (props.n ?? NaN))')
|
|
75
|
+
expect(serializeParsedExpr(rewriteNilReadings(parseExpression('props.n * 2'), typing()))).toContain('"name":"Number"')
|
|
76
|
+
})
|
|
77
|
+
})
|
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
import { describe, test, expect } from 'bun:test'
|
|
2
|
+
import { arithmeticCoercionOrRefuse, refuseAmbiguousNilArithmetic } from '../adapters/ambiguous-nil-arithmetic'
|
|
2
3
|
import { analyzeComponent } from '../analyzer'
|
|
3
|
-
import { parseExpression } from '../expression-parser'
|
|
4
|
+
import { exprToString, parseExpression } from '../expression-parser'
|
|
4
5
|
import {
|
|
6
|
+
ambiguousNilArithmeticOperands,
|
|
5
7
|
arithmeticOperandCoercion,
|
|
8
|
+
arithmeticOperandIsNull,
|
|
6
9
|
arithmeticOperandIsUndefined,
|
|
10
|
+
arithmeticOperandNilIsAmbiguous,
|
|
7
11
|
attrValueMayBeNullish,
|
|
8
12
|
collectAbsentPropSeedReads,
|
|
9
13
|
collectNullableSignalGetters,
|
|
10
14
|
collectNullishAttrContext,
|
|
15
|
+
isNeverNullishOperand,
|
|
16
|
+
nullablePropsYieldedBy,
|
|
11
17
|
nullableSignalAttrGetter,
|
|
12
18
|
} from '../nullable-signal-attr'
|
|
19
|
+
import { BindingScope } from '../scope/binding-scope'
|
|
13
20
|
import type { ComponentIR } from '../types'
|
|
14
21
|
|
|
15
22
|
function nullableGetters(decls: string): Set<string> {
|
|
@@ -120,6 +127,7 @@ describe('attrValueMayBeNullish (#3322)', () => {
|
|
|
120
127
|
const plain = createMemo(() => t())
|
|
121
128
|
const typed = createMemo<string | undefined>(() => t())
|
|
122
129
|
const loop = createMemo(() => loop())
|
|
130
|
+
const tail = createMemo(() => t().at(-1))
|
|
123
131
|
return <p>x</p>
|
|
124
132
|
}
|
|
125
133
|
`
|
|
@@ -157,6 +165,20 @@ describe('attrValueMayBeNullish (#3322)', () => {
|
|
|
157
165
|
expect(mayBe('0')).toBe(false)
|
|
158
166
|
})
|
|
159
167
|
|
|
168
|
+
test('`.at(i)` / `.find()` / `.findLast()` read undefined, directly or through a memo', () => {
|
|
169
|
+
expect(mayBe('t().at(-1)')).toBe(true)
|
|
170
|
+
expect(mayBe('tail()')).toBe(true)
|
|
171
|
+
expect(mayBe("t() ? t().at(9) : 'x'")).toBe(true)
|
|
172
|
+
expect(mayBe("t().at(-1) ?? 'x'")).toBe(false)
|
|
173
|
+
expect(mayBe('t().slice(-1)')).toBe(false)
|
|
174
|
+
expect(mayBe("t().indexOf('a')")).toBe(false)
|
|
175
|
+
expect(mayBe("xs.find(x => x === 'z')")).toBe(true)
|
|
176
|
+
expect(mayBe('xs.findLast(x => x)')).toBe(true)
|
|
177
|
+
expect(mayBe("xs.find(x => x === 'z').name")).toBe(true)
|
|
178
|
+
expect(mayBe('xs.findIndex(x => x)')).toBe(false)
|
|
179
|
+
expect(mayBe('xs.filter(x => x)')).toBe(false)
|
|
180
|
+
})
|
|
181
|
+
|
|
160
182
|
test('a getter a loop binding shadows is not the signal or memo', () => {
|
|
161
183
|
expect(mayBe('label()', ['label'])).toBe(false)
|
|
162
184
|
})
|
|
@@ -196,7 +218,8 @@ describe('arithmeticOperandCoercion (#3474)', () => {
|
|
|
196
218
|
for (const op of ['-', '*', '/', '%', '**']) expect(coercion(ctx, op, 'props.b', 'x')).toBe('undefined')
|
|
197
219
|
expect(coercion(ctx, '+', 'props.b', '-2')).toBe('undefined')
|
|
198
220
|
expect(coercion(ctx, '*', 'props.u', '2')).toBe('undefined')
|
|
199
|
-
// A type that admits both keeps the null
|
|
221
|
+
// A type that admits both keeps the null spelling; the arithmetic over it
|
|
222
|
+
// is refused (`arithmeticOperandNilIsAmbiguous`, #3700).
|
|
200
223
|
expect(coercion(ctx, '*', 'props.both', '2')).toBe('null')
|
|
201
224
|
})
|
|
202
225
|
|
|
@@ -208,6 +231,30 @@ describe('arithmeticOperandCoercion (#3474)', () => {
|
|
|
208
231
|
expect(unary('props.c')).toBeNull()
|
|
209
232
|
})
|
|
210
233
|
|
|
234
|
+
test('unary plus passes no other operand and coerces like unary minus (#3824 step 3)', () => {
|
|
235
|
+
const ctx = ctxOf('props: { a: number | null; b?: number; c: number }')
|
|
236
|
+
const unary = (operand: string) => arithmeticOperandCoercion('+', parseExpression(operand), undefined, ctx)
|
|
237
|
+
expect(unary('props.a')).toBe('null')
|
|
238
|
+
expect(unary('props.b')).toBe('undefined')
|
|
239
|
+
expect(unary('props.c')).toBeNull()
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
test('a string or boolean relational operand that may only be absent reads NaN and keeps a present value (#3824 step 3)', () => {
|
|
243
|
+
const ctx = ctxOf('props: { s?: string; b?: boolean; t: string | null; c: string; u: string | undefined }')
|
|
244
|
+
for (const op of ['<', '<=', '>', '>=']) {
|
|
245
|
+
expect(coercion(ctx, op, 'props.s', '1')).toBe('undefined-keep')
|
|
246
|
+
expect(coercion(ctx, op, 'props.s', "'a'")).toBe('undefined-keep')
|
|
247
|
+
expect(coercion(ctx, op, 'props.b', '1')).toBe('undefined-keep')
|
|
248
|
+
expect(coercion(ctx, op, 'props.u', '1')).toBe('undefined-keep')
|
|
249
|
+
// A `null` reading is the helper's own; a required prop has no nil.
|
|
250
|
+
expect(coercion(ctx, op, 'props.t', '1')).toBeNull()
|
|
251
|
+
expect(coercion(ctx, op, 'props.c', '1')).toBeNull()
|
|
252
|
+
}
|
|
253
|
+
// Arithmetic over a string prop is not a relational: left as it is.
|
|
254
|
+
expect(coercion(ctx, '*', 'props.s', '2')).toBeNull()
|
|
255
|
+
expect(arithmeticOperandCoercion('<', parseExpression('props.s'), parseExpression('1'), ctx, n => n === 'props')).toBeNull()
|
|
256
|
+
})
|
|
257
|
+
|
|
211
258
|
test('an absent-only prop is not certainly undefined: arithmeticOperandIsUndefined stays false', () => {
|
|
212
259
|
const ctx = ctxOf('props: { b?: number }')
|
|
213
260
|
expect(arithmeticOperandIsUndefined(parseExpression('props.b'), ctx)).toBe(false)
|
|
@@ -231,3 +278,185 @@ describe('arithmeticOperandCoercion (#3474)', () => {
|
|
|
231
278
|
expect(coercion(ctxOf('', 'const [t] = createSignal<number>()'), '+', 't()', '1')).toBe('undefined')
|
|
232
279
|
})
|
|
233
280
|
})
|
|
281
|
+
|
|
282
|
+
describe('arithmeticOperandNilIsAmbiguous (#3700)', () => {
|
|
283
|
+
function ctxOf(signature: string, decls = ''): ReturnType<typeof collectNullishAttrContext> {
|
|
284
|
+
const source = `
|
|
285
|
+
'use client'
|
|
286
|
+
import { createMemo } from '@barefootjs/client'
|
|
287
|
+
export function C(${signature}) {
|
|
288
|
+
${decls}
|
|
289
|
+
return <p>x</p>
|
|
290
|
+
}
|
|
291
|
+
`
|
|
292
|
+
return collectNullishAttrContext({ metadata: analyzeComponent(source, 'C.tsx') } as unknown as ComponentIR)
|
|
293
|
+
}
|
|
294
|
+
const ambiguous = (ctx: ReturnType<typeof collectNullishAttrContext>, op: string, operand: string, other?: string) =>
|
|
295
|
+
arithmeticOperandNilIsAmbiguous(op, parseExpression(operand), other === undefined ? undefined : parseExpression(other), ctx)
|
|
296
|
+
|
|
297
|
+
test('a number prop that admits both null and absence, in either props form', () => {
|
|
298
|
+
for (const signature of ['props: { n?: number | null }', 'props: { n: number | null | undefined }']) {
|
|
299
|
+
const ctx = ctxOf(signature)
|
|
300
|
+
for (const op of ['-', '*', '/', '%', '**']) expect(ambiguous(ctx, op, 'props.n', 'x')).toBe(true)
|
|
301
|
+
expect(ambiguous(ctx, '+', 'props.n', '1')).toBe(true)
|
|
302
|
+
expect(ambiguous(ctx, '-', 'props.n')).toBe(true)
|
|
303
|
+
expect(ambiguous(ctx, '+', 'props.n')).toBe(true)
|
|
304
|
+
// `+` against a non-number may concatenate: no `ToNumber`, no ambiguity.
|
|
305
|
+
expect(ambiguous(ctx, '+', 'props.n', "'px'")).toBe(false)
|
|
306
|
+
}
|
|
307
|
+
const destructured = ctxOf('{ n, m: alias }: { n?: number | null; m?: null | number }')
|
|
308
|
+
expect(ambiguous(destructured, '*', 'n', '2')).toBe(true)
|
|
309
|
+
expect(ambiguous(destructured, '*', 'alias', '2')).toBe(true)
|
|
310
|
+
})
|
|
311
|
+
|
|
312
|
+
test('a relational operand is a numeric consumer too (#3824 step 3)', () => {
|
|
313
|
+
const ctx = ctxOf('props: { n?: number | null; a: number | null; b?: number }')
|
|
314
|
+
for (const op of ['<', '<=', '>', '>=']) {
|
|
315
|
+
expect(ambiguous(ctx, op, 'props.n', '1')).toBe(true)
|
|
316
|
+
expect(ambiguous(ctx, op, 'props.n', "'x'")).toBe(true)
|
|
317
|
+
expect(ambiguous(ctx, op, 'props.a', '1')).toBe(false)
|
|
318
|
+
expect(ambiguous(ctx, op, 'props.b', '1')).toBe(false)
|
|
319
|
+
}
|
|
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
|
+
})
|
|
325
|
+
|
|
326
|
+
test('a string or boolean prop admitting both nils is ambiguous under a comparison only (#3824 step 3)', () => {
|
|
327
|
+
const ctx = ctxOf('props: { s?: string | null; b?: boolean | null; t?: string; m: string | null }', 'const id = createMemo(() => props.s)')
|
|
328
|
+
for (const op of ['<', '<=', '>', '>=']) {
|
|
329
|
+
expect(ambiguous(ctx, op, 'props.s', '1')).toBe(true)
|
|
330
|
+
expect(ambiguous(ctx, op, 'props.s', "'a'")).toBe(true)
|
|
331
|
+
expect(ambiguous(ctx, op, 'props.b', '1')).toBe(true)
|
|
332
|
+
expect(ambiguous(ctx, op, 'id()', '1')).toBe(true)
|
|
333
|
+
expect(ambiguous(ctx, op, 'props.t', '1')).toBe(false)
|
|
334
|
+
expect(ambiguous(ctx, op, 'props.m', '1')).toBe(false)
|
|
335
|
+
}
|
|
336
|
+
expect(ambiguous(ctx, '*', 'props.s', '2')).toBe(false)
|
|
337
|
+
expect(ambiguous(ctx, '+', 'props.s')).toBe(false)
|
|
338
|
+
})
|
|
339
|
+
|
|
340
|
+
test('a prop with one nil reading, a defaulted one and a shadowed one are not ambiguous', () => {
|
|
341
|
+
const ctx = ctxOf('props: { a: number | null; b?: number; c: number }')
|
|
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)
|
|
344
|
+
const destructured = ctxOf('{ n }: { n?: number | null }')
|
|
345
|
+
expect(arithmeticOperandNilIsAmbiguous('*', parseExpression('n'), parseExpression('2'), destructured, name => name === 'n')).toBe(false)
|
|
346
|
+
})
|
|
347
|
+
|
|
348
|
+
test('an identity memo over the prop is ambiguous too; the null spelling is kept for it', () => {
|
|
349
|
+
const ctx = ctxOf('props: { n?: number | null }', 'const m = createMemo(() => props.n)')
|
|
350
|
+
expect(ambiguous(ctx, '*', 'm()', '2')).toBe(true)
|
|
351
|
+
expect(arithmeticOperandCoercion('*', parseExpression('m()'), parseExpression('2'), ctx)).toBe('null')
|
|
352
|
+
expect(arithmeticOperandIsNull(parseExpression('m()'), ctx)).toBe(true)
|
|
353
|
+
})
|
|
354
|
+
|
|
355
|
+
test('a destructured union binding keeps its declared type for the classification', () => {
|
|
356
|
+
const ctx = ctxOf('{ a, u }: { a: number | null; u?: number | undefined }')
|
|
357
|
+
expect(arithmeticOperandCoercion('*', parseExpression('a'), parseExpression('2'), ctx)).toBe('null')
|
|
358
|
+
expect(arithmeticOperandCoercion('*', parseExpression('u'), parseExpression('2'), ctx)).toBe('undefined')
|
|
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'])
|
|
394
|
+
})
|
|
395
|
+
})
|
|
396
|
+
|
|
397
|
+
describe('a prop that can be absent, and an operand that is never nullish', () => {
|
|
398
|
+
function irOf(signature: string): ComponentIR {
|
|
399
|
+
const source = `
|
|
400
|
+
export function C(${signature}) {
|
|
401
|
+
return <p>x</p>
|
|
402
|
+
}
|
|
403
|
+
`
|
|
404
|
+
return { metadata: analyzeComponent(source, 'C.tsx') } as unknown as ComponentIR
|
|
405
|
+
}
|
|
406
|
+
const mayBeNullish = (ir: ComponentIR, src: string, shadowed: (n: string) => boolean = () => false) =>
|
|
407
|
+
attrValueMayBeNullish(parseExpression(src), collectNullishAttrContext(ir), shadowed)
|
|
408
|
+
|
|
409
|
+
test('an && / ternary over an absent-able prop may be nullish, in both props forms', () => {
|
|
410
|
+
const object = irOf('props: { on?: boolean; label: string | undefined; n: number; d?: string }')
|
|
411
|
+
expect(mayBeNullish(object, "props.on && 'y'")).toBe(true)
|
|
412
|
+
expect(mayBeNullish(object, "props.n > 0 ? props.label : 'x'")).toBe(true)
|
|
413
|
+
expect(mayBeNullish(object, "props.n && 'y'")).toBe(false)
|
|
414
|
+
expect(mayBeNullish(object, "props.on ?? 'y'")).toBe(false)
|
|
415
|
+
expect(mayBeNullish(object, "props.on && 'y'", n => n === 'props')).toBe(false)
|
|
416
|
+
const destructured = irOf("{ on, d = 'z' }: { on?: boolean; d?: string }")
|
|
417
|
+
expect(mayBeNullish(destructured, "on && 'y'")).toBe(true)
|
|
418
|
+
expect(mayBeNullish(destructured, "on && 'y'", n => n === 'on')).toBe(false)
|
|
419
|
+
expect(mayBeNullish(destructured, "d && 'y'")).toBe(false)
|
|
420
|
+
})
|
|
421
|
+
|
|
422
|
+
test('nullablePropsYieldedBy names the props a value can be read whole from', () => {
|
|
423
|
+
const ir = irOf('props: { on?: boolean; label?: string; n?: number }')
|
|
424
|
+
const nullable = collectNullishAttrContext(ir).nullableProps!
|
|
425
|
+
expect([...nullablePropsYieldedBy(parseExpression("props.on && (props.n > 0 ? props.label : 'x')"), nullable)].sort()).toEqual(['label', 'on'])
|
|
426
|
+
expect([...nullablePropsYieldedBy(parseExpression("props.on ?? props.label"), nullable)]).toEqual(['label'])
|
|
427
|
+
})
|
|
428
|
+
|
|
429
|
+
test('isNeverNullishOperand: a comparison and a required scalar prop, not an optional or shadowed one', () => {
|
|
430
|
+
const object = irOf("props: { flag: boolean; s: string; n: number; tone: 'a' | 'b'; opt?: boolean; u: string | undefined; list: string[] }").metadata
|
|
431
|
+
const never = (src: string, isBound: (n: string) => boolean = () => false) =>
|
|
432
|
+
isNeverNullishOperand(parseExpression(src)!, object, isBound)
|
|
433
|
+
for (const src of ['props.flag', 'props.s', 'props.n', 'props.tone', 'props.n > 1', '!props.opt']) expect(never(src)).toBe(true)
|
|
434
|
+
for (const src of ['props.opt', 'props.u', 'props.list', 'flag']) expect(never(src)).toBe(false)
|
|
435
|
+
expect(never('props.flag', n => n === 'props')).toBe(false)
|
|
436
|
+
const destructured = irOf('{ flag, opt }: { flag: boolean; opt?: boolean }').metadata
|
|
437
|
+
expect(isNeverNullishOperand(parseExpression('flag')!, destructured, () => false)).toBe(true)
|
|
438
|
+
expect(isNeverNullishOperand(parseExpression('flag')!, destructured, n => n === 'flag')).toBe(false)
|
|
439
|
+
expect(isNeverNullishOperand(parseExpression('opt')!, destructured, () => false)).toBe(false)
|
|
440
|
+
})
|
|
441
|
+
})
|
|
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
|
+
})
|