Skip to content

Commit 8dc3b2e

Browse files
committed
Merge pull request #7688 from Microsoft/Fix7629
Fix #7629: Check if errors are needed before reporting them in enumRelatedTo
2 parents 6cc1b17 + bdb741e commit 8dc3b2e

File tree

5 files changed

+201
-5
lines changed

5 files changed

+201
-5
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5643,7 +5643,7 @@ namespace ts {
56435643
}
56445644
if (source.flags & TypeFlags.Enum && target === numberType) return Ternary.True;
56455645
if (source.flags & TypeFlags.Enum && target.flags & TypeFlags.Enum) {
5646-
if (result = enumRelatedTo(source, target)) {
5646+
if (result = enumRelatedTo(source, target, reportErrors)) {
56475647
return result;
56485648
}
56495649
}
@@ -6266,7 +6266,7 @@ namespace ts {
62666266
return Ternary.False;
62676267
}
62686268

6269-
function enumRelatedTo(source: Type, target: Type) {
6269+
function enumRelatedTo(source: Type, target: Type, reportErrors?: boolean) {
62706270
if (source.symbol.name !== target.symbol.name ||
62716271
source.symbol.flags & SymbolFlags.ConstEnum ||
62726272
target.symbol.flags & SymbolFlags.ConstEnum) {
@@ -6277,9 +6277,11 @@ namespace ts {
62776277
if (property.flags & SymbolFlags.EnumMember) {
62786278
const targetProperty = getPropertyOfType(targetEnumType, property.name);
62796279
if (!targetProperty || !(targetProperty.flags & SymbolFlags.EnumMember)) {
6280-
reportError(Diagnostics.Property_0_is_missing_in_type_1,
6281-
property.name,
6282-
typeToString(target, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType));
6280+
if (reportErrors) {
6281+
reportError(Diagnostics.Property_0_is_missing_in_type_1,
6282+
property.name,
6283+
typeToString(target, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType));
6284+
}
62836285
return Ternary.False;
62846286
}
62856287
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//// [enumAssignmentCompat4.ts]
2+
namespace M {
3+
export enum MyEnum {
4+
BAR
5+
}
6+
export var object2 = {
7+
foo: MyEnum.BAR
8+
};
9+
}
10+
11+
namespace N {
12+
export enum MyEnum {
13+
FOO
14+
};
15+
export var object1 = {
16+
foo: MyEnum.FOO
17+
};
18+
}
19+
20+
let broken = [
21+
N.object1,
22+
M.object2
23+
];
24+
25+
26+
//// [enumAssignmentCompat4.js]
27+
var M;
28+
(function (M) {
29+
(function (MyEnum) {
30+
MyEnum[MyEnum["BAR"] = 0] = "BAR";
31+
})(M.MyEnum || (M.MyEnum = {}));
32+
var MyEnum = M.MyEnum;
33+
M.object2 = {
34+
foo: MyEnum.BAR
35+
};
36+
})(M || (M = {}));
37+
var N;
38+
(function (N) {
39+
(function (MyEnum) {
40+
MyEnum[MyEnum["FOO"] = 0] = "FOO";
41+
})(N.MyEnum || (N.MyEnum = {}));
42+
var MyEnum = N.MyEnum;
43+
;
44+
N.object1 = {
45+
foo: MyEnum.FOO
46+
};
47+
})(N || (N = {}));
48+
var broken = [
49+
N.object1,
50+
M.object2
51+
];
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
=== tests/cases/compiler/enumAssignmentCompat4.ts ===
2+
namespace M {
3+
>M : Symbol(M, Decl(enumAssignmentCompat4.ts, 0, 0))
4+
5+
export enum MyEnum {
6+
>MyEnum : Symbol(MyEnum, Decl(enumAssignmentCompat4.ts, 0, 13))
7+
8+
BAR
9+
>BAR : Symbol(MyEnum.BAR, Decl(enumAssignmentCompat4.ts, 1, 24))
10+
}
11+
export var object2 = {
12+
>object2 : Symbol(object2, Decl(enumAssignmentCompat4.ts, 4, 14))
13+
14+
foo: MyEnum.BAR
15+
>foo : Symbol(foo, Decl(enumAssignmentCompat4.ts, 4, 26))
16+
>MyEnum.BAR : Symbol(MyEnum.BAR, Decl(enumAssignmentCompat4.ts, 1, 24))
17+
>MyEnum : Symbol(MyEnum, Decl(enumAssignmentCompat4.ts, 0, 13))
18+
>BAR : Symbol(MyEnum.BAR, Decl(enumAssignmentCompat4.ts, 1, 24))
19+
20+
};
21+
}
22+
23+
namespace N {
24+
>N : Symbol(N, Decl(enumAssignmentCompat4.ts, 7, 1))
25+
26+
export enum MyEnum {
27+
>MyEnum : Symbol(MyEnum, Decl(enumAssignmentCompat4.ts, 9, 13))
28+
29+
FOO
30+
>FOO : Symbol(MyEnum.FOO, Decl(enumAssignmentCompat4.ts, 10, 24))
31+
32+
};
33+
export var object1 = {
34+
>object1 : Symbol(object1, Decl(enumAssignmentCompat4.ts, 13, 14))
35+
36+
foo: MyEnum.FOO
37+
>foo : Symbol(foo, Decl(enumAssignmentCompat4.ts, 13, 26))
38+
>MyEnum.FOO : Symbol(MyEnum.FOO, Decl(enumAssignmentCompat4.ts, 10, 24))
39+
>MyEnum : Symbol(MyEnum, Decl(enumAssignmentCompat4.ts, 9, 13))
40+
>FOO : Symbol(MyEnum.FOO, Decl(enumAssignmentCompat4.ts, 10, 24))
41+
42+
};
43+
}
44+
45+
let broken = [
46+
>broken : Symbol(broken, Decl(enumAssignmentCompat4.ts, 18, 3))
47+
48+
N.object1,
49+
>N.object1 : Symbol(N.object1, Decl(enumAssignmentCompat4.ts, 13, 14))
50+
>N : Symbol(N, Decl(enumAssignmentCompat4.ts, 7, 1))
51+
>object1 : Symbol(N.object1, Decl(enumAssignmentCompat4.ts, 13, 14))
52+
53+
M.object2
54+
>M.object2 : Symbol(M.object2, Decl(enumAssignmentCompat4.ts, 4, 14))
55+
>M : Symbol(M, Decl(enumAssignmentCompat4.ts, 0, 0))
56+
>object2 : Symbol(M.object2, Decl(enumAssignmentCompat4.ts, 4, 14))
57+
58+
];
59+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
=== tests/cases/compiler/enumAssignmentCompat4.ts ===
2+
namespace M {
3+
>M : typeof M
4+
5+
export enum MyEnum {
6+
>MyEnum : MyEnum
7+
8+
BAR
9+
>BAR : MyEnum
10+
}
11+
export var object2 = {
12+
>object2 : { foo: MyEnum; }
13+
>{ foo: MyEnum.BAR } : { foo: MyEnum; }
14+
15+
foo: MyEnum.BAR
16+
>foo : MyEnum
17+
>MyEnum.BAR : MyEnum
18+
>MyEnum : typeof MyEnum
19+
>BAR : MyEnum
20+
21+
};
22+
}
23+
24+
namespace N {
25+
>N : typeof N
26+
27+
export enum MyEnum {
28+
>MyEnum : MyEnum
29+
30+
FOO
31+
>FOO : MyEnum
32+
33+
};
34+
export var object1 = {
35+
>object1 : { foo: MyEnum; }
36+
>{ foo: MyEnum.FOO } : { foo: MyEnum; }
37+
38+
foo: MyEnum.FOO
39+
>foo : MyEnum
40+
>MyEnum.FOO : MyEnum
41+
>MyEnum : typeof MyEnum
42+
>FOO : MyEnum
43+
44+
};
45+
}
46+
47+
let broken = [
48+
>broken : ({ foo: N.MyEnum; } | { foo: M.MyEnum; })[]
49+
>[ N.object1, M.object2] : ({ foo: N.MyEnum; } | { foo: M.MyEnum; })[]
50+
51+
N.object1,
52+
>N.object1 : { foo: N.MyEnum; }
53+
>N : typeof N
54+
>object1 : { foo: N.MyEnum; }
55+
56+
M.object2
57+
>M.object2 : { foo: M.MyEnum; }
58+
>M : typeof M
59+
>object2 : { foo: M.MyEnum; }
60+
61+
];
62+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace M {
2+
export enum MyEnum {
3+
BAR
4+
}
5+
export var object2 = {
6+
foo: MyEnum.BAR
7+
};
8+
}
9+
10+
namespace N {
11+
export enum MyEnum {
12+
FOO
13+
};
14+
export var object1 = {
15+
foo: MyEnum.FOO
16+
};
17+
}
18+
19+
let broken = [
20+
N.object1,
21+
M.object2
22+
];

0 commit comments

Comments
 (0)