@barefootjs/jsx 0.39.7 → 0.39.9
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/callback-method.d.ts +36 -7
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/component-props.d.ts +80 -13
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +153 -0
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -0
- package/dist/adapters/jsx-adapter.d.ts +4 -0
- package/dist/adapters/jsx-adapter.d.ts.map +1 -1
- package/dist/adapters/literal-value.d.ts +2 -2
- package/dist/adapters/literal-value.d.ts.map +1 -1
- package/dist/adapters/loop-plan.d.ts +133 -9
- package/dist/adapters/loop-plan.d.ts.map +1 -1
- package/dist/adapters/parsed-expr-emitter.d.ts +44 -0
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/adapters/style-object.d.ts +33 -0
- package/dist/adapters/style-object.d.ts.map +1 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/assignment-target.d.ts +26 -0
- package/dist/assignment-target.d.ts.map +1 -0
- package/dist/children-nullish.d.ts +22 -0
- package/dist/children-nullish.d.ts.map +1 -0
- package/dist/errors.d.ts +2 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/expression-parser.d.ts +9 -19
- package/dist/expression-parser.d.ts.map +1 -1
- package/dist/free-refs.d.ts +6 -0
- package/dist/free-refs.d.ts.map +1 -1
- package/dist/index.d.ts +17 -14
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15040 -13899
- package/dist/ir-to-client-js/compute-inlinability.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/event-collision.d.ts +2 -1
- package/dist/ir-to-client-js/control-flow/plan/event-collision.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/insert.d.ts +2 -2
- package/dist/ir-to-client-js/control-flow/plan/insert.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +24 -1
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts +16 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts +6 -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-delegation.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts +10 -9
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts.map +1 -1
- package/dist/ir-to-client-js/csr-substitute.d.ts +13 -2
- package/dist/ir-to-client-js/csr-substitute.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-reactive.d.ts +1 -1
- package/dist/ir-to-client-js/emit-reactive.d.ts.map +1 -1
- package/dist/ir-to-client-js/emit-registration.d.ts +10 -8
- package/dist/ir-to-client-js/emit-registration.d.ts.map +1 -1
- package/dist/ir-to-client-js/generate-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts +0 -9
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/identifiers.d.ts +6 -8
- package/dist/ir-to-client-js/identifiers.d.ts.map +1 -1
- package/dist/ir-to-client-js/imports.d.ts +23 -2
- package/dist/ir-to-client-js/imports.d.ts.map +1 -1
- package/dist/ir-to-client-js/phases/props-extraction.d.ts +6 -3
- package/dist/ir-to-client-js/phases/props-extraction.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +8 -0
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts +1 -1
- package/dist/ir-to-client-js/prune-unused-prop-extractions.d.ts.map +1 -1
- package/dist/ir-to-client-js/reactivity.d.ts.map +1 -1
- package/dist/ir-to-client-js/rewrite-destructured-props.d.ts +1 -1
- package/dist/ir-to-client-js/rewrite-destructured-props.d.ts.map +1 -1
- package/dist/ir-to-client-js/rewrite-props-object.d.ts.map +1 -1
- package/dist/ir-to-client-js/safe-html.d.ts +20 -2
- 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 +1 -0
- 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 +3 -3
- package/dist/ir-to-client-js/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +2 -11
- package/dist/ir-to-client-js/utils.d.ts.map +1 -1
- package/dist/jsx-to-ir.d.ts.map +1 -1
- package/dist/nullable-signal-attr.d.ts +40 -10
- package/dist/nullable-signal-attr.d.ts.map +1 -1
- package/dist/prop-rewrite.d.ts +35 -8
- package/dist/prop-rewrite.d.ts.map +1 -1
- package/dist/props-binding.d.ts +66 -1
- package/dist/props-binding.d.ts.map +1 -1
- package/dist/scanner/js-scanner.d.ts.map +1 -1
- package/dist/scanner/js-scanner.js +30 -2
- package/dist/scope/binding-scope.d.ts +30 -2
- package/dist/scope/binding-scope.d.ts.map +1 -1
- package/dist/scope/lexical-scope-walk.d.ts +26 -0
- package/dist/scope/lexical-scope-walk.d.ts.map +1 -0
- package/dist/signal-prop-name-collision.d.ts +6 -5
- package/dist/signal-prop-name-collision.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts +28 -0
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +39 -14
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/types.d.ts +133 -19
- package/dist/types.d.ts.map +1 -1
- package/dist/value-references.d.ts +21 -0
- package/dist/value-references.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +18 -18
- package/src/__tests__/adapter-output.test.ts +5 -3
- package/src/__tests__/analyzer.test.ts +33 -0
- package/src/__tests__/arithmetic-lowering.test.ts +48 -0
- package/src/__tests__/binding-scope.test.ts +29 -0
- package/src/__tests__/children-read.test.ts +226 -0
- package/src/__tests__/client-template-escape-soundness.test.ts +31 -1
- package/src/__tests__/component-props.test.ts +68 -1
- package/src/__tests__/constant-inlining-tree.test.ts +136 -0
- package/src/__tests__/create-query.test.ts +1 -1
- package/src/__tests__/destructure-default-reads-earlier-param.test.ts +138 -0
- package/src/__tests__/dsl-template-adapter.test.ts +173 -0
- package/src/__tests__/emit-to-string-call.test.ts +58 -0
- package/src/__tests__/event-handler-verbatim.test.ts +73 -0
- package/src/__tests__/expression-parser.test.ts +1 -44
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +90 -31
- package/src/__tests__/loop-plan.test.ts +131 -1
- package/src/__tests__/loop-row-child-children-handler.test.ts +129 -0
- package/src/__tests__/loop-row-child-children-inner-loop-shadow.test.ts +74 -0
- package/src/__tests__/loop-row-child-children-ref.test.ts +117 -0
- package/src/__tests__/nullable-signal-attr.test.ts +67 -3
- package/src/__tests__/props-alias-const.test.ts +60 -1
- package/src/__tests__/props-named-callback-param.test.ts +256 -0
- package/src/__tests__/reference-graph-identifiers.test.ts +161 -0
- package/src/__tests__/renamed-body-destructure-template.test.ts +86 -0
- package/src/__tests__/rewrite-props-object.test.ts +90 -0
- package/src/__tests__/runtime-import-detection.test.ts +134 -0
- package/src/__tests__/signal-prop-name-collision.test.ts +30 -0
- package/src/__tests__/ssr-defaults.test.ts +19 -1
- package/src/__tests__/ssr-seed-plan.test.ts +119 -0
- package/src/__tests__/style-object-plan.test.ts +54 -0
- package/src/__tests__/written-binding-substitution.test.ts +430 -0
- package/src/__tests__/zero-param-helper-inline.test.ts +57 -0
- package/src/adapters/callback-method.ts +48 -10
- package/src/adapters/component-props.ts +130 -12
- package/src/adapters/dsl-template-adapter.ts +259 -0
- package/src/adapters/jsx-adapter.ts +12 -6
- package/src/adapters/literal-value.ts +2 -2
- package/src/adapters/loop-plan.ts +201 -13
- package/src/adapters/parsed-expr-emitter.ts +67 -0
- package/src/adapters/style-object.ts +61 -0
- package/src/analyzer.ts +396 -16
- package/src/assignment-target.ts +79 -0
- package/src/children-nullish.ts +33 -0
- package/src/debug.ts +2 -2
- package/src/errors.ts +23 -0
- package/src/expression-parser.ts +56 -53
- package/src/free-refs.ts +28 -17
- package/src/index.ts +31 -10
- package/src/ir-to-client-js/compute-inlinability.ts +35 -5
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +21 -5
- package/src/ir-to-client-js/control-flow/plan/event-collision.ts +2 -1
- package/src/ir-to-client-js/control-flow/plan/insert.ts +2 -2
- package/src/ir-to-client-js/control-flow/plan/loop.ts +25 -1
- package/src/ir-to-client-js/control-flow/shared.ts +41 -3
- package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +23 -3
- package/src/ir-to-client-js/control-flow/stringify/event-delegation.ts +3 -2
- package/src/ir-to-client-js/control-flow/stringify/event-listener.ts +13 -12
- package/src/ir-to-client-js/csr-substitute.ts +82 -24
- package/src/ir-to-client-js/emit-reactive.ts +3 -3
- package/src/ir-to-client-js/emit-registration.ts +34 -58
- package/src/ir-to-client-js/generate-init.ts +17 -1
- package/src/ir-to-client-js/html-template.ts +48 -57
- package/src/ir-to-client-js/identifiers.ts +75 -24
- package/src/ir-to-client-js/imports.ts +56 -19
- package/src/ir-to-client-js/index.ts +2 -1
- package/src/ir-to-client-js/phases/event-handlers.ts +2 -2
- package/src/ir-to-client-js/phases/props-extraction.ts +39 -17
- package/src/ir-to-client-js/plan/build-declaration-emit.ts +2 -2
- package/src/ir-to-client-js/plan/build-static-array-child-init.ts +7 -0
- package/src/ir-to-client-js/plan/static-array-child-init.ts +8 -0
- package/src/ir-to-client-js/prune-unused-prop-extractions.ts +7 -4
- package/src/ir-to-client-js/reactivity.ts +2 -2
- package/src/ir-to-client-js/rewrite-destructured-props.ts +22 -5
- package/src/ir-to-client-js/rewrite-props-object.ts +14 -8
- package/src/ir-to-client-js/safe-html.ts +29 -35
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +4 -1
- package/src/ir-to-client-js/types.ts +3 -2
- package/src/ir-to-client-js/utils.ts +42 -122
- package/src/jsx-to-ir.ts +310 -90
- package/src/nullable-signal-attr.ts +149 -33
- package/src/prop-rewrite.ts +72 -72
- package/src/props-binding.ts +90 -3
- package/src/scanner/__tests__/js-scanner.test.ts +37 -0
- package/src/scanner/js-scanner.ts +64 -4
- package/src/scope/binding-scope.ts +34 -3
- package/src/scope/lexical-scope-walk.ts +167 -0
- package/src/signal-prop-name-collision.ts +50 -24
- package/src/ssr-defaults.ts +47 -20
- package/src/ssr-seed-plan.ts +103 -23
- package/src/types.ts +140 -20
- package/src/value-references.ts +132 -0
package/src/jsx-to-ir.ts
CHANGED
|
@@ -33,15 +33,18 @@ import {
|
|
|
33
33
|
type PreambleRegionSource,
|
|
34
34
|
type PreambleValueDeclaration,
|
|
35
35
|
tsxSourceText, type TsxSourceText,
|
|
36
|
+
type TypedExprSource,
|
|
36
37
|
type SourceLocation,
|
|
37
38
|
type TypeInfo,
|
|
38
39
|
type OriginInfo,
|
|
39
40
|
type IRMetadata,
|
|
40
41
|
isReactiveOrigin,
|
|
42
|
+
isChildrenPropParam,
|
|
41
43
|
AttrValueOf,
|
|
42
44
|
} from './types.ts'
|
|
43
45
|
import { type AnalyzerContext, type MultiReturnJsxInfo, getSourceLocation, collectReactiveGetterNames, tsTypeToTypeInfo } from './analyzer-context.ts'
|
|
44
46
|
import { parseExpression, isSupported, parseBlockBody, foldBlockToExpr, predicateTernaryToLogical, tsNodeToParsedExpr, sortComparatorFromArrow, stringifyParsedExpr, cssKebabCase, CALLBACK_METHODS, unboundCallbackParamRead, unboundCallbackParamReason, type ParsedExpr } from './expression-parser.ts'
|
|
47
|
+
import { fallbackChainParts, nullishChainParts } from './children-nullish.ts'
|
|
45
48
|
import type { IRLoopSort, FunctionInfo, ConstantInfo, ParamInfo, FreeReference } from './types.ts'
|
|
46
49
|
import { formatParamWithType } from './module-exports.ts'
|
|
47
50
|
import { createError, ErrorCodes, internalInvariant } from './errors.ts'
|
|
@@ -52,7 +55,7 @@ import {
|
|
|
52
55
|
collectAstPropRefs,
|
|
53
56
|
rewriteScopedValueRefs,
|
|
54
57
|
} from './prop-rewrite.ts'
|
|
55
|
-
import { boundPropLocalNames, buildPropAliasMap, isPropsAliasConst, resolveAliasOrigin, resolveRestSpreadOriginCore, livePropReadExpr } from './props-binding.ts'
|
|
58
|
+
import { boundPropLocalNames, buildPropAliasMap, isPropsAliasConst, isRenamedBodyPropParam, keepsDeclaredValue, resolveAliasOrigin, resolveRestSpreadOriginCore, livePropReadExpr } from './props-binding.ts'
|
|
56
59
|
import { PROPS_PARAM } from './ir-to-client-js/utils.ts'
|
|
57
60
|
import { walkIR } from './ir-to-client-js/walker.ts'
|
|
58
61
|
import { resolveFreeRefs, rebindHelperParamRefs, isNameBound as isNameBoundInEnv, type BindingEnvironment } from './free-refs.ts'
|
|
@@ -64,7 +67,7 @@ import { toLocaleDatePlugin, foldedArgToClientJs } from './to-locale-date-loweri
|
|
|
64
67
|
import type { LoweringMatcher } from './lowering-registry.ts'
|
|
65
68
|
import { extractFreeIdentifiersFromNode, initializerShapeContainsJsx, extractMultiReturnJsxBranches, rootIdentifierOf, type MultiReturnJsxBranches } from './analyzer.ts'
|
|
66
69
|
import { iterateJsTokens, replaceInExprContexts } from './scanner/js-scanner.ts'
|
|
67
|
-
import { reconstructAsSegments } from './strip-types.ts'
|
|
70
|
+
import { reconstructAsSegments, reconstructWithoutTypes } from './strip-types.ts'
|
|
68
71
|
import { templatePartsToJsExpr } from './template-parts.ts'
|
|
69
72
|
import { toHTMLAttrName, decodeEntities, BF_KEY, keyAttrName } from '@barefootjs/shared'
|
|
70
73
|
import { BindingScope } from './scope/binding-scope.ts'
|
|
@@ -742,17 +745,7 @@ function collectBranchLocalPropRefsViaSubstitution(
|
|
|
742
745
|
*/
|
|
743
746
|
function getDestructuredPropNames(ctx: TransformContext): Set<string> | null {
|
|
744
747
|
if (ctx._destructuredPropNames === undefined) {
|
|
745
|
-
const shadowed =
|
|
746
|
-
if (ctx.analyzer.propsObjectName) {
|
|
747
|
-
for (const s of ctx.analyzer.signals) {
|
|
748
|
-
shadowed.add(s.getter)
|
|
749
|
-
if (s.setter) shadowed.add(s.setter)
|
|
750
|
-
}
|
|
751
|
-
for (const m of ctx.analyzer.memos) shadowed.add(m.name)
|
|
752
|
-
for (const c of ctx.analyzer.localConstants) {
|
|
753
|
-
if (!isPropsAliasConst(c, ctx.analyzer.propsObjectName)) shadowed.add(c.name)
|
|
754
|
-
}
|
|
755
|
-
}
|
|
748
|
+
const shadowed = propShadowingNames(ctx.analyzer)
|
|
756
749
|
const eligible = ctx.analyzer.propsParams.filter(p => !shadowed.has(p.name))
|
|
757
750
|
const names = eligible.map(p => p.name)
|
|
758
751
|
ctx._destructuredPropNames = names.length > 0 ? new Set(names) : null
|
|
@@ -769,6 +762,35 @@ function getDestructuredPropNames(ctx: TransformContext): Set<string> | null {
|
|
|
769
762
|
return ctx._destructuredPropNames ?? null
|
|
770
763
|
}
|
|
771
764
|
|
|
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
|
+
|
|
772
794
|
/**
|
|
773
795
|
* Companion to `getDestructuredPropNames`: local-name → `ParamInfo` for the
|
|
774
796
|
* same shadow-filtered eligible set. Both caches are written in one pass.
|
|
@@ -795,7 +817,7 @@ export function createTransformContext(analyzer: AnalyzerContext): TransformCont
|
|
|
795
817
|
// Shared by `patterns.props` (regex matching) and `boundPropNames`
|
|
796
818
|
// (exact-name binding checks, #2835) so both agree on which prop
|
|
797
819
|
// params are in play — `children` excluded from both the same way.
|
|
798
|
-
const propsParamsForMatching = analyzer.propsParams.filter(p => p
|
|
820
|
+
const propsParamsForMatching = analyzer.propsParams.filter(p => !isChildrenPropParam(p))
|
|
799
821
|
return {
|
|
800
822
|
analyzer,
|
|
801
823
|
sourceFile: analyzer.sourceFile,
|
|
@@ -965,6 +987,7 @@ function makeBindingEnv(ctx: TransformContext): BindingEnvironment {
|
|
|
965
987
|
memos: a.memos,
|
|
966
988
|
propsParams: a.propsParams,
|
|
967
989
|
propsObjectName: a.propsObjectName,
|
|
990
|
+
propsObjectAliases: propsObjectAliasNames(a, ctx.scope),
|
|
968
991
|
restPropsName: a.restPropsName,
|
|
969
992
|
localConstants: a.localConstants,
|
|
970
993
|
localFunctions: a.localFunctions,
|
|
@@ -1000,8 +1023,193 @@ function parseValueExpr(trimmed: string): ParsedExpr {
|
|
|
1000
1023
|
return parseExpression(trimmed.startsWith('{') ? `(${trimmed})` : trimmed)
|
|
1001
1024
|
}
|
|
1002
1025
|
|
|
1003
|
-
|
|
1004
|
-
const
|
|
1026
|
+
const childrenHopCache = new WeakMap<AnalyzerContext, ReadonlyMap<string, ParsedExpr | undefined>>()
|
|
1027
|
+
const childrenAliasHopCache = new WeakMap<AnalyzerContext, ReadonlyMap<string, string | undefined>>()
|
|
1028
|
+
|
|
1029
|
+
/**
|
|
1030
|
+
* The component-scope consts a read of the children prop may hop through,
|
|
1031
|
+
* by name, with their parsed initializers: a `const` that is not module
|
|
1032
|
+
* scope and is not mutated after its declaration, so it holds the one value
|
|
1033
|
+
* it was declared with (`const c = children`, `const p = props`,
|
|
1034
|
+
* `const { children: kids } = props`).
|
|
1035
|
+
*/
|
|
1036
|
+
function childrenHopConstants(analyzer: AnalyzerContext): ReadonlyMap<string, ParsedExpr | undefined> {
|
|
1037
|
+
let consts = childrenHopCache.get(analyzer)
|
|
1038
|
+
if (!consts) {
|
|
1039
|
+
const map = new Map<string, ParsedExpr | undefined>()
|
|
1040
|
+
for (const c of analyzer.localConstants) {
|
|
1041
|
+
if (c.isModule || c.declarationKind !== 'const' || c.mutatedAfterDeclaration || map.has(c.name)) continue
|
|
1042
|
+
map.set(c.name, c.parsed)
|
|
1043
|
+
}
|
|
1044
|
+
consts = map
|
|
1045
|
+
childrenHopCache.set(analyzer, consts)
|
|
1046
|
+
}
|
|
1047
|
+
return consts
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
/**
|
|
1051
|
+
* {@link childrenHopConstants} as the identifier hops `resolveAliasOrigin`
|
|
1052
|
+
* walks (`const c = children` → `children`). Every hop is a component-scope
|
|
1053
|
+
* initializer, so it resolves in the component scope: a loop row that
|
|
1054
|
+
* shadows a name at a use site does not shadow it inside an outer const's
|
|
1055
|
+
* initializer (`const c = props.children` read in
|
|
1056
|
+
* `rows.map(props => …{c}…)` still reads the props object).
|
|
1057
|
+
*/
|
|
1058
|
+
function childrenAliasHops(analyzer: AnalyzerContext): ReadonlyMap<string, string | undefined> {
|
|
1059
|
+
let hops = childrenAliasHopCache.get(analyzer)
|
|
1060
|
+
if (!hops) {
|
|
1061
|
+
const map = new Map<string, string | undefined>()
|
|
1062
|
+
for (const [name, parsed] of childrenHopConstants(analyzer)) {
|
|
1063
|
+
map.set(name, parsed?.kind === 'identifier' ? parsed.name : undefined)
|
|
1064
|
+
}
|
|
1065
|
+
hops = map
|
|
1066
|
+
childrenAliasHopCache.set(analyzer, hops)
|
|
1067
|
+
}
|
|
1068
|
+
return hops
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
/** Whether the component-scope name `name` holds the props object: the props param, or a const alias of it (`const p = props`). */
|
|
1072
|
+
function isPropsObjectBinding(name: string, analyzer: AnalyzerContext): boolean {
|
|
1073
|
+
const propsObjectName = analyzer.propsObjectName
|
|
1074
|
+
if (propsObjectName === null) return false
|
|
1075
|
+
return resolveAliasOrigin(childrenAliasHops(analyzer), name, n => (n === propsObjectName ? true : null)) === true
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
/**
|
|
1079
|
+
* The component-scope consts that alias the props object (`const p =
|
|
1080
|
+
* props`), for the free-reference walk's `p.children` exemption
|
|
1081
|
+
* (`BindingEnvironment.propsObjectAliases`). Leaves out a name the
|
|
1082
|
+
* enclosing loop rows bind at the use site.
|
|
1083
|
+
*/
|
|
1084
|
+
function propsObjectAliasNames(analyzer: AnalyzerContext, scope: BindingScope): ReadonlySet<string> {
|
|
1085
|
+
const names = new Set<string>()
|
|
1086
|
+
if (analyzer.propsObjectName === null) return names
|
|
1087
|
+
for (const name of childrenHopConstants(analyzer).keys()) {
|
|
1088
|
+
if (!scope.isBound(name) && isPropsObjectBinding(name, analyzer)) names.add(name)
|
|
1089
|
+
}
|
|
1090
|
+
return names
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
/**
|
|
1094
|
+
* Whether `parsed`, read in the component scope, is `<props object>.children`
|
|
1095
|
+
* (`props.children`, `p.children` for `const p = props`).
|
|
1096
|
+
*/
|
|
1097
|
+
function isPropsChildrenMember(parsed: ParsedExpr | undefined, analyzer: AnalyzerContext): boolean {
|
|
1098
|
+
return (
|
|
1099
|
+
parsed?.kind === 'member' &&
|
|
1100
|
+
!parsed.computed &&
|
|
1101
|
+
parsed.property === 'children' &&
|
|
1102
|
+
parsed.object.kind === 'identifier' &&
|
|
1103
|
+
isPropsObjectBinding(parsed.object.name, analyzer)
|
|
1104
|
+
)
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
/**
|
|
1108
|
+
* Whether the component-scope name `name` holds the component's own
|
|
1109
|
+
* children prop with no default: the props param that binds it, renamed or
|
|
1110
|
+
* not (`{ children }`, `{ children: kids }`, `isChildrenPropParam`), a body
|
|
1111
|
+
* destructure or member read of it (`const { children: kids } = props`,
|
|
1112
|
+
* `const kids = p.children`), or a const alias hop of any of these
|
|
1113
|
+
* (`const c = children`, #3572). A use site checks its own shadowing first
|
|
1114
|
+
* (`childrenReadOf`); the hops and initializers resolve in the component
|
|
1115
|
+
* scope they were declared in.
|
|
1116
|
+
*/
|
|
1117
|
+
function isChildrenBinding(name: string, analyzer: AnalyzerContext): boolean {
|
|
1118
|
+
const consts = childrenHopConstants(analyzer)
|
|
1119
|
+
const terminal = (n: string): true | null => {
|
|
1120
|
+
if (consts.has(n)) return isPropsChildrenMember(consts.get(n), analyzer) ? true : null
|
|
1121
|
+
return analyzer.propsParams.some(p => p.name === n && isChildrenPropParam(p)) ? true : null
|
|
1122
|
+
}
|
|
1123
|
+
return resolveAliasOrigin(childrenAliasHops(analyzer), name, terminal) === true
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
/**
|
|
1127
|
+
* Whether the expression text reads nothing but the component's own
|
|
1128
|
+
* children prop (`childrenReadOf`: bare, or under `?? ''`). That read is
|
|
1129
|
+
* content the parent renders, never a reactive value, so it takes no text
|
|
1130
|
+
* slot — even where the regex reactivity classifier would match a local
|
|
1131
|
+
* that shares the prop's name (`const { children: kids, label: children } =
|
|
1132
|
+
* props`, where `kids` holds `props.children`, #3572).
|
|
1133
|
+
*/
|
|
1134
|
+
function isWholeChildrenRead(exprText: string, ctx: TransformContext): boolean {
|
|
1135
|
+
const read = childrenReadOf(parseExpression(exprText.trim()), ctx.analyzer, ctx.scope)
|
|
1136
|
+
return read === 'bare' || read === 'nullish-empty'
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
/** Whether a text-position expression is reactive: not a whole children read (`isWholeChildrenRead`), and reactive by the regex/checker or origin classifier. */
|
|
1140
|
+
function isReactiveTextRead(exprText: string, ctx: TransformContext, expr: ts.Expression, origin: OriginInfo): boolean {
|
|
1141
|
+
if (isWholeChildrenRead(exprText, ctx)) return false
|
|
1142
|
+
return isReactiveExpression(exprText, ctx, expr) || isReactiveOrigin(origin)
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
/** `''`, or a `??` chain of `''` (`'' ?? ''`). */
|
|
1146
|
+
function isEmptyStringFallback(fallback: ParsedExpr): boolean {
|
|
1147
|
+
if (fallback.kind === 'logical' && fallback.op === '??') {
|
|
1148
|
+
return isEmptyStringFallback(fallback.left) && isEmptyStringFallback(fallback.right)
|
|
1149
|
+
}
|
|
1150
|
+
return fallback.kind === 'literal' && fallback.value === ''
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
/**
|
|
1154
|
+
* How `parsed` reads the component's own `children` prop
|
|
1155
|
+
* (`IRExpression.childrenRead`, #3539): a name holding the prop
|
|
1156
|
+
* (`isChildrenBinding`: `children`, a renamed `{ children: kids }`, a body
|
|
1157
|
+
* alias, or a const alias hop of one, #3572) or `<propsObject>.children`
|
|
1158
|
+
* (through a const alias of the props object too), that read on the left of
|
|
1159
|
+
* `?? ''`, on the left of another `?? fallback`, or on the left of
|
|
1160
|
+
* `|| fallback` (#3554). A chained
|
|
1161
|
+
* `(children ?? '<b>x</b>') ?? ''` reads as `children ?? ('<b>x</b>' ?? '')`
|
|
1162
|
+
* (`nullishChainParts`), so its inner fallback stays escaped text. A name the enclosing loop rows bind
|
|
1163
|
+
* (item, index, destructured names and preamble locals, through `scope`)
|
|
1164
|
+
* shadows the prop; a component-body `const children` cannot coexist with a
|
|
1165
|
+
* destructured `children` param, so the destructure is the binding when it
|
|
1166
|
+
* lists the name.
|
|
1167
|
+
*/
|
|
1168
|
+
function childrenReadOf(
|
|
1169
|
+
parsed: ParsedExpr,
|
|
1170
|
+
analyzer: AnalyzerContext,
|
|
1171
|
+
scope: BindingScope,
|
|
1172
|
+
): IRExpression['childrenRead'] | null {
|
|
1173
|
+
const chain = nullishChainParts(parsed)
|
|
1174
|
+
if (chain) {
|
|
1175
|
+
if (childrenReadOf(chain.head, analyzer, scope) !== 'bare') return null
|
|
1176
|
+
return isEmptyStringFallback(chain.fallback) ? 'nullish-empty' : 'nullish-fallback'
|
|
1177
|
+
}
|
|
1178
|
+
const orChain = fallbackChainParts(parsed, '||')
|
|
1179
|
+
if (orChain) return childrenReadOf(orChain.head, analyzer, scope) === 'bare' ? 'or-fallback' : null
|
|
1180
|
+
// Shadowing is checked once, for the name the expression itself reads.
|
|
1181
|
+
if (parsed.kind === 'identifier') {
|
|
1182
|
+
return !scope.isBound(parsed.name) && isChildrenBinding(parsed.name, analyzer) ? 'bare' : null
|
|
1183
|
+
}
|
|
1184
|
+
const readsProps =
|
|
1185
|
+
parsed.kind === 'member' && parsed.object.kind === 'identifier' && !scope.isBound(parsed.object.name)
|
|
1186
|
+
return readsProps && isPropsChildrenMember(parsed, analyzer) ? 'bare' : null
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
/**
|
|
1190
|
+
* Attach `parsed` to every expression-valued attribute / prop so adapters can
|
|
1191
|
+
* lower from the tree instead of re-parsing the string. Element attrs,
|
|
1192
|
+
* component props (e.g. `opts={{ … }}` → Go map), and a provider's `value`
|
|
1193
|
+
* prop all carry it; only `expression` values do (a `spread` / `template`
|
|
1194
|
+
* value can't be the inline object literal the consumers read).
|
|
1195
|
+
*/
|
|
1196
|
+
function attachParsedAttrValues(
|
|
1197
|
+
node: IRNode,
|
|
1198
|
+
parse: (trimmed: string) => ParsedExpr,
|
|
1199
|
+
parseValue: (trimmed: string) => ParsedExpr,
|
|
1200
|
+
): void {
|
|
1201
|
+
const attach = (value: { kind: string; expr?: string; parsed?: ParsedExpr }, spreads: boolean): void => {
|
|
1202
|
+
const trimmed = value.kind === 'expression' || (spreads && value.kind === 'spread') ? value.expr?.trim() : undefined
|
|
1203
|
+
if (trimmed) value.parsed = value.kind === 'spread' ? parse(trimmed) : parseValue(trimmed)
|
|
1204
|
+
}
|
|
1205
|
+
if (node.type === 'element') {
|
|
1206
|
+
for (const attr of node.attrs) attach(attr.value, true)
|
|
1207
|
+
} else if (node.type === 'component') {
|
|
1208
|
+
for (const prop of node.props) attach(prop.value, false)
|
|
1209
|
+
} else if (node.type === 'provider') {
|
|
1210
|
+
attach(node.valueProp.value, false)
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1005
1213
|
|
|
1006
1214
|
/**
|
|
1007
1215
|
* Attach `parsed` (`parseExpression(expr.trim())`) to every `expression` node
|
|
@@ -1014,59 +1222,35 @@ const EMPTY_BOUND: ReadonlySet<string> = new Set()
|
|
|
1014
1222
|
* (#2206) so a bare-identifier `.map`/`.filter`/… callback (`tags.map(format)`)
|
|
1015
1223
|
* resolves to its declaration wherever it can appear — text/attr/prop/provider
|
|
1016
1224
|
* expressions and the loop-array expression alike — not just one hardcoded
|
|
1017
|
-
* spot. `
|
|
1018
|
-
*
|
|
1019
|
-
*
|
|
1020
|
-
*
|
|
1225
|
+
* spot. `scope` carries the enclosing loop rows' bindings down into that
|
|
1226
|
+
* resolution (Fable review, #2214) so a loop item variable that happens to
|
|
1227
|
+
* share a name with a module-scope const/function shadows it, same as real
|
|
1228
|
+
* JS scoping — resolution stays off within that loop's body.
|
|
1021
1229
|
*/
|
|
1022
|
-
function attachParsedExpressions(node: IRNode, analyzer: AnalyzerContext,
|
|
1230
|
+
function attachParsedExpressions(node: IRNode, analyzer: AnalyzerContext, scope: BindingScope = BindingScope.EMPTY): void {
|
|
1231
|
+
const bound = scope.boundNames()
|
|
1023
1232
|
const parse = (trimmed: string) => resolveCallbackMethodFunctionReferences(parseExpression(trimmed), analyzer, bound)
|
|
1024
1233
|
const parseValue = (trimmed: string) => resolveCallbackMethodFunctionReferences(parseValueExpr(trimmed), analyzer, bound)
|
|
1025
1234
|
if (node.type === 'expression') {
|
|
1026
1235
|
const trimmed = node.expr.trim()
|
|
1027
1236
|
if (trimmed) node.parsed = parse(trimmed)
|
|
1237
|
+
const childrenRead = node.parsed ? childrenReadOf(node.parsed, analyzer, scope) : null
|
|
1238
|
+
if (childrenRead) node.childrenRead = childrenRead
|
|
1028
1239
|
} else if (node.type === 'conditional' || node.type === 'if-statement') {
|
|
1029
1240
|
const trimmed = node.condition.trim()
|
|
1030
1241
|
if (trimmed) node.parsedCondition = parse(trimmed)
|
|
1031
1242
|
}
|
|
1032
|
-
|
|
1033
|
-
// lower from the tree instead of re-parsing the string. Element attrs,
|
|
1034
|
-
// component props (e.g. `opts={{ … }}` → Go map), and a provider's `value`
|
|
1035
|
-
// prop all carry it; only `expression` values do (a `spread` / `template`
|
|
1036
|
-
// value can't be the inline object literal the consumers read).
|
|
1037
|
-
if (node.type === 'element') {
|
|
1038
|
-
for (const attr of node.attrs) {
|
|
1039
|
-
if (attr.value.kind === 'expression') {
|
|
1040
|
-
const trimmed = attr.value.expr.trim()
|
|
1041
|
-
if (trimmed) attr.value.parsed = parseValue(trimmed)
|
|
1042
|
-
} else if (attr.value.kind === 'spread') {
|
|
1043
|
-
const trimmed = attr.value.expr.trim()
|
|
1044
|
-
if (trimmed) attr.value.parsed = parse(trimmed)
|
|
1045
|
-
}
|
|
1046
|
-
}
|
|
1047
|
-
} else if (node.type === 'component') {
|
|
1048
|
-
for (const prop of node.props) {
|
|
1049
|
-
if (prop.value.kind === 'expression') {
|
|
1050
|
-
const trimmed = prop.value.expr.trim()
|
|
1051
|
-
if (trimmed) prop.value.parsed = parseValue(trimmed)
|
|
1052
|
-
}
|
|
1053
|
-
}
|
|
1054
|
-
} else if (node.type === 'provider') {
|
|
1055
|
-
if (node.valueProp.value.kind === 'expression') {
|
|
1056
|
-
const trimmed = node.valueProp.value.expr.trim()
|
|
1057
|
-
if (trimmed) node.valueProp.value.parsed = parseValue(trimmed)
|
|
1058
|
-
}
|
|
1059
|
-
}
|
|
1243
|
+
attachParsedAttrValues(node, parse, parseValue)
|
|
1060
1244
|
switch (node.type) {
|
|
1061
1245
|
case 'element':
|
|
1062
1246
|
case 'component':
|
|
1063
1247
|
case 'fragment':
|
|
1064
1248
|
case 'provider':
|
|
1065
|
-
for (const child of node.children) attachParsedExpressions(child, analyzer,
|
|
1249
|
+
for (const child of node.children) attachParsedExpressions(child, analyzer, scope)
|
|
1066
1250
|
break
|
|
1067
1251
|
case 'async':
|
|
1068
|
-
attachParsedExpressions(node.fallback, analyzer,
|
|
1069
|
-
for (const child of node.children) attachParsedExpressions(child, analyzer,
|
|
1252
|
+
attachParsedExpressions(node.fallback, analyzer, scope)
|
|
1253
|
+
for (const child of node.children) attachParsedExpressions(child, analyzer, scope)
|
|
1070
1254
|
break
|
|
1071
1255
|
case 'loop': {
|
|
1072
1256
|
// Attach the parse of the SAME `array` string the adapters consume
|
|
@@ -1077,36 +1261,35 @@ function attachParsedExpressions(node: IRNode, analyzer: AnalyzerContext, bound:
|
|
|
1077
1261
|
// own `param`/`index` exist), so it parses against the unextended `bound`.
|
|
1078
1262
|
const trimmedArray = node.array.trim()
|
|
1079
1263
|
if (trimmedArray) node.arrayParsed = parse(trimmedArray)
|
|
1080
|
-
// The loop's item
|
|
1081
|
-
//
|
|
1264
|
+
// The loop row's bindings (item or destructured names, index, preamble
|
|
1265
|
+
// locals) shadow any same-named module-scope const/function and the
|
|
1266
|
+
// children prop for everything inside the loop body (Fable review,
|
|
1082
1267
|
// #2214) — mirrors an arrow's own params in `resolveCallbackMethodFunctionReferences`.
|
|
1083
|
-
const
|
|
1084
|
-
|
|
1085
|
-
if (node.index) loopBound.add(node.index)
|
|
1086
|
-
for (const child of node.children) attachParsedExpressions(child, analyzer, loopBound)
|
|
1268
|
+
const loopScope = scope.enterLoopRow(node)
|
|
1269
|
+
for (const child of node.children) attachParsedExpressions(child, analyzer, loopScope)
|
|
1087
1270
|
// Loops also hold expression nodes off the main `children` array.
|
|
1088
1271
|
if (node.childComponent) {
|
|
1089
|
-
for (const child of node.childComponent.children) attachParsedExpressions(child, analyzer,
|
|
1272
|
+
for (const child of node.childComponent.children) attachParsedExpressions(child, analyzer, loopScope)
|
|
1090
1273
|
}
|
|
1091
1274
|
for (const nested of node.nestedComponents ?? []) {
|
|
1092
|
-
for (const child of nested.children) attachParsedExpressions(child, analyzer,
|
|
1275
|
+
for (const child of nested.children) attachParsedExpressions(child, analyzer, loopScope)
|
|
1093
1276
|
}
|
|
1094
1277
|
for (const seg of node.flatMapCallback?.segments ?? []) {
|
|
1095
|
-
if (seg.kind === 'jsx') attachParsedExpressions(seg.ir, analyzer,
|
|
1278
|
+
if (seg.kind === 'jsx') attachParsedExpressions(seg.ir, analyzer, loopScope)
|
|
1096
1279
|
}
|
|
1097
1280
|
// Preamble leaves (array-builder bodies) are nested IR the same way.
|
|
1098
1281
|
for (const seg of node.preamble?.segments ?? []) {
|
|
1099
|
-
if (seg.kind === 'jsx') attachParsedExpressions(seg.ir, analyzer,
|
|
1282
|
+
if (seg.kind === 'jsx') attachParsedExpressions(seg.ir, analyzer, loopScope)
|
|
1100
1283
|
}
|
|
1101
1284
|
break
|
|
1102
1285
|
}
|
|
1103
1286
|
case 'conditional':
|
|
1104
|
-
attachParsedExpressions(node.whenTrue, analyzer,
|
|
1105
|
-
attachParsedExpressions(node.whenFalse, analyzer,
|
|
1287
|
+
attachParsedExpressions(node.whenTrue, analyzer, scope)
|
|
1288
|
+
attachParsedExpressions(node.whenFalse, analyzer, scope)
|
|
1106
1289
|
break
|
|
1107
1290
|
case 'if-statement':
|
|
1108
|
-
attachParsedExpressions(node.consequent, analyzer,
|
|
1109
|
-
if (node.alternate) attachParsedExpressions(node.alternate, analyzer,
|
|
1291
|
+
attachParsedExpressions(node.consequent, analyzer, scope)
|
|
1292
|
+
if (node.alternate) attachParsedExpressions(node.alternate, analyzer, scope)
|
|
1110
1293
|
break
|
|
1111
1294
|
}
|
|
1112
1295
|
}
|
|
@@ -2195,7 +2378,8 @@ function transformSelfClosingComponent(
|
|
|
2195
2378
|
|
|
2196
2379
|
/**
|
|
2197
2380
|
* Check if a fragment is "transparent" (just passes through children).
|
|
2198
|
-
* Pattern: <>{children}
|
|
2381
|
+
* Pattern: <>{children}</>, <>{props.children}</>, or the children prop
|
|
2382
|
+
* through another binding (`<>{kids}</>` for `{ children: kids }`).
|
|
2199
2383
|
* Transparent fragments don't need scope markers on their children.
|
|
2200
2384
|
*/
|
|
2201
2385
|
function isTransparentFragment(
|
|
@@ -2219,19 +2403,11 @@ function isTransparentFragment(
|
|
|
2219
2403
|
if (!ts.isJsxExpression(child)) return false
|
|
2220
2404
|
if (!child.expression) return false
|
|
2221
2405
|
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
//
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
// Check for custom props object name (e.g., p.children)
|
|
2229
|
-
const propsName = ctx.analyzer.propsObjectName
|
|
2230
|
-
if (propsName && exprText === `${propsName}.children`) {
|
|
2231
|
-
return true
|
|
2232
|
-
}
|
|
2233
|
-
|
|
2234
|
-
return false
|
|
2406
|
+
// A bare read of the component's own children prop, through whichever
|
|
2407
|
+
// binding reads it (`childrenReadOf`, #3572): the same answer the
|
|
2408
|
+
// expression node itself gets.
|
|
2409
|
+
const parsed = parseExpression(child.expression.getText(ctx.sourceFile).trim())
|
|
2410
|
+
return childrenReadOf(parsed, ctx.analyzer, ctx.scope) === 'bare'
|
|
2235
2411
|
}
|
|
2236
2412
|
|
|
2237
2413
|
// #1335: a fragment-wrapped jsx-children prop value lands as
|
|
@@ -2657,7 +2833,7 @@ function transformExpressionInner(
|
|
|
2657
2833
|
// result so memo-as-value (Case 2) and renamed-prop (Case 3) cases —
|
|
2658
2834
|
// which the regex path misses — light up the reactive flag during the
|
|
2659
2835
|
// migration period.
|
|
2660
|
-
const reactive =
|
|
2836
|
+
const reactive = isReactiveTextRead(exprText, ctx, expr, origin)
|
|
2661
2837
|
// @client expressions always need slotId and are treated as reactive for client-side evaluation
|
|
2662
2838
|
// Expressions inside loops that reference the loop parameter need slotId
|
|
2663
2839
|
// so fine-grained effects can target them for per-item signal updates.
|
|
@@ -3694,15 +3870,15 @@ function resolveCallbackMethodFunctionReferenceIdentifier(name: string, analyzer
|
|
|
3694
3870
|
* const/function is never resolved against the const/function tables
|
|
3695
3871
|
* (Fable review, #2214): it grows exactly like {@link freeVarsInBody}'s own
|
|
3696
3872
|
* `bound` set, at the SAME `arrow` case. The initial call from
|
|
3697
|
-
* `attachParsedExpressions` seeds it with the enclosing loop'
|
|
3698
|
-
*
|
|
3873
|
+
* `attachParsedExpressions` seeds it with the enclosing loop rows' bound
|
|
3874
|
+
* names too (a loop's item variable shadows a same-named module const the
|
|
3699
3875
|
* same way a callback arrow's param does — `origin.freeRefs` already tags
|
|
3700
3876
|
* such a name `kind: 'render-item'`, this walk now honors it).
|
|
3701
3877
|
*/
|
|
3702
3878
|
function resolveCallbackMethodFunctionReferences(
|
|
3703
3879
|
expr: ParsedExpr,
|
|
3704
3880
|
analyzer: AnalyzerContext,
|
|
3705
|
-
bound: ReadonlySet<string
|
|
3881
|
+
bound: ReadonlySet<string>,
|
|
3706
3882
|
): ParsedExpr {
|
|
3707
3883
|
function visit(e: ParsedExpr, bound: ReadonlySet<string>): ParsedExpr {
|
|
3708
3884
|
switch (e.kind) {
|
|
@@ -7117,6 +7293,17 @@ function computeReactivityFlags(
|
|
|
7117
7293
|
}
|
|
7118
7294
|
}
|
|
7119
7295
|
|
|
7296
|
+
/**
|
|
7297
|
+
* An event handler's source as `IREvent.handler`: one expression that
|
|
7298
|
+
* emitters pass verbatim as the listener argument, so a comma sequence
|
|
7299
|
+
* (`{a, b}`) is parenthesized (#3538).
|
|
7300
|
+
*/
|
|
7301
|
+
function eventHandlerText(handlerExpr: ts.Expression, ctx: TransformContext): string {
|
|
7302
|
+
const isSequence =
|
|
7303
|
+
ts.isBinaryExpression(handlerExpr) && handlerExpr.operatorToken.kind === ts.SyntaxKind.CommaToken
|
|
7304
|
+
return isSequence ? `(${ctx.getJS(handlerExpr)})` : ctx.getJS(handlerExpr)
|
|
7305
|
+
}
|
|
7306
|
+
|
|
7120
7307
|
/**
|
|
7121
7308
|
* Emit BF047 if a ref / event-handler callback body references a
|
|
7122
7309
|
* JSX-typed branch-local — the multi-return pipeline has no way to
|
|
@@ -7183,12 +7370,12 @@ function processAttributes(
|
|
|
7183
7370
|
// (`click`, not `Click`), so strip the `on` prefix and downcase.
|
|
7184
7371
|
if (isEventHandlerName(rawName)) {
|
|
7185
7372
|
if (attr.initializer && ts.isJsxExpression(attr.initializer) && attr.initializer.expression) {
|
|
7186
|
-
const
|
|
7187
|
-
reportJsxBranchLocalInCallback(
|
|
7373
|
+
const handlerExpr = attr.initializer.expression
|
|
7374
|
+
reportJsxBranchLocalInCallback(handlerExpr, ctx)
|
|
7188
7375
|
events.push({
|
|
7189
|
-
name:
|
|
7376
|
+
name: rawName.slice(2).toLowerCase(),
|
|
7190
7377
|
originalAttr: rawName,
|
|
7191
|
-
handler: ctx
|
|
7378
|
+
handler: eventHandlerText(handlerExpr, ctx),
|
|
7192
7379
|
loc: getSourceLocation(attr, ctx.sourceFile, ctx.filePath),
|
|
7193
7380
|
})
|
|
7194
7381
|
}
|
|
@@ -7906,17 +8093,50 @@ function getAttributeValue(attr: ts.JsxAttribute, ctx: TransformContext): AttrVa
|
|
|
7906
8093
|
if (ts.isBinaryExpression(expr) && expr.operatorToken.kind === ts.SyntaxKind.BarBarToken) {
|
|
7907
8094
|
if (ts.isIdentifier(expr.right) && expr.right.text === 'undefined') {
|
|
7908
8095
|
const baseExpr = ctx.getJS(expr.left)
|
|
7909
|
-
return AttrValueOf.expression(baseExpr, {
|
|
8096
|
+
return AttrValueOf.expression(baseExpr, {
|
|
8097
|
+
presenceOrUndefined: true,
|
|
8098
|
+
typed: typedSourceBeside(expr.left, baseExpr, ctx),
|
|
8099
|
+
})
|
|
7910
8100
|
}
|
|
7911
8101
|
}
|
|
7912
8102
|
|
|
7913
8103
|
const exprText = ctx.getJS(expr)
|
|
7914
|
-
return AttrValueOf.expression(exprText)
|
|
8104
|
+
return AttrValueOf.expression(exprText, { typed: typedSourceBeside(expr, exprText, ctx) })
|
|
7915
8105
|
}
|
|
7916
8106
|
|
|
7917
8107
|
return AttrValueOf.booleanAttr()
|
|
7918
8108
|
}
|
|
7919
8109
|
|
|
8110
|
+
/**
|
|
8111
|
+
* The author's text for `node` with its TypeScript syntax intact, paired with
|
|
8112
|
+
* the `js` that `ctx.getJS(node)` produced — so a TSX-emitting adapter can
|
|
8113
|
+
* write it in place of `js`, and an assertion the source needed to type-check
|
|
8114
|
+
* (`value={store as never}`) is still there when the emitted template is
|
|
8115
|
+
* type-checked (#2573).
|
|
8116
|
+
*
|
|
8117
|
+
* `undefined` (the adapter keeps `js`) unless `js` is exactly `node`'s type
|
|
8118
|
+
* strip: a synthetic re-parse (not a live position in this source file), or a
|
|
8119
|
+
* `ctx.getJS` that substitutes on top of the strip (branch-scope locals,
|
|
8120
|
+
* inlined helper parameters), has no author text that matches it. Also
|
|
8121
|
+
* `undefined` when the strip removed nothing.
|
|
8122
|
+
*/
|
|
8123
|
+
function typedSourceBeside(
|
|
8124
|
+
node: ts.Node,
|
|
8125
|
+
js: string,
|
|
8126
|
+
ctx: TransformContext,
|
|
8127
|
+
): TypedExprSource | undefined {
|
|
8128
|
+
let own: ts.SourceFile | undefined
|
|
8129
|
+
try {
|
|
8130
|
+
own = node.getSourceFile()
|
|
8131
|
+
} catch {
|
|
8132
|
+
own = undefined
|
|
8133
|
+
}
|
|
8134
|
+
if (own !== ctx.sourceFile) return undefined
|
|
8135
|
+
if (reconstructWithoutTypes(node, ctx.sourceFile, ctx.analyzer.typeExcludeRanges) !== js) return undefined
|
|
8136
|
+
const tsx = node.getText(ctx.sourceFile)
|
|
8137
|
+
return tsx === js ? undefined : { tsx: tsxSourceText(tsx), forExpr: js }
|
|
8138
|
+
}
|
|
8139
|
+
|
|
7920
8140
|
/**
|
|
7921
8141
|
* Convert a static style object literal to a CSS string at compile time.
|
|
7922
8142
|
* Returns null if any property value is non-static (dynamic expression, template literal, etc.).
|