Skip to content

Commit 3f3b05e

Browse files
committed
Properly track method/property distinction for intersection members
1 parent 6632e32 commit 3f3b05e

File tree

4 files changed

+32
-26
lines changed

4 files changed

+32
-26
lines changed

src/compiler/checker.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5129,7 +5129,8 @@ namespace ts {
51295129
const excludeModifiers = isUnion ? ModifierFlags.NonPublicAccessibilityModifier : 0;
51305130
// Flags we want to propagate to the result if they exist in all source symbols
51315131
let commonFlags = isUnion ? SymbolFlags.None : SymbolFlags.Optional;
5132-
let checkFlags = CheckFlags.SyntheticProperty;
5132+
let syntheticFlag = CheckFlags.SyntheticMethod;
5133+
let checkFlags = 0;
51335134
for (const current of types) {
51345135
const type = getApparentType(current);
51355136
if (type !== unknownType) {
@@ -5148,6 +5149,9 @@ namespace ts {
51485149
(modifiers & ModifierFlags.Protected ? CheckFlags.ContainsProtected : 0) |
51495150
(modifiers & ModifierFlags.Private ? CheckFlags.ContainsPrivate : 0) |
51505151
(modifiers & ModifierFlags.Static ? CheckFlags.ContainsStatic : 0);
5152+
if (!isMethodLike(prop)) {
5153+
syntheticFlag = CheckFlags.SyntheticProperty;
5154+
}
51515155
}
51525156
else if (isUnion) {
51535157
checkFlags |= CheckFlags.Partial;
@@ -5177,7 +5181,7 @@ namespace ts {
51775181
propTypes.push(type);
51785182
}
51795183
const result = createSymbol(SymbolFlags.Property | commonFlags, name);
5180-
result.checkFlags = checkFlags;
5184+
result.checkFlags = syntheticFlag | checkFlags;
51815185
result.containingType = containingType;
51825186
result.declarations = declarations;
51835187
result.type = isUnion ? getUnionType(propTypes) : getIntersectionType(propTypes);
@@ -8630,7 +8634,7 @@ namespace ts {
86308634
// Invoke the callback for each underlying property symbol of the given symbol and return the first
86318635
// value that isn't undefined.
86328636
function forEachProperty<T>(prop: Symbol, callback: (p: Symbol) => T): T {
8633-
if (getCheckFlags(prop) & CheckFlags.SyntheticProperty) {
8637+
if (getCheckFlags(prop) & CheckFlags.Synthetic) {
86348638
for (const t of (<TransientSymbol>prop).containingType.types) {
86358639
const p = getPropertyOfType(t, prop.name);
86368640
const result = p && forEachProperty(p, callback);
@@ -13112,7 +13116,7 @@ namespace ts {
1311213116
const flags = getCombinedModifierFlags(s.valueDeclaration);
1311313117
return s.parent && s.parent.flags & SymbolFlags.Class ? flags : flags & ~ModifierFlags.AccessibilityModifier;
1311413118
}
13115-
if (getCheckFlags(s) & CheckFlags.SyntheticProperty) {
13119+
if (getCheckFlags(s) & CheckFlags.Synthetic) {
1311613120
const checkFlags = (<TransientSymbol>s).checkFlags;
1311713121
const accessModifier = checkFlags & CheckFlags.ContainsPrivate ? ModifierFlags.Private :
1311813122
checkFlags & CheckFlags.ContainsPublic ? ModifierFlags.Public :
@@ -13130,6 +13134,10 @@ namespace ts {
1313013134
return s.valueDeclaration ? getCombinedNodeFlags(s.valueDeclaration) : 0;
1313113135
}
1313213136

13137+
function isMethodLike(symbol: Symbol) {
13138+
return !!(symbol.flags & SymbolFlags.Method || getCheckFlags(symbol) & CheckFlags.SyntheticMethod);
13139+
}
13140+
1313313141
/**
1313413142
* Check whether the requested property access is valid.
1313513143
* Returns true if node is a valid property access, and false otherwise.
@@ -13159,11 +13167,11 @@ namespace ts {
1315913167
// where this references the constructor function object of a derived class,
1316013168
// a super property access is permitted and must specify a public static member function of the base class.
1316113169
if (languageVersion < ScriptTarget.ES2015) {
13162-
const propKind = getDeclarationKindFromSymbol(prop);
13163-
if (propKind !== SyntaxKind.MethodDeclaration && propKind !== SyntaxKind.MethodSignature) {
13164-
// `prop` refers to a *property* declared in the super class
13165-
// rather than a *method*, so it does not satisfy the above criteria.
13166-
13170+
const hasNonMethodDeclaration = forEachProperty(prop, p => {
13171+
const propKind = getDeclarationKindFromSymbol(p);
13172+
return propKind !== SyntaxKind.MethodDeclaration && propKind !== SyntaxKind.MethodSignature;
13173+
});
13174+
if (hasNonMethodDeclaration) {
1316713175
error(errorNode, Diagnostics.Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword);
1316813176
return false;
1316913177
}
@@ -19697,7 +19705,7 @@ namespace ts {
1969719705
else {
1969819706
// derived overrides base.
1969919707
const derivedDeclarationFlags = getDeclarationModifierFlagsFromSymbol(derived);
19700-
if ((baseDeclarationFlags & ModifierFlags.Private) || (derivedDeclarationFlags & ModifierFlags.Private)) {
19708+
if (baseDeclarationFlags & ModifierFlags.Private || derivedDeclarationFlags & ModifierFlags.Private) {
1970119709
// either base or derived property is private - not override, skip it
1970219710
continue;
1970319711
}
@@ -19707,28 +19715,24 @@ namespace ts {
1970719715
continue;
1970819716
}
1970919717

19710-
if ((base.flags & derived.flags & SymbolFlags.Method) || ((base.flags & SymbolFlags.PropertyOrAccessor) && (derived.flags & SymbolFlags.PropertyOrAccessor))) {
19718+
if (isMethodLike(base) && isMethodLike(derived) || base.flags & SymbolFlags.PropertyOrAccessor && derived.flags & SymbolFlags.PropertyOrAccessor) {
1971119719
// method is overridden with method or property/accessor is overridden with property/accessor - correct case
1971219720
continue;
1971319721
}
1971419722

1971519723
let errorMessage: DiagnosticMessage;
19716-
if (base.flags & SymbolFlags.Method) {
19724+
if (isMethodLike(base)) {
1971719725
if (derived.flags & SymbolFlags.Accessor) {
1971819726
errorMessage = Diagnostics.Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_accessor;
1971919727
}
1972019728
else {
19721-
Debug.assert((derived.flags & SymbolFlags.Property) !== 0);
1972219729
errorMessage = Diagnostics.Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_property;
1972319730
}
1972419731
}
1972519732
else if (base.flags & SymbolFlags.Property) {
19726-
Debug.assert((derived.flags & SymbolFlags.Method) !== 0);
1972719733
errorMessage = Diagnostics.Class_0_defines_instance_member_property_1_but_extended_class_2_defines_it_as_instance_member_function;
1972819734
}
1972919735
else {
19730-
Debug.assert((base.flags & SymbolFlags.Accessor) !== 0);
19731-
Debug.assert((derived.flags & SymbolFlags.Method) !== 0);
1973219736
errorMessage = Diagnostics.Class_0_defines_instance_member_accessor_1_but_extended_class_2_defines_it_as_instance_member_function;
1973319737
}
1973419738

@@ -21387,7 +21391,7 @@ namespace ts {
2138721391
}
2138821392

2138921393
function getRootSymbols(symbol: Symbol): Symbol[] {
21390-
if (getCheckFlags(symbol) & CheckFlags.SyntheticProperty) {
21394+
if (getCheckFlags(symbol) & CheckFlags.Synthetic) {
2139121395
const symbols: Symbol[] = [];
2139221396
const name = symbol.name;
2139321397
forEach(getSymbolLinks(symbol).containingType.types, t => {

src/compiler/types.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,13 +2811,15 @@ namespace ts {
28112811
export const enum CheckFlags {
28122812
Instantiated = 1 << 0, // Instantiated symbol
28132813
SyntheticProperty = 1 << 1, // Property in union or intersection type
2814-
Readonly = 1 << 2, // Readonly transient symbol
2815-
Partial = 1 << 3, // Synthetic property present in some but not all constituents
2816-
HasNonUniformType = 1 << 4, // Synthetic property with non-uniform type in constituents
2817-
ContainsPublic = 1 << 5, // Synthetic property with public constituent(s)
2818-
ContainsProtected = 1 << 6, // Synthetic property with protected constituent(s)
2819-
ContainsPrivate = 1 << 7, // Synthetic property with private constituent(s)
2820-
ContainsStatic = 1 << 8, // Synthetic property with static constituent(s)
2814+
SyntheticMethod = 1 << 2, // Method in union or intersection type
2815+
Readonly = 1 << 3, // Readonly transient symbol
2816+
Partial = 1 << 4, // Synthetic property present in some but not all constituents
2817+
HasNonUniformType = 1 << 5, // Synthetic property with non-uniform type in constituents
2818+
ContainsPublic = 1 << 6, // Synthetic property with public constituent(s)
2819+
ContainsProtected = 1 << 7, // Synthetic property with protected constituent(s)
2820+
ContainsPrivate = 1 << 8, // Synthetic property with private constituent(s)
2821+
ContainsStatic = 1 << 9, // Synthetic property with static constituent(s)
2822+
Synthetic = SyntheticProperty | SyntheticMethod
28212823
}
28222824

28232825
/* @internal */

src/services/findAllReferences.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ namespace ts.FindAllReferences {
281281

282282
// if this symbol is visible from its parent container, e.g. exported, then bail out
283283
// if symbol correspond to the union property - bail out
284-
if (symbol.parent || (symbol.flags & SymbolFlags.Transient && (<TransientSymbol>symbol).checkFlags & CheckFlags.SyntheticProperty)) {
284+
if (symbol.parent || (symbol.flags & SymbolFlags.Transient && (<TransientSymbol>symbol).checkFlags & CheckFlags.Synthetic)) {
285285
return undefined;
286286
}
287287

src/services/symbolDisplay.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace ts.SymbolDisplay {
5252
if (flags & SymbolFlags.Constructor) return ScriptElementKind.constructorImplementationElement;
5353

5454
if (flags & SymbolFlags.Property) {
55-
if (flags & SymbolFlags.Transient && (<TransientSymbol>symbol).checkFlags & CheckFlags.SyntheticProperty) {
55+
if (flags & SymbolFlags.Transient && (<TransientSymbol>symbol).checkFlags & CheckFlags.Synthetic) {
5656
// If union property is result of union of non method (property/accessors/variables), it is labeled as property
5757
const unionPropertyKind = forEach(typeChecker.getRootSymbols(symbol), rootSymbol => {
5858
const rootSymbolFlags = rootSymbol.getFlags();

0 commit comments

Comments
 (0)