@barefootjs/jsx 0.39.9 → 0.40.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts +41 -0
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -0
- package/dist/adapters/attr-plan.d.ts +1 -20
- package/dist/adapters/attr-plan.d.ts.map +1 -1
- package/dist/adapters/boolean-carrier.d.ts +31 -0
- package/dist/adapters/boolean-carrier.d.ts.map +1 -0
- package/dist/adapters/component-props.d.ts +22 -7
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +17 -0
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +95 -2
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/loop-plan.d.ts +41 -1
- package/dist/adapters/loop-plan.d.ts.map +1 -1
- package/dist/adapters/number-value.d.ts +43 -0
- package/dist/adapters/number-value.d.ts.map +1 -0
- package/dist/analyzer-context.d.ts +7 -0
- package/dist/analyzer-context.d.ts.map +1 -1
- package/dist/analyzer.d.ts +34 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts.map +1 -1
- package/dist/css-layer-prefixer.d.ts.map +1 -1
- package/dist/debug.d.ts.map +1 -1
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/index.d.ts +10 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2102 -1344
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts +29 -0
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-lazy-row.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/build-lazy-row.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-reactive-effects.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/inner-loop.d.ts +5 -4
- package/dist/ir-to-client-js/control-flow/plan/inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +15 -0
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/reactive-effects.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/reactive-effects.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +20 -0
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/lazy-row.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts +3 -2
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +7 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
- package/dist/ir-to-client-js/safe-html.d.ts +9 -3
- package/dist/ir-to-client-js/safe-html.d.ts.map +1 -1
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/types.d.ts +30 -0
- package/dist/ir-to-client-js/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +9 -1
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/module-exports.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +29 -3
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/props-binding.d.ts +27 -3
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/scope/binding-scope.d.ts +24 -1
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/scope/root-prop-alias.d.ts +14 -1
- package/dist/scope/root-prop-alias.d.ts.map +1 -1
- package/dist/signal-prop-name-collision.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts +4 -2
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +14 -0
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/template-parts.d.ts +1 -1
- package/dist/template-parts.d.ts.map +1 -1
- package/dist/types.d.ts +35 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/attr-plan.test.ts +1 -14
- package/src/__tests__/binding-scope-ratchet.test.ts +12 -2
- package/src/__tests__/boolean-carrier.test.ts +74 -0
- package/src/__tests__/client-template-escape-soundness.test.ts +17 -4
- package/src/__tests__/client-template-static-delimiters.test.ts +57 -0
- package/src/__tests__/component-props.test.ts +14 -0
- package/src/__tests__/computed-const.test.ts +85 -0
- package/src/__tests__/dsl-template-adapter.test.ts +70 -0
- package/src/__tests__/expression-parser.test.ts +24 -0
- package/src/__tests__/extract-function-params.test.ts +37 -0
- package/src/__tests__/issue-2960-multi-root-cond-same-tag.test.ts +18 -0
- package/src/__tests__/issue-3688-props-spread-child-csr.test.ts +142 -0
- package/src/__tests__/jsx-text-linebreak-whitespace.test.ts +11 -0
- package/src/__tests__/loop-plan.test.ts +66 -1
- package/src/__tests__/loop-row-prop-read.test.ts +111 -0
- package/src/__tests__/memo-body-expr.test.ts +137 -0
- package/src/__tests__/nullable-signal-attr.test.ts +97 -2
- package/src/__tests__/profile-binding-ids.test.ts +14 -0
- package/src/__tests__/root-prop-alias.test.ts +34 -1
- package/src/__tests__/signal-prop-name-collision.test.ts +102 -2
- package/src/__tests__/source-file-cache.test.ts +105 -0
- package/src/__tests__/ssr-seed-plan.test.ts +43 -0
- package/src/__tests__/template-parts-literal-boundary.test.ts +41 -0
- package/src/adapters/ambiguous-nil-arithmetic.ts +94 -0
- package/src/adapters/attr-plan.ts +8 -56
- package/src/adapters/boolean-carrier.ts +76 -0
- package/src/adapters/component-props.ts +28 -8
- package/src/adapters/computed-const.ts +206 -7
- package/src/adapters/dsl-template-adapter.ts +183 -2
- package/src/adapters/loop-plan.ts +67 -2
- package/src/adapters/number-value.ts +95 -0
- package/src/analyzer-context.ts +8 -0
- package/src/analyzer.ts +187 -66
- package/src/augment-inherited-props.ts +5 -9
- package/src/css-layer-prefixer.ts +4 -5
- package/src/debug.ts +1 -0
- package/src/expression-parser.ts +4 -1
- package/src/index.ts +23 -5
- package/src/ir-to-client-js/collect-elements.ts +104 -46
- package/src/ir-to-client-js/compute-inlinability.ts +2 -2
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +15 -1
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +79 -4
- package/src/ir-to-client-js/control-flow/plan/build-lazy-row.ts +3 -0
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/build-reactive-effects.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/inner-loop.ts +5 -4
- package/src/ir-to-client-js/control-flow/plan/loop-child-arm.ts +2 -0
- package/src/ir-to-client-js/control-flow/plan/loop.ts +12 -0
- package/src/ir-to-client-js/control-flow/plan/reactive-effects.ts +2 -0
- package/src/ir-to-client-js/control-flow/shared.ts +3 -2
- package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +27 -0
- package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +8 -2
- package/src/ir-to-client-js/control-flow/stringify/event-listener.ts +8 -1
- package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +8 -7
- package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +8 -7
- package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +4 -4
- package/src/ir-to-client-js/control-flow/stringify/loop.ts +3 -3
- package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +6 -6
- package/src/ir-to-client-js/emit-reactive.ts +1 -1
- package/src/ir-to-client-js/html-template.ts +122 -75
- package/src/ir-to-client-js/plan/build-static-array-child-init.ts +11 -1
- package/src/ir-to-client-js/plan/static-array-child-init.ts +7 -1
- package/src/ir-to-client-js/reactivity.ts +14 -0
- package/src/ir-to-client-js/safe-html.ts +11 -4
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +6 -4
- package/src/ir-to-client-js/types.ts +23 -0
- package/src/ir-to-client-js/utils.ts +16 -2
- package/src/jsx-to-ir.ts +112 -55
- package/src/loop-destructure.ts +4 -1
- package/src/module-exports.ts +13 -21
- package/src/nullable-signal-attr.ts +134 -13
- package/src/props-binding.ts +54 -3
- package/src/rich-type-refusal.ts +3 -1
- package/src/scope/binding-scope.ts +35 -2
- package/src/scope/root-prop-alias.ts +17 -1
- package/src/signal-prop-name-collision.ts +113 -22
- package/src/ssr-defaults.ts +10 -4
- package/src/ssr-seed-plan.ts +82 -17
- package/src/template-parts.ts +25 -3
- package/src/template-position-walk.ts +3 -1
- package/src/types.ts +32 -1
|
@@ -172,6 +172,11 @@ export interface InteractiveElement {
|
|
|
172
172
|
export interface ReactiveComponentProp {
|
|
173
173
|
slotId: string
|
|
174
174
|
propName: string
|
|
175
|
+
/**
|
|
176
|
+
* The JS expression whose value the mirror writes: the signal/memo call
|
|
177
|
+
* (`count()`), read caller-first when a later `{...props}` spread
|
|
178
|
+
* overrides the prop (#3688).
|
|
179
|
+
*/
|
|
175
180
|
expression: string
|
|
176
181
|
componentName: string
|
|
177
182
|
}
|
|
@@ -522,6 +527,14 @@ export interface NestedLoop extends LoopCore {
|
|
|
522
527
|
insideConditional?: boolean
|
|
523
528
|
/** Offset of this loop's items past its preceding container siblings (#1693). */
|
|
524
529
|
offset?: LoopOffset
|
|
530
|
+
/**
|
|
531
|
+
* The inner `.map()`'s `.filter()` / `.toSorted()` chain (#3694), so a
|
|
532
|
+
* reactive inner `mapArray` reconciles the rows the template rendered
|
|
533
|
+
* rather than the base array's.
|
|
534
|
+
*/
|
|
535
|
+
filterPredicate?: { param: string; raw: string }
|
|
536
|
+
sortComparator?: { paramA: string; paramB: string; raw: string }
|
|
537
|
+
chainOrder?: 'filter-sort' | 'sort-filter'
|
|
525
538
|
// Per-item bindings (events / reactiveAttrs / reactiveTexts / refs / conditionals)
|
|
526
539
|
// now live on `LoopCore.bindings` — see issue #1244 §B.
|
|
527
540
|
}
|
|
@@ -534,6 +547,14 @@ export interface LoopChildEvent {
|
|
|
534
547
|
nestedLoops: NestedLoop[]
|
|
535
548
|
/** DOM nesting depth (0 = loop body root). Deepest-first sorting for event delegation (#774). */
|
|
536
549
|
domDepth: number
|
|
550
|
+
/**
|
|
551
|
+
* For a parent-owned (`^sN`) element forwarded as the `children` of a
|
|
552
|
+
* component nested BELOW the row root (#3695): those components' slot
|
|
553
|
+
* ids, outermost first. The per-row lookup crosses into their scopes only
|
|
554
|
+
* (see `ConditionalBranchReactiveAttr.receiverSlotIds`, #3324). Absent
|
|
555
|
+
* when no such component encloses the element.
|
|
556
|
+
*/
|
|
557
|
+
receiverSlotIds?: readonly string[]
|
|
537
558
|
}
|
|
538
559
|
|
|
539
560
|
export interface LoopChildReactiveAttr extends AttrMeta {
|
|
@@ -565,6 +586,8 @@ export interface LoopChildReactiveText {
|
|
|
565
586
|
* constants' own `freeIdentifiers`.
|
|
566
587
|
*/
|
|
567
588
|
freeIdentifiers?: ReadonlySet<string>
|
|
589
|
+
/** A read of the component's own prop (`IRExpression.loopRowPropRead`): claimed and written as markup (`RowTextSlot`). */
|
|
590
|
+
markup?: true
|
|
568
591
|
}
|
|
569
592
|
|
|
570
593
|
/**
|
|
@@ -191,7 +191,21 @@ export function varSlotId(slotId: string): string {
|
|
|
191
191
|
*/
|
|
192
192
|
export function profileBindingId(componentName: string | undefined, slotId: string): string {
|
|
193
193
|
if (!componentName || slotId === '?') return ''
|
|
194
|
-
return `, ${
|
|
194
|
+
return `, ${codeSafeStringLiteral(`${componentName}#binding:${slotId}`)}`
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* A double-quoted JS string literal that is safe to splice into emitted code
|
|
199
|
+
* wherever it lands. `JSON.stringify` alone leaves `<`, `>` and `/` raw (a
|
|
200
|
+
* `</script>` closes an inline script) and, before ES2019, U+2028 / U+2029
|
|
201
|
+
* ended a string literal. Those are escaped as `\uXXXX`, which evaluates to the
|
|
202
|
+
* same string, so a value without them prints exactly as `JSON.stringify` did.
|
|
203
|
+
*/
|
|
204
|
+
export function codeSafeStringLiteral(value: string): string {
|
|
205
|
+
return JSON.stringify(value).replace(
|
|
206
|
+
/[<>/\u2028\u2029]/g,
|
|
207
|
+
ch => `\\u${ch.charCodeAt(0).toString(16).padStart(4, '0')}`,
|
|
208
|
+
)
|
|
195
209
|
}
|
|
196
210
|
|
|
197
211
|
/**
|
|
@@ -213,7 +227,7 @@ export { templatePartsToJsExpr }
|
|
|
213
227
|
* `boolean-shorthand`, `jsx-children`) — callers must branch on `value.kind`
|
|
214
228
|
* before reaching them.
|
|
215
229
|
*
|
|
216
|
-
* When `opts.useTemplate` is set, prefers `templateExpr` /
|
|
230
|
+
* When `opts.useTemplate` is set, prefers `templateExpr` /
|
|
217
231
|
* `templateCondition` / `templateKey` (the prop-rewritten variant) over the
|
|
218
232
|
* raw source form, for use in SSR template-literal interpolation.
|
|
219
233
|
*/
|
package/src/jsx-to-ir.ts
CHANGED
|
@@ -55,7 +55,7 @@ import {
|
|
|
55
55
|
collectAstPropRefs,
|
|
56
56
|
rewriteScopedValueRefs,
|
|
57
57
|
} from './prop-rewrite.ts'
|
|
58
|
-
import { boundPropLocalNames, buildPropAliasMap,
|
|
58
|
+
import { boundPropLocalNames, buildPropAliasMap, templatePropReadParams, resolveAliasOrigin, resolveRestSpreadOriginCore, livePropReadExpr } from './props-binding.ts'
|
|
59
59
|
import { PROPS_PARAM } from './ir-to-client-js/utils.ts'
|
|
60
60
|
import { walkIR } from './ir-to-client-js/walker.ts'
|
|
61
61
|
import { resolveFreeRefs, rebindHelperParamRefs, isNameBound as isNameBoundInEnv, type BindingEnvironment } from './free-refs.ts'
|
|
@@ -745,8 +745,7 @@ function collectBranchLocalPropRefsViaSubstitution(
|
|
|
745
745
|
*/
|
|
746
746
|
function getDestructuredPropNames(ctx: TransformContext): Set<string> | null {
|
|
747
747
|
if (ctx._destructuredPropNames === undefined) {
|
|
748
|
-
const
|
|
749
|
-
const eligible = ctx.analyzer.propsParams.filter(p => !shadowed.has(p.name))
|
|
748
|
+
const eligible = templatePropReadParams(ctx.analyzer)
|
|
750
749
|
const names = eligible.map(p => p.name)
|
|
751
750
|
ctx._destructuredPropNames = names.length > 0 ? new Set(names) : null
|
|
752
751
|
ctx._destructuredPropAliases = buildPropAliasMap(eligible) ?? null
|
|
@@ -762,35 +761,6 @@ function getDestructuredPropNames(ctx: TransformContext): Set<string> | null {
|
|
|
762
761
|
return ctx._destructuredPropNames ?? null
|
|
763
762
|
}
|
|
764
763
|
|
|
765
|
-
/**
|
|
766
|
-
* The component-scope names that shadow a prop of the same name in a
|
|
767
|
-
* `(props)`-arg component (`getDestructuredPropNames`' shadow guard): its
|
|
768
|
-
* signals, memos and locals, except a local that is the prop itself.
|
|
769
|
-
*/
|
|
770
|
-
function propShadowingNames(analyzer: AnalyzerContext): Set<string> {
|
|
771
|
-
const shadowed = new Set<string>()
|
|
772
|
-
if (!analyzer.propsObjectName) return shadowed
|
|
773
|
-
for (const s of analyzer.signals) {
|
|
774
|
-
shadowed.add(s.getter)
|
|
775
|
-
if (s.setter) shadowed.add(s.setter)
|
|
776
|
-
}
|
|
777
|
-
for (const m of analyzer.memos) shadowed.add(m.name)
|
|
778
|
-
for (const c of analyzer.localConstants) {
|
|
779
|
-
// A body destructure binding that is reassigned or mutated no longer
|
|
780
|
-
// holds its prop, renamed or not, so the template keeps it a local.
|
|
781
|
-
if (c.propsDestructure && !keepsDeclaredValue(c)) {
|
|
782
|
-
shadowed.add(c.name)
|
|
783
|
-
continue
|
|
784
|
-
}
|
|
785
|
-
if (isPropsAliasConst(c, analyzer.propsObjectName)) continue
|
|
786
|
-
// A renamed body binding with a default reads as its own prop entry,
|
|
787
|
-
// like the parameter form (#3579).
|
|
788
|
-
if (isRenamedBodyPropParam(c, analyzer.propsParams)) continue
|
|
789
|
-
shadowed.add(c.name)
|
|
790
|
-
}
|
|
791
|
-
return shadowed
|
|
792
|
-
}
|
|
793
|
-
|
|
794
764
|
/**
|
|
795
765
|
* Companion to `getDestructuredPropNames`: local-name → `ParamInfo` for the
|
|
796
766
|
* same shadow-filtered eligible set. Both caches are written in one pass.
|
|
@@ -1186,6 +1156,61 @@ function childrenReadOf(
|
|
|
1186
1156
|
return readsProps && isPropsChildrenMember(parsed, analyzer) ? 'bare' : null
|
|
1187
1157
|
}
|
|
1188
1158
|
|
|
1159
|
+
/**
|
|
1160
|
+
* Whether `parsed`, read in the component scope, is `<props object>.<name>`
|
|
1161
|
+
* for a prop other than `children` (`props.footer`, `p.footer` for `const p
|
|
1162
|
+
* = props`).
|
|
1163
|
+
*/
|
|
1164
|
+
function isPropsValueMember(parsed: ParsedExpr | undefined, analyzer: AnalyzerContext): boolean {
|
|
1165
|
+
return (
|
|
1166
|
+
parsed?.kind === 'member' &&
|
|
1167
|
+
!parsed.computed &&
|
|
1168
|
+
parsed.property !== 'children' &&
|
|
1169
|
+
parsed.object.kind === 'identifier' &&
|
|
1170
|
+
isPropsObjectBinding(parsed.object.name, analyzer)
|
|
1171
|
+
)
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
/**
|
|
1175
|
+
* Whether the component-scope name `name` holds one of the component's own
|
|
1176
|
+
* props other than `children`: a destructured props param (renamed or not),
|
|
1177
|
+
* a body destructure or member read of the props object (`const { footer }
|
|
1178
|
+
* = props`, `const f = props.footer`), or a const alias hop of one.
|
|
1179
|
+
*/
|
|
1180
|
+
function isPropValueBinding(name: string, analyzer: AnalyzerContext): boolean {
|
|
1181
|
+
const consts = childrenHopConstants(analyzer)
|
|
1182
|
+
const params = boundPropLocalNames(analyzer)
|
|
1183
|
+
const terminal = (n: string): true | null => {
|
|
1184
|
+
if (consts.has(n)) return isPropsValueMember(consts.get(n), analyzer) ? true : null
|
|
1185
|
+
return params.has(n) && !analyzer.propsParams.some(p => p.name === n && isChildrenPropParam(p)) ? true : null
|
|
1186
|
+
}
|
|
1187
|
+
return resolveAliasOrigin(childrenAliasHops(analyzer), name, terminal) === true
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
/**
|
|
1191
|
+
* Whether a child-position expression inside a loop row is a bare read of one
|
|
1192
|
+
* of the component's own props other than `children`
|
|
1193
|
+
* (`IRExpression.loopRowPropRead`). A name or `props` object the enclosing
|
|
1194
|
+
* loop rows bind shadows the prop.
|
|
1195
|
+
*/
|
|
1196
|
+
function isLoopRowPropRead(parsed: ParsedExpr, analyzer: AnalyzerContext, scope: BindingScope): boolean {
|
|
1197
|
+
if (!scope.insideLoopRow()) return false
|
|
1198
|
+
if (parsed.kind === 'identifier') return !scope.isBound(parsed.name) && isPropValueBinding(parsed.name, analyzer)
|
|
1199
|
+
return (
|
|
1200
|
+
parsed.kind === 'member' &&
|
|
1201
|
+
parsed.object.kind === 'identifier' &&
|
|
1202
|
+
!scope.isBound(parsed.object.name) &&
|
|
1203
|
+
isPropsValueMember(parsed, analyzer)
|
|
1204
|
+
)
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
/** Set `childrenRead` and `loopRowPropRead` on an expression child from its parse. */
|
|
1208
|
+
function attachPropReadFlags(node: IRExpression, parsed: ParsedExpr, analyzer: AnalyzerContext, scope: BindingScope): void {
|
|
1209
|
+
const childrenRead = childrenReadOf(parsed, analyzer, scope)
|
|
1210
|
+
if (childrenRead) node.childrenRead = childrenRead
|
|
1211
|
+
if (isLoopRowPropRead(parsed, analyzer, scope)) node.loopRowPropRead = true
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1189
1214
|
/**
|
|
1190
1215
|
* Attach `parsed` to every expression-valued attribute / prop so adapters can
|
|
1191
1216
|
* lower from the tree instead of re-parsing the string. Element attrs,
|
|
@@ -1197,10 +1222,18 @@ function attachParsedAttrValues(
|
|
|
1197
1222
|
node: IRNode,
|
|
1198
1223
|
parse: (trimmed: string) => ParsedExpr,
|
|
1199
1224
|
parseValue: (trimmed: string) => ParsedExpr,
|
|
1225
|
+
resolve: (parsed: ParsedExpr) => ParsedExpr,
|
|
1200
1226
|
): void {
|
|
1201
|
-
const attach = (value:
|
|
1202
|
-
|
|
1203
|
-
|
|
1227
|
+
const attach = (value: AttrValue, spreads: boolean): void => {
|
|
1228
|
+
if (value.kind === 'template' || value.kind === 'expression') {
|
|
1229
|
+
for (const part of value.parts ?? []) {
|
|
1230
|
+
if (part.type === 'expression') part.parsed = resolve(part.parsed)
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
if (value.kind === 'expression' || (spreads && value.kind === 'spread')) {
|
|
1234
|
+
const trimmed = value.expr.trim()
|
|
1235
|
+
if (trimmed) value.parsed = value.kind === 'spread' ? parse(trimmed) : parseValue(trimmed)
|
|
1236
|
+
}
|
|
1204
1237
|
}
|
|
1205
1238
|
if (node.type === 'element') {
|
|
1206
1239
|
for (const attr of node.attrs) attach(attr.value, true)
|
|
@@ -1234,13 +1267,13 @@ function attachParsedExpressions(node: IRNode, analyzer: AnalyzerContext, scope:
|
|
|
1234
1267
|
if (node.type === 'expression') {
|
|
1235
1268
|
const trimmed = node.expr.trim()
|
|
1236
1269
|
if (trimmed) node.parsed = parse(trimmed)
|
|
1237
|
-
|
|
1238
|
-
if (childrenRead) node.childrenRead = childrenRead
|
|
1270
|
+
if (node.parsed) attachPropReadFlags(node, node.parsed, analyzer, scope)
|
|
1239
1271
|
} else if (node.type === 'conditional' || node.type === 'if-statement') {
|
|
1240
1272
|
const trimmed = node.condition.trim()
|
|
1241
1273
|
if (trimmed) node.parsedCondition = parse(trimmed)
|
|
1242
1274
|
}
|
|
1243
|
-
attachParsedAttrValues(node, parse, parseValue
|
|
1275
|
+
attachParsedAttrValues(node, parse, parseValue,
|
|
1276
|
+
parsed => resolveCallbackMethodFunctionReferences(parsed, analyzer, bound))
|
|
1244
1277
|
switch (node.type) {
|
|
1245
1278
|
case 'element':
|
|
1246
1279
|
case 'component':
|
|
@@ -2665,7 +2698,11 @@ function cleanJsxWhitespace(raw: string): string {
|
|
|
2665
2698
|
const isLastLine = i === lines.length - 1
|
|
2666
2699
|
let line = lines[i].replace(/\t/g, ' ')
|
|
2667
2700
|
if (!isFirstLine) line = line.replace(/^ +/, '')
|
|
2668
|
-
if (!isLastLine)
|
|
2701
|
+
if (!isLastLine) {
|
|
2702
|
+
let end = line.length
|
|
2703
|
+
while (end > 0 && line[end - 1] === ' ') end--
|
|
2704
|
+
line = line.slice(0, end)
|
|
2705
|
+
}
|
|
2669
2706
|
if (line !== '') kept.push(line)
|
|
2670
2707
|
}
|
|
2671
2708
|
return kept.join(' ')
|
|
@@ -6769,7 +6806,8 @@ function attrValueText(value: AttrValue): string {
|
|
|
6769
6806
|
if (value.kind !== 'template') return ''
|
|
6770
6807
|
const out: string[] = []
|
|
6771
6808
|
for (const p of value.parts) {
|
|
6772
|
-
if (p.type === '
|
|
6809
|
+
if (p.type === 'expression') out.push(p.expr)
|
|
6810
|
+
else if (p.type === 'ternary') out.push(p.condition, p.whenTrue, p.whenFalse)
|
|
6773
6811
|
else if (p.type === 'lookup') out.push(p.key)
|
|
6774
6812
|
}
|
|
6775
6813
|
return out.join(' ')
|
|
@@ -8178,10 +8216,10 @@ function parseTemplateLiteral(
|
|
|
8178
8216
|
if (ternary) {
|
|
8179
8217
|
parts.push(ternary)
|
|
8180
8218
|
} else {
|
|
8181
|
-
//
|
|
8219
|
+
// Preserve the expression as a distinct part.
|
|
8182
8220
|
const val = ctx.getJS(span.expression)
|
|
8183
8221
|
const tVal = rewriteBarePropRefs(val, span.expression, ctx)
|
|
8184
|
-
parts.push({ type: '
|
|
8222
|
+
parts.push({ type: 'expression', expr: val, templateExpr: tVal, parsed: parseValueExpr(val) })
|
|
8185
8223
|
}
|
|
8186
8224
|
} else {
|
|
8187
8225
|
// Try to resolve `${IDENT}` against local consts (string literal
|
|
@@ -8192,10 +8230,10 @@ function parseTemplateLiteral(
|
|
|
8192
8230
|
if (resolved) {
|
|
8193
8231
|
parts.push(...resolved)
|
|
8194
8232
|
} else {
|
|
8195
|
-
//
|
|
8233
|
+
// Preserve unresolved expressions for the target renderer.
|
|
8196
8234
|
const val = ctx.getJS(span.expression)
|
|
8197
8235
|
const tVal = rewriteBarePropRefs(val, span.expression, ctx)
|
|
8198
|
-
parts.push({ type: '
|
|
8236
|
+
parts.push({ type: 'expression', expr: val, templateExpr: tVal, parsed: parseValueExpr(val) })
|
|
8199
8237
|
}
|
|
8200
8238
|
}
|
|
8201
8239
|
|
|
@@ -8484,7 +8522,7 @@ function tryResolveIdentifierAsTemplateLiteral(
|
|
|
8484
8522
|
// substitution or a Record lookup) to bother emitting structured
|
|
8485
8523
|
// IR — otherwise the existing identifier-text path is more
|
|
8486
8524
|
// efficient. Spans that don't resolve fall through to a bare
|
|
8487
|
-
//
|
|
8525
|
+
// expression part (matching `parseTemplateLiteral`'s default).
|
|
8488
8526
|
let resolvedAny = false
|
|
8489
8527
|
const parts: IRTemplatePart[] = []
|
|
8490
8528
|
if (ast.head.text) parts.push({ type: 'string', value: ast.head.text })
|
|
@@ -8495,15 +8533,14 @@ function tryResolveIdentifierAsTemplateLiteral(
|
|
|
8495
8533
|
resolvedAny = true
|
|
8496
8534
|
parts.push(...resolved)
|
|
8497
8535
|
} else {
|
|
8498
|
-
// Unresolved
|
|
8499
|
-
// as a bare ${expr} placeholder so adapters can substitute the
|
|
8500
|
-
// matching prop/param at render time.
|
|
8536
|
+
// Unresolved spans retain their parsed expression for adapter emission.
|
|
8501
8537
|
const text = astText(span.expression)
|
|
8502
8538
|
const templateText = rewriteBarePropRefs(text, span.expression, ctx)
|
|
8503
8539
|
parts.push({
|
|
8504
|
-
type: '
|
|
8505
|
-
|
|
8506
|
-
|
|
8540
|
+
type: 'expression',
|
|
8541
|
+
expr: text,
|
|
8542
|
+
templateExpr: templateText,
|
|
8543
|
+
parsed: parseValueExpr(text),
|
|
8507
8544
|
})
|
|
8508
8545
|
}
|
|
8509
8546
|
if (span.literal.text) parts.push({ type: 'string', value: span.literal.text })
|
|
@@ -9006,7 +9043,7 @@ function processComponentProps(
|
|
|
9006
9043
|
// rewrite, so `IRProp` declares no such field. Strip it here rather
|
|
9007
9044
|
// than letting the shared helper's per-key marking leak into IRProp
|
|
9008
9045
|
// as an inert flag nothing reads.
|
|
9009
|
-
props.push(...expandSpreadAttribute(attr, ctx).map(({ restExpandedKey: _restExpandedKey, ...prop }) => prop))
|
|
9046
|
+
props.push(...expandSpreadAttribute(attr, ctx).map(({ restExpandedKey: _restExpandedKey, ...prop }) => markPropsObjectSpread(prop, ctx)))
|
|
9010
9047
|
continue
|
|
9011
9048
|
}
|
|
9012
9049
|
|
|
@@ -9072,7 +9109,7 @@ function processComponentProps(
|
|
|
9072
9109
|
// shorthand (`<X disabled />` → `disabled={true}`).
|
|
9073
9110
|
if (value.kind === 'template') {
|
|
9074
9111
|
// Collapse using the raw part values, but also carry the
|
|
9075
|
-
// template-variant projection (parts' `
|
|
9112
|
+
// template-variant projection (parts' `templateExpr` /
|
|
9076
9113
|
// `templateCondition` / `templateKey`, i.e. destructured-prop refs
|
|
9077
9114
|
// rewritten to `_p.X`) as `templateExpr`. Without it, the
|
|
9078
9115
|
// `templateExpr` derivation below walks the ORIGINAL initializer AST
|
|
@@ -9813,6 +9850,25 @@ function forwardsCallerRestProps(attrs: IRAttribute[], ctx: TransformContext): b
|
|
|
9813
9850
|
return false
|
|
9814
9851
|
}
|
|
9815
9852
|
|
|
9853
|
+
/**
|
|
9854
|
+
* Mark a child-component spread of the component's own props object
|
|
9855
|
+
* (`SpreadAttr.propsObject`, #3688). Resolved with the same rule — and the
|
|
9856
|
+
* same `expr` / `templateExpr` pair — as `forwardsCallerRestProps`, narrowed
|
|
9857
|
+
* to the `'props'` origin: a `{...rest}` bag leaves out the keys the
|
|
9858
|
+
* component destructured, so it does not merge as the caller's props.
|
|
9859
|
+
*/
|
|
9860
|
+
function markPropsObjectSpread<T extends { value: AttrValue }>(prop: T, ctx: TransformContext): T {
|
|
9861
|
+
const value = prop.value
|
|
9862
|
+
if (value.kind !== 'spread') return prop
|
|
9863
|
+
const constants = constantValuesByName(ctx)
|
|
9864
|
+
for (const name of new Set([value.expr, value.templateExpr])) {
|
|
9865
|
+
if (name && resolveRestSpreadOriginCore(ctx.analyzer, constants, name) === 'props') {
|
|
9866
|
+
return { ...prop, value: { ...value, propsObject: true } }
|
|
9867
|
+
}
|
|
9868
|
+
}
|
|
9869
|
+
return prop
|
|
9870
|
+
}
|
|
9871
|
+
|
|
9816
9872
|
/** `ctx.analyzer.localConstants` indexed by name → value text, memoized per component walk. */
|
|
9817
9873
|
function constantValuesByName(ctx: TransformContext): ReadonlyMap<string, string | undefined> {
|
|
9818
9874
|
if (ctx._constantValuesByName) return ctx._constantValuesByName
|
|
@@ -9891,10 +9947,11 @@ function attrValueReactivityProbe(value: AttrValue): string | null {
|
|
|
9891
9947
|
return value.expr
|
|
9892
9948
|
case 'template':
|
|
9893
9949
|
return value.parts.map((p: IRTemplatePart) => {
|
|
9894
|
-
if (p.type === 'string') return
|
|
9950
|
+
if (p.type === 'string') return ''
|
|
9951
|
+
if (p.type === 'expression') return p.expr
|
|
9895
9952
|
if (p.type === 'ternary') return p.condition
|
|
9896
9953
|
return p.key
|
|
9897
|
-
}).join('')
|
|
9954
|
+
}).join(' ')
|
|
9898
9955
|
case 'literal':
|
|
9899
9956
|
case 'boolean-attr':
|
|
9900
9957
|
case 'boolean-shorthand':
|
package/src/loop-destructure.ts
CHANGED
|
@@ -140,7 +140,10 @@ function restNamesMisused(loop: IRLoop, names: string[]): boolean {
|
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
142
|
const part = (p: IRTemplatePart): void => {
|
|
143
|
-
if (p.type === '
|
|
143
|
+
if (p.type === 'expression') {
|
|
144
|
+
check(p.expr)
|
|
145
|
+
check(p.templateExpr)
|
|
146
|
+
} else if (p.type === 'ternary') {
|
|
144
147
|
check(p.condition)
|
|
145
148
|
check(p.templateCondition)
|
|
146
149
|
check(p.whenTrue)
|
package/src/module-exports.ts
CHANGED
|
@@ -304,27 +304,19 @@ function isAssignmentOperator(kind: ts.SyntaxKind): boolean {
|
|
|
304
304
|
* Strips type annotations and default values.
|
|
305
305
|
*/
|
|
306
306
|
export function extractFunctionParams(value: string): string {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
}
|
|
321
|
-
// Function expression: function(a, b) { ... }
|
|
322
|
-
const funcMatch = value.match(/^(?:async\s*)?function\s*\w*\s*\(([^)]*)\)/)
|
|
323
|
-
if (funcMatch) {
|
|
324
|
-
return funcMatch[1]
|
|
325
|
-
.split(',')
|
|
326
|
-
.map((p) => p.trim().split(':')[0].split('=')[0].trim())
|
|
327
|
-
.filter(Boolean)
|
|
307
|
+
for (const kind of [ts.ScriptKind.TSX, ts.ScriptKind.TS]) {
|
|
308
|
+
const source = ts.createSourceFile('function.tsx', `(${value}\n)`, ts.ScriptTarget.Latest, true, kind)
|
|
309
|
+
const diagnostics = 'parseDiagnostics' in source ? source.parseDiagnostics : undefined
|
|
310
|
+
if (Array.isArray(diagnostics) && diagnostics.length > 0) continue
|
|
311
|
+
const statement = source.statements[0]
|
|
312
|
+
if (!statement || !ts.isExpressionStatement(statement)) return ''
|
|
313
|
+
|
|
314
|
+
let expression = statement.expression
|
|
315
|
+
while (ts.isParenthesizedExpression(expression)) expression = expression.expression
|
|
316
|
+
if (!ts.isArrowFunction(expression) && !ts.isFunctionExpression(expression)) return ''
|
|
317
|
+
|
|
318
|
+
return expression.parameters
|
|
319
|
+
.map(parameter => `${parameter.dotDotDotToken ? '...' : ''}${parameter.name.getText(source)}`)
|
|
328
320
|
.join(', ')
|
|
329
321
|
}
|
|
330
322
|
return ''
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import type { ParsedExpr } from './expression-parser.ts'
|
|
19
|
+
import { BindingScope } from './scope/binding-scope.ts'
|
|
19
20
|
import type { ComponentIR, MemoInfo, SignalInfo, TypeInfo } from './types.ts'
|
|
20
21
|
|
|
21
22
|
function isNullishType(type: TypeInfo): boolean {
|
|
@@ -189,10 +190,12 @@ export interface NullishAttrContext {
|
|
|
189
190
|
* decides. A prop whose type admits `null` (`n: number | null`) reads its
|
|
190
191
|
* nil as `null`. A prop that can only be absent (`n?: number`,
|
|
191
192
|
* `n: number | undefined`) reads it as `undefined`; a present value, `0`
|
|
192
|
-
* included, is unaffected. A prop that admits both (`n?: number | null
|
|
193
|
-
*
|
|
193
|
+
* included, is unaffected. A prop that admits both (`n?: number | null`,
|
|
194
|
+
* `n: number | null | undefined`) is `'either'`: the type cannot say which
|
|
195
|
+
* one its nil stands for, so arithmetic over it is refused
|
|
196
|
+
* (`arithmeticOperandNilIsAmbiguous`).
|
|
194
197
|
*/
|
|
195
|
-
export type NullableNumberPropNil = 'null' | 'undefined'
|
|
198
|
+
export type NullableNumberPropNil = 'null' | 'undefined' | 'either'
|
|
196
199
|
|
|
197
200
|
export function collectNullishAttrContext(ir: ComponentIR): NullishAttrContext {
|
|
198
201
|
const signals = ir.metadata.signals ?? []
|
|
@@ -218,10 +221,11 @@ function nullableNumberPropNil(type: TypeInfo | undefined, optional: boolean): N
|
|
|
218
221
|
const members = type.kind === 'union' ? (type.unionTypes ?? []) : [type]
|
|
219
222
|
const families = new Set(members.map(m => (m.kind === 'primitive' ? m.primitive : m.kind)))
|
|
220
223
|
const admitsNull = families.has('null')
|
|
221
|
-
const
|
|
224
|
+
const admitsAbsent = optional || families.has('undefined')
|
|
222
225
|
families.delete('null')
|
|
223
226
|
families.delete('undefined')
|
|
224
|
-
if (!
|
|
227
|
+
if (!(admitsNull || admitsAbsent) || families.size !== 1 || !families.has('number')) return null
|
|
228
|
+
if (admitsNull && admitsAbsent) return 'either'
|
|
225
229
|
return admitsNull ? 'null' : 'undefined'
|
|
226
230
|
}
|
|
227
231
|
|
|
@@ -230,7 +234,7 @@ function collectNullableNumberProps(ir: ComponentIR): ReadonlyMap<string, Nullab
|
|
|
230
234
|
const objectName = ir.metadata.propsObjectName
|
|
231
235
|
for (const p of ir.metadata.propsParams) {
|
|
232
236
|
if (p.isRest || p.defaultValue !== undefined) continue
|
|
233
|
-
const nil = nullableNumberPropNil(p.type, p.optional)
|
|
237
|
+
const nil = nullableNumberPropNil(p.declaredType ?? p.type, p.optional)
|
|
234
238
|
if (nil !== null) names.set(objectName ? `${objectName}.${p.sourceName ?? p.name}` : p.name, nil)
|
|
235
239
|
}
|
|
236
240
|
return names
|
|
@@ -249,6 +253,8 @@ function collectNullableNumberProps(ir: ComponentIR): ReadonlyMap<string, Nullab
|
|
|
249
253
|
* (`pair[1]`, `row[key]`), or a member read of a value that can itself be
|
|
250
254
|
* nullish;
|
|
251
255
|
* - a ternary branch, or an `??` / `||` / `&&` operand the result can be;
|
|
256
|
+
* - a method call that reads `undefined` when nothing is there
|
|
257
|
+
* (`word.at(-1)`, `items.find(...)`, {@link methodMayReturnUndefined});
|
|
252
258
|
* - a literal `undefined` / `null`.
|
|
253
259
|
*
|
|
254
260
|
* Like the bare-signal guard it is presence-only: a non-nil value renders
|
|
@@ -267,6 +273,7 @@ export function attrValueMayBeNullish(
|
|
|
267
273
|
// loop bindings at the top level, none inside a memo body — its getters
|
|
268
274
|
// resolve where the memo is declared, not where it is read.
|
|
269
275
|
const walk = (expr: ParsedExpr, shadowed: (name: string) => boolean): boolean => {
|
|
276
|
+
if (methodMayReturnUndefined(expr)) return true
|
|
270
277
|
switch (expr.kind) {
|
|
271
278
|
case 'identifier':
|
|
272
279
|
return expr.name === 'undefined'
|
|
@@ -303,6 +310,24 @@ export function attrValueMayBeNullish(
|
|
|
303
310
|
return parsed !== undefined && walk(parsed, isShadowed)
|
|
304
311
|
}
|
|
305
312
|
|
|
313
|
+
/** Array / string methods that read `undefined` when nothing matches. */
|
|
314
|
+
const UNDEFINED_ON_MISS_METHODS: ReadonlySet<string> = new Set(['find', 'findLast'])
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Whether `expr` is a method call that reads `undefined` for some receiver:
|
|
318
|
+
* `.at(i)` out of range (an empty string or array included, `''.at(-1)`),
|
|
319
|
+
* or `.find()` / `.findLast()` with no matching element.
|
|
320
|
+
*/
|
|
321
|
+
function methodMayReturnUndefined(expr: ParsedExpr): boolean {
|
|
322
|
+
if (expr.kind === 'array-method') return expr.method === 'at'
|
|
323
|
+
return (
|
|
324
|
+
expr.kind === 'call' &&
|
|
325
|
+
expr.callee.kind === 'member' &&
|
|
326
|
+
!expr.callee.computed &&
|
|
327
|
+
UNDEFINED_ON_MISS_METHODS.has(expr.callee.property)
|
|
328
|
+
)
|
|
329
|
+
}
|
|
330
|
+
|
|
306
331
|
/**
|
|
307
332
|
* Whether an arithmetic operand's SSR value is certainly `null` — a literal
|
|
308
333
|
* `null`, a read of a signal seeded with `null`, or a memo whose body is
|
|
@@ -317,7 +342,8 @@ export function arithmeticOperandIsNull(
|
|
|
317
342
|
ctx: NullishAttrContext,
|
|
318
343
|
isShadowed: (name: string) => boolean = () => false,
|
|
319
344
|
): boolean {
|
|
320
|
-
|
|
345
|
+
const seed = arithmeticOperandSeed(parsed, ctx, isShadowed)
|
|
346
|
+
return seed === 'null' || seed === 'either'
|
|
321
347
|
}
|
|
322
348
|
|
|
323
349
|
/**
|
|
@@ -339,9 +365,11 @@ export function arithmeticOperandIsUndefined(
|
|
|
339
365
|
* What an arithmetic operand's SSR nil stands for: `'null'` / `'undefined'`
|
|
340
366
|
* when the operand is that value (a nullable number prop's `null` reading
|
|
341
367
|
* included), `'absent'` when it is a number prop that may be absent
|
|
342
|
-
* (`n?: number`), whose nil is `undefined` but which may hold a number
|
|
368
|
+
* (`n?: number`), whose nil is `undefined` but which may hold a number,
|
|
369
|
+
* `'either'` when it is a number prop whose nil may be either
|
|
370
|
+
* (`n?: number | null`).
|
|
343
371
|
*/
|
|
344
|
-
type OperandSeed = 'null' | 'undefined' | 'absent' | null
|
|
372
|
+
type OperandSeed = 'null' | 'undefined' | 'absent' | 'either' | null
|
|
345
373
|
|
|
346
374
|
function arithmeticOperandSeed(
|
|
347
375
|
parsed: ParsedExpr | undefined,
|
|
@@ -353,7 +381,7 @@ function arithmeticOperandSeed(
|
|
|
353
381
|
if (expr.kind === 'literal') return expr.literalType === 'null' ? 'null' : null
|
|
354
382
|
if (expr.kind === 'identifier' && expr.name === 'undefined' && !shadowed('undefined')) return 'undefined'
|
|
355
383
|
const prop = nullableNumberPropRead(expr, ctx, shadowed)
|
|
356
|
-
if (prop !== null) return prop === 'undefined' ? 'absent' :
|
|
384
|
+
if (prop !== null) return prop === 'undefined' ? 'absent' : prop
|
|
357
385
|
const getter = expr.kind === 'identifier' ? expr.name : bareGetterCallName(expr)
|
|
358
386
|
if (getter === null || shadowed(getter)) return null
|
|
359
387
|
if (ctx.nullSeededSignals?.has(getter)) return 'null'
|
|
@@ -419,10 +447,103 @@ export function arithmeticOperandCoercion(
|
|
|
419
447
|
ctx: NullishAttrContext,
|
|
420
448
|
isShadowed: (name: string) => boolean = () => false,
|
|
421
449
|
): ArithmeticCoercion {
|
|
422
|
-
|
|
423
|
-
if (!numeric) return null
|
|
450
|
+
if (!isNumericArithmetic(op, other)) return null
|
|
424
451
|
const seed = arithmeticOperandSeed(operand, ctx, isShadowed)
|
|
425
|
-
|
|
452
|
+
if (seed === 'absent') return 'undefined'
|
|
453
|
+
// `'either'` is refused (`arithmeticOperandNilIsAmbiguous`); the spelling
|
|
454
|
+
// keeps the `null` reading so the refused template stays well-formed.
|
|
455
|
+
return seed === 'either' ? 'null' : seed
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* Whether an arithmetic operand reads a number prop whose type admits both
|
|
460
|
+
* `null` and absence (`n?: number | null`), directly or through a memo
|
|
461
|
+
* (#3700). JS reads the omitted prop as `undefined` (`NaN` in arithmetic)
|
|
462
|
+
* and a `null` one as `0`; a template engine holds both as one nil and the
|
|
463
|
+
* type cannot say which, so no spelling of {@link arithmeticOperandCoercion}
|
|
464
|
+
* renders both. Template adapters refuse such an operand instead of
|
|
465
|
+
* rendering one reading silently. Same operators and scope as
|
|
466
|
+
* {@link arithmeticOperandCoercion}.
|
|
467
|
+
*/
|
|
468
|
+
export function arithmeticOperandNilIsAmbiguous(
|
|
469
|
+
op: string,
|
|
470
|
+
operand: ParsedExpr | undefined,
|
|
471
|
+
other: ParsedExpr | undefined,
|
|
472
|
+
ctx: NullishAttrContext,
|
|
473
|
+
isShadowed: (name: string) => boolean = () => false,
|
|
474
|
+
): boolean {
|
|
475
|
+
return isNumericArithmetic(op, other) && arithmeticOperandSeed(operand, ctx, isShadowed) === 'either'
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Every arithmetic operand in `expr` that {@link arithmeticOperandNilIsAmbiguous}
|
|
480
|
+
* matches, for a whole expression a template adapter lowers without visiting
|
|
481
|
+
* each operator through its emitter (a memo body, a `.filter()` predicate, an
|
|
482
|
+
* expression the runtime evaluator runs). Operands of binary operators and of
|
|
483
|
+
* unary minus count. `scope` is where the expression is read; each arrow in
|
|
484
|
+
* it enters its params as a callback frame (`BindingScope.enterCallback`),
|
|
485
|
+
* so they shadow within its body only.
|
|
486
|
+
*/
|
|
487
|
+
export function ambiguousNilArithmeticOperands(
|
|
488
|
+
expr: ParsedExpr,
|
|
489
|
+
ctx: NullishAttrContext,
|
|
490
|
+
scope: BindingScope = BindingScope.EMPTY,
|
|
491
|
+
): ParsedExpr[] {
|
|
492
|
+
const found: ParsedExpr[] = []
|
|
493
|
+
const visit = (e: ParsedExpr, at: BindingScope): void => {
|
|
494
|
+
if (e.kind === 'arrow') {
|
|
495
|
+
visit(e.body, at.enterCallback(e.params))
|
|
496
|
+
return
|
|
497
|
+
}
|
|
498
|
+
for (const [op, operand, other] of arithmeticOperandsOf(e)) {
|
|
499
|
+
if (arithmeticOperandNilIsAmbiguous(op, operand, other, ctx, at.asShadowPredicate())) found.push(operand)
|
|
500
|
+
}
|
|
501
|
+
for (const child of parsedExprChildren(e)) visit(child, at)
|
|
502
|
+
}
|
|
503
|
+
if (!ctx.nullableNumberProps?.size) return found
|
|
504
|
+
visit(expr, scope)
|
|
505
|
+
return found
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/** The `[op, operand, other]` triples of an arithmetic node's operands. */
|
|
509
|
+
function arithmeticOperandsOf(e: ParsedExpr): Array<[string, ParsedExpr, ParsedExpr | undefined]> {
|
|
510
|
+
if (e.kind === 'binary') return [[e.op, e.left, e.right], [e.op, e.right, e.left]]
|
|
511
|
+
if (e.kind === 'unary' && e.op === '-') return [['-', e.argument, undefined]]
|
|
512
|
+
return []
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/** The value-position sub-expressions of `e` (an arrow's body included). */
|
|
516
|
+
function parsedExprChildren(e: ParsedExpr): ParsedExpr[] {
|
|
517
|
+
switch (e.kind) {
|
|
518
|
+
case 'call':
|
|
519
|
+
return [e.callee, ...e.args]
|
|
520
|
+
case 'member':
|
|
521
|
+
return [e.object]
|
|
522
|
+
case 'index-access':
|
|
523
|
+
return [e.object, e.index]
|
|
524
|
+
case 'binary':
|
|
525
|
+
case 'logical':
|
|
526
|
+
return [e.left, e.right]
|
|
527
|
+
case 'unary':
|
|
528
|
+
return [e.argument]
|
|
529
|
+
case 'conditional':
|
|
530
|
+
return [e.test, e.consequent, e.alternate]
|
|
531
|
+
case 'template-literal':
|
|
532
|
+
return e.parts.flatMap(p => (p.type === 'expression' ? [p.expr] : []))
|
|
533
|
+
case 'array-literal':
|
|
534
|
+
return e.elements
|
|
535
|
+
case 'array-method':
|
|
536
|
+
return [e.object, ...e.args, ...(e.method === 'flat' && e.depthExpr ? [e.depthExpr] : [])]
|
|
537
|
+
case 'object-literal':
|
|
538
|
+
return e.properties.map(p => (p.kind === 'spread' ? p.expr : p.value))
|
|
539
|
+
default:
|
|
540
|
+
return []
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/** The operators {@link arithmeticOperandCoercion} applies `ToNumber` under. */
|
|
545
|
+
function isNumericArithmetic(op: string, other: ParsedExpr | undefined): boolean {
|
|
546
|
+
return NULL_COERCING_ARITHMETIC_OPS.has(op) || (op === '+' && isNumberLiteral(other))
|
|
426
547
|
}
|
|
427
548
|
|
|
428
549
|
function isNumberLiteral(expr: ParsedExpr | undefined): boolean {
|