@barefootjs/jsx 0.40.1 → 0.41.0
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/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 +45 -5
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/component-props.d.ts +3 -4
- package/dist/adapters/component-props.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 +24 -12
- 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 +59 -0
- package/dist/adapters/loop-row-type.d.ts.map +1 -0
- package/dist/adapters/nil-reading.d.ts +63 -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 +80 -12
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/adapters/primitive-plan.d.ts +320 -0
- package/dist/adapters/primitive-plan.d.ts.map +1 -0
- package/dist/adapters/prop-classes.d.ts +5 -0
- package/dist/adapters/prop-classes.d.ts.map +1 -1
- 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 +20 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4903 -2311
- package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
- package/dist/ir-to-client-js/child-props-object.d.ts +106 -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/phases/props-extraction.d.ts +3 -3
- package/dist/ir-to-client-js/plan/build-declaration-emit.d.ts.map +1 -1
- package/dist/ir-to-client-js/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/prune-unused-prop-extractions.d.ts +3 -1
- package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
- package/dist/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 +51 -34
- 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 +58 -13
- 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 +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/ssr-defaults.d.ts +39 -3
- package/dist/ssr-defaults.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 +9 -9
- package/src/__tests__/binding-scope.test.ts +89 -1
- package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -0
- package/src/__tests__/body-destructured-prop-default-arrow.test.ts +2 -2
- package/src/__tests__/body-destructured-props-live.test.ts +5 -5
- package/src/__tests__/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__/client-js-generation.test.ts +3 -3
- package/src/__tests__/computed-const.test.ts +103 -0
- package/src/__tests__/destructure-default-reads-earlier-param.test.ts +7 -7
- package/src/__tests__/destructured-props-live.test.ts +1 -1
- package/src/__tests__/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__/issue-3861-object-spread-child.test.ts +102 -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__/memo-body-expr.test.ts +2 -2
- 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 +78 -0
- package/src/__tests__/nullable-signal-attr.test.ts +109 -48
- package/src/__tests__/primitive-plan.test.ts +494 -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__/prop-default-undefined-only.test.ts +115 -0
- package/src/__tests__/prop-references.test.ts +2 -2
- package/src/__tests__/props-destructuring.test.ts +17 -0
- package/src/__tests__/props-object-whole-read.test.ts +40 -0
- package/src/__tests__/prune-unused-prop-extractions.test.ts +3 -3
- package/src/__tests__/reactive-attrs-in-map.test.ts +1 -1
- package/src/__tests__/reactive-factory-inlining.test.ts +4 -1
- package/src/__tests__/renamed-body-destructure-template.test.ts +7 -7
- package/src/__tests__/rest-binding-gaps.test.ts +192 -0
- package/src/__tests__/rewrite-destructured-props.test.ts +1 -1
- package/src/__tests__/root-prop-alias.test.ts +53 -8
- package/src/__tests__/ssr-defaults.test.ts +113 -25
- 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__/written-binding-substitution.test.ts +8 -8
- package/src/adapters/boolean-result.ts +72 -66
- package/src/adapters/brace-delimiter-text.ts +25 -0
- package/src/adapters/callback-method.ts +159 -27
- package/src/adapters/component-props.ts +3 -4
- package/src/adapters/computed-const.ts +238 -4
- package/src/adapters/dsl-template-adapter.ts +62 -26
- package/src/adapters/loop-bound-names.ts +9 -1
- package/src/adapters/loop-plan.ts +1 -1
- package/src/adapters/loop-row-type.ts +149 -0
- package/src/adapters/nil-reading.ts +295 -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 +183 -19
- package/src/adapters/primitive-plan.ts +765 -0
- package/src/adapters/prop-classes.ts +16 -3
- package/src/analyzer-context.ts +6 -0
- package/src/analyzer.ts +532 -26
- 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 +35 -16
- package/src/ir-to-client-js/build-references.ts +69 -0
- package/src/ir-to-client-js/child-props-object.ts +240 -0
- package/src/ir-to-client-js/collect-elements.ts +31 -63
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +8 -1
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +31 -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 +43 -17
- package/src/ir-to-client-js/imports.ts +2 -2
- package/src/ir-to-client-js/index.ts +53 -11
- package/src/ir-to-client-js/phases/props-extraction.ts +11 -7
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +40 -7
- package/src/ir-to-client-js/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/prune-unused-prop-extractions.ts +58 -5
- package/src/ir-to-client-js/rewrite-destructured-props.ts +31 -1
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +26 -10
- package/src/jsx-to-ir.ts +459 -37
- package/src/nullable-signal-attr.ts +200 -110
- package/src/prop-rewrite.ts +30 -6
- package/src/props-binding.ts +84 -19
- 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/ssr-defaults.ts +100 -11
- package/src/strip-types.ts +31 -3
- package/src/template-position-walk.ts +57 -28
- package/src/types.ts +87 -0
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts +0 -41
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +0 -1
- package/src/adapters/ambiguous-nil-arithmetic.ts +0 -94
package/src/jsx-to-ir.ts
CHANGED
|
@@ -43,15 +43,18 @@ import {
|
|
|
43
43
|
AttrValueOf,
|
|
44
44
|
} from './types.ts'
|
|
45
45
|
import { type AnalyzerContext, type MultiReturnJsxInfo, getSourceLocation, collectReactiveGetterNames, tsTypeToTypeInfo } from './analyzer-context.ts'
|
|
46
|
-
import { parseExpression, isSupported, parseBlockBody, foldBlockToExpr, predicateTernaryToLogical, tsNodeToParsedExpr, sortComparatorFromArrow, stringifyParsedExpr, cssKebabCase, CALLBACK_METHODS, unboundCallbackParamRead, unboundCallbackParamReason, type ParsedExpr } from './expression-parser.ts'
|
|
46
|
+
import { parseExpression, freeIdentifiers, isSupported, parseBlockBody, foldBlockToExpr, predicateTernaryToLogical, tsNodeToParsedExpr, sortComparatorFromArrow, stringifyParsedExpr, cssKebabCase, CALLBACK_METHODS, unboundCallbackParamRead, unboundCallbackParamReason, type ParsedExpr } from './expression-parser.ts'
|
|
47
47
|
import { fallbackChainParts, nullishChainParts } from './children-nullish.ts'
|
|
48
|
-
import type { IRLoopSort, FunctionInfo, ConstantInfo, ParamInfo, FreeReference } from './types.ts'
|
|
48
|
+
import type { IRLoopSort, FunctionInfo, ConstantInfo, ParamInfo, FreeReference, SpreadAttr, JsSourceText } from './types.ts'
|
|
49
|
+
import { resolveStaticConstBinding } from './augment-inherited-props.ts'
|
|
50
|
+
import { componentScopeNames } from './adapters/computed-const.ts'
|
|
49
51
|
import { formatParamWithType } from './module-exports.ts'
|
|
50
52
|
import { createError, ErrorCodes, internalInvariant } from './errors.ts'
|
|
51
53
|
import { CLIENT_BUILTIN_SOURCE, isClientBuiltinName, type ClientBuiltinTag } from './builtins.ts'
|
|
52
54
|
import { containsReactiveExpression, collectReactiveBrandLeaves } from './reactivity-checker.ts'
|
|
53
55
|
import {
|
|
54
56
|
rewriteBarePropRefs as rewriteBarePropRefsCore,
|
|
57
|
+
rewriteBarePropRefsStructural as rewriteBarePropRefsStructuralCore,
|
|
55
58
|
collectAstPropRefs,
|
|
56
59
|
rewriteScopedValueRefs,
|
|
57
60
|
} from './prop-rewrite.ts'
|
|
@@ -624,6 +627,24 @@ function lowerToLocaleDateCalls(text: string, expr: ts.Node, ctx: TransformConte
|
|
|
624
627
|
* Returns undefined if no rewriting is needed (SolidJS-style or no props).
|
|
625
628
|
*/
|
|
626
629
|
function rewriteBarePropRefs(text: string, expr: ts.Node, ctx: TransformContext): string | undefined {
|
|
630
|
+
return rewriteBarePropRefsVia(text, expr, ctx, rewriteBarePropRefsCore) ?? undefined
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
/**
|
|
634
|
+
* `rewriteBarePropRefs` with no regex fallback (#3845): `null` when `text`
|
|
635
|
+
* does not parse as an expression, for a caller that refuses the shape
|
|
636
|
+
* rather than emit a scope-blind splice.
|
|
637
|
+
*/
|
|
638
|
+
function rewriteBarePropRefsStructural(text: string, expr: ts.Node, ctx: TransformContext): string | undefined | null {
|
|
639
|
+
return rewriteBarePropRefsVia(text, expr, ctx, rewriteBarePropRefsStructuralCore)
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
function rewriteBarePropRefsVia(
|
|
643
|
+
text: string,
|
|
644
|
+
expr: ts.Node,
|
|
645
|
+
ctx: TransformContext,
|
|
646
|
+
core: typeof rewriteBarePropRefsStructuralCore,
|
|
647
|
+
): string | undefined | null {
|
|
627
648
|
// #2292: lower a Date-typed prop's catalogued accessor call BEFORE the
|
|
628
649
|
// bare-prop-name rewrite below, so the receiver identifier still picks
|
|
629
650
|
// up the usual `_p.` prefix (destructured mode) or falls through to the
|
|
@@ -676,7 +697,7 @@ function rewriteBarePropRefs(text: string, expr: ts.Node, ctx: TransformContext)
|
|
|
676
697
|
return info ? livePropReadExpr(info, undefined, false) : `${PROPS_PARAM}.${callerKey}`
|
|
677
698
|
}
|
|
678
699
|
: undefined
|
|
679
|
-
return
|
|
700
|
+
return core(dateLowered, expr, propNames, extraPropRefs, propAliases ?? undefined, replacementFor)
|
|
680
701
|
}
|
|
681
702
|
|
|
682
703
|
/**
|
|
@@ -3990,6 +4011,7 @@ type FilterPredicateResult = {
|
|
|
3990
4011
|
param: string
|
|
3991
4012
|
predicate?: ParsedExpr
|
|
3992
4013
|
raw: string
|
|
4014
|
+
templateRaw?: string
|
|
3993
4015
|
}
|
|
3994
4016
|
|
|
3995
4017
|
/**
|
|
@@ -4085,7 +4107,7 @@ function extractFilterPredicate(
|
|
|
4085
4107
|
}
|
|
4086
4108
|
const unbound = unboundFilterParamReason(callback, predicate, ctx)
|
|
4087
4109
|
if (unbound) return { result: null, unsupportedReason: unbound }
|
|
4088
|
-
return {
|
|
4110
|
+
return withTemplatePredicate({ param, predicate, raw }, callback, ctx)
|
|
4089
4111
|
}
|
|
4090
4112
|
|
|
4091
4113
|
// Expression body: filter(t => !t.done)
|
|
@@ -4100,7 +4122,53 @@ function extractFilterPredicate(
|
|
|
4100
4122
|
const unbound = unboundFilterParamReason(callback, predicate, ctx)
|
|
4101
4123
|
if (unbound) return { result: null, unsupportedReason: unbound }
|
|
4102
4124
|
|
|
4103
|
-
return {
|
|
4125
|
+
return withTemplatePredicate({ param, predicate, raw }, callback, ctx)
|
|
4126
|
+
}
|
|
4127
|
+
|
|
4128
|
+
/**
|
|
4129
|
+
* Attach the predicate's client-template text (`templateRaw`, #3845): `raw`
|
|
4130
|
+
* with each destructured-prop read rewritten to `_p.<key>`, the same rewrite
|
|
4131
|
+
* `templateArray` gets. The predicate's own parameters are bound for the
|
|
4132
|
+
* rewrite, so `filter((one) => one)` keeps its parameter even when a prop is
|
|
4133
|
+
* named `one`.
|
|
4134
|
+
*
|
|
4135
|
+
* The rewrite is structural only. A block body is rewritten as the body of
|
|
4136
|
+
* an arrow (`() => { … }`), so its `let` / `const` shadow a prop for their
|
|
4137
|
+
* block and a `var` for the whole body (`walkWithScope`). The regex fallback
|
|
4138
|
+
* is never used: it cannot see an inner binding and would turn
|
|
4139
|
+
* `const one = 'a'` into `const _p.one = 'a'`. A text that does not parse is
|
|
4140
|
+
* refused as off-subset, like any other predicate the compiler cannot
|
|
4141
|
+
* model.
|
|
4142
|
+
*/
|
|
4143
|
+
function withTemplatePredicate(
|
|
4144
|
+
result: FilterPredicateResult,
|
|
4145
|
+
callback: ts.ArrowFunction,
|
|
4146
|
+
ctx: TransformContext,
|
|
4147
|
+
): FilterExtractionResult {
|
|
4148
|
+
const savedScope = ctx.scope
|
|
4149
|
+
const params = new Set<string>()
|
|
4150
|
+
for (const p of callback.parameters) collectBindingNames(p.name, params)
|
|
4151
|
+
ctx.scope = savedScope.enterCallback([...params])
|
|
4152
|
+
try {
|
|
4153
|
+
const templateRaw = rewritePredicateBody(result.raw, callback.body, ctx)
|
|
4154
|
+
if (templateRaw === null) {
|
|
4155
|
+
return { result: null, unsupportedReason: 'Filter predicate cannot be rewritten for the client template' }
|
|
4156
|
+
}
|
|
4157
|
+
return { result: templateRaw === undefined ? result : { ...result, templateRaw } }
|
|
4158
|
+
} finally {
|
|
4159
|
+
ctx.scope = savedScope
|
|
4160
|
+
}
|
|
4161
|
+
}
|
|
4162
|
+
|
|
4163
|
+
/** Arrow head a block-bodied predicate is parsed under, so its body is a function body. */
|
|
4164
|
+
const BLOCK_PREDICATE_HEAD = '() => '
|
|
4165
|
+
|
|
4166
|
+
/** The structural prop rewrite of a predicate body: `null` when it cannot be done. */
|
|
4167
|
+
function rewritePredicateBody(raw: string, body: ts.ConciseBody, ctx: TransformContext): string | undefined | null {
|
|
4168
|
+
if (!ts.isBlock(body)) return rewriteBarePropRefsStructural(raw, body, ctx)
|
|
4169
|
+
const rewritten = rewriteBarePropRefsStructural(`${BLOCK_PREDICATE_HEAD}${raw}`, body, ctx)
|
|
4170
|
+
if (rewritten === undefined || rewritten === null) return rewritten
|
|
4171
|
+
return rewritten.startsWith(BLOCK_PREDICATE_HEAD) ? rewritten.slice(BLOCK_PREDICATE_HEAD.length) : null
|
|
4104
4172
|
}
|
|
4105
4173
|
|
|
4106
4174
|
/**
|
|
@@ -7236,44 +7304,60 @@ interface ProcessedAttributes {
|
|
|
7236
7304
|
// runtime to spread at hydration time, which loses that per-key resolution
|
|
7237
7305
|
// and is reserved for cases where the key set isn't statically known.
|
|
7238
7306
|
//
|
|
7239
|
-
//
|
|
7240
|
-
//
|
|
7241
|
-
//
|
|
7307
|
+
// `closedRestSpreadSource` restricts unrolling to the rest-prop itself:
|
|
7308
|
+
// arbitrary `{...someObject}` spreads can't be unrolled because we don't
|
|
7309
|
+
// have a static key list for them.
|
|
7242
7310
|
//
|
|
7243
7311
|
// The shape returned satisfies both IRAttribute and IRProp.
|
|
7244
7312
|
function expandSpreadAttribute(
|
|
7245
7313
|
attr: ts.JsxSpreadAttribute,
|
|
7246
7314
|
ctx: TransformContext,
|
|
7315
|
+
constEntries?: ReadonlyArray<{ key: string; text: string }>,
|
|
7316
|
+
restFallbacks?: ReadonlyMap<string, RestKeyFallback>,
|
|
7247
7317
|
): Array<{
|
|
7248
7318
|
name: string
|
|
7249
7319
|
value: AttrValue
|
|
7250
7320
|
loc: SourceLocation
|
|
7251
7321
|
freeIdentifiers?: ReadonlySet<string>
|
|
7252
7322
|
restExpandedKey?: boolean
|
|
7323
|
+
restFallback?: JsSourceText
|
|
7253
7324
|
}> {
|
|
7254
7325
|
const spreadExpr = ctx.getJS(attr.expression)
|
|
7255
7326
|
const expandedKeys = ctx.analyzer.restPropsExpandedKeys
|
|
7256
|
-
const restName = ctx.analyzer.restPropsName
|
|
7257
7327
|
const loc = getSourceLocation(attr, ctx.sourceFile, ctx.filePath)
|
|
7258
7328
|
const spreadFreeIdentifiers = extractFreeIdentifiersFromNode(attr.expression)
|
|
7329
|
+
const source = closedRestSpreadSource(attr, ctx)
|
|
7330
|
+
if (source === null && refuseBodyRestSpreadUnderShadowedProps(attr, ctx)) return []
|
|
7259
7331
|
|
|
7260
|
-
if (
|
|
7332
|
+
if (source !== null) {
|
|
7261
7333
|
// Expanded per-key attrs reference `${restName}.${key}` — `restName` is
|
|
7262
7334
|
// the only free identifier (the `.${key}` tail is a member-access name,
|
|
7263
7335
|
// not an identifier reference). `restExpandedKey` (#3057) marks these so
|
|
7264
7336
|
// Phase 2 (`collectElements`) can bind them reactively off a live
|
|
7265
7337
|
// `_p.<key>` read instead of the one-time `${restName}.${key}` snapshot
|
|
7266
|
-
// — see `IRAttribute.restExpandedKey`'s docstring.
|
|
7267
|
-
|
|
7268
|
-
|
|
7269
|
-
|
|
7270
|
-
|
|
7271
|
-
|
|
7272
|
-
|
|
7273
|
-
|
|
7274
|
-
|
|
7338
|
+
// — see `IRAttribute.restExpandedKey`'s docstring. `source` is the
|
|
7339
|
+
// props object for a body rest (`closedRestSpreadSource`). A key an
|
|
7340
|
+
// earlier attribute also writes keeps that value when the caller
|
|
7341
|
+
// omitted it (`restFallbacks`, {@link closedRestFallbacks}).
|
|
7342
|
+
const perKeyFreeIds: ReadonlySet<string> = new Set([source])
|
|
7343
|
+
return expandedKeys.map(key => {
|
|
7344
|
+
const read = `${source}.${key}`
|
|
7345
|
+
const fallback = restFallbacks?.get(key)
|
|
7346
|
+
return {
|
|
7347
|
+
name: key,
|
|
7348
|
+
value: fallback === undefined
|
|
7349
|
+
? AttrValueOf.expression(read)
|
|
7350
|
+
: AttrValueOf.expression(restKeyWithFallback(read, fallback.text), { templateExpr: restKeyWithFallback(read, fallback.templateText) }),
|
|
7351
|
+
loc,
|
|
7352
|
+
freeIdentifiers: fallback === undefined ? perKeyFreeIds : new Set([source, ...fallback.freeIdentifiers]),
|
|
7353
|
+
restExpandedKey: true,
|
|
7354
|
+
...(fallback !== undefined && { restFallback: fallback.templateText }),
|
|
7355
|
+
}
|
|
7356
|
+
})
|
|
7275
7357
|
}
|
|
7276
7358
|
|
|
7359
|
+
if (constEntries) return constEntries.map(({ key, text }) => ({ name: key, value: AttrValueOf.expression(text), loc }))
|
|
7360
|
+
|
|
7277
7361
|
// Allocate a component-scoped slot ID so adapters that need a
|
|
7278
7362
|
// structured plumbing path (Go's `.Spread_N` struct field) can
|
|
7279
7363
|
// address this spread without recomputing identity downstream
|
|
@@ -7281,14 +7365,354 @@ function expandSpreadAttribute(
|
|
|
7281
7365
|
// it. The closed-type expansion branch above returns before this
|
|
7282
7366
|
// point, so closed-type rest spreads stay on the per-key fast path.
|
|
7283
7367
|
const slotId = generateSpreadSlotId(ctx)
|
|
7368
|
+
const value = AttrValueOf.spread(spreadExpr, ctx.getTemplateJS(attr.expression), slotId)
|
|
7369
|
+
const callerBag = callerBagOfSpread(value, ctx)
|
|
7284
7370
|
return [{
|
|
7285
7371
|
name: '...',
|
|
7286
|
-
value:
|
|
7372
|
+
value: callerBag ? { ...value, callerBag } : value,
|
|
7287
7373
|
loc,
|
|
7288
7374
|
freeIdentifiers: spreadFreeIdentifiers,
|
|
7289
7375
|
}]
|
|
7290
7376
|
}
|
|
7291
7377
|
|
|
7378
|
+
/**
|
|
7379
|
+
* The name each key of a closed-type rest spread (`{...rest}` with a
|
|
7380
|
+
* statically known key set, `IRMetadata.restPropsExpandedKeys`) is read
|
|
7381
|
+
* off, or null when `attr` is not one. A rest a body destructure of the
|
|
7382
|
+
* props object binds (`const { label, ...rest } = props`) holds the props
|
|
7383
|
+
* object's keys minus the ones the destructure takes, so its keys read the
|
|
7384
|
+
* props object (#3844): a template binds every declared prop as its own
|
|
7385
|
+
* variable, while its rest bag holds only undeclared keys. A loop row or
|
|
7386
|
+
* callback parameter spelled like the rest binding is that binding, not
|
|
7387
|
+
* the component's rest props (#3851).
|
|
7388
|
+
*/
|
|
7389
|
+
function closedRestSpreadSource(attr: ts.JsxSpreadAttribute, ctx: TransformContext): string | null {
|
|
7390
|
+
const restName = ctx.analyzer.restPropsName
|
|
7391
|
+
if (ctx.analyzer.restPropsExpandedKeys.length === 0 || !restName || ctx.scope.isBound(restName)) return null
|
|
7392
|
+
let expr: ts.Expression = attr.expression
|
|
7393
|
+
while (ts.isParenthesizedExpression(expr)) expr = expr.expression
|
|
7394
|
+
if (!ts.isIdentifier(expr) || !isRestBinding(expr.text, restName, ctx)) return null
|
|
7395
|
+
const source = ctx.analyzer.restPropsOmittedKeys && ctx.analyzer.propsObjectName ? ctx.analyzer.propsObjectName : restName
|
|
7396
|
+
// A body rest's keys read the props object, which a loop row or callback
|
|
7397
|
+
// parameter of the same name hides here: no spelling of the per-key read
|
|
7398
|
+
// reaches the component's props (`expandSpreadAttribute` refuses it).
|
|
7399
|
+
return source !== restName && ctx.scope.isBound(source) ? null : source
|
|
7400
|
+
}
|
|
7401
|
+
|
|
7402
|
+
/**
|
|
7403
|
+
* Whether the use-site identifier `name` holds the component's rest props:
|
|
7404
|
+
* the rest binding itself, or a component-scope `const` alias hop of it
|
|
7405
|
+
* (`const a = rest`, through any number of hops). An alias reads the same
|
|
7406
|
+
* object, so its spread unrolls per key like the rest's own. A name the
|
|
7407
|
+
* enclosing loop rows or callbacks bind is that binding.
|
|
7408
|
+
*/
|
|
7409
|
+
function isRestBinding(name: string, restName: string, ctx: TransformContext): boolean {
|
|
7410
|
+
if (name === restName) return true
|
|
7411
|
+
if (ctx.scope.isBound(name)) return false
|
|
7412
|
+
return resolveAliasOrigin(childrenAliasHops(ctx.analyzer), name, n => (n === restName ? true : null)) === true
|
|
7413
|
+
}
|
|
7414
|
+
|
|
7415
|
+
/**
|
|
7416
|
+
* BF101 for a spread of a props-object body rest (`const { a, ...rest } =
|
|
7417
|
+
* props`) where a loop row or callback parameter shadows the props
|
|
7418
|
+
* object's name (`rows.map(props => <li {...rest} />)`): its keys are read
|
|
7419
|
+
* off the props object, which that binding hides (#3854).
|
|
7420
|
+
*/
|
|
7421
|
+
function refuseBodyRestSpreadUnderShadowedProps(attr: ts.JsxSpreadAttribute, ctx: TransformContext): boolean {
|
|
7422
|
+
const restName = ctx.analyzer.restPropsName
|
|
7423
|
+
const propsName = ctx.analyzer.propsObjectName
|
|
7424
|
+
if (!restName || !propsName || !ctx.analyzer.restPropsOmittedKeys || ctx.analyzer.restPropsExpandedKeys.length === 0) return false
|
|
7425
|
+
if (ctx.scope.isBound(restName) || !ctx.scope.isBound(propsName)) return false
|
|
7426
|
+
let expr: ts.Expression = attr.expression
|
|
7427
|
+
while (ts.isParenthesizedExpression(expr)) expr = expr.expression
|
|
7428
|
+
if (!ts.isIdentifier(expr) || !isRestBinding(expr.text, restName, ctx)) return false
|
|
7429
|
+
ctx.analyzer.errors.push({
|
|
7430
|
+
code: 'BF101',
|
|
7431
|
+
severity: 'error',
|
|
7432
|
+
message: `\`{...${restName}}\` spreads the component's props through a rest, inside a binding that shadows the props object's name \`${propsName}\`, so its keys have no spelling that reaches the component's props.`,
|
|
7433
|
+
loc: getSourceLocation(attr, ctx.sourceFile, ctx.filePath),
|
|
7434
|
+
suggestion: { message: `Rename the inner \`${propsName}\` binding.` },
|
|
7435
|
+
})
|
|
7436
|
+
return true
|
|
7437
|
+
}
|
|
7438
|
+
|
|
7439
|
+
/**
|
|
7440
|
+
* What a closed-type rest spread's keys fall back to (#3851). JSX applies
|
|
7441
|
+
* attributes and spreads in order, and a spread overrides only with the
|
|
7442
|
+
* keys its object has: `<Chip tone="x" {...rest} />` keeps `tone="x"` when
|
|
7443
|
+
* the caller omitted `tone`. A closed rest spread is expanded into one
|
|
7444
|
+
* attribute per key the props type declares, so each key an earlier
|
|
7445
|
+
* attribute (or an expanded const spread) writes reads `rest.key ??
|
|
7446
|
+
* (earlier)` instead ({@link restKeyWithFallback}), and the earlier explicit
|
|
7447
|
+
* attribute is dropped: the expanded key now carries both values.
|
|
7448
|
+
*
|
|
7449
|
+
* `fallbacks` maps each closed rest spread to its keys' fallback sources,
|
|
7450
|
+
* and `properties` is `properties` without the dropped attributes. An
|
|
7451
|
+
* earlier value that has no source form (a JSX element prop) or may have an
|
|
7452
|
+
* effect (`isReadOnlyAttrValue`) is refused with BF021. `ref`, `key` and
|
|
7453
|
+
* event handlers are not attribute writes and are left as they are; an
|
|
7454
|
+
* open spread (`{...obj}`) writes keys nothing knows statically, so no
|
|
7455
|
+
* fallback reaches across it.
|
|
7456
|
+
*/
|
|
7457
|
+
function closedRestFallbacks(
|
|
7458
|
+
properties: readonly ts.JsxAttributeLike[],
|
|
7459
|
+
entries: ReadonlyMap<ts.JsxSpreadAttribute, ReadonlyArray<{ key: string; text: string }>>,
|
|
7460
|
+
ctx: TransformContext,
|
|
7461
|
+
): { properties: ts.JsxAttributeLike[]; fallbacks: Map<ts.JsxSpreadAttribute, Map<string, RestKeyFallback>> } {
|
|
7462
|
+
const fallbacks = new Map<ts.JsxSpreadAttribute, Map<string, RestKeyFallback>>()
|
|
7463
|
+
const dropped = new Set<ts.JsxAttributeLike>()
|
|
7464
|
+
const lastWrite = new Map<string, KeyWrite>()
|
|
7465
|
+
for (const attr of properties) {
|
|
7466
|
+
if (ts.isJsxAttribute(attr)) {
|
|
7467
|
+
const name = attr.name.getText(ctx.sourceFile)
|
|
7468
|
+
if (name !== 'ref' && name !== 'key' && !isEventHandlerName(name)) lastWrite.set(name, { attr, value: attrValueFallback(attr, ctx) })
|
|
7469
|
+
continue
|
|
7470
|
+
}
|
|
7471
|
+
if (!ts.isJsxSpreadAttribute(attr)) continue
|
|
7472
|
+
const constEntries = entries.get(attr)
|
|
7473
|
+
const source = constEntries ? null : closedRestSpreadSource(attr, ctx)
|
|
7474
|
+
if (constEntries) {
|
|
7475
|
+
for (const e of constEntries) lastWrite.set(e.key, { value: { text: e.text, templateText: e.text, freeIdentifiers: new Set() } })
|
|
7476
|
+
} else if (source === null) {
|
|
7477
|
+
lastWrite.clear()
|
|
7478
|
+
} else {
|
|
7479
|
+
const keyFallbacks = closedRestKeyFallbacks(attr, source, lastWrite, dropped, ctx)
|
|
7480
|
+
if (keyFallbacks.size > 0) fallbacks.set(attr, keyFallbacks)
|
|
7481
|
+
}
|
|
7482
|
+
}
|
|
7483
|
+
return { properties: properties.filter(attr => !dropped.has(attr)), fallbacks }
|
|
7484
|
+
}
|
|
7485
|
+
|
|
7486
|
+
/** {@link closedRestFallbacks} for one closed rest spread reading its keys off `source`. */
|
|
7487
|
+
function closedRestKeyFallbacks(
|
|
7488
|
+
attr: ts.JsxSpreadAttribute,
|
|
7489
|
+
source: string,
|
|
7490
|
+
lastWrite: Map<string, KeyWrite>,
|
|
7491
|
+
dropped: Set<ts.JsxAttributeLike>,
|
|
7492
|
+
ctx: TransformContext,
|
|
7493
|
+
): Map<string, RestKeyFallback> {
|
|
7494
|
+
const keyFallbacks = new Map<string, RestKeyFallback>()
|
|
7495
|
+
for (const key of ctx.analyzer.restPropsExpandedKeys) {
|
|
7496
|
+
const read = `${source}.${key}`
|
|
7497
|
+
const earlier = lastWrite.get(key)
|
|
7498
|
+
lastWrite.set(key, { value: { text: read, templateText: read, freeIdentifiers: new Set([source]) } })
|
|
7499
|
+
if (!earlier) continue
|
|
7500
|
+
if (earlier.value === null || (earlier.attr && !isReadOnlyAttrValue(earlier.attr.initializer))) {
|
|
7501
|
+
const why = earlier.value === null ? 'its value has no expression form to keep' : 'its value may have an effect, which JS runs exactly once'
|
|
7502
|
+
ctx.analyzer.errors.push(createError(ErrorCodes.UNSUPPORTED_JSX_PATTERN, getSourceLocation(earlier.attr ?? attr, ctx.sourceFile, ctx.filePath), {
|
|
7503
|
+
message: `'${key}' is written before a {...${ctx.getJS(attr.expression)}} spread that may also carry it, and ${why} when the caller omits '${key}'.`,
|
|
7504
|
+
suggestion: { message: `Write '${key}' after the spread, or compute the value in a local const and pass that.` },
|
|
7505
|
+
}))
|
|
7506
|
+
continue
|
|
7507
|
+
}
|
|
7508
|
+
keyFallbacks.set(key, earlier.value)
|
|
7509
|
+
lastWrite.set(key, {
|
|
7510
|
+
value: {
|
|
7511
|
+
text: restKeyWithFallback(read, earlier.value.text),
|
|
7512
|
+
templateText: restKeyWithFallback(read, earlier.value.templateText),
|
|
7513
|
+
freeIdentifiers: new Set([source, ...earlier.value.freeIdentifiers]),
|
|
7514
|
+
},
|
|
7515
|
+
})
|
|
7516
|
+
if (earlier.attr) dropped.add(earlier.attr)
|
|
7517
|
+
}
|
|
7518
|
+
return keyFallbacks
|
|
7519
|
+
}
|
|
7520
|
+
|
|
7521
|
+
/** A value a closed rest spread's key falls back to ({@link closedRestFallbacks}): its source text and free names. */
|
|
7522
|
+
interface RestKeyFallback {
|
|
7523
|
+
text: string
|
|
7524
|
+
/** `text` as a client-JS template reads it (`TransformContext.getTemplateJS`). */
|
|
7525
|
+
templateText: string
|
|
7526
|
+
freeIdentifiers: ReadonlySet<string>
|
|
7527
|
+
}
|
|
7528
|
+
|
|
7529
|
+
/** The last write of a key before a spread: the explicit attribute that made it, if any, and its value (null when it has no expression form). */
|
|
7530
|
+
interface KeyWrite {
|
|
7531
|
+
attr?: ts.JsxAttribute
|
|
7532
|
+
value: RestKeyFallback | null
|
|
7533
|
+
}
|
|
7534
|
+
|
|
7535
|
+
/**
|
|
7536
|
+
* `read`, or `fallback` when the key `read` reads is absent. A caller's
|
|
7537
|
+
* explicit `null` reads as absent too, as everywhere a template tests
|
|
7538
|
+
* whether a spread carries a key: no template backend tells the two apart.
|
|
7539
|
+
*/
|
|
7540
|
+
function restKeyWithFallback(read: string, fallback: string): string {
|
|
7541
|
+
return `${read} ?? (${fallback})`
|
|
7542
|
+
}
|
|
7543
|
+
|
|
7544
|
+
/**
|
|
7545
|
+
* A JSX attribute's value as a fallback of a later closed rest spread
|
|
7546
|
+
* ({@link closedRestFallbacks}): `true` for a bare attribute, the string
|
|
7547
|
+
* literal, or the expression. Null for a value with no expression form (a
|
|
7548
|
+
* JSX element, an empty `{}`).
|
|
7549
|
+
*/
|
|
7550
|
+
function attrValueFallback(attr: ts.JsxAttribute, ctx: TransformContext): RestKeyFallback | null {
|
|
7551
|
+
const value = attr.initializer
|
|
7552
|
+
if (value === undefined) return { text: 'true', templateText: 'true', freeIdentifiers: new Set() }
|
|
7553
|
+
if (ts.isStringLiteral(value)) {
|
|
7554
|
+
const text = JSON.stringify(value.text)
|
|
7555
|
+
return { text, templateText: text, freeIdentifiers: new Set() }
|
|
7556
|
+
}
|
|
7557
|
+
if (!ts.isJsxExpression(value) || value.expression === undefined) return null
|
|
7558
|
+
const expr = unwrapTransparentTsWrappers(value.expression)
|
|
7559
|
+
if (ts.isJsxElement(expr) || ts.isJsxSelfClosingElement(expr) || ts.isJsxFragment(expr)) return null
|
|
7560
|
+
return {
|
|
7561
|
+
text: ctx.getJS(value.expression),
|
|
7562
|
+
templateText: ctx.getTemplateJS(value.expression),
|
|
7563
|
+
freeIdentifiers: extractFreeIdentifiersFromNode(value.expression),
|
|
7564
|
+
}
|
|
7565
|
+
}
|
|
7566
|
+
|
|
7567
|
+
/**
|
|
7568
|
+
* How an element's or component call's static const spreads resolve
|
|
7569
|
+
* against everything else it is given (#3844): `entries` maps each spread
|
|
7570
|
+
* of a const holding a static object literal to the `key: value-source`
|
|
7571
|
+
* pairs it passes, and `properties` is the attribute list without the
|
|
7572
|
+
* explicit attributes such a spread overrides.
|
|
7573
|
+
*
|
|
7574
|
+
* JSX applies attributes and spreads in order and the last write of a key
|
|
7575
|
+
* wins, so the plan walks them from the last: a key an explicit attribute
|
|
7576
|
+
* or an expanded spread writes is dropped from every earlier explicit
|
|
7577
|
+
* attribute and expanded spread. A spread that is not expanded (`{...rest}`,
|
|
7578
|
+
* a computed object) overrides nothing statically; what it holds is merged
|
|
7579
|
+
* at render time after what comes before it, as before. Each key is
|
|
7580
|
+
* therefore written once, with the value JS gives it. When an overridden
|
|
7581
|
+
* attribute's value may have an effect, nothing is expanded (see the end
|
|
7582
|
+
* of the function).
|
|
7583
|
+
*/
|
|
7584
|
+
function staticSpreadPlan(attributes: ts.JsxAttributes, ctx: TransformContext): {
|
|
7585
|
+
properties: ts.JsxAttributeLike[]
|
|
7586
|
+
entries: Map<ts.JsxSpreadAttribute, Array<{ key: string; text: string }>>
|
|
7587
|
+
} {
|
|
7588
|
+
const entries = new Map<ts.JsxSpreadAttribute, Array<{ key: string; text: string }>>()
|
|
7589
|
+
const overridden = new Set<ts.JsxAttributeLike>()
|
|
7590
|
+
const written = new Set<string>()
|
|
7591
|
+
for (const attr of [...attributes.properties].reverse()) {
|
|
7592
|
+
if (ts.isJsxAttribute(attr)) {
|
|
7593
|
+
const name = attr.name.getText(ctx.sourceFile)
|
|
7594
|
+
if (written.has(name)) overridden.add(attr)
|
|
7595
|
+
written.add(name)
|
|
7596
|
+
continue
|
|
7597
|
+
}
|
|
7598
|
+
const spread = expandStaticConstSpread(attr, ctx)
|
|
7599
|
+
if (!spread) continue
|
|
7600
|
+
entries.set(attr, spread.filter(e => !written.has(e.key)))
|
|
7601
|
+
for (const e of spread) written.add(e.key)
|
|
7602
|
+
}
|
|
7603
|
+
// Dropping an overridden attribute drops the evaluation of its value. JS
|
|
7604
|
+
// still evaluates it, so a value that may have an effect (a call, an
|
|
7605
|
+
// assignment) keeps every spread of the element a spread, merged in order
|
|
7606
|
+
// at render time.
|
|
7607
|
+
if ([...overridden].some(attr => !ts.isJsxAttribute(attr) || !isEffectFreeAttrValue(attr.initializer))) {
|
|
7608
|
+
return { properties: [...attributes.properties], entries: new Map() }
|
|
7609
|
+
}
|
|
7610
|
+
return { properties: attributes.properties.filter(attr => !overridden.has(attr)), entries }
|
|
7611
|
+
}
|
|
7612
|
+
|
|
7613
|
+
/**
|
|
7614
|
+
* Whether evaluating a JSX attribute's value can have no effect: no value
|
|
7615
|
+
* (`disabled`), a string, or an expression {@link isEffectFreeExpression}
|
|
7616
|
+
* accepts.
|
|
7617
|
+
*/
|
|
7618
|
+
function isEffectFreeAttrValue(value: ts.JsxAttributeValue | undefined): boolean {
|
|
7619
|
+
if (value === undefined || ts.isStringLiteral(value)) return true
|
|
7620
|
+
if (!ts.isJsxExpression(value)) return false
|
|
7621
|
+
return value.expression === undefined || isEffectFreeExpression(value.expression)
|
|
7622
|
+
}
|
|
7623
|
+
|
|
7624
|
+
/**
|
|
7625
|
+
* Whether a JSX attribute's value reads without an effect for a closed
|
|
7626
|
+
* rest spread's fallback ({@link closedRestFallbacks}), which evaluates it
|
|
7627
|
+
* only when the caller omits the key: an {@link isEffectFreeAttrValue}
|
|
7628
|
+
* value, or a member read off a name (`props.title`, `row?.label`). A prop
|
|
7629
|
+
* or row read has no effect the render can observe; anything else (a call,
|
|
7630
|
+
* an assignment) is refused, since JS evaluates it exactly once.
|
|
7631
|
+
*/
|
|
7632
|
+
function isReadOnlyAttrValue(value: ts.JsxAttributeValue | undefined): boolean {
|
|
7633
|
+
if (isEffectFreeAttrValue(value)) return true
|
|
7634
|
+
if (value === undefined || !ts.isJsxExpression(value) || value.expression === undefined) return false
|
|
7635
|
+
let expr: ts.Expression = value.expression
|
|
7636
|
+
while (ts.isParenthesizedExpression(expr) || ts.isPropertyAccessExpression(expr)) expr = expr.expression
|
|
7637
|
+
return ts.isIdentifier(expr)
|
|
7638
|
+
}
|
|
7639
|
+
|
|
7640
|
+
/**
|
|
7641
|
+
* A literal (string, number, boolean, `null`, a template literal without
|
|
7642
|
+
* substitutions, a negated number) or a plain name (`label`), possibly
|
|
7643
|
+
* parenthesized. Anything else is treated as possibly having an effect: a
|
|
7644
|
+
* call, an assignment, `new`, an operator, and also a member access
|
|
7645
|
+
* (`props.value.title`), which can run a getter.
|
|
7646
|
+
*/
|
|
7647
|
+
function isEffectFreeExpression(expr: ts.Expression): boolean {
|
|
7648
|
+
if (ts.isParenthesizedExpression(expr)) return isEffectFreeExpression(expr.expression)
|
|
7649
|
+
if (ts.isStringLiteral(expr) || ts.isNumericLiteral(expr) || ts.isNoSubstitutionTemplateLiteral(expr) || ts.isIdentifier(expr)) return true
|
|
7650
|
+
if (expr.kind === ts.SyntaxKind.TrueKeyword || expr.kind === ts.SyntaxKind.FalseKeyword || expr.kind === ts.SyntaxKind.NullKeyword) return true
|
|
7651
|
+
if (ts.isPrefixUnaryExpression(expr) && expr.operator === ts.SyntaxKind.MinusToken) return ts.isNumericLiteral(expr.operand)
|
|
7652
|
+
return false
|
|
7653
|
+
}
|
|
7654
|
+
|
|
7655
|
+
/**
|
|
7656
|
+
* The entries of a spread of a const holding a static object literal
|
|
7657
|
+
* (`const base = { label: 'x', tone: 'info' }`, `<Chip {...base} />`), as
|
|
7658
|
+
* the `key: value-source` pairs the spread passes, one per key with the
|
|
7659
|
+
* value of its last occurrence in the literal (#3844). Carried that way,
|
|
7660
|
+
* every backend reads each value as a literal instead of a bag no template
|
|
7661
|
+
* binds; {@link staticSpreadPlan} resolves them against the element's other
|
|
7662
|
+
* attributes.
|
|
7663
|
+
*
|
|
7664
|
+
* Only when the read reaches the const (`resolveStaticConstBinding`; a
|
|
7665
|
+
* module const also not shadowed by a component binding) and every entry
|
|
7666
|
+
* is a plain `key: value` whose value reads no name (a literal, or an array
|
|
7667
|
+
* or object of literals), and whose key is not `key`, `ref` or an event
|
|
7668
|
+
* handler name. `null` otherwise, and the spread stays a spread.
|
|
7669
|
+
*/
|
|
7670
|
+
function expandStaticConstSpread(attr: ts.JsxSpreadAttribute, ctx: TransformContext): Array<{ key: string; text: string }> | null {
|
|
7671
|
+
let expr: ts.Expression = attr.expression
|
|
7672
|
+
while (ts.isParenthesizedExpression(expr)) expr = expr.expression
|
|
7673
|
+
if (!ts.isIdentifier(expr)) return null
|
|
7674
|
+
const name = expr.text
|
|
7675
|
+
const constant = resolveStaticConstBinding(name, ctx.analyzer.localConstants, n => ctx.scope.isBound(n))
|
|
7676
|
+
if (!constant || constant.parsed?.kind !== 'object-literal') return null
|
|
7677
|
+
if (constant.isModule && componentScopeNames(ctx.analyzer).has(name)) return null
|
|
7678
|
+
const init = parseConstInitializer(constant)
|
|
7679
|
+
if (!init || !ts.isObjectLiteralExpression(init)) return null
|
|
7680
|
+
return staticObjectSpreadEntries(init)
|
|
7681
|
+
}
|
|
7682
|
+
|
|
7683
|
+
/**
|
|
7684
|
+
* {@link expandStaticConstSpread}'s entries of the object literal `init`,
|
|
7685
|
+
* or `null` when one of them has no per-key form.
|
|
7686
|
+
*/
|
|
7687
|
+
function staticObjectSpreadEntries(init: ts.ObjectLiteralExpression): Array<{ key: string; text: string }> | null {
|
|
7688
|
+
const entries = new Map<string, string>()
|
|
7689
|
+
for (const prop of init.properties) {
|
|
7690
|
+
if (!ts.isPropertyAssignment(prop)) return null
|
|
7691
|
+
const key = ts.isIdentifier(prop.name) || ts.isStringLiteral(prop.name) ? prop.name.text : null
|
|
7692
|
+
if (key === null || key === 'key' || key === 'ref' || isEventHandlerName(key)) return null
|
|
7693
|
+
if (freeIdentifiers(tsNodeToParsedExpr(prop.initializer))?.size !== 0) return null
|
|
7694
|
+
entries.set(key, astText(prop.initializer))
|
|
7695
|
+
}
|
|
7696
|
+
return [...entries].map(([key, text]) => ({ key, text }))
|
|
7697
|
+
}
|
|
7698
|
+
|
|
7699
|
+
/**
|
|
7700
|
+
* Which caller prop bag a spread forwards whole (`SpreadAttr.callerBag`):
|
|
7701
|
+
* its `expr` / `templateExpr` resolved through any alias hop
|
|
7702
|
+
* (`resolveRestSpreadOriginCore`). A name an enclosing loop row or callback
|
|
7703
|
+
* binds is that binding, not the component's bag (#3713: `items.map((props)
|
|
7704
|
+
* => <Tag {...props} />)`).
|
|
7705
|
+
*/
|
|
7706
|
+
function callerBagOfSpread(value: SpreadAttr, ctx: TransformContext): 'props' | 'rest' | undefined {
|
|
7707
|
+
const constants = constantValuesByName(ctx)
|
|
7708
|
+
for (const name of new Set([value.expr, value.templateExpr])) {
|
|
7709
|
+
if (!name || ctx.scope.isBound(name)) continue
|
|
7710
|
+
const origin = resolveRestSpreadOriginCore(ctx.analyzer, constants, name)
|
|
7711
|
+
if (origin !== null) return origin
|
|
7712
|
+
}
|
|
7713
|
+
return undefined
|
|
7714
|
+
}
|
|
7715
|
+
|
|
7292
7716
|
// Extract free identifiers from an attribute's initializer expression
|
|
7293
7717
|
// (#1267). Returns undefined for boolean shorthand / string-literal attrs
|
|
7294
7718
|
// that have no expression. Downstream callers can ask
|
|
@@ -7377,11 +7801,13 @@ function processAttributes(
|
|
|
7377
7801
|
let ref: string | null = null
|
|
7378
7802
|
let refExpr: ts.Expression | null = null
|
|
7379
7803
|
let hasSpread = false
|
|
7804
|
+
const spreadPlan = staticSpreadPlan(attributes, ctx)
|
|
7805
|
+
const restPlan = closedRestFallbacks(spreadPlan.properties, spreadPlan.entries, ctx)
|
|
7380
7806
|
|
|
7381
|
-
for (const attr of
|
|
7807
|
+
for (const attr of restPlan.properties) {
|
|
7382
7808
|
if (ts.isJsxSpreadAttribute(attr)) {
|
|
7383
7809
|
hasSpread = true
|
|
7384
|
-
attrs.push(...expandSpreadAttribute(attr, ctx))
|
|
7810
|
+
attrs.push(...expandSpreadAttribute(attr, ctx, spreadPlan.entries.get(attr), restPlan.fallbacks.get(attr)))
|
|
7385
7811
|
continue
|
|
7386
7812
|
}
|
|
7387
7813
|
|
|
@@ -9033,8 +9459,10 @@ function processComponentProps(
|
|
|
9033
9459
|
ctx: TransformContext
|
|
9034
9460
|
): IRProp[] {
|
|
9035
9461
|
const props: IRProp[] = []
|
|
9462
|
+
const spreadPlan = staticSpreadPlan(attributes, ctx)
|
|
9463
|
+
const restPlan = closedRestFallbacks(spreadPlan.properties, spreadPlan.entries, ctx)
|
|
9036
9464
|
|
|
9037
|
-
for (const attr of
|
|
9465
|
+
for (const attr of restPlan.properties) {
|
|
9038
9466
|
if (ts.isJsxSpreadAttribute(attr)) {
|
|
9039
9467
|
// `restExpandedKey` (#3057) is consumed only by the element-attribute
|
|
9040
9468
|
// path (`collect-elements.ts`'s `attr.restExpandedKey` branch) — a
|
|
@@ -9043,7 +9471,7 @@ function processComponentProps(
|
|
|
9043
9471
|
// rewrite, so `IRProp` declares no such field. Strip it here rather
|
|
9044
9472
|
// than letting the shared helper's per-key marking leak into IRProp
|
|
9045
9473
|
// as an inert flag nothing reads.
|
|
9046
|
-
props.push(...expandSpreadAttribute(attr, ctx).map(({ restExpandedKey: _restExpandedKey, ...prop }) => markPropsObjectSpread(prop
|
|
9474
|
+
props.push(...expandSpreadAttribute(attr, ctx, spreadPlan.entries.get(attr), restPlan.fallbacks.get(attr)).map(({ restExpandedKey: _restExpandedKey, restFallback: _restFallback, ...prop }) => markPropsObjectSpread(prop)))
|
|
9047
9475
|
continue
|
|
9048
9476
|
}
|
|
9049
9477
|
|
|
@@ -9852,21 +10280,15 @@ function forwardsCallerRestProps(attrs: IRAttribute[], ctx: TransformContext): b
|
|
|
9852
10280
|
|
|
9853
10281
|
/**
|
|
9854
10282
|
* Mark a child-component spread of the component's own props object
|
|
9855
|
-
* (`SpreadAttr.propsObject`, #3688)
|
|
9856
|
-
*
|
|
9857
|
-
*
|
|
9858
|
-
*
|
|
10283
|
+
* (`SpreadAttr.propsObject`, #3688): the `'props'` case of
|
|
10284
|
+
* `SpreadAttr.callerBag` (`callerBagOfSpread`). A `{...rest}` bag leaves out
|
|
10285
|
+
* the keys the component destructured, so it does not merge as the caller's
|
|
10286
|
+
* props.
|
|
9859
10287
|
*/
|
|
9860
|
-
function markPropsObjectSpread<T extends { value: AttrValue }>(prop: T
|
|
10288
|
+
function markPropsObjectSpread<T extends { value: AttrValue }>(prop: T): T {
|
|
9861
10289
|
const value = prop.value
|
|
9862
|
-
if (value.kind !== 'spread') return prop
|
|
9863
|
-
|
|
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
|
|
10290
|
+
if (value.kind !== 'spread' || value.callerBag !== 'props') return prop
|
|
10291
|
+
return { ...prop, value: { ...value, propsObject: true } }
|
|
9870
10292
|
}
|
|
9871
10293
|
|
|
9872
10294
|
/** `ctx.analyzer.localConstants` indexed by name → value text, memoized per component walk. */
|