From bdb741e92a701b611873231fe2b04cdfced88157 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Fri, 25 Mar 2016 12:39:43 -0700 Subject: [PATCH] Fix #7629: Check if errros are needed before reporting them in enumRelatedTo --- src/compiler/checker.ts | 12 ++-- .../reference/enumAssignmentCompat4.js | 51 +++++++++++++++ .../reference/enumAssignmentCompat4.symbols | 59 ++++++++++++++++++ .../reference/enumAssignmentCompat4.types | 62 +++++++++++++++++++ tests/cases/compiler/enumAssignmentCompat4.ts | 22 +++++++ 5 files changed, 201 insertions(+), 5 deletions(-) create mode 100644 tests/baselines/reference/enumAssignmentCompat4.js create mode 100644 tests/baselines/reference/enumAssignmentCompat4.symbols create mode 100644 tests/baselines/reference/enumAssignmentCompat4.types create mode 100644 tests/cases/compiler/enumAssignmentCompat4.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7254f1c2a9db6..9509c286236ea 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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; } } @@ -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) { @@ -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; } } diff --git a/tests/baselines/reference/enumAssignmentCompat4.js b/tests/baselines/reference/enumAssignmentCompat4.js new file mode 100644 index 0000000000000..ad28324ce0fe1 --- /dev/null +++ b/tests/baselines/reference/enumAssignmentCompat4.js @@ -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 +]; diff --git a/tests/baselines/reference/enumAssignmentCompat4.symbols b/tests/baselines/reference/enumAssignmentCompat4.symbols new file mode 100644 index 0000000000000..a3a807316106b --- /dev/null +++ b/tests/baselines/reference/enumAssignmentCompat4.symbols @@ -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)) + +]; + diff --git a/tests/baselines/reference/enumAssignmentCompat4.types b/tests/baselines/reference/enumAssignmentCompat4.types new file mode 100644 index 0000000000000..db7f5ce8ba21d --- /dev/null +++ b/tests/baselines/reference/enumAssignmentCompat4.types @@ -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; } + +]; + diff --git a/tests/cases/compiler/enumAssignmentCompat4.ts b/tests/cases/compiler/enumAssignmentCompat4.ts new file mode 100644 index 0000000000000..0a5fc89660450 --- /dev/null +++ b/tests/cases/compiler/enumAssignmentCompat4.ts @@ -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 +];