diff --git a/tests/baselines/reference/constEnum1.js b/tests/baselines/reference/constEnum1.js new file mode 100644 index 00000000000..d5ccede3ff1 --- /dev/null +++ b/tests/baselines/reference/constEnum1.js @@ -0,0 +1,34 @@ +//// [constEnum1.ts] + +// An enum declaration that specifies a const modifier is a constant enum declaration. +// In a constant enum declaration, all members must have constant values and +// it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression. + +const enum E { + a = 10, + b = a, + c = (a+1), + e, + d = ~e, + f = a << 2 >> 1, + g = a << 2 >>> 1, + h = a | b +} + +//// [constEnum1.js] +// An enum declaration that specifies a const modifier is a constant enum declaration. +// In a constant enum declaration, all members must have constant values and +// it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression. + + +//// [constEnum1.d.ts] +declare const enum E { + a = 10, + b = 10, + c = 11, + e = 12, + d = -13, + f = 20, + g = 20, + h = 10, +} diff --git a/tests/baselines/reference/constEnum1.types b/tests/baselines/reference/constEnum1.types new file mode 100644 index 00000000000..ef0e64d4377 --- /dev/null +++ b/tests/baselines/reference/constEnum1.types @@ -0,0 +1,54 @@ +=== tests/cases/conformance/constEnums/constEnum1.ts === + +// An enum declaration that specifies a const modifier is a constant enum declaration. +// In a constant enum declaration, all members must have constant values and +// it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression. + +const enum E { +>E : E, Symbol(E, Decl(constEnum1.ts, 0, 0)) + + a = 10, +>a : E, Symbol(E.a, Decl(constEnum1.ts, 5, 14)) +>10 : number + + b = a, +>b : E, Symbol(E.b, Decl(constEnum1.ts, 6, 11)) +>a : E, Symbol(E.a, Decl(constEnum1.ts, 5, 14)) + + c = (a+1), +>c : E, Symbol(E.c, Decl(constEnum1.ts, 7, 10)) +>(a+1) : number +>a+1 : number +>a : E, Symbol(E.a, Decl(constEnum1.ts, 5, 14)) +>1 : number + + e, +>e : E, Symbol(E.e, Decl(constEnum1.ts, 8, 14)) + + d = ~e, +>d : E, Symbol(E.d, Decl(constEnum1.ts, 9, 6)) +>~e : number +>e : E, Symbol(E.e, Decl(constEnum1.ts, 8, 14)) + + f = a << 2 >> 1, +>f : E, Symbol(E.f, Decl(constEnum1.ts, 10, 11)) +>a << 2 >> 1 : number +>a << 2 : number +>a : E, Symbol(E.a, Decl(constEnum1.ts, 5, 14)) +>2 : number +>1 : number + + g = a << 2 >>> 1, +>g : E, Symbol(E.g, Decl(constEnum1.ts, 11, 20)) +>a << 2 >>> 1 : number +>a << 2 : number +>a : E, Symbol(E.a, Decl(constEnum1.ts, 5, 14)) +>2 : number +>1 : number + + h = a | b +>h : E, Symbol(E.h, Decl(constEnum1.ts, 12, 21)) +>a | b : number +>a : E, Symbol(E.a, Decl(constEnum1.ts, 5, 14)) +>b : E, Symbol(E.b, Decl(constEnum1.ts, 6, 11)) +} diff --git a/tests/baselines/reference/constEnum2.errors.txt b/tests/baselines/reference/constEnum2.errors.txt new file mode 100644 index 00000000000..f0cac0c2f5d --- /dev/null +++ b/tests/baselines/reference/constEnum2.errors.txt @@ -0,0 +1,29 @@ +tests/cases/conformance/constEnums/constEnum2.ts(11,9): error TS2474: In 'const' enum declarations member initializer must be constant expression. +tests/cases/conformance/constEnums/constEnum2.ts(12,9): error TS2474: In 'const' enum declarations member initializer must be constant expression. +tests/cases/conformance/constEnums/constEnum2.ts(13,5): error TS1005: ',' expected. +tests/cases/conformance/constEnums/constEnum2.ts(13,9): error TS2474: In 'const' enum declarations member initializer must be constant expression. + + +==== tests/cases/conformance/constEnums/constEnum2.ts (4 errors) ==== + + // An enum declaration that specifies a const modifier is a constant enum declaration. + // In a constant enum declaration, all members must have constant values and + // it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression. + + // Error : not a constant enum expression + + const CONST = 9000 % 2; + const enum D { + d = 10, + e = 199 * Math.floor(Math.random() * 1000), + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2474: In 'const' enum declarations member initializer must be constant expression. + f = d - (100 * Math.floor(Math.random() % 8)) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2474: In 'const' enum declarations member initializer must be constant expression. + g = CONST, + ~ +!!! error TS1005: ',' expected. + ~~~~~ +!!! error TS2474: In 'const' enum declarations member initializer must be constant expression. + } \ No newline at end of file diff --git a/tests/baselines/reference/constEnum2.js b/tests/baselines/reference/constEnum2.js new file mode 100644 index 00000000000..c0c640b87fd --- /dev/null +++ b/tests/baselines/reference/constEnum2.js @@ -0,0 +1,32 @@ +//// [constEnum2.ts] + +// An enum declaration that specifies a const modifier is a constant enum declaration. +// In a constant enum declaration, all members must have constant values and +// it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression. + +// Error : not a constant enum expression + +const CONST = 9000 % 2; +const enum D { + d = 10, + e = 199 * Math.floor(Math.random() * 1000), + f = d - (100 * Math.floor(Math.random() % 8)) + g = CONST, +} + +//// [constEnum2.js] +// An enum declaration that specifies a const modifier is a constant enum declaration. +// In a constant enum declaration, all members must have constant values and +// it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression. +// Error : not a constant enum expression +var CONST = 9000 % 2; + + +//// [constEnum2.d.ts] +declare const CONST: number; +declare const enum D { + d = 10, + e, + f, + g, +} diff --git a/tests/baselines/reference/constEnumPropertyAccess1.js b/tests/baselines/reference/constEnumPropertyAccess1.js new file mode 100644 index 00000000000..114f1e1928e --- /dev/null +++ b/tests/baselines/reference/constEnumPropertyAccess1.js @@ -0,0 +1,67 @@ +//// [constEnumPropertyAccess1.ts] + +// constant enum declarations are completely erased in the emitted JavaScript code. +// it is an error to reference a constant enum object in any other context +// than a property access that selects one of the enum's members + +const enum G { + A = 1, + B = 2, + C = A + B, + D = A * 2 +} + +var o: { + [idx: number]: boolean +} = { + 1: true + }; + +var a = G.A; +var a1 = G["A"]; +var g = o[G.A]; + +class C { + [G.A]() { } + get [G.B]() { + return true; + } + set [G.B](x: number) { } +} + + + +//// [constEnumPropertyAccess1.js] +// constant enum declarations are completely erased in the emitted JavaScript code. +// it is an error to reference a constant enum object in any other context +// than a property access that selects one of the enum's members +var o = { + 1: true +}; +var a = 1 /* A */; +var a1 = 1 /* "A" */; +var g = o[1 /* A */]; +class C { + [1 /* A */]() { } + get [2 /* B */]() { + return true; + } + set [2 /* B */](x) { } +} + + +//// [constEnumPropertyAccess1.d.ts] +declare const enum G { + A = 1, + B = 2, + C = 3, + D = 2, +} +declare var o: { + [idx: number]: boolean; +}; +declare var a: G; +declare var a1: G; +declare var g: boolean; +declare class C { +} diff --git a/tests/baselines/reference/constEnumPropertyAccess1.types b/tests/baselines/reference/constEnumPropertyAccess1.types new file mode 100644 index 00000000000..bf0c727f1c0 --- /dev/null +++ b/tests/baselines/reference/constEnumPropertyAccess1.types @@ -0,0 +1,88 @@ +=== tests/cases/conformance/constEnums/constEnumPropertyAccess1.ts === + +// constant enum declarations are completely erased in the emitted JavaScript code. +// it is an error to reference a constant enum object in any other context +// than a property access that selects one of the enum's members + +const enum G { +>G : G, Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0)) + + A = 1, +>A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14)) +>1 : number + + B = 2, +>B : G, Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10)) +>2 : number + + C = A + B, +>C : G, Symbol(G.C, Decl(constEnumPropertyAccess1.ts, 7, 10)) +>A + B : number +>A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14)) +>B : G, Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10)) + + D = A * 2 +>D : G, Symbol(G.D, Decl(constEnumPropertyAccess1.ts, 8, 14)) +>A * 2 : number +>A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14)) +>2 : number +} + +var o: { +>o : { [idx: number]: boolean; }, Symbol(o, Decl(constEnumPropertyAccess1.ts, 12, 3)) + + [idx: number]: boolean +>idx : number, Symbol(idx, Decl(constEnumPropertyAccess1.ts, 13, 5)) + +} = { +>{ 1: true } : { [x: number]: boolean; 1: boolean; } + + 1: true +>true : boolean + + }; + +var a = G.A; +>a : G, Symbol(a, Decl(constEnumPropertyAccess1.ts, 18, 3)) +>G.A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14)) +>G : typeof G, Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0)) +>A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14)) + +var a1 = G["A"]; +>a1 : G, Symbol(a1, Decl(constEnumPropertyAccess1.ts, 19, 3)) +>G["A"] : G +>G : typeof G, Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0)) +>"A" : string, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14)) + +var g = o[G.A]; +>g : boolean, Symbol(g, Decl(constEnumPropertyAccess1.ts, 20, 3)) +>o[G.A] : boolean +>o : { [idx: number]: boolean; }, Symbol(o, Decl(constEnumPropertyAccess1.ts, 12, 3)) +>G.A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14)) +>G : typeof G, Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0)) +>A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14)) + +class C { +>C : C, Symbol(C, Decl(constEnumPropertyAccess1.ts, 20, 15)) + + [G.A]() { } +>G.A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14)) +>G : typeof G, Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0)) +>A : G, Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14)) + + get [G.B]() { +>G.B : G, Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10)) +>G : typeof G, Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0)) +>B : G, Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10)) + + return true; +>true : boolean + } + set [G.B](x: number) { } +>G.B : G, Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10)) +>G : typeof G, Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0)) +>B : G, Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10)) +>x : number, Symbol(x, Decl(constEnumPropertyAccess1.ts, 27, 14)) +} + + diff --git a/tests/baselines/reference/constEnumPropertyAccess2.errors.txt b/tests/baselines/reference/constEnumPropertyAccess2.errors.txt new file mode 100644 index 00000000000..19b273faffa --- /dev/null +++ b/tests/baselines/reference/constEnumPropertyAccess2.errors.txt @@ -0,0 +1,35 @@ +tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(14,9): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. +tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(15,12): error TS2476: A const enum member can only be accessed using a string literal. +tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(17,1): error TS2322: Type 'string' is not assignable to type 'G'. +tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(19,1): error TS2364: Invalid left-hand side of assignment expression. + + +==== tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts (4 errors) ==== + + // constant enum declarations are completely erased in the emitted JavaScript code. + // it is an error to reference a constant enum object in any other context + // than a property access that selects one of the enum's members + + const enum G { + A = 1, + B = 2, + C = A + B, + D = A * 2 + } + + // Error from referring constant enum in any other context than a property access + var z = G; + ~ +!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. + var z1 = G[G.A]; + ~~~ +!!! error TS2476: A const enum member can only be accessed using a string literal. + var g: G; + g = "string"; + ~ +!!! error TS2322: Type 'string' is not assignable to type 'G'. + function foo(x: G) { } + G.B = 3; + ~~~ +!!! error TS2364: Invalid left-hand side of assignment expression. + \ No newline at end of file diff --git a/tests/baselines/reference/constEnumPropertyAccess2.js b/tests/baselines/reference/constEnumPropertyAccess2.js new file mode 100644 index 00000000000..46a1d918b94 --- /dev/null +++ b/tests/baselines/reference/constEnumPropertyAccess2.js @@ -0,0 +1,46 @@ +//// [constEnumPropertyAccess2.ts] + +// constant enum declarations are completely erased in the emitted JavaScript code. +// it is an error to reference a constant enum object in any other context +// than a property access that selects one of the enum's members + +const enum G { + A = 1, + B = 2, + C = A + B, + D = A * 2 +} + +// Error from referring constant enum in any other context than a property access +var z = G; +var z1 = G[G.A]; +var g: G; +g = "string"; +function foo(x: G) { } +G.B = 3; + + +//// [constEnumPropertyAccess2.js] +// constant enum declarations are completely erased in the emitted JavaScript code. +// it is an error to reference a constant enum object in any other context +// than a property access that selects one of the enum's members +// Error from referring constant enum in any other context than a property access +var z = G; +var z1 = G[1 /* A */]; +var g; +g = "string"; +function foo(x) { } +2 /* B */ = 3; + + +//// [constEnumPropertyAccess2.d.ts] +declare const enum G { + A = 1, + B = 2, + C = 3, + D = 2, +} +declare var z: typeof G; +declare var z1: any; +declare var g: G; +declare function foo(x: G): void; diff --git a/tests/cases/conformance/constEnums/constEnum1.ts b/tests/cases/conformance/constEnums/constEnum1.ts new file mode 100644 index 00000000000..6bf59b772e9 --- /dev/null +++ b/tests/cases/conformance/constEnums/constEnum1.ts @@ -0,0 +1,16 @@ +// @declaration: true + +// An enum declaration that specifies a const modifier is a constant enum declaration. +// In a constant enum declaration, all members must have constant values and +// it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression. + +const enum E { + a = 10, + b = a, + c = (a+1), + e, + d = ~e, + f = a << 2 >> 1, + g = a << 2 >>> 1, + h = a | b +} \ No newline at end of file diff --git a/tests/cases/conformance/constEnums/constEnum2.ts b/tests/cases/conformance/constEnums/constEnum2.ts new file mode 100644 index 00000000000..a90e83d6c43 --- /dev/null +++ b/tests/cases/conformance/constEnums/constEnum2.ts @@ -0,0 +1,15 @@ +// @declaration: true + +// An enum declaration that specifies a const modifier is a constant enum declaration. +// In a constant enum declaration, all members must have constant values and +// it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression. + +// Error : not a constant enum expression + +const CONST = 9000 % 2; +const enum D { + d = 10, + e = 199 * Math.floor(Math.random() * 1000), + f = d - (100 * Math.floor(Math.random() % 8)) + g = CONST, +} \ No newline at end of file diff --git a/tests/cases/conformance/constEnums/constEnumPropertyAccess1.ts b/tests/cases/conformance/constEnums/constEnumPropertyAccess1.ts new file mode 100644 index 00000000000..bf1b277e3b5 --- /dev/null +++ b/tests/cases/conformance/constEnums/constEnumPropertyAccess1.ts @@ -0,0 +1,32 @@ +// @declaration: true +// @target: es6 + +// constant enum declarations are completely erased in the emitted JavaScript code. +// it is an error to reference a constant enum object in any other context +// than a property access that selects one of the enum's members + +const enum G { + A = 1, + B = 2, + C = A + B, + D = A * 2 +} + +var o: { + [idx: number]: boolean +} = { + 1: true + }; + +var a = G.A; +var a1 = G["A"]; +var g = o[G.A]; + +class C { + [G.A]() { } + get [G.B]() { + return true; + } + set [G.B](x: number) { } +} + diff --git a/tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts b/tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts new file mode 100644 index 00000000000..229f034269b --- /dev/null +++ b/tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts @@ -0,0 +1,20 @@ +// @declaration: true + +// constant enum declarations are completely erased in the emitted JavaScript code. +// it is an error to reference a constant enum object in any other context +// than a property access that selects one of the enum's members + +const enum G { + A = 1, + B = 2, + C = A + B, + D = A * 2 +} + +// Error from referring constant enum in any other context than a property access +var z = G; +var z1 = G[G.A]; +var g: G; +g = "string"; +function foo(x: G) { } +G.B = 3;