Skip to content

Commit

Permalink
Fix #7629: Check if errros are needed before reporting them in enumRe…
Browse files Browse the repository at this point in the history
…latedTo
  • Loading branch information
mhegazy committed Mar 25, 2016
1 parent 6cc1b17 commit bdb741e
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5643,7 +5643,7 @@ namespace ts {
}
if (source.flags & TypeFlags.Enum && target === numberType) return Ternary.True;
if (source.flags & TypeFlags.Enum && target.flags & TypeFlags.Enum) {
if (result = enumRelatedTo(source, target)) {
if (result = enumRelatedTo(source, target, reportErrors)) {
return result;
}
}
Expand Down Expand Up @@ -6266,7 +6266,7 @@ namespace ts {
return Ternary.False;
}

function enumRelatedTo(source: Type, target: Type) {
function enumRelatedTo(source: Type, target: Type, reportErrors?: boolean) {
if (source.symbol.name !== target.symbol.name ||
source.symbol.flags & SymbolFlags.ConstEnum ||
target.symbol.flags & SymbolFlags.ConstEnum) {
Expand All @@ -6277,9 +6277,11 @@ namespace ts {
if (property.flags & SymbolFlags.EnumMember) {
const targetProperty = getPropertyOfType(targetEnumType, property.name);
if (!targetProperty || !(targetProperty.flags & SymbolFlags.EnumMember)) {
reportError(Diagnostics.Property_0_is_missing_in_type_1,
property.name,
typeToString(target, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType));
if (reportErrors) {
reportError(Diagnostics.Property_0_is_missing_in_type_1,
property.name,
typeToString(target, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType));
}
return Ternary.False;
}
}
Expand Down
51 changes: 51 additions & 0 deletions tests/baselines/reference/enumAssignmentCompat4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//// [enumAssignmentCompat4.ts]
namespace M {
export enum MyEnum {
BAR
}
export var object2 = {
foo: MyEnum.BAR
};
}

namespace N {
export enum MyEnum {
FOO
};
export var object1 = {
foo: MyEnum.FOO
};
}

let broken = [
N.object1,
M.object2
];


//// [enumAssignmentCompat4.js]
var M;
(function (M) {
(function (MyEnum) {
MyEnum[MyEnum["BAR"] = 0] = "BAR";
})(M.MyEnum || (M.MyEnum = {}));
var MyEnum = M.MyEnum;
M.object2 = {
foo: MyEnum.BAR
};
})(M || (M = {}));
var N;
(function (N) {
(function (MyEnum) {
MyEnum[MyEnum["FOO"] = 0] = "FOO";
})(N.MyEnum || (N.MyEnum = {}));
var MyEnum = N.MyEnum;
;
N.object1 = {
foo: MyEnum.FOO
};
})(N || (N = {}));
var broken = [
N.object1,
M.object2
];
59 changes: 59 additions & 0 deletions tests/baselines/reference/enumAssignmentCompat4.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
=== tests/cases/compiler/enumAssignmentCompat4.ts ===
namespace M {
>M : Symbol(M, Decl(enumAssignmentCompat4.ts, 0, 0))

export enum MyEnum {
>MyEnum : Symbol(MyEnum, Decl(enumAssignmentCompat4.ts, 0, 13))

BAR
>BAR : Symbol(MyEnum.BAR, Decl(enumAssignmentCompat4.ts, 1, 24))
}
export var object2 = {
>object2 : Symbol(object2, Decl(enumAssignmentCompat4.ts, 4, 14))

foo: MyEnum.BAR
>foo : Symbol(foo, Decl(enumAssignmentCompat4.ts, 4, 26))
>MyEnum.BAR : Symbol(MyEnum.BAR, Decl(enumAssignmentCompat4.ts, 1, 24))
>MyEnum : Symbol(MyEnum, Decl(enumAssignmentCompat4.ts, 0, 13))
>BAR : Symbol(MyEnum.BAR, Decl(enumAssignmentCompat4.ts, 1, 24))

};
}

namespace N {
>N : Symbol(N, Decl(enumAssignmentCompat4.ts, 7, 1))

export enum MyEnum {
>MyEnum : Symbol(MyEnum, Decl(enumAssignmentCompat4.ts, 9, 13))

FOO
>FOO : Symbol(MyEnum.FOO, Decl(enumAssignmentCompat4.ts, 10, 24))

};
export var object1 = {
>object1 : Symbol(object1, Decl(enumAssignmentCompat4.ts, 13, 14))

foo: MyEnum.FOO
>foo : Symbol(foo, Decl(enumAssignmentCompat4.ts, 13, 26))
>MyEnum.FOO : Symbol(MyEnum.FOO, Decl(enumAssignmentCompat4.ts, 10, 24))
>MyEnum : Symbol(MyEnum, Decl(enumAssignmentCompat4.ts, 9, 13))
>FOO : Symbol(MyEnum.FOO, Decl(enumAssignmentCompat4.ts, 10, 24))

};
}

let broken = [
>broken : Symbol(broken, Decl(enumAssignmentCompat4.ts, 18, 3))

N.object1,
>N.object1 : Symbol(N.object1, Decl(enumAssignmentCompat4.ts, 13, 14))
>N : Symbol(N, Decl(enumAssignmentCompat4.ts, 7, 1))
>object1 : Symbol(N.object1, Decl(enumAssignmentCompat4.ts, 13, 14))

M.object2
>M.object2 : Symbol(M.object2, Decl(enumAssignmentCompat4.ts, 4, 14))
>M : Symbol(M, Decl(enumAssignmentCompat4.ts, 0, 0))
>object2 : Symbol(M.object2, Decl(enumAssignmentCompat4.ts, 4, 14))

];

62 changes: 62 additions & 0 deletions tests/baselines/reference/enumAssignmentCompat4.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
=== tests/cases/compiler/enumAssignmentCompat4.ts ===
namespace M {
>M : typeof M

export enum MyEnum {
>MyEnum : MyEnum

BAR
>BAR : MyEnum
}
export var object2 = {
>object2 : { foo: MyEnum; }
>{ foo: MyEnum.BAR } : { foo: MyEnum; }

foo: MyEnum.BAR
>foo : MyEnum
>MyEnum.BAR : MyEnum
>MyEnum : typeof MyEnum
>BAR : MyEnum

};
}

namespace N {
>N : typeof N

export enum MyEnum {
>MyEnum : MyEnum

FOO
>FOO : MyEnum

};
export var object1 = {
>object1 : { foo: MyEnum; }
>{ foo: MyEnum.FOO } : { foo: MyEnum; }

foo: MyEnum.FOO
>foo : MyEnum
>MyEnum.FOO : MyEnum
>MyEnum : typeof MyEnum
>FOO : MyEnum

};
}

let broken = [
>broken : ({ foo: N.MyEnum; } | { foo: M.MyEnum; })[]
>[ N.object1, M.object2] : ({ foo: N.MyEnum; } | { foo: M.MyEnum; })[]

N.object1,
>N.object1 : { foo: N.MyEnum; }
>N : typeof N
>object1 : { foo: N.MyEnum; }

M.object2
>M.object2 : { foo: M.MyEnum; }
>M : typeof M
>object2 : { foo: M.MyEnum; }

];

22 changes: 22 additions & 0 deletions tests/cases/compiler/enumAssignmentCompat4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace M {
export enum MyEnum {
BAR
}
export var object2 = {
foo: MyEnum.BAR
};
}

namespace N {
export enum MyEnum {
FOO
};
export var object1 = {
foo: MyEnum.FOO
};
}

let broken = [
N.object1,
M.object2
];

0 comments on commit bdb741e

Please sign in to comment.