Improve ancestor lookup when checking if an expression is spread into an object

This commit is contained in:
Mateusz Burzyński
2022-07-17 08:55:02 +02:00
parent 46aadc79ab
commit 232a668ce0
6 changed files with 62 additions and 22 deletions

View File

@@ -28086,8 +28086,7 @@ namespace ts {
// type with those properties for which the binding pattern specifies a default value.
// If the object literal is spread into another object literal, skip this step and let the top-level object
// literal handle it instead.
const spreadOrOutsidePossibleObjectParent = findAncestor(node, n => n.kind === SyntaxKind.SpreadAssignment || isStatement(n) || isArrowFunction(n))!;
if (contextualTypeHasPattern && spreadOrOutsidePossibleObjectParent.kind !== SyntaxKind.SpreadAssignment) {
if (contextualTypeHasPattern && findAncestor(node, n => n === contextualType.pattern!.parent || (n.kind === SyntaxKind.SpreadAssignment && getContextualType(n.parent as Expression, /*contextFlags*/undefined)!.pattern === contextualType.pattern))!.kind !== SyntaxKind.SpreadAssignment) {
for (const prop of getPropertiesOfType(contextualType)) {
if (!propertiesTable.get(prop.escapedName) && !getPropertyOfType(spread, prop.escapedName)) {
if (!(prop.flags & SymbolFlags.Optional)) {

View File

@@ -0,0 +1,23 @@
=== tests/cases/compiler/spreadExpressionContainingObjectExpressionContextualType.ts ===
// repro #49585
const { value } = (() => ({
>value : Symbol(value, Decl(spreadExpressionContainingObjectExpressionContextualType.ts, 2, 7))
value: "",
>value : Symbol(value, Decl(spreadExpressionContainingObjectExpressionContextualType.ts, 2, 27))
...(true ? {} : {}),
}))();
// repro 49684#discussion_r920545763
const { value2 } = {
>value2 : Symbol(value2, Decl(spreadExpressionContainingObjectExpressionContextualType.ts, 9, 7))
value2: "",
>value2 : Symbol(value2, Decl(spreadExpressionContainingObjectExpressionContextualType.ts, 9, 20))
...(() => true ? {} : {})(),
};

View File

@@ -1,4 +1,4 @@
=== tests/cases/compiler/spreadExpressionWithinIIFEReturnAssignedtoPattern.ts ===
=== tests/cases/compiler/spreadExpressionContainingObjectExpressionContextualType.ts ===
// repro #49585
const { value } = (() => ({
@@ -21,3 +21,25 @@ const { value } = (() => ({
>{} : {}
}))();
// repro 49684#discussion_r920545763
const { value2 } = {
>value2 : string
>{ value2: "", ...(() => true ? {} : {})(),} : { value2: string; }
value2: "",
>value2 : string
>"" : ""
...(() => true ? {} : {})(),
>(() => true ? {} : {})() : {}
>(() => true ? {} : {}) : () => {}
>() => true ? {} : {} : () => {}
>true ? {} : {} : {}
>true : true
>{} : {}
>{} : {}
};

View File

@@ -1,11 +0,0 @@
=== tests/cases/compiler/spreadExpressionWithinIIFEReturnAssignedtoPattern.ts ===
// repro #49585
const { value } = (() => ({
>value : Symbol(value, Decl(spreadExpressionWithinIIFEReturnAssignedtoPattern.ts, 2, 7))
value: "",
>value : Symbol(value, Decl(spreadExpressionWithinIIFEReturnAssignedtoPattern.ts, 2, 27))
...(true ? {} : {}),
}))();

View File

@@ -0,0 +1,15 @@
// @noEmit: true
// repro #49585
const { value } = (() => ({
value: "",
...(true ? {} : {}),
}))();
// repro 49684#discussion_r920545763
const { value2 } = {
value2: "",
...(() => true ? {} : {})(),
};

View File

@@ -1,8 +0,0 @@
// @noEmit: true
// repro #49585
const { value } = (() => ({
value: "",
...(true ? {} : {}),
}))();