@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
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Props expansion, dependency analysis, and controlled component detection.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import type { ParamInfo, SignalInfo } from '../types.ts'
|
|
5
|
+
import type { ConstantInfo, ParamInfo, SignalInfo } from '../types.ts'
|
|
6
6
|
import type { ClientJsContext } from './types.ts'
|
|
7
7
|
import type { BindingScope } from '../scope/binding-scope.ts'
|
|
8
8
|
import { resolveRestSpreadOriginCore } from '../props-binding.ts'
|
|
@@ -118,13 +118,32 @@ export function expandDynamicPropValue(value: string, ctx: ClientJsContext, scop
|
|
|
118
118
|
// Re-embedding it here would silently drop the mutation; keep the bare
|
|
119
119
|
// reference instead so the emitted getter reads the LIVE binding.
|
|
120
120
|
if (constant?.mutatedAfterDeclaration) return value
|
|
121
|
-
if (constant && constant.value) {
|
|
121
|
+
if (constant && constant.value && !initializerCapturedBy(constant, scope)) {
|
|
122
122
|
return constant.value
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
return value
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
+
/**
|
|
129
|
+
* Would `constant`'s initializer text, spliced in place of a reference to it
|
|
130
|
+
* at a site inside `scope`, read a different binding than the declaration
|
|
131
|
+
* does (#3736)? The initializer resolves in the scope the const is declared
|
|
132
|
+
* in, so a name it reads that a loop row or callback here binds
|
|
133
|
+
* (`const b = a` read in `rows.map(a => …)`) would re-resolve to the row's
|
|
134
|
+
* binding. The reference then stays the const itself, which init declares.
|
|
135
|
+
*
|
|
136
|
+
* A SHADOW GUARD, so `isBound` (every binding source hides the outer name),
|
|
137
|
+
* not `valueBoundNames`.
|
|
138
|
+
*/
|
|
139
|
+
function initializerCapturedBy(constant: ConstantInfo, scope: BindingScope | undefined): boolean {
|
|
140
|
+
if (!scope || !constant.freeIdentifiers) return false
|
|
141
|
+
for (const name of constant.freeIdentifiers) {
|
|
142
|
+
if (scope.isBound(name)) return true
|
|
143
|
+
}
|
|
144
|
+
return false
|
|
145
|
+
}
|
|
146
|
+
|
|
128
147
|
/**
|
|
129
148
|
* Expand a local constant for reactivity detection in stateless components.
|
|
130
149
|
* Stateful components use props.xxx directly, so expansion is unnecessary.
|
|
@@ -165,7 +184,7 @@ export function expandConstantForReactivity(
|
|
|
165
184
|
// binding's `value` is a stale pre-mutation snapshot, so it must not be
|
|
166
185
|
// substituted in place of a live reference.
|
|
167
186
|
if (constant?.mutatedAfterDeclaration) return { expr, freeIds: originalFreeIds }
|
|
168
|
-
if (constant && constant.value) {
|
|
187
|
+
if (constant && constant.value && !initializerCapturedBy(constant, scope)) {
|
|
169
188
|
return { expr: constant.value, freeIds: constant.freeIdentifiers }
|
|
170
189
|
}
|
|
171
190
|
return { expr, freeIds: originalFreeIds }
|
|
@@ -37,7 +37,8 @@ import { collectValueReferencedNames } from '../value-references.ts'
|
|
|
37
37
|
import { PROPS_PARAM } from './utils.ts'
|
|
38
38
|
|
|
39
39
|
/**
|
|
40
|
-
* Does `init` read `<objectName>.<key
|
|
40
|
+
* Does `init` read `<objectName>.<key>`, `<objectName>.<key> ?? <fallback>`,
|
|
41
|
+
* or a defaulted `<objectName>.<key> === undefined ? <default> : <objectName>.<key>`?
|
|
41
42
|
* The one recognizer for "a prop read initializer", shared by
|
|
42
43
|
* `propExtractionName` below (which additionally requires the bound name to
|
|
43
44
|
* equal `key` unless `key` is `children`, and always reads off `_p`), `rewriteBodyAliasReads`
|
|
@@ -50,7 +51,9 @@ import { PROPS_PARAM } from './utils.ts'
|
|
|
50
51
|
export function parsePropReadInitializer(
|
|
51
52
|
init: ts.Expression,
|
|
52
53
|
objectName: string = PROPS_PARAM,
|
|
53
|
-
): { key: string; fallback: ts.Expression | null } | null {
|
|
54
|
+
): { key: string; fallback: ts.Expression | null; coalescesNull: boolean } | null {
|
|
55
|
+
const defaulted = parseDefaultedPropRead(init, objectName)
|
|
56
|
+
if (defaulted) return { ...defaulted, coalescesNull: false }
|
|
54
57
|
let core: ts.Expression = init
|
|
55
58
|
let fallback: ts.Expression | null = null
|
|
56
59
|
if (
|
|
@@ -60,9 +63,30 @@ export function parsePropReadInitializer(
|
|
|
60
63
|
fallback = core.right
|
|
61
64
|
core = core.left
|
|
62
65
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return { key
|
|
66
|
+
const key = propReadKey(core, objectName)
|
|
67
|
+
// A `??` fallback (`props.x ?? d`, the author's own) replaces `null` too.
|
|
68
|
+
return key === null ? null : { key, fallback, coalescesNull: fallback !== null }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** `<objectName>.<key>`'s key, or null for any other expression. */
|
|
72
|
+
function propReadKey(expr: ts.Expression, objectName: string): string | null {
|
|
73
|
+
if (!ts.isPropertyAccessExpression(expr)) return null
|
|
74
|
+
if (!ts.isIdentifier(expr.expression) || expr.expression.text !== objectName) return null
|
|
75
|
+
return expr.name.text
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* A destructure default as `composePropRead` spells it:
|
|
80
|
+
* `<objectName>.<key> === undefined ? <default> : <objectName>.<key>`.
|
|
81
|
+
*/
|
|
82
|
+
function parseDefaultedPropRead(init: ts.Expression, objectName: string): { key: string; fallback: ts.Expression } | null {
|
|
83
|
+
if (!ts.isConditionalExpression(init)) return null
|
|
84
|
+
const test = init.condition
|
|
85
|
+
if (!ts.isBinaryExpression(test) || test.operatorToken.kind !== ts.SyntaxKind.EqualsEqualsEqualsToken) return null
|
|
86
|
+
if (!ts.isIdentifier(test.right) || test.right.text !== 'undefined') return null
|
|
87
|
+
const key = propReadKey(test.left, objectName)
|
|
88
|
+
if (key === null || propReadKey(init.whenFalse, objectName) !== key) return null
|
|
89
|
+
return { key, fallback: init.whenTrue }
|
|
66
90
|
}
|
|
67
91
|
|
|
68
92
|
/** Is `stmt` a single-declarator `const X = _p.X…` prop extraction whose
|
|
@@ -80,9 +104,38 @@ function propExtractionName(stmt: ts.Statement): string | null {
|
|
|
80
104
|
|
|
81
105
|
const parsed = parsePropReadInitializer(decl.initializer)
|
|
82
106
|
if (parsed === null || (parsed.key !== decl.name.text && parsed.key !== 'children')) return null
|
|
107
|
+
// A default that does something when it runs (`{ a = 1, b = (a = 2) }`)
|
|
108
|
+
// runs whether or not its binding is read, so its declaration stays.
|
|
109
|
+
if (parsed.fallback !== null && mayHaveSideEffect(parsed.fallback)) return null
|
|
83
110
|
return decl.name.text
|
|
84
111
|
}
|
|
85
112
|
|
|
113
|
+
/** `=`, `+=`, `??=` and the other compound assignments. */
|
|
114
|
+
function isAssignmentOperator(kind: ts.SyntaxKind): boolean {
|
|
115
|
+
return kind >= ts.SyntaxKind.FirstAssignment && kind <= ts.SyntaxKind.LastAssignment
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Whether evaluating `expr` may do more than produce a value: an
|
|
120
|
+
* assignment, an update, a call, `new`, `delete`, `await` or `yield`. The
|
|
121
|
+
* body of a function `expr` creates is not evaluated, so it does not count.
|
|
122
|
+
*/
|
|
123
|
+
function mayHaveSideEffect(expr: ts.Node): boolean {
|
|
124
|
+
if (ts.isArrowFunction(expr) || ts.isFunctionExpression(expr)) return false
|
|
125
|
+
if (ts.isBinaryExpression(expr) && isAssignmentOperator(expr.operatorToken.kind)) return true
|
|
126
|
+
if (ts.isPrefixUnaryExpression(expr) || ts.isPostfixUnaryExpression(expr)) {
|
|
127
|
+
const op = expr.operator
|
|
128
|
+
if (op === ts.SyntaxKind.PlusPlusToken || op === ts.SyntaxKind.MinusMinusToken) return true
|
|
129
|
+
}
|
|
130
|
+
if (
|
|
131
|
+
ts.isCallExpression(expr) || ts.isNewExpression(expr) || ts.isDeleteExpression(expr) ||
|
|
132
|
+
ts.isAwaitExpression(expr) || ts.isYieldExpression(expr) || ts.isTaggedTemplateExpression(expr)
|
|
133
|
+
) {
|
|
134
|
+
return true
|
|
135
|
+
}
|
|
136
|
+
return ts.forEachChild(expr, child => (mayHaveSideEffect(child) ? true : undefined)) ?? false
|
|
137
|
+
}
|
|
138
|
+
|
|
86
139
|
/**
|
|
87
140
|
* Remove prop-extraction consts inside `init*` functions whose bound name
|
|
88
141
|
* is never referenced as a value anywhere else in `code`. Returns the code
|
|
@@ -113,6 +113,31 @@ function bodyAliasDefaultParts(ctx: ClientJsContext, local: string, key: string)
|
|
|
113
113
|
return param?.defaultParts ? { defaultParts: param.defaultParts } : {}
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
+
/**
|
|
117
|
+
* Drop from `aliases` every body alias whose default reads an earlier
|
|
118
|
+
* binding the init body keeps as a local (`const { a = [], b = a } = props;
|
|
119
|
+
* a.push(x)`, or a `let` destructure): JS binds `b` to that local's value
|
|
120
|
+
* once, so `b` keeps its declaration (`const b = _p.b === undefined ? a :
|
|
121
|
+
* _p.b`) instead of a live read that would build a fresh `a` default. The
|
|
122
|
+
* kept locals are the body-destructured constants that are not aliases;
|
|
123
|
+
* an alias dropped here is kept too, so the walk repeats until it settles.
|
|
124
|
+
*/
|
|
125
|
+
function keepAliasesReadingRetainedLocals(aliases: Map<string, { key: string }>, ctx: ClientJsContext): void {
|
|
126
|
+
const retained = new Set(
|
|
127
|
+
ctx.localConstants.filter(c => c.propsDestructure && !c.isModule && !aliases.has(c.name)).map(c => c.name),
|
|
128
|
+
)
|
|
129
|
+
for (let changed = true; changed; ) {
|
|
130
|
+
changed = false
|
|
131
|
+
for (const [local, alias] of aliases) {
|
|
132
|
+
const parts = bodyAliasDefaultParts(ctx, local, alias.key).defaultParts ?? []
|
|
133
|
+
if (!parts.some(part => part.kind === 'param' && retained.has(part.param.name))) continue
|
|
134
|
+
aliases.delete(local)
|
|
135
|
+
retained.add(local)
|
|
136
|
+
changed = true
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
116
141
|
/**
|
|
117
142
|
* Rewrite every bare value-position read of a BODY-destructured (or
|
|
118
143
|
* bare-member-alias) prop local in `code` to a live `_p.<key>` read, for a
|
|
@@ -154,6 +179,7 @@ function rewriteBodyAliasReads(code: string, ctx: ClientJsContext): string {
|
|
|
154
179
|
for (const [local, alias] of aliases) {
|
|
155
180
|
if (alias.key === 'children') aliases.delete(local)
|
|
156
181
|
}
|
|
182
|
+
keepAliasesReadingRetainedLocals(aliases, ctx)
|
|
157
183
|
if (aliases.size === 0) return code
|
|
158
184
|
// Quick exit when none of the candidate local names appear at all.
|
|
159
185
|
if (![...aliases.keys()].some((name) => identifierPattern(name).test(code))) return code
|
|
@@ -161,7 +187,7 @@ function rewriteBodyAliasReads(code: string, ctx: ClientJsContext): string {
|
|
|
161
187
|
const sourceFile = ts.createSourceFile('init-body.ts', code, ts.ScriptTarget.Latest, /*setParentNodes*/ true, ts.ScriptKind.TS)
|
|
162
188
|
|
|
163
189
|
const spans: Array<readonly [number, number]> = []
|
|
164
|
-
const located = new Map<string, Pick<LivePropReadInfo, 'defaultValue' | 'defaultContainsArrow' | 'defaultParts'> & { key: string }>()
|
|
190
|
+
const located = new Map<string, Pick<LivePropReadInfo, 'defaultValue' | 'defaultContainsArrow' | 'defaultParts' | 'coalescesNull'> & { key: string }>()
|
|
165
191
|
for (const stmt of sourceFile.statements) {
|
|
166
192
|
if (!ts.isFunctionDeclaration(stmt) || !stmt.name?.text.startsWith('init') || !stmt.body) continue
|
|
167
193
|
for (const inner of stmt.body.statements) {
|
|
@@ -179,6 +205,9 @@ function rewriteBodyAliasReads(code: string, ctx: ClientJsContext): string {
|
|
|
179
205
|
located.set(decl.name.text, {
|
|
180
206
|
key: alias.key,
|
|
181
207
|
defaultValue: parsedInit.fallback?.getText(sourceFile),
|
|
208
|
+
// An author's `props.x ?? d` keeps its `??`; a destructure default
|
|
209
|
+
// reads `undefined` only (`composePropRead`).
|
|
210
|
+
coalescesNull: parsedInit.coalescesNull,
|
|
182
211
|
// `alias.defaultContainsArrow` mirrors `c.containsArrow`, computed
|
|
183
212
|
// against the ORIGINAL initializer before #2940's fix wrapped an
|
|
184
213
|
// arrow/function-expression default in its own protective parens
|
|
@@ -221,6 +250,7 @@ function rewriteBodyAliasReads(code: string, ctx: ClientJsContext): string {
|
|
|
221
250
|
sourceName: info.key,
|
|
222
251
|
defaultValue: info.defaultValue,
|
|
223
252
|
defaultContainsArrow: info.defaultContainsArrow,
|
|
253
|
+
coalescesNull: info.coalescesNull,
|
|
224
254
|
...(info.defaultParts && { defaultParts: info.defaultParts }),
|
|
225
255
|
}
|
|
226
256
|
return livePropReadExpr(prop, undefined, false)
|
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
* single-comp:
|
|
8
8
|
* <i>// Initialize static array children (hydrate skips nested instances)
|
|
9
9
|
* <i>if (<container>) {
|
|
10
|
-
* <i> const __childScopes =
|
|
10
|
+
* <i> const __childScopes = childScopesBySlot(<container>, __scopeId, '<slot>')
|
|
11
|
+
* <i> // or qsaChildScopes(<container>, <selector>) without a slot
|
|
11
12
|
* <i> __childScopes.forEach((childScope, <idx>) => {
|
|
12
13
|
* <i> const <param> = <array>[<idx>]
|
|
13
14
|
* <i> <outerPreludeStatements*> // raw outer preamble (#1064)
|
|
@@ -23,7 +24,7 @@
|
|
|
23
24
|
* <i> const __iterEl = <container>.children[<offset>]
|
|
24
25
|
* <i> if (__iterEl) {
|
|
25
26
|
* <i> <outerPreludeStatements*> // raw outer preamble (#1064)
|
|
26
|
-
* <i> const __compEl =
|
|
27
|
+
* <i> const __compEl = childScopeBySlot(__iterEl, __scopeId, '<slot>')
|
|
27
28
|
* <i> if (__compEl) initChild('<name>', __compEl, <props>)
|
|
28
29
|
* <i> }
|
|
29
30
|
* <i> })
|
|
@@ -51,6 +52,7 @@
|
|
|
51
52
|
*/
|
|
52
53
|
|
|
53
54
|
import type {
|
|
55
|
+
ChildScopesLookup,
|
|
54
56
|
ComponentRootedInnerLoopInitPlan,
|
|
55
57
|
InnerLoopNestedInitPlan,
|
|
56
58
|
OuterNestedInitPlan,
|
|
@@ -92,11 +94,25 @@ function stringifyOne(lines: string[], plan: StaticArrayChildInitPlan): void {
|
|
|
92
94
|
}
|
|
93
95
|
}
|
|
94
96
|
|
|
97
|
+
/** Spell a `ChildScopesLookup` as the runtime call that finds every scope. */
|
|
98
|
+
function childScopesCall(containerVar: string, lookup: ChildScopesLookup): string {
|
|
99
|
+
return lookup.by === 'slot'
|
|
100
|
+
? `childScopesBySlot(${containerVar}, __scopeId, '${lookup.slotId}')`
|
|
101
|
+
: `qsaChildScopes(${containerVar}, ${lookup.selector})`
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Spell a `ChildScopesLookup` as the runtime call that finds one scope. */
|
|
105
|
+
function childScopeCall(elVar: string, lookup: ChildScopesLookup): string {
|
|
106
|
+
return lookup.by === 'slot'
|
|
107
|
+
? `childScopeBySlot(${elVar}, __scopeId, '${lookup.slotId}')`
|
|
108
|
+
: `qsaChildScope(${elVar}, ${lookup.selector})`
|
|
109
|
+
}
|
|
110
|
+
|
|
95
111
|
function emitSingleComp(lines: string[], plan: SingleCompInitPlan): void {
|
|
96
|
-
const { containerVar, componentName,
|
|
112
|
+
const { containerVar, componentName, lookup, arrayExpr, param, indexParam, outerPreludeStatements, propsExpr, rowEvents, innerLoops } = plan
|
|
97
113
|
lines.push(` // Initialize static array children (hydrate skips nested instances)`)
|
|
98
114
|
lines.push(` if (${containerVar}) {`)
|
|
99
|
-
lines.push(` const __childScopes =
|
|
115
|
+
lines.push(` const __childScopes = ${childScopesCall(containerVar, lookup)}`)
|
|
100
116
|
lines.push(` __childScopes.forEach((childScope, ${indexParam}) => {`)
|
|
101
117
|
lines.push(` const ${param} = ${arrayExpr}[${indexParam}]`)
|
|
102
118
|
// Outer `.map()` callback preamble locals — emitted unwrapped after
|
|
@@ -114,7 +130,7 @@ function emitSingleComp(lines: string[], plan: SingleCompInitPlan): void {
|
|
|
114
130
|
}
|
|
115
131
|
|
|
116
132
|
function emitOuterNested(lines: string[], plan: OuterNestedInitPlan): void {
|
|
117
|
-
const { containerVar, componentName,
|
|
133
|
+
const { containerVar, componentName, lookup, arrayExpr, param, indexParam, offsetExpr, outerPreludeStatements, propsExpr } = plan
|
|
118
134
|
lines.push(` // Initialize nested ${componentName} in static array`)
|
|
119
135
|
lines.push(` if (${containerVar}) {`)
|
|
120
136
|
lines.push(` ${arrayExpr}.forEach((${param}, ${indexParam}) => {`)
|
|
@@ -127,7 +143,7 @@ function emitOuterNested(lines: string[], plan: OuterNestedInitPlan): void {
|
|
|
127
143
|
for (const stmt of outerPreludeStatements) {
|
|
128
144
|
lines.push(` ${stmt}`)
|
|
129
145
|
}
|
|
130
|
-
lines.push(` const __compEl =
|
|
146
|
+
lines.push(` const __compEl = ${childScopeCall('__iterEl', lookup)}`)
|
|
131
147
|
lines.push(` if (__compEl) initChild('${nameForRegistryRef(componentName)}', __compEl, ${propsExpr})`)
|
|
132
148
|
lines.push(` }`)
|
|
133
149
|
lines.push(` })`)
|
|
@@ -183,7 +199,7 @@ function emitInnerLoopNested(lines: string[], plan: InnerLoopNestedInitPlan): vo
|
|
|
183
199
|
// re-declare `const __compEl` in the same scope (#1664).
|
|
184
200
|
comps.forEach((comp, i) => {
|
|
185
201
|
const compElVar = comps.length > 1 ? `__compEl${i}` : '__compEl'
|
|
186
|
-
lines.push(` const ${compElVar} =
|
|
202
|
+
lines.push(` const ${compElVar} = ${childScopeCall('__innerEl', comp.lookup)}`)
|
|
187
203
|
lines.push(` if (${compElVar}) initChild('${nameForRegistryRef(comp.componentName)}', ${compElVar}, ${comp.propsExpr})`)
|
|
188
204
|
})
|
|
189
205
|
// Plain-element reactive attrs / texts / refs inside this inner loop's
|
|
@@ -211,7 +227,7 @@ function emitInnerLoopNested(lines: string[], plan: InnerLoopNestedInitPlan): vo
|
|
|
211
227
|
const writer = claimWriterVarName(slots, varSlotId)
|
|
212
228
|
lines.push(` const ${writer} = lazySlots(__innerEl, ${claimPlanLiteral(slots)})`)
|
|
213
229
|
for (const text of texts) {
|
|
214
|
-
lines.push(` createEffect(() => { ${writer}('${text.slotId}', ${rowTextSlotValue(text, text.expression
|
|
230
|
+
lines.push(` createEffect(() => { ${writer}('${text.slotId}', ${rowTextSlotValue(text, text.expression)}) })`)
|
|
215
231
|
}
|
|
216
232
|
}
|
|
217
233
|
// Ref callbacks fire on every forEach iteration — initial mount and any
|
|
@@ -229,7 +245,7 @@ function emitInnerLoopNested(lines: string[], plan: InnerLoopNestedInitPlan): vo
|
|
|
229
245
|
*
|
|
230
246
|
* <i>// Initialize component-rooted inner-loop components (depth N)
|
|
231
247
|
* <i>if (<container>) {
|
|
232
|
-
* <i> const <scopes_c> =
|
|
248
|
+
* <i> const <scopes_c> = childScopesBySlot(<container>, __scopeId, '<slot_c>') // per comp
|
|
233
249
|
* <i> let <cursor_c> = 0
|
|
234
250
|
* <i> <outerArr>.forEach((<outerParam>[, <outerIdx>]) => {
|
|
235
251
|
* <i> <outerPreludeStatements*>
|
|
@@ -270,7 +286,7 @@ function emitComponentRootedInnerLoop(lines: string[], plan: ComponentRootedInne
|
|
|
270
286
|
lines.push(` // Initialize component-rooted inner-loop components (depth ${depth})`)
|
|
271
287
|
lines.push(` if (${containerVar}) {`)
|
|
272
288
|
comps.forEach((comp, i) => {
|
|
273
|
-
lines.push(` const ${scopesVar(i)} =
|
|
289
|
+
lines.push(` const ${scopesVar(i)} = ${childScopesCall(containerVar, comp.lookup)}`)
|
|
274
290
|
lines.push(` let ${cursorVar(i)} = 0`)
|
|
275
291
|
})
|
|
276
292
|
// Declared index names are appended to the forEach heads (#2231);
|