Merge pull request #2792 from Microsoft/conformanceConstEnum

Conformance for spec update section 9.4, 12.1.4 const enum
This commit is contained in:
Yui
2015-05-04 10:46:21 -07:00
21 changed files with 705 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
tests/cases/conformance/ambient/ambientEnumDeclaration1.ts(5,5): error TS1066: Ambient enum elements can only have integer literal initializers.
tests/cases/conformance/ambient/ambientEnumDeclaration1.ts(6,5): error TS1066: Ambient enum elements can only have integer literal initializers.
tests/cases/conformance/ambient/ambientEnumDeclaration1.ts(7,5): error TS1066: Ambient enum elements can only have integer literal initializers.
tests/cases/conformance/ambient/ambientEnumDeclaration1.ts(8,5): error TS1066: Ambient enum elements can only have integer literal initializers.
==== tests/cases/conformance/ambient/ambientEnumDeclaration1.ts (4 errors) ====
// In ambient enum declarations, all values specified in enum member declarations must be classified as constant enum expressions.
declare enum E {
a = 10,
b = 10 + 1,
~
!!! error TS1066: Ambient enum elements can only have integer literal initializers.
c = b,
~
!!! error TS1066: Ambient enum elements can only have integer literal initializers.
d = (c) + 1,
~
!!! error TS1066: Ambient enum elements can only have integer literal initializers.
e = 10 << 2 * 8,
~
!!! error TS1066: Ambient enum elements can only have integer literal initializers.
}

View File

@@ -0,0 +1,13 @@
//// [ambientEnumDeclaration1.ts]
// In ambient enum declarations, all values specified in enum member declarations must be classified as constant enum expressions.
declare enum E {
a = 10,
b = 10 + 1,
c = b,
d = (c) + 1,
e = 10 << 2 * 8,
}
//// [ambientEnumDeclaration1.js]
// In ambient enum declarations, all values specified in enum member declarations must be classified as constant enum expressions.

View File

@@ -0,0 +1,17 @@
//// [ambientEnumDeclaration2.ts]
// In ambient enum declarations that specify no const modifier, enum member declarations
// that omit a value are considered computed members (as opposed to having auto- incremented values assigned).
declare enum E {
a, // E.a
b, // E.b
}
declare const enum E1 {
a, // E.a = 0
b, // E.b = 1
}
//// [ambientEnumDeclaration2.js]
// In ambient enum declarations that specify no const modifier, enum member declarations
// that omit a value are considered computed members (as opposed to having auto- incremented values assigned).

View File

@@ -0,0 +1,23 @@
=== tests/cases/conformance/ambient/ambientEnumDeclaration2.ts ===
// In ambient enum declarations that specify no const modifier, enum member declarations
// that omit a value are considered computed members (as opposed to having auto- incremented values assigned).
declare enum E {
>E : Symbol(E, Decl(ambientEnumDeclaration2.ts, 0, 0))
a, // E.a
>a : Symbol(E.a, Decl(ambientEnumDeclaration2.ts, 3, 16))
b, // E.b
>b : Symbol(E.b, Decl(ambientEnumDeclaration2.ts, 4, 6))
}
declare const enum E1 {
>E1 : Symbol(E1, Decl(ambientEnumDeclaration2.ts, 6, 1))
a, // E.a = 0
>a : Symbol(E1.a, Decl(ambientEnumDeclaration2.ts, 8, 23))
b, // E.b = 1
>b : Symbol(E1.b, Decl(ambientEnumDeclaration2.ts, 9, 6))
}

View File

@@ -0,0 +1,23 @@
=== tests/cases/conformance/ambient/ambientEnumDeclaration2.ts ===
// In ambient enum declarations that specify no const modifier, enum member declarations
// that omit a value are considered computed members (as opposed to having auto- incremented values assigned).
declare enum E {
>E : E
a, // E.a
>a : E
b, // E.b
>b : E
}
declare const enum E1 {
>E1 : E1
a, // E.a = 0
>a : E1
b, // E.b = 1
>b : E1
}

View File

@@ -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,
}

View File

@@ -0,0 +1,40 @@
=== 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 : Symbol(E, Decl(constEnum1.ts, 0, 0))
a = 10,
>a : Symbol(E.a, Decl(constEnum1.ts, 5, 14))
b = a,
>b : Symbol(E.b, Decl(constEnum1.ts, 6, 11))
>a : Symbol(E.a, Decl(constEnum1.ts, 5, 14))
c = (a+1),
>c : Symbol(E.c, Decl(constEnum1.ts, 7, 10))
>a : Symbol(E.a, Decl(constEnum1.ts, 5, 14))
e,
>e : Symbol(E.e, Decl(constEnum1.ts, 8, 14))
d = ~e,
>d : Symbol(E.d, Decl(constEnum1.ts, 9, 6))
>e : Symbol(E.e, Decl(constEnum1.ts, 8, 14))
f = a << 2 >> 1,
>f : Symbol(E.f, Decl(constEnum1.ts, 10, 11))
>a : Symbol(E.a, Decl(constEnum1.ts, 5, 14))
g = a << 2 >>> 1,
>g : Symbol(E.g, Decl(constEnum1.ts, 11, 20))
>a : Symbol(E.a, Decl(constEnum1.ts, 5, 14))
h = a | b
>h : Symbol(E.h, Decl(constEnum1.ts, 12, 21))
>a : Symbol(E.a, Decl(constEnum1.ts, 5, 14))
>b : Symbol(E.b, Decl(constEnum1.ts, 6, 11))
}

View File

@@ -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
a = 10,
>a : E
>10 : number
b = a,
>b : E
>a : E
c = (a+1),
>c : E
>(a+1) : number
>a+1 : number
>a : E
>1 : number
e,
>e : E
d = ~e,
>d : E
>~e : number
>e : E
f = a << 2 >> 1,
>f : E
>a << 2 >> 1 : number
>a << 2 : number
>a : E
>2 : number
>1 : number
g = a << 2 >>> 1,
>g : E
>a << 2 >>> 1 : number
>a << 2 : number
>a : E
>2 : number
>1 : number
h = a | b
>h : E
>a | b : number
>a : E
>b : E
}

View File

@@ -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.
}

View File

@@ -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,
}

View File

@@ -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 {
}

View File

@@ -0,0 +1,76 @@
=== 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 : Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0))
A = 1,
>A : Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
B = 2,
>B : Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10))
C = A + B,
>C : Symbol(G.C, Decl(constEnumPropertyAccess1.ts, 7, 10))
>A : Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
>B : Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10))
D = A * 2
>D : Symbol(G.D, Decl(constEnumPropertyAccess1.ts, 8, 14))
>A : Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
}
var o: {
>o : Symbol(o, Decl(constEnumPropertyAccess1.ts, 12, 3))
[idx: number]: boolean
>idx : Symbol(idx, Decl(constEnumPropertyAccess1.ts, 13, 5))
} = {
1: true
};
var a = G.A;
>a : Symbol(a, Decl(constEnumPropertyAccess1.ts, 18, 3))
>G.A : Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
>G : Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0))
>A : Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
var a1 = G["A"];
>a1 : Symbol(a1, Decl(constEnumPropertyAccess1.ts, 19, 3))
>G : Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0))
>"A" : Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
var g = o[G.A];
>g : Symbol(g, Decl(constEnumPropertyAccess1.ts, 20, 3))
>o : Symbol(o, Decl(constEnumPropertyAccess1.ts, 12, 3))
>G.A : Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
>G : Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0))
>A : Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
class C {
>C : Symbol(C, Decl(constEnumPropertyAccess1.ts, 20, 15))
[G.A]() { }
>G.A : Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
>G : Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0))
>A : Symbol(G.A, Decl(constEnumPropertyAccess1.ts, 5, 14))
get [G.B]() {
>G.B : Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10))
>G : Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0))
>B : Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10))
return true;
}
set [G.B](x: number) { }
>G.B : Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10))
>G : Symbol(G, Decl(constEnumPropertyAccess1.ts, 0, 0))
>B : Symbol(G.B, Decl(constEnumPropertyAccess1.ts, 6, 10))
>x : Symbol(x, Decl(constEnumPropertyAccess1.ts, 27, 14))
}

View File

@@ -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
A = 1,
>A : G
>1 : number
B = 2,
>B : G
>2 : number
C = A + B,
>C : G
>A + B : number
>A : G
>B : G
D = A * 2
>D : G
>A * 2 : number
>A : G
>2 : number
}
var o: {
>o : { [idx: number]: boolean; }
[idx: number]: boolean
>idx : number
} = {
>{ 1: true } : { [x: number]: boolean; 1: boolean; }
1: true
>true : boolean
};
var a = G.A;
>a : G
>G.A : G
>G : typeof G
>A : G
var a1 = G["A"];
>a1 : G
>G["A"] : G
>G : typeof G
>"A" : string
var g = o[G.A];
>g : boolean
>o[G.A] : boolean
>o : { [idx: number]: boolean; }
>G.A : G
>G : typeof G
>A : G
class C {
>C : C
[G.A]() { }
>G.A : G
>G : typeof G
>A : G
get [G.B]() {
>G.B : G
>G : typeof G
>B : G
return true;
>true : boolean
}
set [G.B](x: number) { }
>G.B : G
>G : typeof G
>B : G
>x : number
}

View File

@@ -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.

View File

@@ -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;

View File

@@ -0,0 +1,9 @@
// In ambient enum declarations, all values specified in enum member declarations must be classified as constant enum expressions.
declare enum E {
a = 10,
b = 10 + 1,
c = b,
d = (c) + 1,
e = 10 << 2 * 8,
}

View File

@@ -0,0 +1,12 @@
// In ambient enum declarations that specify no const modifier, enum member declarations
// that omit a value are considered computed members (as opposed to having auto- incremented values assigned).
declare enum E {
a, // E.a
b, // E.b
}
declare const enum E1 {
a, // E.a = 0
b, // E.b = 1
}

View File

@@ -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
}

View File

@@ -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,
}

View File

@@ -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) { }
}

View File

@@ -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;