@barefootjs/jsx 0.40.1 → 0.40.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts +4 -3
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -1
- package/dist/adapters/boolean-result.d.ts +32 -10
- package/dist/adapters/boolean-result.d.ts.map +1 -1
- package/dist/adapters/brace-delimiter-text.d.ts +18 -0
- package/dist/adapters/brace-delimiter-text.d.ts.map +1 -0
- package/dist/adapters/callback-method.d.ts +38 -5
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +11 -1
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +21 -0
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/loop-bound-names.d.ts +9 -1
- package/dist/adapters/loop-bound-names.d.ts.map +1 -1
- package/dist/adapters/loop-row-type.d.ts +52 -0
- package/dist/adapters/loop-row-type.d.ts.map +1 -0
- package/dist/adapters/nil-reading.d.ts +68 -0
- package/dist/adapters/nil-reading.d.ts.map +1 -0
- package/dist/adapters/non-primitive-equality.d.ts +33 -0
- package/dist/adapters/non-primitive-equality.d.ts.map +1 -0
- package/dist/adapters/number-value.d.ts +5 -3
- package/dist/adapters/number-value.d.ts.map +1 -1
- package/dist/adapters/operand-typing.d.ts +126 -0
- package/dist/adapters/operand-typing.d.ts.map +1 -0
- package/dist/adapters/parsed-expr-emitter.d.ts +70 -12
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/adapters/primitive-plan.d.ts +291 -0
- package/dist/adapters/primitive-plan.d.ts.map +1 -0
- package/dist/analyzer-context.d.ts +4 -0
- package/dist/analyzer-context.d.ts.map +1 -1
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +63 -8
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/compiler.d.ts.map +1 -1
- package/dist/event-handler-name.d.ts +12 -0
- package/dist/event-handler-name.d.ts.map +1 -1
- package/dist/expression-parser.d.ts +8 -0
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/index.d.ts +19 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4628 -2198
- package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
- package/dist/ir-to-client-js/child-props-object.d.ts +64 -0
- package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -0
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +4 -2
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/imports.d.ts +2 -2
- package/dist/ir-to-client-js/imports.d.ts.map +1 -1
- package/dist/ir-to-client-js/index.d.ts +2 -6
- package/dist/ir-to-client-js/index.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +30 -18
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/prop-handling.d.ts.map +1 -1
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +3 -2
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts +1 -0
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +68 -9
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/prop-rewrite.d.ts +7 -0
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-object-whole-read.d.ts +15 -0
- package/dist/props-object-whole-read.d.ts.map +1 -0
- package/dist/rich-type-refusal.d.ts.map +1 -1
- package/dist/scope/binding-scope.d.ts +85 -10
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/scope/lexical-scope-walk.d.ts +3 -2
- package/dist/scope/lexical-scope-walk.d.ts.map +1 -1
- package/dist/scope/root-prop-alias.d.ts +10 -7
- package/dist/scope/root-prop-alias.d.ts.map +1 -1
- package/dist/strip-types.d.ts +13 -0
- package/dist/strip-types.d.ts.map +1 -1
- package/dist/template-position-walk.d.ts +21 -3
- package/dist/template-position-walk.d.ts.map +1 -1
- package/dist/types.d.ts +90 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +3 -3
- package/src/__tests__/binding-scope.test.ts +89 -1
- package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -0
- package/src/__tests__/boolean-result.test.ts +22 -5
- package/src/__tests__/brace-delimiter-text.test.ts +26 -0
- package/src/__tests__/callback-method.test.ts +62 -2
- package/src/__tests__/child-components-in-map.test.ts +1 -1
- package/src/__tests__/computed-const.test.ts +103 -0
- package/src/__tests__/emit-to-string-call.test.ts +2 -1
- package/src/__tests__/hydration-payload-analysis.test.ts +166 -0
- package/src/__tests__/issue-2724-prop-alias-loop-not-static.test.ts +7 -7
- package/src/__tests__/issue-2754-rest-spread-needs-slot.test.ts +5 -2
- package/src/__tests__/issue-2798-static-nested-loop-bindings.test.ts +1 -1
- package/src/__tests__/issue-2835-props-type-member-name-collision.test.ts +4 -4
- package/src/__tests__/issue-3044-nested-prop-object-array-not-static.test.ts +1 -1
- package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +2 -1
- package/src/__tests__/issue-3713-props-spread-loop-row.test.ts +65 -0
- package/src/__tests__/lookup-literal-const.test.ts +9 -3
- package/src/__tests__/loop-index-reactivity.test.ts +2 -2
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +1 -1
- package/src/__tests__/loop-plan.test.ts +1 -1
- package/src/__tests__/loop-row-const-initializer-shadow.test.ts +146 -0
- package/src/__tests__/loop-row-type.test.ts +64 -0
- package/src/__tests__/lowering-value-position.test.ts +4 -0
- package/src/__tests__/module-const-destructure-default.test.ts +156 -0
- package/src/__tests__/nested-loop-index-param.test.ts +1 -1
- package/src/__tests__/nested-loop-reactive-attrs.test.ts +2 -2
- package/src/__tests__/nested-row-shadowing.test.ts +2 -2
- package/src/__tests__/nil-reading.test.ts +77 -0
- package/src/__tests__/nullable-signal-attr.test.ts +134 -0
- package/src/__tests__/primitive-plan.test.ts +416 -0
- package/src/__tests__/profile-loop-binding-ids.test.ts +2 -2
- package/src/__tests__/profile-nested-binding-ids.test.ts +2 -2
- package/src/__tests__/props-object-whole-read.test.ts +40 -0
- package/src/__tests__/reactive-attrs-in-map.test.ts +1 -1
- package/src/__tests__/reactive-factory-inlining.test.ts +4 -1
- package/src/__tests__/rest-binding-gaps.test.ts +192 -0
- package/src/__tests__/root-prop-alias.test.ts +53 -8
- package/src/__tests__/static-loop-csr-materialize.test.ts +2 -2
- package/src/__tests__/static-record-literal-array.test.ts +171 -0
- package/src/__tests__/string-concat-identifier.test.ts +40 -1
- package/src/adapters/ambiguous-nil-arithmetic.ts +22 -13
- package/src/adapters/boolean-result.ts +72 -66
- package/src/adapters/brace-delimiter-text.ts +25 -0
- package/src/adapters/callback-method.ts +151 -27
- package/src/adapters/computed-const.ts +238 -4
- package/src/adapters/dsl-template-adapter.ts +40 -2
- package/src/adapters/loop-bound-names.ts +9 -1
- package/src/adapters/loop-plan.ts +1 -1
- package/src/adapters/loop-row-type.ts +139 -0
- package/src/adapters/nil-reading.ts +297 -0
- package/src/adapters/non-primitive-equality.ts +92 -0
- package/src/adapters/number-value.ts +17 -4
- package/src/adapters/operand-typing.ts +522 -0
- package/src/adapters/parsed-expr-emitter.ts +154 -19
- package/src/adapters/primitive-plan.ts +696 -0
- package/src/analyzer-context.ts +6 -0
- package/src/analyzer.ts +505 -19
- package/src/augment-inherited-props.ts +108 -10
- package/src/compiler.ts +17 -0
- package/src/event-handler-name.ts +15 -0
- package/src/expression-parser.ts +70 -1
- package/src/index.ts +33 -7
- package/src/ir-to-client-js/build-references.ts +69 -0
- package/src/ir-to-client-js/child-props-object.ts +106 -0
- package/src/ir-to-client-js/collect-elements.ts +30 -55
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +6 -1
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +29 -28
- package/src/ir-to-client-js/control-flow/shared.ts +20 -22
- package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +5 -3
- package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +2 -2
- package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +1 -1
- package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +2 -2
- package/src/ir-to-client-js/control-flow/stringify/loop.ts +1 -1
- package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +3 -3
- package/src/ir-to-client-js/html-template.ts +25 -8
- package/src/ir-to-client-js/imports.ts +1 -1
- package/src/ir-to-client-js/index.ts +53 -11
- package/src/ir-to-client-js/plan/build-static-array-child-init.ts +31 -14
- package/src/ir-to-client-js/plan/static-array-child-init.ts +27 -18
- package/src/ir-to-client-js/prop-handling.ts +22 -3
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +26 -10
- package/src/jsx-to-ir.ts +446 -37
- package/src/nullable-signal-attr.ts +246 -29
- package/src/prop-rewrite.ts +30 -6
- package/src/props-object-whole-read.ts +87 -0
- package/src/rich-type-refusal.ts +2 -1
- package/src/scope/binding-scope.ts +182 -18
- package/src/scope/lexical-scope-walk.ts +4 -3
- package/src/scope/root-prop-alias.ts +15 -8
- package/src/strip-types.ts +31 -3
- package/src/template-position-walk.ts +57 -28
- package/src/types.ts +87 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The nil-reading rewrite (`spec/js-primitive-semantics.md`, "Nil reading is
|
|
3
|
+
* a tree rewrite"; #3824 step 4a in `spec/js-undefined-value.md`).
|
|
4
|
+
*
|
|
5
|
+
* Every runtime holds JS `null` and `undefined` as one nil, and its
|
|
6
|
+
* primitive helpers (`to_number`, `to_string`, `plus`, the relational ones)
|
|
7
|
+
* read that nil as `null`. Where Phase-1 types say which value a nil stands
|
|
8
|
+
* for, and the consumer's coercion is known statically, the operand is
|
|
9
|
+
* rewritten before planning so that it is never nil:
|
|
10
|
+
*
|
|
11
|
+
* | Consumer | nil reads as `null` | nil reads as `undefined` |
|
|
12
|
+
* | --- | --- | --- |
|
|
13
|
+
* | numeric (`- * / %`, unary `-` / `+`, relational, numeric `+`) | `x ?? 0` | `x ?? NaN` |
|
|
14
|
+
* | string (`+` concatenation, template-literal part) | `x ?? 'null'` | `x ?? 'undefined'` |
|
|
15
|
+
*
|
|
16
|
+
* After the rewrite the operand is non-nil, so the plan can name the native
|
|
17
|
+
* operator (`(n ?? NaN) * 2` stays native) and the helpers never need to
|
|
18
|
+
* know about `undefined`.
|
|
19
|
+
*
|
|
20
|
+
* A `+` whose present branch concatenates and whose nil branch adds
|
|
21
|
+
* (`o.id + 1` with `id?: string`) is split on nil by the numeric row:
|
|
22
|
+
* `(o.id ?? NaN) + 1` is `NaN` for an omitted field and `'o1'` for a present
|
|
23
|
+
* one, because the operand is then a string or a number and the plan takes
|
|
24
|
+
* the runtime's `plus`, which decides per value. With a `null` reading the
|
|
25
|
+
* nil branch is `0 + 1`, as JS's `null + 1`.
|
|
26
|
+
*
|
|
27
|
+
* What the rewrite leaves alone (`spec/js-undefined-value.md` §7 (4)):
|
|
28
|
+
*
|
|
29
|
+
* - A numeric operand whose nil may be either (`n?: number | null`): the
|
|
30
|
+
* template adapters refuse it with BF101 (#3700) where the coercion is
|
|
31
|
+
* static, and keep today's output elsewhere.
|
|
32
|
+
* - A `+` whose other operand is neither a proven number nor a proven
|
|
33
|
+
* string: numeric addition or concatenation is decided at run time, and a
|
|
34
|
+
* nil reading as `undefined` cannot be told from `null` there. It keeps
|
|
35
|
+
* today's output (the runtime's `plus` reads the nil as `null`) until the
|
|
36
|
+
* runtimes carry `undefined` (#3848).
|
|
37
|
+
* - A string operand whose nil may be either (`c?: string | null`): it keeps
|
|
38
|
+
* today's display reading, the empty string, until the runtimes carry
|
|
39
|
+
* `undefined` (#3848).
|
|
40
|
+
* - A nil Phase 1 cannot type (an index read, an untyped value): the
|
|
41
|
+
* runtime's helpers read it as `null`.
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
import type { ParsedExpr, TemplatePart } from '../expression-parser.ts'
|
|
45
|
+
import { operandFacts } from './primitive-plan.ts'
|
|
46
|
+
import type { NilReading, OperandFacts, ScopedOperandTyping } from './operand-typing.ts'
|
|
47
|
+
|
|
48
|
+
const NUMERIC_BINARY_OPS: ReadonlySet<string> = new Set(['-', '*', '/', '%', '**', '<', '<=', '>', '>='])
|
|
49
|
+
|
|
50
|
+
/** The literal `NaN`, which every emitter spells through its runtime's `number('NaN')`. */
|
|
51
|
+
export const NAN_LITERAL: ParsedExpr = { kind: 'literal', value: Number.NaN, literalType: 'number', raw: 'NaN' }
|
|
52
|
+
|
|
53
|
+
const ZERO_LITERAL: ParsedExpr = { kind: 'literal', value: 0, literalType: 'number', raw: '0' }
|
|
54
|
+
|
|
55
|
+
function stringLiteral(value: string): ParsedExpr {
|
|
56
|
+
return { kind: 'literal', value, literalType: 'string' }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The reading of `facts`' nil, or null when the value is never nil or Phase 1 proves nothing about it. */
|
|
60
|
+
function readingOf(facts: OperandFacts): NilReading | null {
|
|
61
|
+
if (facts.types.has('unknown') || !facts.types.has('nil')) return null
|
|
62
|
+
return facts.nil ?? null
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The types `facts` allow besides nil. */
|
|
66
|
+
function presentTypes(facts: OperandFacts): Set<string> {
|
|
67
|
+
const types = new Set<string>(facts.types)
|
|
68
|
+
types.delete('nil')
|
|
69
|
+
return types
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The `??` nodes the rewrite builds, so a target can check their left operand can hold nil. */
|
|
73
|
+
const NIL_READINGS = new WeakSet<ParsedExpr>()
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Whether `l ?? r` was built by the rewrite (its right operand is the
|
|
77
|
+
* rewrite's fallback). A target whose host types an absent value as a
|
|
78
|
+
* concrete zero (Go) must give `l` a nillable representation, or refuse:
|
|
79
|
+
* the nil test would see a present `''` / `0` / `false` as absent.
|
|
80
|
+
*/
|
|
81
|
+
export function isNilReadingFallback(right: ParsedExpr): boolean {
|
|
82
|
+
return NIL_READINGS.has(right)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** `operand ?? fallback`; the fallback alone when the operand can only be nil. */
|
|
86
|
+
function coalesce(operand: ParsedExpr, facts: OperandFacts, fallback: ParsedExpr): ParsedExpr {
|
|
87
|
+
if (presentTypes(facts).size === 0) return fallback
|
|
88
|
+
const right: ParsedExpr = { ...fallback }
|
|
89
|
+
NIL_READINGS.add(right)
|
|
90
|
+
return { kind: 'logical', op: '??', left: operand, right }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** An operand of a numeric consumer: its nil as `0` (`null`) or `NaN` (`undefined`). */
|
|
94
|
+
function numericOperand(operand: ParsedExpr, facts: OperandFacts): ParsedExpr {
|
|
95
|
+
const reading = readingOf(facts)
|
|
96
|
+
if (reading === 'null') return coalesce(operand, facts, ZERO_LITERAL)
|
|
97
|
+
if (reading === 'undefined') return coalesce(operand, facts, NAN_LITERAL)
|
|
98
|
+
return operand
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* An operand of a string consumer: its nil as `'null'` / `'undefined'`, and
|
|
103
|
+
* an `either` nil as today's display reading, the empty string (#3848).
|
|
104
|
+
*/
|
|
105
|
+
function stringOperand(operand: ParsedExpr, facts: OperandFacts): ParsedExpr {
|
|
106
|
+
const reading = readingOf(facts)
|
|
107
|
+
if (reading === null) return operand
|
|
108
|
+
return coalesce(operand, facts, stringLiteral(reading === 'either' ? '' : reading))
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const PRIMITIVE_TYPES: ReadonlySet<string> = new Set(['number', 'string', 'boolean'])
|
|
112
|
+
|
|
113
|
+
/** Whether `facts` prove exactly the one type `type`, never nil. */
|
|
114
|
+
function isExactly(facts: OperandFacts, type: string): boolean {
|
|
115
|
+
return facts.types.size === 1 && facts.types.has(type as never)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* One operand of a `+` whose other operand has `other` facts: a string
|
|
120
|
+
* consumer when the other is a proven string; a numeric consumer (the nil
|
|
121
|
+
* split, for a string operand) when the other is a proven number and this
|
|
122
|
+
* one is a primitive; left alone otherwise.
|
|
123
|
+
*/
|
|
124
|
+
function plusOperand(operand: ParsedExpr, facts: OperandFacts, other: OperandFacts): ParsedExpr {
|
|
125
|
+
if (isExactly(other, 'string')) return stringOperand(operand, facts)
|
|
126
|
+
if (!isExactly(other, 'number')) return operand
|
|
127
|
+
const present = presentTypes(facts)
|
|
128
|
+
return [...present].every(t => PRIMITIVE_TYPES.has(t)) ? numericOperand(operand, facts) : operand
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function rewriteBinary(expr: Extract<ParsedExpr, { kind: 'binary' }>, typing: ScopedOperandTyping): ParsedExpr {
|
|
132
|
+
const lf = operandFacts(expr.left, typing)
|
|
133
|
+
const rf = operandFacts(expr.right, typing)
|
|
134
|
+
let left = expr.left
|
|
135
|
+
let right = expr.right
|
|
136
|
+
if (NUMERIC_BINARY_OPS.has(expr.op)) {
|
|
137
|
+
left = numericOperand(expr.left, lf)
|
|
138
|
+
right = numericOperand(expr.right, rf)
|
|
139
|
+
} else if (expr.op === '+') {
|
|
140
|
+
left = plusOperand(expr.left, lf, rf)
|
|
141
|
+
right = plusOperand(expr.right, rf, lf)
|
|
142
|
+
}
|
|
143
|
+
return left === expr.left && right === expr.right ? expr : { ...expr, left, right }
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function rewriteTemplateLiteral(expr: Extract<ParsedExpr, { kind: 'template-literal' }>, typing: ScopedOperandTyping): ParsedExpr {
|
|
147
|
+
let changed = false
|
|
148
|
+
const parts: TemplatePart[] = expr.parts.map(part => {
|
|
149
|
+
if (part.type !== 'expression') return part
|
|
150
|
+
const rewritten = stringOperand(part.expr, operandFacts(part.expr, typing))
|
|
151
|
+
if (rewritten === part.expr) return part
|
|
152
|
+
changed = true
|
|
153
|
+
return { ...part, expr: rewritten }
|
|
154
|
+
})
|
|
155
|
+
return changed ? { ...expr, parts } : expr
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* `expr` with the operands of its own coercion rewritten by their nil
|
|
160
|
+
* reading in `typing`'s scope; `expr` itself when nothing changes. Shallow:
|
|
161
|
+
* only `expr`'s direct operands are rewritten, as `emitParsedExpr` calls it
|
|
162
|
+
* at every node it dispatches. Idempotent: a rewritten operand is never nil.
|
|
163
|
+
*/
|
|
164
|
+
export function rewriteNilReadings(expr: ParsedExpr, typing: ScopedOperandTyping): ParsedExpr {
|
|
165
|
+
switch (expr.kind) {
|
|
166
|
+
case 'binary':
|
|
167
|
+
return rewriteBinary(expr, typing)
|
|
168
|
+
case 'unary': {
|
|
169
|
+
if (expr.op !== '-' && expr.op !== '+') return expr
|
|
170
|
+
const argument = numericOperand(expr.argument, operandFacts(expr.argument, typing))
|
|
171
|
+
return argument === expr.argument ? expr : { ...expr, argument }
|
|
172
|
+
}
|
|
173
|
+
case 'template-literal':
|
|
174
|
+
return rewriteTemplateLiteral(expr, typing)
|
|
175
|
+
default:
|
|
176
|
+
return expr
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* {@link rewriteNilReadings} at every node of `expr`, children first, for a
|
|
182
|
+
* tree a template adapter hands to the runtime evaluator or to its own
|
|
183
|
+
* walker instead of `emitParsedExpr` (a callback body, a memo body). An
|
|
184
|
+
* arrow's params shadow outer names within its body.
|
|
185
|
+
*/
|
|
186
|
+
export function rewriteNilReadingsDeep(expr: ParsedExpr, typing: ScopedOperandTyping): ParsedExpr {
|
|
187
|
+
const walk = (e: ParsedExpr, at: ScopedOperandTyping): ParsedExpr => {
|
|
188
|
+
if (e.kind === 'arrow') {
|
|
189
|
+
const body = walk(e.body, at.enterCallback(e.params))
|
|
190
|
+
return body === e.body ? e : { ...e, body }
|
|
191
|
+
}
|
|
192
|
+
return rewriteNilReadings(mapChildren(e, child => walk(child, at)), at)
|
|
193
|
+
}
|
|
194
|
+
return walk(expr, typing)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** `items` mapped by `f`, or `items` itself when no item changes. */
|
|
198
|
+
function mapList(items: readonly ParsedExpr[], f: (child: ParsedExpr) => ParsedExpr): ParsedExpr[] | null {
|
|
199
|
+
let changed = false
|
|
200
|
+
const out = items.map(item => {
|
|
201
|
+
const next = f(item)
|
|
202
|
+
if (next !== item) changed = true
|
|
203
|
+
return next
|
|
204
|
+
})
|
|
205
|
+
return changed ? out : null
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function mapTemplateParts(parts: readonly TemplatePart[], f: (child: ParsedExpr) => ParsedExpr): TemplatePart[] | null {
|
|
209
|
+
let changed = false
|
|
210
|
+
const out = parts.map(p => {
|
|
211
|
+
if (p.type !== 'expression') return p
|
|
212
|
+
const expr = f(p.expr)
|
|
213
|
+
if (expr === p.expr) return p
|
|
214
|
+
changed = true
|
|
215
|
+
return { ...p, expr }
|
|
216
|
+
})
|
|
217
|
+
return changed ? out : null
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
type ObjectLiteral = Extract<ParsedExpr, { kind: 'object-literal' }>
|
|
221
|
+
|
|
222
|
+
function mapProperties(properties: ObjectLiteral['properties'], f: (child: ParsedExpr) => ParsedExpr): ObjectLiteral['properties'] | null {
|
|
223
|
+
let changed = false
|
|
224
|
+
const out = properties.map(p => {
|
|
225
|
+
const value = p.kind === 'spread' ? p.expr : p.value
|
|
226
|
+
const next = f(value)
|
|
227
|
+
if (next === value) return p
|
|
228
|
+
changed = true
|
|
229
|
+
return p.kind === 'spread' ? { ...p, expr: next } : { ...p, value: next }
|
|
230
|
+
})
|
|
231
|
+
return changed ? out : null
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** `e` with each operand child replaced by `f(child)`; `e` itself when none changes. */
|
|
235
|
+
function mapOperands(e: ParsedExpr, f: (child: ParsedExpr) => ParsedExpr): ParsedExpr | undefined {
|
|
236
|
+
switch (e.kind) {
|
|
237
|
+
case 'binary':
|
|
238
|
+
case 'logical': {
|
|
239
|
+
const left = f(e.left)
|
|
240
|
+
const right = f(e.right)
|
|
241
|
+
return left === e.left && right === e.right ? e : ({ ...e, left, right } as ParsedExpr)
|
|
242
|
+
}
|
|
243
|
+
case 'unary': {
|
|
244
|
+
const argument = f(e.argument)
|
|
245
|
+
return argument === e.argument ? e : { ...e, argument }
|
|
246
|
+
}
|
|
247
|
+
case 'conditional': {
|
|
248
|
+
const test = f(e.test)
|
|
249
|
+
const consequent = f(e.consequent)
|
|
250
|
+
const alternate = f(e.alternate)
|
|
251
|
+
return test === e.test && consequent === e.consequent && alternate === e.alternate ? e : { ...e, test, consequent, alternate }
|
|
252
|
+
}
|
|
253
|
+
default:
|
|
254
|
+
return undefined
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** `e` with each value-position child replaced by `f(child)`; `e` itself when none changes. */
|
|
259
|
+
function mapChildren(e: ParsedExpr, f: (child: ParsedExpr) => ParsedExpr): ParsedExpr {
|
|
260
|
+
const operands = mapOperands(e, f)
|
|
261
|
+
if (operands !== undefined) return operands
|
|
262
|
+
switch (e.kind) {
|
|
263
|
+
case 'call': {
|
|
264
|
+
const callee = f(e.callee)
|
|
265
|
+
const args = mapList(e.args, f)
|
|
266
|
+
return callee === e.callee && !args ? e : { ...e, callee, args: args ?? e.args }
|
|
267
|
+
}
|
|
268
|
+
case 'member': {
|
|
269
|
+
const object = f(e.object)
|
|
270
|
+
return object === e.object ? e : { ...e, object }
|
|
271
|
+
}
|
|
272
|
+
case 'index-access': {
|
|
273
|
+
const object = f(e.object)
|
|
274
|
+
const index = f(e.index)
|
|
275
|
+
return object === e.object && index === e.index ? e : { ...e, object, index }
|
|
276
|
+
}
|
|
277
|
+
case 'template-literal': {
|
|
278
|
+
const parts = mapTemplateParts(e.parts, f)
|
|
279
|
+
return parts ? { ...e, parts } : e
|
|
280
|
+
}
|
|
281
|
+
case 'array-literal': {
|
|
282
|
+
const elements = mapList(e.elements, f)
|
|
283
|
+
return elements ? { ...e, elements } : e
|
|
284
|
+
}
|
|
285
|
+
case 'array-method': {
|
|
286
|
+
const object = f(e.object)
|
|
287
|
+
const args = mapList(e.args, f)
|
|
288
|
+
return object === e.object && !args ? e : ({ ...e, object, args: args ?? e.args } as ParsedExpr)
|
|
289
|
+
}
|
|
290
|
+
case 'object-literal': {
|
|
291
|
+
const properties = mapProperties(e.properties, f)
|
|
292
|
+
return properties ? { ...e, properties } : e
|
|
293
|
+
}
|
|
294
|
+
default:
|
|
295
|
+
return e
|
|
296
|
+
}
|
|
297
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The refusal of `===` / `!==` over an operand that may be an object or an
|
|
3
|
+
* array (#3774; `spec/js-primitive-semantics.md`, "Decisions" (4), option
|
|
4
|
+
* (b)).
|
|
5
|
+
*
|
|
6
|
+
* JS compares objects and arrays by reference. A template runtime holds a
|
|
7
|
+
* decoded prop as a value (PHP copy-on-write arrays, Go structs, Ruby and
|
|
8
|
+
* Python containers compared by contents), so no runtime can tell two
|
|
9
|
+
* distinct structures with equal contents apart. Every template adapter
|
|
10
|
+
* therefore refuses the comparison with BF101, as the callback evaluator
|
|
11
|
+
* contract already does (`spec/compiler.md`, "ParsedExpr Evaluator
|
|
12
|
+
* Semantics"). A nullish test (`x === null`, `x === undefined`) is not a
|
|
13
|
+
* strict equality and keeps the target's nil test.
|
|
14
|
+
*
|
|
15
|
+
* The primitive plan names the refusal per node (`mode: 'refuse'`), and
|
|
16
|
+
* every emitter spells the runtime's `strict_eq` for it; the error itself
|
|
17
|
+
* is recorded here, once per component, over every template position, each
|
|
18
|
+
* read in its own scope, and over the memo bodies the adapters seed at SSR.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { exprToString, type ParsedExpr } from '../expression-parser.ts'
|
|
22
|
+
import { parsedExprChildren } from '../action-accessor.ts'
|
|
23
|
+
import { BindingScope } from '../scope/binding-scope.ts'
|
|
24
|
+
import { walkTemplatePositions } from '../template-position-walk.ts'
|
|
25
|
+
import type { ComponentIR, CompilerError, SourceLocation } from '../types.ts'
|
|
26
|
+
import type { OperandTyping, ScopedOperandTyping } from './operand-typing.ts'
|
|
27
|
+
import { isNullishTest, mayHoldObject } from './primitive-plan.ts'
|
|
28
|
+
|
|
29
|
+
/** Where a template adapter records the refusal. */
|
|
30
|
+
export interface NonPrimitiveEqualitySink {
|
|
31
|
+
readonly errors: CompilerError[]
|
|
32
|
+
readonly componentName: string
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Each `===` / `!==` in `expr` with an operand that may be an object, read in `typing`'s scope. */
|
|
36
|
+
function refusedComparisons(expr: ParsedExpr, typing: ScopedOperandTyping, isObjectTypeName: (name: string) => boolean): ParsedExpr[] {
|
|
37
|
+
const found: ParsedExpr[] = []
|
|
38
|
+
const visit = (e: ParsedExpr, at: ScopedOperandTyping): void => {
|
|
39
|
+
if (
|
|
40
|
+
e.kind === 'binary' &&
|
|
41
|
+
(e.op === '===' || e.op === '!==') &&
|
|
42
|
+
!isNullishTest(e.op, e.left, e.right, at) &&
|
|
43
|
+
(mayHoldObject(e.left, at, isObjectTypeName) || mayHoldObject(e.right, at, isObjectTypeName))
|
|
44
|
+
) {
|
|
45
|
+
found.push(e)
|
|
46
|
+
}
|
|
47
|
+
for (const child of parsedExprChildren(e)) visit(child.expr, child.params ? at.enterCallback(child.params) : at)
|
|
48
|
+
}
|
|
49
|
+
visit(expr, typing)
|
|
50
|
+
return found
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Record BF101 for every `===` / `!==` over an operand that may be an
|
|
55
|
+
* object in `ir`'s template positions and memo bodies. One error per
|
|
56
|
+
* comparison text per compile.
|
|
57
|
+
*/
|
|
58
|
+
export function refuseNonPrimitiveStrictEquality(ir: ComponentIR, typing: OperandTyping, sink: NonPrimitiveEqualitySink): void {
|
|
59
|
+
const component = BindingScope.forComponent(ir.metadata.propsObjectName ?? null)
|
|
60
|
+
// A named type is an object when the component defines it with fields
|
|
61
|
+
// (`interface Row { … }`, `type Pt = { … }`); a literal union alias is not.
|
|
62
|
+
const objectTypes = new Set(
|
|
63
|
+
(ir.metadata.typeDefinitions ?? []).filter(td => td.properties !== undefined && !td.stringLiteralUnion).map(td => td.name),
|
|
64
|
+
)
|
|
65
|
+
const isObjectTypeName = (name: string): boolean => objectTypes.has(name)
|
|
66
|
+
const check = (expr: ParsedExpr, scope: BindingScope, loc: SourceLocation): void => {
|
|
67
|
+
for (const comparison of refusedComparisons(expr, typing.at(scope), isObjectTypeName)) record(exprToString(comparison), loc, sink)
|
|
68
|
+
}
|
|
69
|
+
walkTemplatePositions(ir.root, ({ expr, loc, scope }) => check(expr, scope ?? component, loc), component)
|
|
70
|
+
for (const memo of ir.metadata.memos ?? []) {
|
|
71
|
+
if (memo.parsed) check(memo.parsed, component, ir.root.loc)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function record(comparison: string, loc: SourceLocation, sink: NonPrimitiveEqualitySink): void {
|
|
76
|
+
const message =
|
|
77
|
+
`\`${comparison}\` compares an object or array with \`===\` / \`!==\`. JS compares objects and arrays by reference, ` +
|
|
78
|
+
'but a template engine holds a decoded prop as a value and cannot tell two distinct structures with equal contents apart, ' +
|
|
79
|
+
'so the server render cannot give the JS answer.'
|
|
80
|
+
if (sink.errors.some(e => e.code === 'BF101' && e.message === message)) return
|
|
81
|
+
sink.errors.push({
|
|
82
|
+
code: 'BF101',
|
|
83
|
+
severity: 'error',
|
|
84
|
+
message,
|
|
85
|
+
loc,
|
|
86
|
+
suggestion: {
|
|
87
|
+
message:
|
|
88
|
+
'Compare a primitive field instead (`a.id === b.id`), or defer the comparison to the client with /* @client */.',
|
|
89
|
+
escape: [{ kind: 'rewrite' }, { kind: 'client-directive' }],
|
|
90
|
+
},
|
|
91
|
+
})
|
|
92
|
+
}
|
|
@@ -6,9 +6,11 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import type { ParsedExpr } from '../expression-parser.ts'
|
|
9
|
+
import type { StringNameTest } from './parsed-expr-emitter.ts'
|
|
9
10
|
import type { SsrSeedPlan } from '../ssr-seed-plan.ts'
|
|
10
11
|
import type { ComponentIR, TypeInfo } from '../types.ts'
|
|
11
12
|
import { isNumericArithmeticResult } from './parsed-expr-emitter.ts'
|
|
13
|
+
import { isRuntimeNumberLiteral } from './primitive-plan.ts'
|
|
12
14
|
|
|
13
15
|
/**
|
|
14
16
|
* Number-typed signals, memos, props and same-file local consts: a strict
|
|
@@ -36,16 +38,25 @@ export function collectNumberValueNames(ir: Pick<ComponentIR, 'metadata'>): { na
|
|
|
36
38
|
if (!ir.metadata.propsObjectName) names.add(p.name)
|
|
37
39
|
}
|
|
38
40
|
for (const c of ir.metadata.localConstants) {
|
|
39
|
-
|
|
41
|
+
// The analyzer types an exponent literal (`const TINY = 1e-7`) `unknown`;
|
|
42
|
+
// its parsed initializer still says it is a number.
|
|
43
|
+
if (isNumber(c.type) || isNumberLiteralInitializer(c.parsed)) names.add(c.name)
|
|
40
44
|
}
|
|
41
45
|
return { names, props }
|
|
42
46
|
}
|
|
43
47
|
|
|
48
|
+
/** A number literal, written negated or not (`1e-7`, `-0`). */
|
|
49
|
+
function isNumberLiteralInitializer(expr: ParsedExpr | undefined): boolean {
|
|
50
|
+
const literal = expr?.kind === 'unary' && expr.op === '-' ? expr.argument : expr
|
|
51
|
+
return literal?.kind === 'literal' && literal.literalType === 'number'
|
|
52
|
+
}
|
|
53
|
+
|
|
44
54
|
/**
|
|
45
55
|
* Whether an expression yields a JS NUMBER: arithmetic
|
|
46
56
|
* ({@link isNumericArithmeticResult}), a read of a name `isNumberName`
|
|
47
57
|
* accepts — a zero-argument getter call, a `props.<key>` member, or a bare
|
|
48
|
-
* identifier —
|
|
58
|
+
* identifier — a number literal that is not a safe integer, or a ternary
|
|
59
|
+
* with such a branch. Backends whose own number
|
|
49
60
|
* stringification differs from JS (Perl spells the infinities `Inf` and
|
|
50
61
|
* prints `%.15g`) route such a value through their `Number::toString`
|
|
51
62
|
* helper wherever JS stringifies it: an output position, or an operand of
|
|
@@ -54,11 +65,13 @@ export function collectNumberValueNames(ir: Pick<ComponentIR, 'metadata'>): { na
|
|
|
54
65
|
*/
|
|
55
66
|
export function isNumberValuedExpr(
|
|
56
67
|
expr: ParsedExpr | null | undefined,
|
|
57
|
-
isStringName:
|
|
68
|
+
isStringName: StringNameTest,
|
|
58
69
|
isNumberName: (n: string, componentProperty?: boolean) => boolean,
|
|
59
70
|
): boolean {
|
|
60
71
|
if (!expr) return false
|
|
61
72
|
if (isNumericArithmeticResult(expr, isStringName)) return true
|
|
73
|
+
// A literal the plan spells through the runtime's `number` (#3824 step 6).
|
|
74
|
+
if (isRuntimeNumberLiteral(expr)) return true
|
|
62
75
|
// Either branch may be the value printed; the helper passes a string
|
|
63
76
|
// branch through unchanged.
|
|
64
77
|
if (expr.kind === 'conditional') {
|
|
@@ -81,7 +94,7 @@ export function isNumberValuedExpr(
|
|
|
81
94
|
*/
|
|
82
95
|
export function numberValuedSeedNames(
|
|
83
96
|
plan: SsrSeedPlan,
|
|
84
|
-
isStringName:
|
|
97
|
+
isStringName: StringNameTest,
|
|
85
98
|
isNumberName: (n: string, componentProperty?: boolean) => boolean,
|
|
86
99
|
): Set<string> {
|
|
87
100
|
const names = new Set<string>()
|