@barefootjs/jsx 0.40.0 → 0.40.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts +42 -0
- package/dist/adapters/ambiguous-nil-arithmetic.d.ts.map +1 -0
- package/dist/adapters/attr-plan.d.ts.map +1 -1
- package/dist/adapters/boolean-carrier.d.ts +31 -0
- package/dist/adapters/boolean-carrier.d.ts.map +1 -0
- package/dist/adapters/boolean-result.d.ts +32 -10
- package/dist/adapters/boolean-result.d.ts.map +1 -1
- package/dist/adapters/brace-delimiter-text.d.ts +18 -0
- package/dist/adapters/brace-delimiter-text.d.ts.map +1 -0
- package/dist/adapters/callback-method.d.ts +38 -5
- package/dist/adapters/callback-method.d.ts.map +1 -1
- package/dist/adapters/component-props.d.ts +22 -7
- package/dist/adapters/component-props.d.ts.map +1 -1
- package/dist/adapters/computed-const.d.ts +28 -1
- package/dist/adapters/computed-const.d.ts.map +1 -1
- package/dist/adapters/dsl-template-adapter.d.ts +39 -1
- package/dist/adapters/dsl-template-adapter.d.ts.map +1 -1
- package/dist/adapters/loop-bound-names.d.ts +9 -1
- package/dist/adapters/loop-bound-names.d.ts.map +1 -1
- package/dist/adapters/loop-row-type.d.ts +52 -0
- package/dist/adapters/loop-row-type.d.ts.map +1 -0
- package/dist/adapters/nil-reading.d.ts +68 -0
- package/dist/adapters/nil-reading.d.ts.map +1 -0
- package/dist/adapters/non-primitive-equality.d.ts +33 -0
- package/dist/adapters/non-primitive-equality.d.ts.map +1 -0
- package/dist/adapters/number-value.d.ts +45 -0
- package/dist/adapters/number-value.d.ts.map +1 -0
- package/dist/adapters/operand-typing.d.ts +126 -0
- package/dist/adapters/operand-typing.d.ts.map +1 -0
- package/dist/adapters/parsed-expr-emitter.d.ts +70 -12
- package/dist/adapters/parsed-expr-emitter.d.ts.map +1 -1
- package/dist/adapters/primitive-plan.d.ts +291 -0
- package/dist/adapters/primitive-plan.d.ts.map +1 -0
- package/dist/analyzer-context.d.ts +11 -0
- package/dist/analyzer-context.d.ts.map +1 -1
- package/dist/analyzer.d.ts +34 -0
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/augment-inherited-props.d.ts +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 +24 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5671 -2586
- package/dist/ir-to-client-js/build-references.d.ts.map +1 -1
- package/dist/ir-to-client-js/child-props-object.d.ts +64 -0
- package/dist/ir-to-client-js/child-props-object.d.ts.map +1 -0
- package/dist/ir-to-client-js/collect-elements.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts +29 -0
- package/dist/ir-to-client-js/control-flow/plan/build-inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-lazy-row.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/build-lazy-row.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/build-reactive-effects.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/inner-loop.d.ts +5 -4
- package/dist/ir-to-client-js/control-flow/plan/inner-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/loop-child-arm.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts +15 -0
- package/dist/ir-to-client-js/control-flow/plan/loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/plan/reactive-effects.d.ts +2 -0
- package/dist/ir-to-client-js/control-flow/plan/reactive-effects.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/shared.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts +22 -0
- package/dist/ir-to-client-js/control-flow/stringify/claim-plan.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/component-loop.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/event-listener.d.ts.map +1 -1
- package/dist/ir-to-client-js/control-flow/stringify/lazy-row.d.ts.map +1 -1
- package/dist/ir-to-client-js/html-template.d.ts +3 -2
- package/dist/ir-to-client-js/html-template.d.ts.map +1 -1
- package/dist/ir-to-client-js/imports.d.ts +2 -2
- package/dist/ir-to-client-js/imports.d.ts.map +1 -1
- package/dist/ir-to-client-js/index.d.ts +2 -6
- package/dist/ir-to-client-js/index.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/build-static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/plan/static-array-child-init.d.ts +37 -19
- 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/reactivity.d.ts.map +1 -1
- package/dist/ir-to-client-js/safe-html.d.ts +9 -3
- package/dist/ir-to-client-js/safe-html.d.ts.map +1 -1
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts +3 -2
- package/dist/ir-to-client-js/stringify/static-array-child-init.d.ts.map +1 -1
- package/dist/ir-to-client-js/types.d.ts +30 -0
- package/dist/ir-to-client-js/types.d.ts.map +1 -1
- package/dist/ir-to-client-js/utils.d.ts +8 -0
- package/dist/ir-to-client-js/utils.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 +95 -10
- 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 +27 -3
- 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 +100 -4
- 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 +16 -0
- package/dist/scope/root-prop-alias.d.ts.map +1 -1
- package/dist/signal-prop-name-collision.d.ts.map +1 -1
- package/dist/ssr-defaults.d.ts +4 -2
- package/dist/ssr-defaults.d.ts.map +1 -1
- package/dist/ssr-seed-plan.d.ts +14 -0
- package/dist/ssr-seed-plan.d.ts.map +1 -1
- package/dist/strip-types.d.ts +13 -0
- package/dist/strip-types.d.ts.map +1 -1
- package/dist/template-parts.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 +120 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/__snapshots__/doc-examples.test.ts.snap +3 -3
- package/src/__tests__/binding-scope-ratchet.test.ts +12 -2
- package/src/__tests__/binding-scope.test.ts +89 -1
- package/src/__tests__/body-destructure-props-object-alias.test.ts +123 -0
- package/src/__tests__/boolean-carrier.test.ts +74 -0
- package/src/__tests__/boolean-result.test.ts +22 -5
- package/src/__tests__/brace-delimiter-text.test.ts +26 -0
- package/src/__tests__/callback-method.test.ts +62 -2
- package/src/__tests__/child-components-in-map.test.ts +1 -1
- package/src/__tests__/component-props.test.ts +14 -0
- package/src/__tests__/computed-const.test.ts +188 -0
- package/src/__tests__/emit-to-string-call.test.ts +2 -1
- package/src/__tests__/expression-parser.test.ts +24 -0
- 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 +143 -0
- package/src/__tests__/issue-3713-props-spread-loop-row.test.ts +65 -0
- package/src/__tests__/lookup-literal-const.test.ts +9 -3
- package/src/__tests__/loop-index-reactivity.test.ts +2 -2
- package/src/__tests__/loop-param-accessor-key-member-safety.test.ts +1 -1
- package/src/__tests__/loop-plan.test.ts +1 -1
- package/src/__tests__/loop-row-const-initializer-shadow.test.ts +146 -0
- package/src/__tests__/loop-row-prop-read.test.ts +111 -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 +137 -0
- package/src/__tests__/module-const-destructure-default.test.ts +156 -0
- package/src/__tests__/nested-loop-index-param.test.ts +1 -1
- package/src/__tests__/nested-loop-reactive-attrs.test.ts +2 -2
- package/src/__tests__/nested-row-shadowing.test.ts +2 -2
- package/src/__tests__/nil-reading.test.ts +77 -0
- package/src/__tests__/nullable-signal-attr.test.ts +231 -2
- package/src/__tests__/primitive-plan.test.ts +416 -0
- package/src/__tests__/profile-binding-ids.test.ts +14 -0
- package/src/__tests__/profile-loop-binding-ids.test.ts +2 -2
- package/src/__tests__/profile-nested-binding-ids.test.ts +2 -2
- package/src/__tests__/props-object-whole-read.test.ts +40 -0
- package/src/__tests__/reactive-attrs-in-map.test.ts +1 -1
- package/src/__tests__/reactive-factory-inlining.test.ts +4 -1
- package/src/__tests__/rest-binding-gaps.test.ts +192 -0
- package/src/__tests__/root-prop-alias.test.ts +79 -1
- package/src/__tests__/signal-prop-name-collision.test.ts +102 -2
- package/src/__tests__/source-file-cache.test.ts +105 -0
- package/src/__tests__/ssr-seed-plan.test.ts +43 -0
- package/src/__tests__/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__/template-parts-literal-boundary.test.ts +19 -0
- package/src/adapters/ambiguous-nil-arithmetic.ts +103 -0
- package/src/adapters/attr-plan.ts +6 -2
- package/src/adapters/boolean-carrier.ts +76 -0
- package/src/adapters/boolean-result.ts +72 -66
- package/src/adapters/brace-delimiter-text.ts +25 -0
- package/src/adapters/callback-method.ts +151 -27
- package/src/adapters/component-props.ts +28 -8
- package/src/adapters/computed-const.ts +443 -10
- package/src/adapters/dsl-template-adapter.ts +79 -2
- package/src/adapters/loop-bound-names.ts +9 -1
- package/src/adapters/loop-plan.ts +1 -1
- package/src/adapters/loop-row-type.ts +139 -0
- package/src/adapters/nil-reading.ts +297 -0
- package/src/adapters/non-primitive-equality.ts +92 -0
- package/src/adapters/number-value.ts +108 -0
- package/src/adapters/operand-typing.ts +522 -0
- package/src/adapters/parsed-expr-emitter.ts +154 -19
- package/src/adapters/primitive-plan.ts +696 -0
- package/src/analyzer-context.ts +14 -0
- package/src/analyzer.ts +692 -85
- 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 +74 -2
- package/src/index.ts +45 -9
- package/src/ir-to-client-js/build-references.ts +69 -0
- package/src/ir-to-client-js/child-props-object.ts +106 -0
- package/src/ir-to-client-js/collect-elements.ts +100 -67
- package/src/ir-to-client-js/compute-inlinability.ts +1 -1
- package/src/ir-to-client-js/control-flow/plan/build-component-loop.ts +15 -1
- package/src/ir-to-client-js/control-flow/plan/build-inner-loop.ts +85 -5
- package/src/ir-to-client-js/control-flow/plan/build-lazy-row.ts +3 -0
- package/src/ir-to-client-js/control-flow/plan/build-loop-child-arm.ts +30 -28
- package/src/ir-to-client-js/control-flow/plan/build-reactive-effects.ts +1 -0
- package/src/ir-to-client-js/control-flow/plan/inner-loop.ts +5 -4
- package/src/ir-to-client-js/control-flow/plan/loop-child-arm.ts +2 -0
- package/src/ir-to-client-js/control-flow/plan/loop.ts +12 -0
- package/src/ir-to-client-js/control-flow/plan/reactive-effects.ts +2 -0
- package/src/ir-to-client-js/control-flow/shared.ts +23 -24
- package/src/ir-to-client-js/control-flow/stringify/claim-plan.ts +29 -0
- package/src/ir-to-client-js/control-flow/stringify/component-loop.ts +8 -2
- package/src/ir-to-client-js/control-flow/stringify/event-listener.ts +8 -1
- package/src/ir-to-client-js/control-flow/stringify/inner-loop.ts +8 -7
- package/src/ir-to-client-js/control-flow/stringify/lazy-row.ts +8 -7
- package/src/ir-to-client-js/control-flow/stringify/loop-child-arm.ts +4 -4
- package/src/ir-to-client-js/control-flow/stringify/loop.ts +3 -3
- package/src/ir-to-client-js/control-flow/stringify/reactive-effects.ts +6 -6
- package/src/ir-to-client-js/emit-reactive.ts +1 -1
- package/src/ir-to-client-js/html-template.ts +95 -38
- package/src/ir-to-client-js/imports.ts +1 -1
- package/src/ir-to-client-js/index.ts +53 -11
- package/src/ir-to-client-js/plan/build-static-array-child-init.ts +42 -15
- package/src/ir-to-client-js/plan/static-array-child-init.ts +34 -19
- package/src/ir-to-client-js/prop-handling.ts +22 -3
- package/src/ir-to-client-js/reactivity.ts +14 -0
- package/src/ir-to-client-js/safe-html.ts +11 -4
- package/src/ir-to-client-js/stringify/static-array-child-init.ts +30 -12
- package/src/ir-to-client-js/types.ts +23 -0
- package/src/ir-to-client-js/utils.ts +15 -1
- package/src/jsx-to-ir.ts +510 -58
- package/src/nullable-signal-attr.ts +366 -28
- package/src/prop-rewrite.ts +30 -6
- package/src/props-binding.ts +54 -3
- package/src/props-object-whole-read.ts +87 -0
- package/src/rich-type-refusal.ts +2 -1
- package/src/scope/binding-scope.ts +203 -11
- package/src/scope/lexical-scope-walk.ts +4 -3
- package/src/scope/root-prop-alias.ts +23 -0
- package/src/signal-prop-name-collision.ts +113 -22
- package/src/ssr-defaults.ts +10 -4
- package/src/ssr-seed-plan.ts +82 -17
- package/src/strip-types.ts +31 -3
- package/src/template-parts.ts +21 -1
- package/src/template-position-walk.ts +57 -28
- package/src/types.ts +117 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a component observes its props object as a whole: any read of the
|
|
3
|
+
* props parameter other than a member read with a known key (`props.x`,
|
|
4
|
+
* `props['x']`). A whole read — `{...props}` on an element or a component,
|
|
5
|
+
* `Object.keys(props)`, `props[key]`, passing `props` to a function,
|
|
6
|
+
* `const { a, ...rest } = props` — can see a key the props type does not
|
|
7
|
+
* declare, so a caller's undeclared prop is not dead there.
|
|
8
|
+
*
|
|
9
|
+
* Syntactic, over the component's TS AST: a nested function, catch clause or
|
|
10
|
+
* block that rebinds the name shadows it, and a type position is not a read.
|
|
11
|
+
* A shadow this misses only makes the answer `true` more often.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import ts from 'typescript'
|
|
15
|
+
|
|
16
|
+
export function observesWholePropsObject(
|
|
17
|
+
fn: ts.FunctionDeclaration | ts.ArrowFunction | ts.FunctionExpression,
|
|
18
|
+
name: string,
|
|
19
|
+
): boolean {
|
|
20
|
+
if (!fn.body) return false
|
|
21
|
+
let found = false
|
|
22
|
+
const visit = (node: ts.Node): void => {
|
|
23
|
+
if (found || ts.isTypeNode(node) || rebinds(node, name)) return
|
|
24
|
+
if (ts.isIdentifier(node) && node.text === name && isWholeRead(node)) {
|
|
25
|
+
found = true
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
ts.forEachChild(node, visit)
|
|
29
|
+
}
|
|
30
|
+
visit(fn.body)
|
|
31
|
+
return found
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** A member read with a key known at compile time (`props.x`, `props['x']`). */
|
|
35
|
+
function isKnownKeyMemberRead(id: ts.Identifier): boolean {
|
|
36
|
+
const parent = id.parent
|
|
37
|
+
if (ts.isPropertyAccessExpression(parent)) return parent.expression === id
|
|
38
|
+
if (ts.isElementAccessExpression(parent) && parent.expression === id) {
|
|
39
|
+
return ts.isStringLiteralLike(parent.argumentExpression) || ts.isNumericLiteral(parent.argumentExpression)
|
|
40
|
+
}
|
|
41
|
+
return false
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** An identifier in a name position (a property name, a declaration, a JSX attribute name), not a read. */
|
|
45
|
+
function isNamePosition(id: ts.Identifier): boolean {
|
|
46
|
+
const parent = id.parent
|
|
47
|
+
if (ts.isPropertyAccessExpression(parent)) return parent.name === id
|
|
48
|
+
if (ts.isPropertyAssignment(parent) || ts.isJsxAttribute(parent)) return parent.name === id
|
|
49
|
+
if (ts.isBindingElement(parent)) return parent.name === id || parent.propertyName === id
|
|
50
|
+
if (ts.isVariableDeclaration(parent) || ts.isParameter(parent)) return parent.name === id
|
|
51
|
+
if (ts.isFunctionDeclaration(parent) || ts.isMethodDeclaration(parent)) return parent.name === id
|
|
52
|
+
return false
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isWholeRead(id: ts.Identifier): boolean {
|
|
56
|
+
if (isNamePosition(id)) return false
|
|
57
|
+
if (isKnownKeyMemberRead(id)) return false
|
|
58
|
+
return !isRestFreeDestructure(id)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** `const { a, b } = props`: reads only the named keys. */
|
|
62
|
+
function isRestFreeDestructure(id: ts.Identifier): boolean {
|
|
63
|
+
const parent = id.parent
|
|
64
|
+
if (!ts.isVariableDeclaration(parent) || parent.initializer !== id) return false
|
|
65
|
+
const pattern = parent.name
|
|
66
|
+
return ts.isObjectBindingPattern(pattern) &&
|
|
67
|
+
pattern.elements.every(el => !el.dotDotDotToken && (!el.propertyName || !ts.isComputedPropertyName(el.propertyName)))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Whether `node` opens a scope that binds `name` again. */
|
|
71
|
+
function rebinds(node: ts.Node, name: string): boolean {
|
|
72
|
+
if (ts.isFunctionLike(node)) return node.parameters.some(p => bindsName(p.name, name))
|
|
73
|
+
if (ts.isCatchClause(node)) return !!node.variableDeclaration && bindsName(node.variableDeclaration.name, name)
|
|
74
|
+
if (ts.isBlock(node)) return node.statements.some(s => declaresInBlock(s, name))
|
|
75
|
+
return false
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function declaresInBlock(stmt: ts.Statement, name: string): boolean {
|
|
79
|
+
if (ts.isVariableStatement(stmt)) return stmt.declarationList.declarations.some(d => bindsName(d.name, name))
|
|
80
|
+
if (ts.isFunctionDeclaration(stmt) || ts.isClassDeclaration(stmt)) return stmt.name?.text === name
|
|
81
|
+
return false
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function bindsName(binding: ts.BindingName, name: string): boolean {
|
|
85
|
+
if (ts.isIdentifier(binding)) return binding.text === name
|
|
86
|
+
return binding.elements.some(el => !ts.isOmittedExpression(el) && bindsName(el.name, name))
|
|
87
|
+
}
|
package/src/rich-type-refusal.ts
CHANGED
|
@@ -65,6 +65,7 @@ import type { ParsedExpr } from './expression-parser.ts'
|
|
|
65
65
|
import { parseExpression } from './expression-parser.ts'
|
|
66
66
|
import { prepareLoweringMatchers, type LoweringMatcher } from './lowering-registry.ts'
|
|
67
67
|
import { ErrorCodes } from './errors.ts'
|
|
68
|
+
import { isHydrationPayloadPropName } from './event-handler-name.ts'
|
|
68
69
|
import {
|
|
69
70
|
resolveReceiverType,
|
|
70
71
|
baseTypeName,
|
|
@@ -119,7 +120,7 @@ export function checkRichTypePropSerialization(root: IRNode, metadata: IRMetadat
|
|
|
119
120
|
const usedProps = new Set(metadata.clientAnalysis.usedProps)
|
|
120
121
|
const loc = declLoc ?? root.loc
|
|
121
122
|
for (const param of metadata.propsParams) {
|
|
122
|
-
if (param.isRest ||
|
|
123
|
+
if (param.isRest || !isHydrationPayloadPropName(param.name)) continue
|
|
123
124
|
if (!usedProps.has(param.name)) continue
|
|
124
125
|
const declared = resolvePropDeclaredType(param.sourceName ?? param.name, metadata)
|
|
125
126
|
const typeName = jsonUnsafeTypeName(declared)
|
|
@@ -29,6 +29,27 @@
|
|
|
29
29
|
* "just names you can't resolve against component-level state."
|
|
30
30
|
*/
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* The identifier a template reads as the component's props object when the
|
|
34
|
+
* component does not name one itself: `props.X` is the component's prop `X`
|
|
35
|
+
* while no binding shadows `props` (#3693). A component that takes its props
|
|
36
|
+
* as one object names it with its parameter (`function C(p)`), and
|
|
37
|
+
* {@link BindingScope.forComponent} makes that name the props object instead.
|
|
38
|
+
*/
|
|
39
|
+
export const PROPS_OBJECT_IDENTIFIER = 'props'
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The identifier that names the props object of a component whose props
|
|
43
|
+
* parameter is `propsObjectName` (`IRMetadata.propsObjectName`): that name
|
|
44
|
+
* (`function C(p)` → `p`), or {@link PROPS_OBJECT_IDENTIFIER} when the
|
|
45
|
+
* component destructures its props (`null`). The single answer to "which
|
|
46
|
+
* identifier is the component's props object"; {@link BindingScope.propsObject}
|
|
47
|
+
* carries it.
|
|
48
|
+
*/
|
|
49
|
+
export function propsObjectIdentifier(propsObjectName: string | null | undefined): string {
|
|
50
|
+
return propsObjectName ?? PROPS_OBJECT_IDENTIFIER
|
|
51
|
+
}
|
|
52
|
+
|
|
32
53
|
/**
|
|
33
54
|
* How a name inside a `ScopeFrame` came to be bound — the row shapes
|
|
34
55
|
* (`'item'`/`'index'`/`'destructure'`/`'preamble'`) for `'loop-row'` frames,
|
|
@@ -81,10 +102,33 @@ export interface LoopBindingSource {
|
|
|
81
102
|
|
|
82
103
|
/** Structural pick of `TypeInfo` (`packages/jsx/src/types.ts`). */
|
|
83
104
|
export interface LoopValueType {
|
|
105
|
+
/** `TypeInfo.kind`; `'unknown'` for `any`, `unknown` and unresolved types. */
|
|
106
|
+
readonly kind?: string
|
|
84
107
|
readonly primitive?: string
|
|
85
108
|
readonly unionTypes?: readonly LoopValueType[]
|
|
109
|
+
/** A named type's name (`Row`), resolved through {@link NamedTypeFields}. */
|
|
110
|
+
readonly raw?: string
|
|
111
|
+
readonly properties?: readonly LoopValueField[]
|
|
112
|
+
readonly elementType?: LoopValueType
|
|
86
113
|
}
|
|
87
114
|
|
|
115
|
+
/** Structural pick of `PropertyInfo` (`packages/jsx/src/types.ts`). */
|
|
116
|
+
export interface LoopValueField {
|
|
117
|
+
readonly name: string
|
|
118
|
+
readonly type: LoopValueType
|
|
119
|
+
/** `name?:` — the field may be absent, so its value may be `undefined`. */
|
|
120
|
+
readonly optional?: boolean
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The value of a field that may be absent: its declared type or `undefined`. */
|
|
124
|
+
const ABSENT: LoopValueType = { kind: 'primitive', raw: 'undefined', primitive: 'undefined' }
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The fields of a named type (`type Row = { … }`, an interface), by name;
|
|
128
|
+
* undefined when the component's file does not define it as an object type.
|
|
129
|
+
*/
|
|
130
|
+
export type NamedTypeFields = (name: string) => readonly LoopValueField[] | undefined
|
|
131
|
+
|
|
88
132
|
/** Structural copy of `LoopBindingPathSegment` (`packages/jsx/src/types.ts`). */
|
|
89
133
|
export type RowPathSegment =
|
|
90
134
|
| { readonly kind: 'field'; readonly key: string; readonly isIdent: boolean }
|
|
@@ -95,6 +139,85 @@ function isStringLoopValue(type: LoopValueType): boolean {
|
|
|
95
139
|
(type.unionTypes !== undefined && type.unionTypes.length > 0 && type.unionTypes.every(isStringLoopValue))
|
|
96
140
|
}
|
|
97
141
|
|
|
142
|
+
/**
|
|
143
|
+
* The type at `segments` below `type`, through object types, named types
|
|
144
|
+
* (`namedFields`) and array elements; null when a step is not known.
|
|
145
|
+
*
|
|
146
|
+
* A union resolves the path in each member and is the union of the results,
|
|
147
|
+
* null when any member has no such path. A field is therefore definitely a
|
|
148
|
+
* string only when it is one in every member: the element type of a
|
|
149
|
+
* `concat` (`loopRowType`) is the union of its receiver's and arguments'
|
|
150
|
+
* element types, so `Row` and a same-shaped `Other` both read `id: string`,
|
|
151
|
+
* while an `any` member or an omitted field reads nothing (#3762 review).
|
|
152
|
+
*/
|
|
153
|
+
export function loopValueTypeAt(
|
|
154
|
+
type: LoopValueType,
|
|
155
|
+
segments: readonly RowPathSegment[],
|
|
156
|
+
namedFields: NamedTypeFields = () => undefined,
|
|
157
|
+
): LoopValueType | null {
|
|
158
|
+
if (segments.length === 0) return type
|
|
159
|
+
if (type.unionTypes && type.unionTypes.length > 0) {
|
|
160
|
+
const members = type.unionTypes.map(member => loopValueTypeAt(member, segments, namedFields))
|
|
161
|
+
return members.every(m => m !== null) ? unionOfLoopValueTypes(members as LoopValueType[]) : null
|
|
162
|
+
}
|
|
163
|
+
const [segment, ...rest] = segments
|
|
164
|
+
const next = segment.kind === 'index' ? arrayElementType(type) : fieldType(type, segment.key, namedFields)
|
|
165
|
+
return next ? loopValueTypeAt(next, rest, namedFields) : null
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* The element type of an array type, or of a union whose every member is an
|
|
170
|
+
* array (a `concat` of arrays, a field read across `concat` rows): the union
|
|
171
|
+
* of their element types. Undefined for anything else. Every reader of an
|
|
172
|
+
* element type goes through this, so a union of arrays reads like an array
|
|
173
|
+
* (#3762 review).
|
|
174
|
+
*/
|
|
175
|
+
export function arrayElementType<T extends LoopValueType>(type: T | null | undefined): T | undefined {
|
|
176
|
+
if (!type) return undefined
|
|
177
|
+
if (type.elementType) return type.elementType as T
|
|
178
|
+
const members = type.unionTypes
|
|
179
|
+
if (!members || members.length === 0) return undefined
|
|
180
|
+
const elements = members.map(member => arrayElementType(member))
|
|
181
|
+
return elements.every(e => e !== undefined) ? unionOfLoopValueTypes(elements as T[]) : undefined
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The union of `members`, normalised: one member, or members that are all
|
|
186
|
+
* structurally the same type, is that member itself, so `Row[] ∪ Row[]`
|
|
187
|
+
* keeps the plain `Row[]` shape.
|
|
188
|
+
*/
|
|
189
|
+
export function unionOfLoopValueTypes<T extends LoopValueType>(members: readonly T[]): T {
|
|
190
|
+
const [first] = members
|
|
191
|
+
if (members.every(member => isSameLoopValueShape(member, first))) return first
|
|
192
|
+
return { kind: 'union', raw: members.map(m => m.raw ?? 'unknown').join(' | '), unionTypes: members } as unknown as T
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Whether two types have the same structure: kind, name, primitive, fields, elements and members. */
|
|
196
|
+
export function isSameLoopValueShape(a: LoopValueType | undefined, b: LoopValueType | undefined): boolean {
|
|
197
|
+
if (a === b) return true
|
|
198
|
+
if (!a || !b) return false
|
|
199
|
+
return a.kind === b.kind && a.raw === b.raw && a.primitive === b.primitive &&
|
|
200
|
+
isSameLoopValueShape(a.elementType, b.elementType) &&
|
|
201
|
+
sameMembers(a.unionTypes, b.unionTypes, isSameLoopValueShape) &&
|
|
202
|
+
sameMembers(a.properties, b.properties, (x, y) =>
|
|
203
|
+
x.name === y.name && Boolean(x.optional) === Boolean(y.optional) && isSameLoopValueShape(x.type, y.type))
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function sameMembers<T>(a: readonly T[] | undefined, b: readonly T[] | undefined, same: (x: T, y: T) => boolean): boolean {
|
|
207
|
+
if (!a || !b) return a === b
|
|
208
|
+
return a.length === b.length && a.every((x, i) => same(x, b[i]))
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** A field's declared type; an optional one may also be `undefined`. */
|
|
212
|
+
function fieldType(type: LoopValueType, key: string, namedFields: NamedTypeFields): LoopValueType | undefined {
|
|
213
|
+
const fields: readonly LoopValueField[] | undefined =
|
|
214
|
+
type.properties ?? (type.raw !== undefined ? namedFields(type.raw) : undefined)
|
|
215
|
+
const field = fields?.find(f => f.name === key)
|
|
216
|
+
// An optional field reads `undefined` when the row omits it, so it is
|
|
217
|
+
// never definitely its declared type (#3762 review).
|
|
218
|
+
return field && (field.optional ? { kind: 'union', unionTypes: [field.type, ABSENT] } : field.type)
|
|
219
|
+
}
|
|
220
|
+
|
|
98
221
|
/**
|
|
99
222
|
* A stack of `ScopeFrame`s, innermost frame at index 0 of the internal
|
|
100
223
|
* array (i.e. `frames[0]` is what `enterLoopRow`/`enterCallback` most
|
|
@@ -103,9 +226,30 @@ function isStringLoopValue(type: LoopValueType): boolean {
|
|
|
103
226
|
* frames exist.
|
|
104
227
|
*/
|
|
105
228
|
export class BindingScope {
|
|
106
|
-
static readonly EMPTY: BindingScope = new BindingScope([])
|
|
229
|
+
static readonly EMPTY: BindingScope = new BindingScope([], PROPS_OBJECT_IDENTIFIER)
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* `propsObject`: the identifier that names the component's props object
|
|
233
|
+
* in this scope's component ({@link forComponent}). Every child scope
|
|
234
|
+
* keeps it.
|
|
235
|
+
*/
|
|
236
|
+
private constructor(
|
|
237
|
+
private readonly frames: readonly ScopeFrame[],
|
|
238
|
+
readonly propsObject: string,
|
|
239
|
+
) {}
|
|
107
240
|
|
|
108
|
-
|
|
241
|
+
/**
|
|
242
|
+
* The empty scope of a component whose props object parameter is
|
|
243
|
+
* `propsObjectName` (`IRMetadata.propsObjectName`, decided in Phase 1):
|
|
244
|
+
* `function C(p)` reads its props as `p.X`, so `p` is the props object.
|
|
245
|
+
* A component that destructures its props (`null`) keeps
|
|
246
|
+
* {@link PROPS_OBJECT_IDENTIFIER}.
|
|
247
|
+
*/
|
|
248
|
+
static forComponent(propsObjectName: string | null): BindingScope {
|
|
249
|
+
const propsObject = propsObjectIdentifier(propsObjectName)
|
|
250
|
+
if (propsObject === PROPS_OBJECT_IDENTIFIER) return BindingScope.EMPTY
|
|
251
|
+
return new BindingScope([], propsObject)
|
|
252
|
+
}
|
|
109
253
|
|
|
110
254
|
/**
|
|
111
255
|
* Child scope with a new `'loop-row'` frame for one loop's per-item
|
|
@@ -137,8 +281,8 @@ export class BindingScope {
|
|
|
137
281
|
* `adapters/loop-bound-names.ts`'s `collectLoopBoundNames` adds BOTH
|
|
138
282
|
* `node.param` AND every `paramBindings[i].name` unconditionally
|
|
139
283
|
* (never skipping `param` in the destructured case) — a deliberately
|
|
140
|
-
* coarser, over-inclusive collection
|
|
141
|
-
*
|
|
284
|
+
* coarser, over-inclusive collection for whole-component "may be
|
|
285
|
+
* shadowed somewhere" checks (safe to over-exclude there). This method
|
|
142
286
|
* follows the precise `jsx-to-ir.ts` `ctx.loopParams` semantics, since
|
|
143
287
|
* that is the mechanism actually doing scope-shadowed name RESOLUTION
|
|
144
288
|
* (the behavior `BindingScope` replaces), not coarse exclusion.
|
|
@@ -164,7 +308,7 @@ export class BindingScope {
|
|
|
164
308
|
for (const name of loop.preamble?.declaredNames ?? []) bindings.set(name, { source: 'preamble' })
|
|
165
309
|
|
|
166
310
|
const frame: ScopeFrame = { kind: 'loop-row', bindings }
|
|
167
|
-
return new BindingScope([frame, ...this.frames])
|
|
311
|
+
return new BindingScope([frame, ...this.frames], this.propsObject)
|
|
168
312
|
}
|
|
169
313
|
|
|
170
314
|
/**
|
|
@@ -176,7 +320,7 @@ export class BindingScope {
|
|
|
176
320
|
const bindings = new Map<string, ScopeBinding>()
|
|
177
321
|
for (const name of params) bindings.set(name, { source: 'param' })
|
|
178
322
|
const frame: ScopeFrame = { kind: 'callback', bindings }
|
|
179
|
-
return new BindingScope([frame, ...this.frames])
|
|
323
|
+
return new BindingScope([frame, ...this.frames], this.propsObject)
|
|
180
324
|
}
|
|
181
325
|
|
|
182
326
|
/**
|
|
@@ -195,7 +339,7 @@ export class BindingScope {
|
|
|
195
339
|
if (hit) bindings.set(alias, hit.binding)
|
|
196
340
|
}
|
|
197
341
|
if (bindings.size === 0) return this
|
|
198
|
-
return new BindingScope([{ kind: 'callback', bindings }, ...this.frames])
|
|
342
|
+
return new BindingScope([{ kind: 'callback', bindings }, ...this.frames], this.propsObject)
|
|
199
343
|
}
|
|
200
344
|
|
|
201
345
|
/** Innermost-first membership check across every frame in the stack. */
|
|
@@ -206,11 +350,59 @@ export class BindingScope {
|
|
|
206
350
|
return false
|
|
207
351
|
}
|
|
208
352
|
|
|
209
|
-
/**
|
|
210
|
-
|
|
211
|
-
|
|
353
|
+
/**
|
|
354
|
+
* Whether a binding here shadows the component's props object
|
|
355
|
+
* ({@link propsObject}): a `.map((props) => …)` row, a
|
|
356
|
+
* destructured `({ props })` field, a preamble local or a callback param.
|
|
357
|
+
* `props.X` then reads that binding's member, not the component's prop
|
|
358
|
+
* `X` (#3693).
|
|
359
|
+
*/
|
|
360
|
+
shadowsPropsObject(): boolean {
|
|
361
|
+
return this.isBound(this.propsObject)
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Resolve string typing at this position, respecting the nearest binding.
|
|
366
|
+
* `componentProperty`: `name` is read as the literal `props.<name>`. That is
|
|
367
|
+
* the component's prop only when `props` is this component's props object
|
|
368
|
+
* and no binding shadows it; while a binding named `props` is in scope, it
|
|
369
|
+
* is that binding's member. `path`: the read is the member chain
|
|
370
|
+
* `name.<path…>` (`row.id`). A loop row binding resolves the chain against
|
|
371
|
+
* its row type (`LoopBindingSource.rowType`), through `namedFields` for a
|
|
372
|
+
* named type; an outer name has typing for itself only.
|
|
373
|
+
*/
|
|
374
|
+
isStringValueName(
|
|
375
|
+
name: string,
|
|
376
|
+
outer: (name: string) => boolean,
|
|
377
|
+
componentProperty = false,
|
|
378
|
+
path: readonly string[] = [],
|
|
379
|
+
namedFields?: NamedTypeFields,
|
|
380
|
+
): boolean {
|
|
381
|
+
if (componentProperty) {
|
|
382
|
+
// The operand classifiers flag only the literal receiver `props`
|
|
383
|
+
// (`props.<name>`). A binding named `props` reads its own member.
|
|
384
|
+
if (this.isBound(PROPS_OBJECT_IDENTIFIER)) {
|
|
385
|
+
return this.isStringValueName(PROPS_OBJECT_IDENTIFIER, outer, false, [name, ...path], namedFields)
|
|
386
|
+
}
|
|
387
|
+
// In a component whose props object has another name (`function C(p)`),
|
|
388
|
+
// an unbound `props` is not that object, so it carries no prop typing.
|
|
389
|
+
if (this.propsObject !== PROPS_OBJECT_IDENTIFIER) return false
|
|
390
|
+
return path.length === 0 && outer(name)
|
|
391
|
+
}
|
|
212
392
|
const hit = this.lookup(name)
|
|
213
|
-
|
|
393
|
+
// An outer name's typing covers the name itself, not a member of it.
|
|
394
|
+
if (!hit) return path.length === 0 && outer(name)
|
|
395
|
+
const { stringValue, row } = hit.binding
|
|
396
|
+
if (path.length === 0 && stringValue !== undefined) return stringValue
|
|
397
|
+
if (!row) return false
|
|
398
|
+
const segments = [...row.segments, ...path.map(key => ({ kind: 'field' as const, key, isIdent: true }))]
|
|
399
|
+
const type = loopValueTypeAt(row.itemType, segments, namedFields)
|
|
400
|
+
return type !== null && isStringLoopValue(type)
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** Whether any frame is a loop row: the position sits inside a `.map()` row body. */
|
|
404
|
+
insideLoopRow(): boolean {
|
|
405
|
+
return this.frames.some(frame => frame.kind === 'loop-row')
|
|
214
406
|
}
|
|
215
407
|
|
|
216
408
|
/** The names the innermost frame binds, in binding order. */
|
|
@@ -141,11 +141,12 @@ function computedNameOf(n: ts.Node): ts.ComputedPropertyName | undefined {
|
|
|
141
141
|
/**
|
|
142
142
|
* Depth-first walk that maintains the binding stack described above and
|
|
143
143
|
* reports every identifier along with whether a binding within `root`
|
|
144
|
-
* currently shadows it.
|
|
144
|
+
* currently shadows it. `isShadowed` answers the same question for any
|
|
145
|
+
* other name at that identifier's position.
|
|
145
146
|
*/
|
|
146
147
|
export function walkWithScope(
|
|
147
148
|
root: ts.Node,
|
|
148
|
-
visit: (ident: ts.Identifier, parent: ts.Node | undefined, shadowed: boolean) => void,
|
|
149
|
+
visit: (ident: ts.Identifier, parent: ts.Node | undefined, shadowed: boolean, isShadowed: (name: string) => boolean) => void,
|
|
149
150
|
): void {
|
|
150
151
|
const scopeStack: Set<string>[] = []
|
|
151
152
|
const isShadowed = (name: string) => scopeStack.some(frame => frame.has(name))
|
|
@@ -157,7 +158,7 @@ export function walkWithScope(
|
|
|
157
158
|
const outerName = frame ? computedNameOf(n) : undefined
|
|
158
159
|
if (outerName) rec(outerName, n)
|
|
159
160
|
if (frame) scopeStack.push(frame)
|
|
160
|
-
if (ts.isIdentifier(n)) visit(n, parent, isShadowed(n.text))
|
|
161
|
+
if (ts.isIdentifier(n)) visit(n, parent, isShadowed(n.text), isShadowed)
|
|
161
162
|
ts.forEachChild(n, child => {
|
|
162
163
|
if (child !== outerName) rec(child, n)
|
|
163
164
|
})
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { collectLoopBoundNames } from '../adapters/loop-bound-names.ts'
|
|
2
|
+
import type { ParsedExpr } from '../expression-parser.ts'
|
|
2
3
|
import type { ComponentIR } from '../types.ts'
|
|
3
4
|
import type { BindingScope } from './binding-scope.ts'
|
|
4
5
|
|
|
@@ -79,3 +80,25 @@ export function rootPropReadName(
|
|
|
79
80
|
if (!scope.isBound(name)) return name
|
|
80
81
|
return aliases.get(name) ?? name
|
|
81
82
|
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Whether `expr` reads the component's props object at a position where
|
|
86
|
+
* `isBound` answers which names a binding shadows: the bare identifier
|
|
87
|
+
* `propsObject` that no binding shadows (#3693). `propsObject` is the
|
|
88
|
+
* component's props object identifier, `BindingScope.propsObject`: the
|
|
89
|
+
* props parameter's own name (`function C(p)` reads `p.X`), or `props` for
|
|
90
|
+
* a component that destructures its props. A `.map((props) => …)` row, a
|
|
91
|
+
* destructured `({ props })` field, a preamble local or a callback param of
|
|
92
|
+
* that name is that binding instead, so `props.X` reads its member rather
|
|
93
|
+
* than flattening to the component's prop `X`. Template emitters flatten a
|
|
94
|
+
* props-object member read only when this holds; `isBound` is the
|
|
95
|
+
* adapter's current `BindingScope` (`scope.isBound`), or, in a filter
|
|
96
|
+
* predicate, the predicate's own params plus the enclosing scope.
|
|
97
|
+
*/
|
|
98
|
+
export function isRootPropsObject(
|
|
99
|
+
expr: ParsedExpr,
|
|
100
|
+
isBound: (name: string) => boolean,
|
|
101
|
+
propsObject: string,
|
|
102
|
+
): boolean {
|
|
103
|
+
return expr.kind === 'identifier' && expr.name === propsObject && !isBound(expr.name)
|
|
104
|
+
}
|
|
@@ -71,36 +71,113 @@ interface SeparatedBinding {
|
|
|
71
71
|
name: string
|
|
72
72
|
loc: SourceLocation
|
|
73
73
|
prefix: 'bfSignal' | 'bfMemo'
|
|
74
|
+
/** Its own initializer derives from the same-named prop. */
|
|
75
|
+
selfDerived: boolean
|
|
74
76
|
}
|
|
75
77
|
|
|
76
78
|
/**
|
|
77
79
|
* Signal getters and memos that share a name with a prop of a bare-props
|
|
78
80
|
* component (`props.label` beside `const [label] = createSignal(...)` or
|
|
79
|
-
* `const label = createMemo(...)`).
|
|
80
|
-
* backend may fold it (Go exports
|
|
81
|
-
* `Label`).
|
|
81
|
+
* `const label = createMemo(...)`). `propNames` holds lower-cased names:
|
|
82
|
+
* the comparison ignores case because a backend may fold it (Go exports
|
|
83
|
+
* both `label` and `Label` as the field `Label`).
|
|
82
84
|
*
|
|
83
85
|
* A binding whose own initializer derives from that same prop
|
|
84
|
-
* (`createSignal(props.label ?? 'x')`) is
|
|
85
|
-
* backends keep it on the prop's variable, seeded from the
|
|
86
|
-
* `extractSsrDefaults` decides that case with the same
|
|
87
|
-
*
|
|
88
|
-
*
|
|
86
|
+
* (`createSignal(props.label ?? 'x')`) is marked `selfDerived`: the
|
|
87
|
+
* template-stash backends keep it on the prop's variable, seeded from the
|
|
88
|
+
* raw prop (`extractSsrDefaults` decides that case with the same
|
|
89
|
+
* `referencesOwnProp` test), unless the component also reads the raw prop
|
|
90
|
+
* (`separateSignalPropBindings`).
|
|
89
91
|
*/
|
|
90
|
-
function collidingBindings(ctx: AnalyzerContext, propsObjectName: string): SeparatedBinding[] {
|
|
91
|
-
const propNames = new Set(ctx.propsParams.map(p => p.name.toLowerCase()))
|
|
92
|
+
function collidingBindings(ctx: AnalyzerContext, propsObjectName: string, propNames: ReadonlySet<string>): SeparatedBinding[] {
|
|
92
93
|
const localConsts = localConstValuesByName(ctx)
|
|
93
|
-
const collides = (name: string
|
|
94
|
-
|
|
94
|
+
const collides = (name: string): boolean => propNames.has(name.toLowerCase())
|
|
95
|
+
const selfDerived = (name: string, derivation: string): boolean =>
|
|
96
|
+
referencesOwnProp(derivation, propsObjectName, name, localConsts)
|
|
95
97
|
const signals: SeparatedBinding[] = ctx.signals
|
|
96
|
-
.filter(s => !s.isModule && !s.getterElided && !s.envReader && collides(s.getter
|
|
97
|
-
.map(s => ({ name: s.getter, loc: s.loc, prefix: 'bfSignal' }))
|
|
98
|
+
.filter(s => !s.isModule && !s.getterElided && !s.envReader && collides(s.getter))
|
|
99
|
+
.map(s => ({ name: s.getter, loc: s.loc, prefix: 'bfSignal', selfDerived: selfDerived(s.getter, s.initialValue) }))
|
|
98
100
|
const memos: SeparatedBinding[] = ctx.memos
|
|
99
|
-
.filter(m => !m.isModule && collides(m.name
|
|
100
|
-
.map(m => ({ name: m.name, loc: m.loc, prefix: 'bfMemo' }))
|
|
101
|
+
.filter(m => !m.isModule && collides(m.name))
|
|
102
|
+
.map(m => ({ name: m.name, loc: m.loc, prefix: 'bfMemo', selfDerived: selfDerived(m.name, m.computation) }))
|
|
101
103
|
return [...signals, ...memos]
|
|
102
104
|
}
|
|
103
105
|
|
|
106
|
+
/**
|
|
107
|
+
* A property read off the props object (`props.label`): its lower-cased
|
|
108
|
+
* name and position. `name` is null for a computed key (`props[k]`), which
|
|
109
|
+
* may read any prop.
|
|
110
|
+
*/
|
|
111
|
+
interface PropsRead {
|
|
112
|
+
name: string | null
|
|
113
|
+
pos: number
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The key a props read names: an identifier member (`props.label`), or an
|
|
118
|
+
* element access with a literal key (`props['label']`, `` props[`label`] ``,
|
|
119
|
+
* `props[0]`), each also optional (`props?.['label']`). The key is the
|
|
120
|
+
* literal's own text, so a numeric key names its number and never matches
|
|
121
|
+
* an identifier. Null for a computed key. Undefined when `node` is no
|
|
122
|
+
* member access off `propsObjectName`.
|
|
123
|
+
*/
|
|
124
|
+
function propsReadKey(node: ts.Node, propsObjectName: string): { receiver: ts.Identifier; key: string | null } | undefined {
|
|
125
|
+
if (ts.isPropertyAccessExpression(node) && ts.isIdentifier(node.name)) {
|
|
126
|
+
return isNamedReceiver(node.expression, propsObjectName) ? { receiver: node.expression, key: node.name.text } : undefined
|
|
127
|
+
}
|
|
128
|
+
if (!ts.isElementAccessExpression(node) || !isNamedReceiver(node.expression, propsObjectName)) return undefined
|
|
129
|
+
const arg = node.argumentExpression
|
|
130
|
+
const literal = ts.isStringLiteral(arg) || ts.isNoSubstitutionTemplateLiteral(arg) || ts.isNumericLiteral(arg)
|
|
131
|
+
return { receiver: node.expression, key: literal ? arg.text : null }
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function isNamedReceiver(expr: ts.Expression, propsObjectName: string): expr is ts.Identifier {
|
|
135
|
+
return ts.isIdentifier(expr) && expr.text === propsObjectName
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Every property read off the props object (`props.label`,
|
|
140
|
+
* `props['label']`) inside the component. An untyped `props` parameter
|
|
141
|
+
* declares no `propsParams`, so these reads are its only prop names.
|
|
142
|
+
* `isPropsObject` decides whether an access's receiver is the props
|
|
143
|
+
* binding itself.
|
|
144
|
+
*/
|
|
145
|
+
function propsReads(
|
|
146
|
+
component: ts.Node,
|
|
147
|
+
propsObjectName: string,
|
|
148
|
+
isPropsObject: (receiver: ts.Identifier) => boolean,
|
|
149
|
+
): PropsRead[] {
|
|
150
|
+
const reads: PropsRead[] = []
|
|
151
|
+
function visit(node: ts.Node): void {
|
|
152
|
+
const read = propsReadKey(node, propsObjectName)
|
|
153
|
+
if (read && isPropsObject(read.receiver)) {
|
|
154
|
+
reads.push({ name: read.key === null ? null : read.key.toLowerCase(), pos: node.getStart() })
|
|
155
|
+
}
|
|
156
|
+
ts.forEachChild(node, visit)
|
|
157
|
+
}
|
|
158
|
+
visit(component)
|
|
159
|
+
return reads
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** The prop names the reads spell; a computed-key read names none. */
|
|
163
|
+
function readNames(reads: readonly PropsRead[]): string[] {
|
|
164
|
+
return reads.flatMap(r => (r.name === null ? [] : [r.name]))
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Whether `binding`, declared by `declaration`, must leave the prop's
|
|
169
|
+
* name. A self-derived binding shares the prop's template variable only
|
|
170
|
+
* while nothing else reads the raw prop: a `props.<name>` read outside its
|
|
171
|
+
* own initializer would otherwise see the derived value. A computed-key
|
|
172
|
+
* read (`props[k]`) may read that prop, so it counts too.
|
|
173
|
+
*/
|
|
174
|
+
function needsSeparation(binding: SeparatedBinding, declaration: ts.VariableDeclaration, reads: readonly PropsRead[]): boolean {
|
|
175
|
+
if (!binding.selfDerived) return true
|
|
176
|
+
const init = declaration.initializer
|
|
177
|
+
const name = binding.name.toLowerCase()
|
|
178
|
+
return reads.some(r => (r.name === null || r.name === name) && (!init || r.pos < init.getStart() || r.pos >= init.getEnd()))
|
|
179
|
+
}
|
|
180
|
+
|
|
104
181
|
/**
|
|
105
182
|
* A prop and a signal getter or memo of the same name are different JS
|
|
106
183
|
* bindings (`props.label` and `label()`), but scalar template backends
|
|
@@ -119,15 +196,29 @@ export function separateSignalPropBindings(
|
|
|
119
196
|
ctx: AnalyzerContext,
|
|
120
197
|
isNonReference: (id: ts.Identifier) => boolean,
|
|
121
198
|
): string | null {
|
|
122
|
-
|
|
123
|
-
const
|
|
124
|
-
if (
|
|
199
|
+
const propsObjectName = ctx.propsObjectName
|
|
200
|
+
const component = ctx.componentNode
|
|
201
|
+
if (!propsObjectName || !component) return null
|
|
202
|
+
const declared = ctx.propsParams.map(p => p.name.toLowerCase())
|
|
203
|
+
// Every same-spelled read is a candidate before binding resolution, so
|
|
204
|
+
// the checker is built only when some binding can collide.
|
|
205
|
+
const spelled = readNames(propsReads(component, propsObjectName, () => true))
|
|
206
|
+
if (collidingBindings(ctx, propsObjectName, new Set([...declared, ...spelled])).length === 0) return null
|
|
125
207
|
|
|
126
208
|
const sf = ctx.sourceFile
|
|
127
209
|
const checker = ctx.checker ?? ts.createProgram([sf.fileName], { noLib: true, noResolve: true }, {
|
|
128
210
|
...ts.createCompilerHost({ noLib: true, noResolve: true }),
|
|
129
211
|
getSourceFile: () => sf,
|
|
130
212
|
}).getTypeChecker()
|
|
213
|
+
// A read counts only when its receiver is the props parameter, not a
|
|
214
|
+
// callback parameter or local that shadows the name.
|
|
215
|
+
const param = component.parameters.find(p => ts.isIdentifier(p.name) && p.name.text === propsObjectName)
|
|
216
|
+
const paramSymbol = param && checker.getSymbolAtLocation(param.name)
|
|
217
|
+
const reads = paramSymbol
|
|
218
|
+
? propsReads(component, propsObjectName, id => checker.getSymbolAtLocation(id) === paramSymbol)
|
|
219
|
+
: []
|
|
220
|
+
const candidates = collidingBindings(ctx, propsObjectName, new Set([...declared, ...readNames(reads)]))
|
|
221
|
+
if (candidates.length === 0) return null
|
|
131
222
|
const occupied = new Set<string>()
|
|
132
223
|
function collectNames(node: ts.Node): void {
|
|
133
224
|
// Also avoid case-fold collisions introduced by backend field naming.
|
|
@@ -145,7 +236,7 @@ export function separateSignalPropBindings(
|
|
|
145
236
|
function collectBindings(node: ts.Node): void {
|
|
146
237
|
if (ts.isVariableDeclaration(node)) {
|
|
147
238
|
const binding = declarations.get(node.getStart(sf))
|
|
148
|
-
if (binding) {
|
|
239
|
+
if (binding && needsSeparation(binding, node, reads)) {
|
|
149
240
|
const names = ts.isIdentifier(node.name) ? [node.name]
|
|
150
241
|
: ts.isArrayBindingPattern(node.name)
|
|
151
242
|
? node.name.elements.flatMap(e => ts.isBindingElement(e) && ts.isIdentifier(e.name) ? [e.name] : [])
|
|
@@ -164,7 +255,7 @@ export function separateSignalPropBindings(
|
|
|
164
255
|
}
|
|
165
256
|
ts.forEachChild(node, collectBindings)
|
|
166
257
|
}
|
|
167
|
-
collectBindings(
|
|
258
|
+
collectBindings(component)
|
|
168
259
|
|
|
169
260
|
const edits: { start: number; end: number; text: string }[] = []
|
|
170
261
|
function collectEdits(node: ts.Node): void {
|
|
@@ -180,7 +271,7 @@ export function separateSignalPropBindings(
|
|
|
180
271
|
}
|
|
181
272
|
ts.forEachChild(node, collectEdits)
|
|
182
273
|
}
|
|
183
|
-
collectEdits(
|
|
274
|
+
collectEdits(component)
|
|
184
275
|
if (edits.length === 0) return null
|
|
185
276
|
let source = sf.text
|
|
186
277
|
for (const edit of edits.sort((a, b) => b.start - a.start)) {
|
package/src/ssr-defaults.ts
CHANGED
|
@@ -176,10 +176,14 @@ function addPropSsrDefaults(
|
|
|
176
176
|
// A prop whose default reads an earlier prop (`{ off = false, on = !off }`,
|
|
177
177
|
// #3531) and that the seed plan re-binds in-template: its value depends on
|
|
178
178
|
// what the caller passed, so no static value is right, and a non-null seed
|
|
179
|
-
// would read as caller-supplied to the template's own `on ?? !off`.
|
|
179
|
+
// would read as caller-supplied to the template's own `on ?? !off`. A
|
|
180
|
+
// computed default that reads no earlier prop (`{ b = 'abc'.length }`)
|
|
181
|
+
// keeps its static value when it has one: that value is the default
|
|
182
|
+
// itself, so the template's `b ?? <default>` yields the same.
|
|
180
183
|
const plan = metadata.ssrSeedPlan ?? computeSsrSeedPlan(metadata)
|
|
184
|
+
const readsEarlier = new Set(metadata.propsParams.filter(p => p.defaultParts).map(p => p.name))
|
|
181
185
|
const templateDefaulted = new Set(
|
|
182
|
-
plan.steps.filter(s => s.kind === 'derived' && s.origin === 'prop').map(s => s.name),
|
|
186
|
+
plan.steps.filter(s => s.kind === 'derived' && s.origin === 'prop' && readsEarlier.has(s.name)).map(s => s.name),
|
|
183
187
|
)
|
|
184
188
|
for (const p of metadata.propsParams) {
|
|
185
189
|
if (p.isRest) continue
|
|
@@ -487,8 +491,10 @@ export function localConstValuesByName(metadata: Pick<IRMetadata, 'localConstant
|
|
|
487
491
|
*
|
|
488
492
|
* Also the analyzer's test for which same-named signal/memo keeps sharing
|
|
489
493
|
* the prop's template variable (`separateSignalPropBindings`): a binding
|
|
490
|
-
* this answers `false` for is renamed before IR is built, so
|
|
491
|
-
*
|
|
494
|
+
* this answers `false` for is renamed before IR is built, and so is one it
|
|
495
|
+
* answers `true` for when the component also reads the raw prop elsewhere.
|
|
496
|
+
* A renamed binding no longer matches here, so the two callers cannot
|
|
497
|
+
* disagree about which collisions stay collapsed.
|
|
492
498
|
*/
|
|
493
499
|
export function referencesOwnProp(
|
|
494
500
|
expr: string | undefined,
|