Skip to content

Commit 876dbe8

Browse files
committed
Omit class methods from spreads. Others stay.
Previously, all methods were omitted except those from the object literal that contained the spread. This gets rid of the ugly third argument to `getSpreadType`. It also fixes a bug that arose from removing the spread type late in the development of object spread; methods from the left-hand-side of a multi-spread object literal were not removed. The spread type code normalised spreads so the left-hand is never an object, but that code was removed.
1 parent 5b075ff commit 876dbe8

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6241,7 +6241,7 @@ namespace ts {
62416241
* this function should be called in a left folding style, with left = previous result of getSpreadType
62426242
* and right = the new element to be spread.
62436243
*/
6244-
function getSpreadType(left: Type, right: Type, isFromObjectLiteral: boolean): Type {
6244+
function getSpreadType(left: Type, right: Type): Type {
62456245
if (left.flags & TypeFlags.Any || right.flags & TypeFlags.Any) {
62466246
return anyType;
62476247
}
@@ -6254,10 +6254,10 @@ namespace ts {
62546254
return left;
62556255
}
62566256
if (left.flags & TypeFlags.Union) {
6257-
return mapType(left, t => getSpreadType(t, right, isFromObjectLiteral));
6257+
return mapType(left, t => getSpreadType(t, right));
62586258
}
62596259
if (right.flags & TypeFlags.Union) {
6260-
return mapType(right, t => getSpreadType(left, t, isFromObjectLiteral));
6260+
return mapType(right, t => getSpreadType(left, t));
62616261
}
62626262

62636263
const members = createMap<Symbol>();
@@ -6276,18 +6276,18 @@ namespace ts {
62766276

62776277
for (const rightProp of getPropertiesOfType(right)) {
62786278
// we approximate own properties as non-methods plus methods that are inside the object literal
6279-
const isOwnProperty = !(rightProp.flags & SymbolFlags.Method) || isFromObjectLiteral;
62806279
const isSetterWithoutGetter = rightProp.flags & SymbolFlags.SetAccessor && !(rightProp.flags & SymbolFlags.GetAccessor);
62816280
if (getDeclarationModifierFlagsFromSymbol(rightProp) & (ModifierFlags.Private | ModifierFlags.Protected)) {
62826281
skippedPrivateMembers[rightProp.name] = true;
62836282
}
6284-
else if (isOwnProperty && !isSetterWithoutGetter) {
6283+
else if (!isClassMethod(rightProp) && !isSetterWithoutGetter) {
62856284
members[rightProp.name] = rightProp;
62866285
}
62876286
}
62886287
for (const leftProp of getPropertiesOfType(left)) {
62896288
if (leftProp.flags & SymbolFlags.SetAccessor && !(leftProp.flags & SymbolFlags.GetAccessor)
6290-
|| leftProp.name in skippedPrivateMembers) {
6289+
|| leftProp.name in skippedPrivateMembers
6290+
|| isClassMethod(leftProp)) {
62916291
continue;
62926292
}
62936293
if (leftProp.name in members) {
@@ -6312,6 +6312,10 @@ namespace ts {
63126312
return createAnonymousType(undefined, members, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo);
63136313
}
63146314

6315+
function isClassMethod(prop: Symbol) {
6316+
return prop.flags & SymbolFlags.Method && find(prop.declarations, decl => isClassLike(decl.parent));
6317+
}
6318+
63156319
function createLiteralType(flags: TypeFlags, text: string) {
63166320
const type = <LiteralType>createType(flags);
63176321
type.text = text;
@@ -11658,7 +11662,7 @@ namespace ts {
1165811662
checkExternalEmitHelpers(memberDecl, ExternalEmitHelpers.Assign);
1165911663
}
1166011664
if (propertiesArray.length > 0) {
11661-
spread = getSpreadType(spread, createObjectLiteralType(), /*isFromObjectLiteral*/ true);
11665+
spread = getSpreadType(spread, createObjectLiteralType());
1166211666
propertiesArray = [];
1166311667
propertiesTable = createMap<Symbol>();
1166411668
hasComputedStringProperty = false;
@@ -11670,7 +11674,7 @@ namespace ts {
1167011674
error(memberDecl, Diagnostics.Spread_types_may_only_be_created_from_object_types);
1167111675
return unknownType;
1167211676
}
11673-
spread = getSpreadType(spread, type, /*isFromObjectLiteral*/ false);
11677+
spread = getSpreadType(spread, type);
1167411678
offset = i + 1;
1167511679
continue;
1167611680
}
@@ -11715,7 +11719,7 @@ namespace ts {
1171511719

1171611720
if (spread !== emptyObjectType) {
1171711721
if (propertiesArray.length > 0) {
11718-
spread = getSpreadType(spread, createObjectLiteralType(), /*isFromObjectLiteral*/ true);
11722+
spread = getSpreadType(spread, createObjectLiteralType());
1171911723
}
1172011724
if (spread.flags & TypeFlags.Object) {
1172111725
// only set the symbol and flags if this is a (fresh) object type

0 commit comments

Comments
 (0)