@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
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { lookupStaticArrayElement, lookupStaticRecordLiteral } from '../augment-inherited-props.ts'
|
|
3
|
+
import { parseExpression } from '../expression-parser.ts'
|
|
4
|
+
import { analyzeComponent } from '../analyzer.ts'
|
|
5
|
+
import { buildMetadata } from '../compiler.ts'
|
|
6
|
+
import type { ConstantInfo } from '../types.ts'
|
|
7
|
+
|
|
8
|
+
const loc = { file: 'c.tsx', start: { line: 1, column: 0 }, end: { line: 1, column: 0 } }
|
|
9
|
+
|
|
10
|
+
function moduleConst(name: string, value: string, extra: Partial<ConstantInfo> = {}): ConstantInfo {
|
|
11
|
+
return {
|
|
12
|
+
name,
|
|
13
|
+
value: value as ConstantInfo['value'],
|
|
14
|
+
parsed: parseExpression(value),
|
|
15
|
+
declarationKind: 'const',
|
|
16
|
+
isModule: true,
|
|
17
|
+
type: null,
|
|
18
|
+
loc,
|
|
19
|
+
...extra,
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const notShadowed = () => false
|
|
24
|
+
const num = (value: number) => ({ kind: 'literal', value, literalType: 'number', raw: String(value) })
|
|
25
|
+
const str = (value: string) => ({ kind: 'literal', value, literalType: 'string' })
|
|
26
|
+
|
|
27
|
+
describe('lookupStaticArrayElement', () => {
|
|
28
|
+
const QS = moduleConst('QS', `['a1', "b\\"c", \`m1\nm2\`, 1e3, 0x10, .5, -0, -1.5]`)
|
|
29
|
+
const read = (key: string) => lookupStaticArrayElement('QS', key, [QS], notShadowed)
|
|
30
|
+
|
|
31
|
+
test('a canonical index reads the element as a typed node', () => {
|
|
32
|
+
expect(read('0')).toEqual(str('a1'))
|
|
33
|
+
expect(read('1')).toEqual(str('b"c'))
|
|
34
|
+
expect(read('2')).toEqual(str('m1\nm2'))
|
|
35
|
+
expect(read('3')).toEqual(num(1000))
|
|
36
|
+
expect(read('4')).toEqual(num(16))
|
|
37
|
+
expect(read('5')).toEqual(num(0.5))
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
test('a negated element stays a unary minus, so -0 keeps its sign', () => {
|
|
41
|
+
expect(read('6')).toEqual({ kind: 'unary', op: '-', argument: num(0) })
|
|
42
|
+
expect(read('7')).toEqual({ kind: 'unary', op: '-', argument: num(1.5) })
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
test('length is the element count', () => {
|
|
46
|
+
expect(read('length')).toEqual(num(8))
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test('a key that names no element falls back to the generic lowering', () => {
|
|
50
|
+
expect(read('01')).toBeNull()
|
|
51
|
+
expect(read('8')).toBeNull()
|
|
52
|
+
expect(read('-1')).toBeNull()
|
|
53
|
+
expect(read('x')).toBeNull()
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('a hole, a spread or a non-literal element is not resolved', () => {
|
|
57
|
+
const holes = moduleConst('H', `['a', , 'c']`)
|
|
58
|
+
expect(lookupStaticArrayElement('H', '0', [holes], notShadowed)).toEqual(str('a'))
|
|
59
|
+
expect(lookupStaticArrayElement('H', '2', [holes], notShadowed)).toBeNull()
|
|
60
|
+
expect(lookupStaticArrayElement('H', 'length', [holes], notShadowed)).toBeNull()
|
|
61
|
+
const spread = moduleConst('S', `[...xs, 'b']`)
|
|
62
|
+
expect(lookupStaticArrayElement('S', '1', [spread], notShadowed)).toBeNull()
|
|
63
|
+
const ref = moduleConst('R', `[other, 'b']`)
|
|
64
|
+
expect(lookupStaticArrayElement('R', '0', [ref], notShadowed)).toBeNull()
|
|
65
|
+
expect(lookupStaticArrayElement('R', '1', [ref], notShadowed)).toEqual(str('b'))
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test('a loop binding, a component binding or a later mutation keeps the read live', () => {
|
|
69
|
+
expect(lookupStaticArrayElement('QS', '0', [QS], name => name === 'QS')).toBeNull()
|
|
70
|
+
const shadowed = moduleConst('QS', `['a1']`, { shadowedInComponent: true })
|
|
71
|
+
expect(lookupStaticArrayElement('QS', '0', [shadowed], notShadowed)).toBeNull()
|
|
72
|
+
const mutated = moduleConst('QS', `['a1']`, { mutatedAfterDeclaration: true })
|
|
73
|
+
expect(lookupStaticArrayElement('QS', 'length', [mutated], notShadowed)).toBeNull()
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test('a record const is not an array, and the record lookup no longer reads arrays', () => {
|
|
77
|
+
const rec = moduleConst('REC', `{ lf: 'r1' }`)
|
|
78
|
+
expect(lookupStaticArrayElement('REC', 'lf', [rec], notShadowed)).toBeNull()
|
|
79
|
+
expect(lookupStaticRecordLiteral('QS', '0', [QS], notShadowed)).toBeNull()
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
describe('resolveStaticConstBinding: a component-local literal const', () => {
|
|
84
|
+
const constantsOf = (source: string) => buildMetadata(analyzeComponent(source, 'c.tsx')).localConstants
|
|
85
|
+
const element = (source: string, key = '0') => lookupStaticArrayElement('QS', key, constantsOf(source), notShadowed)
|
|
86
|
+
const member = (source: string, key = 'k') => lookupStaticRecordLiteral('REC', key, constantsOf(source), notShadowed)
|
|
87
|
+
|
|
88
|
+
test('a component-local array or record const resolves like a module const', () => {
|
|
89
|
+
const source = `export function C() {
|
|
90
|
+
const QS = ['a', 'b']
|
|
91
|
+
const REC = { k: 'v', n: 3 }
|
|
92
|
+
return <p>{QS[0]}{REC.k}</p>
|
|
93
|
+
}`
|
|
94
|
+
expect(element(source, '1')).toEqual(str('b'))
|
|
95
|
+
expect(element(source, 'length')).toEqual(num(2))
|
|
96
|
+
expect(member(source)).toEqual({ kind: 'string', text: 'v' })
|
|
97
|
+
expect(member(source, 'n')).toEqual({ kind: 'number', text: '3' })
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
test('a component-local const shadows the module const of the same name', () => {
|
|
101
|
+
const source = `const QS = ['module']
|
|
102
|
+
const REC = { k: 'module' }
|
|
103
|
+
export function C() {
|
|
104
|
+
const QS = ['local']
|
|
105
|
+
const REC = { k: 'local' }
|
|
106
|
+
return <p>{QS[0]}{REC.k}</p>
|
|
107
|
+
}`
|
|
108
|
+
expect(element(source)).toEqual(str('local'))
|
|
109
|
+
expect(member(source)).toEqual({ kind: 'string', text: 'local' })
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
test('a loop binding of the name keeps the read live', () => {
|
|
113
|
+
const constants = constantsOf(`export function C() {
|
|
114
|
+
const QS = ['a']
|
|
115
|
+
return <p>{QS[0]}</p>
|
|
116
|
+
}`)
|
|
117
|
+
expect(lookupStaticArrayElement('QS', '0', constants, name => name === 'QS')).toBeNull()
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
test('a const the component changes during render is not resolved, local or module', () => {
|
|
121
|
+
expect(element(`export function C() {
|
|
122
|
+
const QS = ['a']
|
|
123
|
+
QS.push('b')
|
|
124
|
+
return <p>{QS[1]}</p>
|
|
125
|
+
}`, '1')).toBeNull()
|
|
126
|
+
expect(member(`export function C() {
|
|
127
|
+
const REC = { k: 'v' }
|
|
128
|
+
REC.k = 'w'
|
|
129
|
+
return <p>{REC.k}</p>
|
|
130
|
+
}`)).toBeNull()
|
|
131
|
+
expect(member(`const REC = { k: 'v' }
|
|
132
|
+
export function C() {
|
|
133
|
+
REC.k = 'w'
|
|
134
|
+
return <p>{REC.k}</p>
|
|
135
|
+
}`)).toBeNull()
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('a let is not resolved', () => {
|
|
139
|
+
expect(element(`export function C() {
|
|
140
|
+
let QS = ['a']
|
|
141
|
+
return <p>{QS[0]}</p>
|
|
142
|
+
}`)).toBeNull()
|
|
143
|
+
})
|
|
144
|
+
|
|
145
|
+
test("an early-return branch's own const does not leak into the main return", () => {
|
|
146
|
+
const source = `export function C({ on }: { on: boolean }) {
|
|
147
|
+
if (on) {
|
|
148
|
+
const QS = ['inner']
|
|
149
|
+
return <p>{QS[0]}</p>
|
|
150
|
+
}
|
|
151
|
+
const QS = ['outer']
|
|
152
|
+
return <p>{QS[0]}</p>
|
|
153
|
+
}`
|
|
154
|
+
expect(element(source)).toEqual(str('outer'))
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
test('each component of a file reads its own declaration', () => {
|
|
158
|
+
const source = `export function A() {
|
|
159
|
+
const QS = ['a']
|
|
160
|
+
return <p>{QS[0]}</p>
|
|
161
|
+
}
|
|
162
|
+
export function B() {
|
|
163
|
+
const QS = ['b']
|
|
164
|
+
return <p>{QS[0]}</p>
|
|
165
|
+
}`
|
|
166
|
+
const read = (name: string) =>
|
|
167
|
+
lookupStaticArrayElement('QS', '0', buildMetadata(analyzeComponent(source, 'c.tsx', name)).localConstants, notShadowed)
|
|
168
|
+
expect(read('A')).toEqual(str('a'))
|
|
169
|
+
expect(read('B')).toEqual(str('b'))
|
|
170
|
+
})
|
|
171
|
+
})
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { describe, test, expect } from 'bun:test'
|
|
2
|
-
import { isStringTypedOperand, isStringConcatBinary } from '../adapters/parsed-expr-emitter'
|
|
2
|
+
import { isStringTypedOperand, isStringConcatBinary, type StringNameTest } from '../adapters/parsed-expr-emitter'
|
|
3
|
+
import { BindingScope } from '../scope/binding-scope'
|
|
3
4
|
import { parseExpression } from '../expression-parser'
|
|
4
5
|
|
|
5
6
|
// #2212: `isStringTypedOperand` gained a bare-`identifier` arm — previously
|
|
@@ -53,3 +54,41 @@ describe('isStringTypedOperand identifier arm (#2212)', () => {
|
|
|
53
54
|
expect(isStringConcatBinary(parsed.op, parsed.left, parsed.right, isStr)).toBe(false)
|
|
54
55
|
})
|
|
55
56
|
})
|
|
57
|
+
|
|
58
|
+
// #3762: a field read (`row.id`) is string-typed when the binding's declared
|
|
59
|
+
// row type says the field is; a member of a string-typed name is not.
|
|
60
|
+
describe('isStringTypedOperand member arm (#3762)', () => {
|
|
61
|
+
const rowType = {
|
|
62
|
+
raw: 'Row',
|
|
63
|
+
properties: [
|
|
64
|
+
{ name: 'id', type: { primitive: 'string' } },
|
|
65
|
+
{ name: 'n', type: { primitive: 'number' } },
|
|
66
|
+
{ name: 'meta', type: { properties: [{ name: 'label', type: { primitive: 'string' } }] } },
|
|
67
|
+
],
|
|
68
|
+
}
|
|
69
|
+
const scope = BindingScope.EMPTY.enterLoopRow({ param: 'r', index: 'i', rowType })
|
|
70
|
+
const isStr: StringNameTest = (n, property, path) =>
|
|
71
|
+
scope.isStringValueName(n, name => name === 'label', property, path)
|
|
72
|
+
const concat = (source: string) => {
|
|
73
|
+
const parsed = parseExpression(source)
|
|
74
|
+
if (parsed.kind !== 'binary') throw new Error('expected binary')
|
|
75
|
+
return isStringConcatBinary(parsed.op, parsed.left, parsed.right, isStr)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
test('a string row field, alone or nested, makes + a concatenation', () => {
|
|
79
|
+
expect(concat('r.id + i')).toBe(true)
|
|
80
|
+
expect(concat('r.n + r.id')).toBe(true)
|
|
81
|
+
expect(concat('r.meta.label + r.n')).toBe(true)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('a numeric field, an unknown field and a member of a string name stay numeric', () => {
|
|
85
|
+
expect(concat('r.n + i')).toBe(false)
|
|
86
|
+
expect(concat('r.missing + 1')).toBe(false)
|
|
87
|
+
expect(concat('label.length + 1')).toBe(false)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('a test that ignores the path never types a member as a string', () => {
|
|
91
|
+
const nameOnly: StringNameTest = n => n === 'label'
|
|
92
|
+
expect(isStringTypedOperand(parseExpression('label.length'), nameOnly)).toBe(false)
|
|
93
|
+
})
|
|
94
|
+
})
|
|
@@ -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,103 @@
|
|
|
1
|
+
import { exprToString, type ParsedExpr } from '../expression-parser.ts'
|
|
2
|
+
import {
|
|
3
|
+
ambiguousNilFamily,
|
|
4
|
+
ambiguousNilOperands,
|
|
5
|
+
arithmeticOperandCoercion,
|
|
6
|
+
type AmbiguousNilFamily,
|
|
7
|
+
type ArithmeticCoercion,
|
|
8
|
+
type NullishAttrContext,
|
|
9
|
+
} from '../nullable-signal-attr.ts'
|
|
10
|
+
import { BindingScope } from '../scope/binding-scope.ts'
|
|
11
|
+
import type { CompilerError, MemoInfo } from '../types.ts'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Where a template adapter records the refusal of an arithmetic operand
|
|
15
|
+
* whose nil the declared type cannot classify (#3700).
|
|
16
|
+
*/
|
|
17
|
+
export interface AmbiguousNilArithmeticSink {
|
|
18
|
+
readonly errors: CompilerError[]
|
|
19
|
+
readonly componentName: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The shared arithmetic-operand decision for a template adapter's emission
|
|
24
|
+
* site: the coercion the adapter spells (`arithmeticOperandCoercion`), after
|
|
25
|
+
* refusing (BF101) an operand that reads a number prop declared both
|
|
26
|
+
* optional and nullable (`n?: number | null`, `arithmeticOperandNilIsAmbiguous`).
|
|
27
|
+
* JS reads that prop as `undefined` when omitted (`NaN` in arithmetic) and
|
|
28
|
+
* as `0` when `null`; the engine holds both as one nil, so any spelling
|
|
29
|
+
* renders one of them wrong. Under a comparison the same holds for a string
|
|
30
|
+
* or boolean prop declared `?: string | null` / `?: boolean | null` (#3824
|
|
31
|
+
* step 3). Asked at the emission site so the operand is classified in the
|
|
32
|
+
* scope it is read in (`isShadowed`). One error per operand text per compile.
|
|
33
|
+
*/
|
|
34
|
+
export function arithmeticCoercionOrRefuse(
|
|
35
|
+
op: string,
|
|
36
|
+
operand: ParsedExpr,
|
|
37
|
+
other: ParsedExpr | undefined,
|
|
38
|
+
ctx: NullishAttrContext,
|
|
39
|
+
isShadowed: (name: string) => boolean,
|
|
40
|
+
sink: AmbiguousNilArithmeticSink,
|
|
41
|
+
): ArithmeticCoercion {
|
|
42
|
+
const family = ambiguousNilFamily(op, operand, other, ctx, isShadowed)
|
|
43
|
+
if (family !== null) recordAmbiguousNilArithmetic(exprToString(operand), family, sink)
|
|
44
|
+
return arithmeticOperandCoercion(op, operand, other, ctx, isShadowed)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Refuse (BF101) each arithmetic operand in a whole expression that a
|
|
49
|
+
* template adapter lowers without asking {@link arithmeticCoercionOrRefuse}
|
|
50
|
+
* per operator: a `.filter()` predicate, an expression the runtime evaluator
|
|
51
|
+
* runs, a condition lowered by its own renderer (`ambiguousNilArithmeticOperands`).
|
|
52
|
+
* `scope` is the scope the expression is read in.
|
|
53
|
+
*/
|
|
54
|
+
export function refuseAmbiguousNilArithmetic(
|
|
55
|
+
expr: ParsedExpr,
|
|
56
|
+
ctx: NullishAttrContext,
|
|
57
|
+
scope: BindingScope,
|
|
58
|
+
sink: AmbiguousNilArithmeticSink,
|
|
59
|
+
): void {
|
|
60
|
+
for (const { operand, family } of ambiguousNilOperands(expr, ctx, scope)) {
|
|
61
|
+
recordAmbiguousNilArithmetic(exprToString(operand), family, sink)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Refuse the ambiguous arithmetic in the component's memo bodies. A template
|
|
67
|
+
* adapter seeds every memo at SSR, and some seed a memo through a path that
|
|
68
|
+
* never reaches its operator emitter (a static seed after a speculative
|
|
69
|
+
* lowering, a constructor-time evaluation), so the bodies are checked as a
|
|
70
|
+
* whole, in the component scope they are declared in.
|
|
71
|
+
*/
|
|
72
|
+
export function refuseAmbiguousNilMemoArithmetic(
|
|
73
|
+
memos: readonly MemoInfo[],
|
|
74
|
+
ctx: NullishAttrContext,
|
|
75
|
+
sink: AmbiguousNilArithmeticSink,
|
|
76
|
+
): void {
|
|
77
|
+
for (const memo of memos) {
|
|
78
|
+
if (memo.parsed) refuseAmbiguousNilArithmetic(memo.parsed, ctx, BindingScope.EMPTY, sink)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function recordAmbiguousNilArithmetic(operandText: string, family: AmbiguousNilFamily, sink: AmbiguousNilArithmeticSink): void {
|
|
83
|
+
const message = ambiguousNilMessage(operandText, family)
|
|
84
|
+
if (sink.errors.some(e => e.code === 'BF101' && e.message === message)) return
|
|
85
|
+
sink.errors.push({
|
|
86
|
+
code: 'BF101',
|
|
87
|
+
severity: 'error',
|
|
88
|
+
message,
|
|
89
|
+
loc: { file: `${sink.componentName}.tsx`, start: { line: 1, column: 0 }, end: { line: 1, column: 0 } },
|
|
90
|
+
suggestion: {
|
|
91
|
+
message: `Declare the prop as only optional (\`?: ${family}\`) or only nullable (\`: ${family} | null\`), or give it a default value.`,
|
|
92
|
+
escape: [{ kind: 'rewrite' }],
|
|
93
|
+
},
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The BF101 text for an operand {@link ambiguousNilFamily} matched; the numeric one predates #3824 step 3. */
|
|
98
|
+
function ambiguousNilMessage(operandText: string, family: AmbiguousNilFamily): string {
|
|
99
|
+
if (family === 'number') {
|
|
100
|
+
return `Arithmetic over \`${operandText}\` reads a number prop declared both optional and nullable (\`?: number | null\`). JS reads it as \`undefined\` when the caller omits it (\`NaN\` in arithmetic) and as \`0\` when it is \`null\`, but a template engine holds both as the same nil, so the server render cannot tell which one it is.`
|
|
101
|
+
}
|
|
102
|
+
return `A comparison over \`${operandText}\` reads a ${family} prop declared both optional and nullable (\`?: ${family} | null\`). JS reads it as \`undefined\` when the caller omits it (\`NaN\`, so the comparison is false) and as \`null\` when it is \`null\` (\`0\`), but a template engine holds both as the same nil, so the server render cannot tell which one it is.`
|
|
103
|
+
}
|
|
@@ -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 }
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which values may hold a JS boolean that the backend received as data, for
|
|
3
|
+
* backends whose own stringification of such a value differs from JS's
|
|
4
|
+
* `String(boolean)`: Perl decodes a JSON `false` to an object that prints
|
|
5
|
+
* `0` (and `true` prints `1`). Unlike `collectBooleanValueNames`, the value
|
|
6
|
+
* need not be a boolean every time — an optional prop, a `boolean | string`
|
|
7
|
+
* union or a `??` fallback may also be absent or another type — so the
|
|
8
|
+
* backend routes it through its JS `String` helper, which prints every value
|
|
9
|
+
* kind as JS does, rather than through `bool_str`.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { ParsedExpr } from '../expression-parser.ts'
|
|
13
|
+
import type { IRMetadata, TypeInfo } from '../types.ts'
|
|
14
|
+
|
|
15
|
+
/** Whether a declared type admits a boolean: `boolean`, a boolean literal, or a union with such a member. */
|
|
16
|
+
function admitsBoolean(type: TypeInfo | null | undefined): boolean {
|
|
17
|
+
if (!type) return false
|
|
18
|
+
if (type.kind === 'primitive') return type.primitive === 'boolean'
|
|
19
|
+
return type.kind === 'union' && (type.unionTypes ?? []).some(admitsBoolean)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Signals, memos and props whose declared type admits a boolean, optional or
|
|
24
|
+
* not, plus signals seeded with a boolean literal and no declared type.
|
|
25
|
+
* `names` holds the bare spellings (a getter, a memo, a destructured prop),
|
|
26
|
+
* `props` the caller-facing keys a `props.<key>` member reads. Whether a
|
|
27
|
+
* binding shadows a name is the caller's scope question.
|
|
28
|
+
*/
|
|
29
|
+
export function collectBooleanCarrierNames(
|
|
30
|
+
metadata: Pick<IRMetadata, 'signals' | 'memos' | 'propsParams' | 'propsObjectName'>,
|
|
31
|
+
): { names: Set<string>; props: Set<string> } {
|
|
32
|
+
const names = new Set<string>()
|
|
33
|
+
const props = new Set<string>()
|
|
34
|
+
for (const signal of metadata.signals) {
|
|
35
|
+
const untyped = !signal.type || signal.type.kind === 'unknown'
|
|
36
|
+
const booleanSeed = signal.parsed?.kind === 'literal' && signal.parsed.literalType === 'boolean'
|
|
37
|
+
if (admitsBoolean(signal.type) || (untyped && booleanSeed)) names.add(signal.getter)
|
|
38
|
+
}
|
|
39
|
+
for (const memo of metadata.memos ?? []) {
|
|
40
|
+
if (admitsBoolean(memo.type)) names.add(memo.name)
|
|
41
|
+
}
|
|
42
|
+
for (const param of metadata.propsParams) {
|
|
43
|
+
// A destructured union-typed prop keeps its written type in `declaredType`.
|
|
44
|
+
if (!admitsBoolean(param.declaredType ?? param.type)) continue
|
|
45
|
+
props.add(param.sourceName ?? param.name)
|
|
46
|
+
if (!metadata.propsObjectName) names.add(param.name)
|
|
47
|
+
}
|
|
48
|
+
return { names, props }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether `expr` may evaluate to the value of a name `isCarrierName`
|
|
53
|
+
* accepts — a zero-argument getter call, a `props.<key>` member or a bare
|
|
54
|
+
* identifier — read directly, as an operand of `??` / `||` / `&&` (JS
|
|
55
|
+
* returns an operand, not a coerced boolean), or as a ternary branch.
|
|
56
|
+
*/
|
|
57
|
+
export function mayYieldBooleanCarrier(
|
|
58
|
+
expr: ParsedExpr | null | undefined,
|
|
59
|
+
isCarrierName: (name: string, componentProperty?: boolean) => boolean,
|
|
60
|
+
): boolean {
|
|
61
|
+
if (!expr) return false
|
|
62
|
+
switch (expr.kind) {
|
|
63
|
+
case 'logical':
|
|
64
|
+
return mayYieldBooleanCarrier(expr.left, isCarrierName) || mayYieldBooleanCarrier(expr.right, isCarrierName)
|
|
65
|
+
case 'conditional':
|
|
66
|
+
return mayYieldBooleanCarrier(expr.consequent, isCarrierName) || mayYieldBooleanCarrier(expr.alternate, isCarrierName)
|
|
67
|
+
case 'call':
|
|
68
|
+
return expr.callee.kind === 'identifier' && expr.args.length === 0 && isCarrierName(expr.callee.name)
|
|
69
|
+
case 'member':
|
|
70
|
+
return !expr.computed && expr.object.kind === 'identifier' && expr.object.name === 'props' && isCarrierName(expr.property, true)
|
|
71
|
+
case 'identifier':
|
|
72
|
+
return isCarrierName(expr.name)
|
|
73
|
+
default:
|
|
74
|
+
return false
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -40,17 +40,8 @@
|
|
|
40
40
|
|
|
41
41
|
import { parseExpression, type ParsedExpr } from '../expression-parser.ts'
|
|
42
42
|
import type { IRMetadata, TypeInfo } from '../types.ts'
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
'<',
|
|
46
|
-
'>',
|
|
47
|
-
'<=',
|
|
48
|
-
'>=',
|
|
49
|
-
'==',
|
|
50
|
-
'===',
|
|
51
|
-
'!=',
|
|
52
|
-
'!==',
|
|
53
|
-
])
|
|
43
|
+
import { operandFacts, planTest, type NativeSafeTable, type PrimitivePlan } from './primitive-plan.ts'
|
|
44
|
+
import { isExactly, typingFromNameTest, type ScopedOperandTyping } from './operand-typing.ts'
|
|
54
45
|
|
|
55
46
|
/**
|
|
56
47
|
* Whether `node` evaluates to a JS boolean: structurally, or, when the
|
|
@@ -64,68 +55,66 @@ export function isBooleanResultParsed(
|
|
|
64
55
|
node: ParsedExpr,
|
|
65
56
|
isBooleanName?: (name: string, componentProperty?: boolean) => boolean,
|
|
66
57
|
): boolean {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return COMPARISON_OPS.has(node.op)
|
|
72
|
-
case 'unary':
|
|
73
|
-
return node.op === '!'
|
|
74
|
-
case 'logical':
|
|
75
|
-
// `x > 0 && y < 10` is boolean; `x() || 'fallback'` is not.
|
|
76
|
-
// Only both-sides-boolean qualifies.
|
|
77
|
-
return (
|
|
78
|
-
isBooleanResultParsed(node.left, isBooleanName) && isBooleanResultParsed(node.right, isBooleanName)
|
|
79
|
-
)
|
|
80
|
-
case 'conditional':
|
|
81
|
-
// `cond ? bool : bool` is boolean; `cond ? 'a' : 'b'` is not.
|
|
82
|
-
return (
|
|
83
|
-
isBooleanResultParsed(node.consequent, isBooleanName) &&
|
|
84
|
-
isBooleanResultParsed(node.alternate, isBooleanName)
|
|
85
|
-
)
|
|
86
|
-
case 'call':
|
|
87
|
-
return !!isBooleanName && node.callee.kind === 'identifier' && node.args.length === 0 && isBooleanName(node.callee.name)
|
|
88
|
-
case 'member':
|
|
89
|
-
return !!isBooleanName && !node.computed && node.object.kind === 'identifier' && node.object.name === 'props' &&
|
|
90
|
-
isBooleanName(node.property, true)
|
|
91
|
-
case 'identifier':
|
|
92
|
-
return !!isBooleanName && isBooleanName(node.name)
|
|
93
|
-
default:
|
|
94
|
-
return false
|
|
95
|
-
}
|
|
58
|
+
// A thin wrapper over the primitive plan's single classifier (#3824 step 0):
|
|
59
|
+
// structurally boolean, or a leaf `isBooleanName` proves boolean, combined
|
|
60
|
+
// through `&&` / `||` / `??` / `?:` only when every value operand is.
|
|
61
|
+
return isExactly(operandFacts(node, typingFromNameTest('boolean', isBooleanName)), 'boolean')
|
|
96
62
|
}
|
|
97
63
|
|
|
98
64
|
/**
|
|
99
|
-
* Rewrites the boolean-result
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
* `
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
65
|
+
* Rewrites the boolean-result values a mixed expression can evaluate to, so
|
|
66
|
+
* a taken boolean carries JS `String(boolean)` instead of Perl's `0` / `''`
|
|
67
|
+
* / `1`. The whole value isn't boolean-shaped, so `bool_str` can't wrap it.
|
|
68
|
+
*
|
|
69
|
+
* - A ternary branch (`yes() ? false : s()`, #3349): a literal `true` /
|
|
70
|
+
* `false` becomes the string literal `'true'` / `'false'`, any other
|
|
71
|
+
* boolean-result branch `b` becomes `b ? 'true' : 'false'`.
|
|
72
|
+
* - An operand JS `&&` / `||` / `??` returns (`n > 0 && 'y'`): the left
|
|
73
|
+
* operand is returned only when it decides the result, so its rewritten
|
|
74
|
+
* form `l'` (the operand stringified, a nested chain or a ternary
|
|
75
|
+
* rewritten likewise) is what the expression yields then, while the
|
|
76
|
+
* original operand stays the test: `l && r` becomes `l ? r : l'` and
|
|
77
|
+
* `l || r` becomes `l ? l' : r`. `l ?? r` becomes `l' ?? r`, since the
|
|
78
|
+
* rewrite keeps nullishness. A boolean-result `l` gives `b ? r :
|
|
79
|
+
* 'false'` and `b ? 'true' : r`. The right operand is rewritten as a
|
|
80
|
+
* branch.
|
|
81
|
+
*
|
|
82
|
+
* The existing lowering then renders the rewritten tree unchanged. Nested
|
|
83
|
+
* values are rewritten recursively; a tree with nothing to rewrite is
|
|
84
|
+
* returned as is (the same object). Only an output position may take the
|
|
85
|
+
* rewrite: a `'false'` string is truthy.
|
|
108
86
|
*/
|
|
109
|
-
export function
|
|
110
|
-
if (
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
alternate
|
|
87
|
+
export function stringifyBooleanBranches(node: ParsedExpr): ParsedExpr {
|
|
88
|
+
if (isBooleanResultParsed(node)) return node
|
|
89
|
+
if (node.kind === 'conditional') {
|
|
90
|
+
const consequent = stringifyBooleanBranch(node.consequent)
|
|
91
|
+
const alternate = stringifyBooleanBranch(node.alternate)
|
|
92
|
+
return consequent === node.consequent && alternate === node.alternate ? node : { ...node, consequent, alternate }
|
|
115
93
|
}
|
|
94
|
+
if (node.kind !== 'logical') return node
|
|
95
|
+
const right = stringifyBooleanBranch(node.right)
|
|
96
|
+
const left = stringifyBooleanBranch(node.left)
|
|
97
|
+
if (left === node.left) return right === node.right ? node : { ...node, right }
|
|
98
|
+
const wholly = isBooleanResultParsed(node.left)
|
|
99
|
+
// `??` tests nullishness only, which the rewrite keeps: a stringified
|
|
100
|
+
// boolean is as non-nullish as the boolean, and a boolean is never nullish.
|
|
101
|
+
if (node.op === '??') return wholly ? left : { ...node, left, right }
|
|
102
|
+
// `&&` / `||` test the left operand's truthiness, which a `'false'`
|
|
103
|
+
// string would flip, so the original operand stays the test and the
|
|
104
|
+
// rewritten one is what the expression yields when it returns it.
|
|
105
|
+
return node.op === '&&'
|
|
106
|
+
? { kind: 'conditional', test: node.left, consequent: right, alternate: wholly ? stringLiteral('false') : left }
|
|
107
|
+
: { kind: 'conditional', test: node.left, consequent: wholly ? stringLiteral('true') : left, alternate: right }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function stringLiteral(value: string): ParsedExpr {
|
|
111
|
+
return { kind: 'literal', value, literalType: 'string' }
|
|
116
112
|
}
|
|
117
113
|
|
|
118
114
|
function stringifyBooleanBranch(branch: ParsedExpr): ParsedExpr {
|
|
119
|
-
if (!isBooleanResultParsed(branch)) return
|
|
120
|
-
if (branch.kind === 'literal')
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
return {
|
|
124
|
-
kind: 'conditional',
|
|
125
|
-
test: branch,
|
|
126
|
-
consequent: { kind: 'literal', value: 'true', literalType: 'string' },
|
|
127
|
-
alternate: { kind: 'literal', value: 'false', literalType: 'string' },
|
|
128
|
-
}
|
|
115
|
+
if (!isBooleanResultParsed(branch)) return stringifyBooleanBranches(branch)
|
|
116
|
+
if (branch.kind === 'literal') return stringLiteral(String(branch.value))
|
|
117
|
+
return { kind: 'conditional', test: branch, consequent: stringLiteral('true'), alternate: stringLiteral('false') }
|
|
129
118
|
}
|
|
130
119
|
|
|
131
120
|
|
|
@@ -211,11 +200,28 @@ export function collectBooleanValueNames(
|
|
|
211
200
|
* unparseable expression is not boolean-result.
|
|
212
201
|
*/
|
|
213
202
|
export function isBooleanResultExpr(expr: string): boolean {
|
|
214
|
-
const parsed =
|
|
203
|
+
const parsed = testTree(expr)
|
|
215
204
|
if (!parsed) return false
|
|
216
205
|
return isBooleanResultParsed(parsed)
|
|
217
206
|
}
|
|
218
207
|
|
|
208
|
+
/**
|
|
209
|
+
* The ToBoolean plan (`planTest`, #3824) of a test whose `ParsedExpr` the IR
|
|
210
|
+
* does not carry: a template literal's ternary part, which holds only its
|
|
211
|
+
* condition text. It reads the same tree {@link isBooleanResultExpr} always
|
|
212
|
+
* parsed for such a test. A test the IR carries a tree for is planned with
|
|
213
|
+
* `planTest` on that tree.
|
|
214
|
+
*/
|
|
215
|
+
export function planTestOfSource(expr: string, typing: ScopedOperandTyping, target: NativeSafeTable): PrimitivePlan | null {
|
|
216
|
+
const parsed = testTree(expr)
|
|
217
|
+
return parsed ? planTest(parsed, typing, target) : null
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** The tree of a test held only as source text; null when unparseable. */
|
|
221
|
+
function testTree(expr: string): ParsedExpr | null {
|
|
222
|
+
return parseExpression(expr.trim()) ?? null
|
|
223
|
+
}
|
|
224
|
+
|
|
219
225
|
/**
|
|
220
226
|
* True when `expr`'s top-level shape is an explicit one-argument JS
|
|
221
227
|
* `String(x)` call (the `EVAL_BUILTIN_IDENTS` builtin the compiler
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Static text in a template engine whose tags open with a brace — Jinja,
|
|
3
|
+
* MiniJinja, Twig and Pebble (`{{`, `{%`, `{#`), Go `html/template` (`{{`)
|
|
4
|
+
* — must never put a tag opener into the template: JSX text such as
|
|
5
|
+
* `{{ 1 }}` decodes to `{{ 1 }}`, which Hono prints as
|
|
6
|
+
* written but such an engine compiles as a tag, and so prints `1` or fails
|
|
7
|
+
* to parse.
|
|
8
|
+
*
|
|
9
|
+
* Every `{` that is followed by one of `followers` (the characters that turn
|
|
10
|
+
* a `{` into a tag opener on this engine) is emitted through `literal`
|
|
11
|
+
* instead — an output expression for the brace itself (`{{ '{' }}`,
|
|
12
|
+
* `{{"{"}}`). So is a `{` that ends the text: the text cannot know what the
|
|
13
|
+
* adapter writes after it, and a following output tag (`{{ x }}`) would turn
|
|
14
|
+
* that brace into part of a longer opener. The expression renders the same
|
|
15
|
+
* character either way.
|
|
16
|
+
*/
|
|
17
|
+
export function escapeBraceDelimiters(text: string, followers: string, literal: string): string {
|
|
18
|
+
let out = ''
|
|
19
|
+
for (let i = 0; i < text.length; i++) {
|
|
20
|
+
const ch = text[i]
|
|
21
|
+
const next = text[i + 1]
|
|
22
|
+
out += ch === '{' && (next === undefined || followers.includes(next)) ? literal : ch
|
|
23
|
+
}
|
|
24
|
+
return out
|
|
25
|
+
}
|