Skip to content

Introduce literal freshness for literal enum member types #26556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5886,9 +5886,9 @@ namespace ts {
for (const declaration of symbol.declarations) {
if (declaration.kind === SyntaxKind.EnumDeclaration) {
for (const member of (<EnumDeclaration>declaration).members) {
const memberType = getLiteralType(getEnumMemberValue(member)!, enumCount, getSymbolOfNode(member)); // TODO: GH#18217
const memberType = getFreshTypeOfLiteralType(getLiteralType(getEnumMemberValue(member)!, enumCount, getSymbolOfNode(member))); // TODO: GH#18217
getSymbolLinks(getSymbolOfNode(member)).declaredType = memberType;
memberTypeList.push(memberType);
memberTypeList.push(getRegularTypeOfLiteralType(memberType));
}
}
}
Expand Down Expand Up @@ -8221,7 +8221,7 @@ namespace ts {
const res = tryGetDeclaredTypeOfSymbol(symbol);
if (res) {
return checkNoTypeArguments(node, symbol) ?
res.flags & TypeFlags.TypeParameter ? getConstrainedTypeVariable(<TypeParameter>res, node) : res :
res.flags & TypeFlags.TypeParameter ? getConstrainedTypeVariable(<TypeParameter>res, node) : getRegularTypeOfLiteralType(res) :
errorType;
}

Expand Down Expand Up @@ -12705,7 +12705,7 @@ namespace ts {
}

function getWidenedLiteralType(type: Type): Type {
return type.flags & TypeFlags.EnumLiteral ? getBaseTypeOfEnumLiteralType(<LiteralType>type) :
return type.flags & TypeFlags.EnumLiteral && type.flags & TypeFlags.FreshLiteral ? getBaseTypeOfEnumLiteralType(<LiteralType>type) :
type.flags & TypeFlags.StringLiteral && type.flags & TypeFlags.FreshLiteral ? stringType :
type.flags & TypeFlags.NumberLiteral && type.flags & TypeFlags.FreshLiteral ? numberType :
type.flags & TypeFlags.BooleanLiteral ? booleanType :
Expand Down Expand Up @@ -28090,13 +28090,14 @@ namespace ts {
return false;
}

function literalTypeToNode(type: LiteralType): Expression {
return createLiteral(type.value);
function literalTypeToNode(type: LiteralType, enclosing: Node): Expression {
const enumResult = type.flags & TypeFlags.EnumLiteral && nodeBuilder.symbolToExpression(type.symbol, SymbolFlags.Value, enclosing);
return enumResult || createLiteral(type.value);
}

function createLiteralConstValue(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration) {
const type = getTypeOfSymbol(getSymbolOfNode(node));
return literalTypeToNode(<LiteralType>type);
return literalTypeToNode(<LiteralType>type, node);
}

function createResolver(): EmitResolver {
Expand Down Expand Up @@ -29430,13 +29431,20 @@ namespace ts {
(<PrefixUnaryExpression>expr).operand.kind === SyntaxKind.NumericLiteral;
}

function isSimpleLiteralEnumReference(expr: Expression) {
if (
(isPropertyAccessExpression(expr) || (isElementAccessExpression(expr) && isStringOrNumberLiteralExpression(expr.argumentExpression))) &&
isEntityNameExpression(expr.expression)
) return !!(checkExpressionCached(expr).flags & TypeFlags.EnumLiteral);
}

function checkAmbientInitializer(node: VariableDeclaration | PropertyDeclaration | PropertySignature) {
if (node.initializer) {
const isInvalidInitializer = !isStringOrNumberLiteralExpression(node.initializer);
const isInvalidInitializer = !(isStringOrNumberLiteralExpression(node.initializer) || isSimpleLiteralEnumReference(node.initializer));
const isConstOrReadonly = isDeclarationReadonly(node) || isVariableDeclaration(node) && isVarConst(node);
if (isConstOrReadonly && !node.type) {
if (isInvalidInitializer) {
return grammarErrorOnNode(node.initializer!, Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal);
return grammarErrorOnNode(node.initializer!, Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal_or_literal_enum_reference);
}
}
else {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@
"category": "Error",
"code": 1253
},
"A 'const' initializer in an ambient context must be a string or numeric literal.": {
"A 'const' initializer in an ambient context must be a string or numeric literal or literal enum reference.": {
"category": "Error",
"code": 1254
},
Expand Down
11 changes: 8 additions & 3 deletions tests/baselines/reference/ambientConstLiterals.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function f<T>(x: T): T {
return x;
}

enum E { A, B, C }
enum E { A, B, C, "non identifier" }

const c1 = "abc";
const c2 = 123;
Expand All @@ -13,6 +13,7 @@ const c5 = f(123);
const c6 = f(-123);
const c7 = true;
const c8 = E.A;
const c8b = E["non identifier"];
const c9 = { x: "abc" };
const c10 = [123];
const c11 = "abc" + "def";
Expand All @@ -29,6 +30,7 @@ var E;
E[E["A"] = 0] = "A";
E[E["B"] = 1] = "B";
E[E["C"] = 2] = "C";
E[E["non identifier"] = 3] = "non identifier";
})(E || (E = {}));
var c1 = "abc";
var c2 = 123;
Expand All @@ -38,6 +40,7 @@ var c5 = f(123);
var c6 = f(-123);
var c7 = true;
var c8 = E.A;
var c8b = E["non identifier"];
var c9 = { x: "abc" };
var c10 = [123];
var c11 = "abc" + "def";
Expand All @@ -51,7 +54,8 @@ declare function f<T>(x: T): T;
declare enum E {
A = 0,
B = 1,
C = 2
C = 2,
"non identifier" = 3
}
declare const c1 = "abc";
declare const c2 = 123;
Expand All @@ -60,7 +64,8 @@ declare const c4 = 123;
declare const c5 = 123;
declare const c6 = -123;
declare const c7: boolean;
declare const c8: E;
declare const c8 = E.A;
declare const c8b = E["non identifier"];
declare const c9: {
x: string;
};
Expand Down
22 changes: 14 additions & 8 deletions tests/baselines/reference/ambientConstLiterals.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ function f<T>(x: T): T {
>x : Symbol(x, Decl(ambientConstLiterals.ts, 0, 14))
}

enum E { A, B, C }
enum E { A, B, C, "non identifier" }
>E : Symbol(E, Decl(ambientConstLiterals.ts, 2, 1))
>A : Symbol(E.A, Decl(ambientConstLiterals.ts, 4, 8))
>B : Symbol(E.B, Decl(ambientConstLiterals.ts, 4, 11))
>C : Symbol(E.C, Decl(ambientConstLiterals.ts, 4, 14))
>"non identifier" : Symbol(E["non identifier"], Decl(ambientConstLiterals.ts, 4, 17))

const c1 = "abc";
>c1 : Symbol(c1, Decl(ambientConstLiterals.ts, 6, 5))
Expand Down Expand Up @@ -47,27 +48,32 @@ const c8 = E.A;
>E : Symbol(E, Decl(ambientConstLiterals.ts, 2, 1))
>A : Symbol(E.A, Decl(ambientConstLiterals.ts, 4, 8))

const c8b = E["non identifier"];
>c8b : Symbol(c8b, Decl(ambientConstLiterals.ts, 14, 5))
>E : Symbol(E, Decl(ambientConstLiterals.ts, 2, 1))
>"non identifier" : Symbol(E["non identifier"], Decl(ambientConstLiterals.ts, 4, 17))

const c9 = { x: "abc" };
>c9 : Symbol(c9, Decl(ambientConstLiterals.ts, 14, 5))
>x : Symbol(x, Decl(ambientConstLiterals.ts, 14, 12))
>c9 : Symbol(c9, Decl(ambientConstLiterals.ts, 15, 5))
>x : Symbol(x, Decl(ambientConstLiterals.ts, 15, 12))

const c10 = [123];
>c10 : Symbol(c10, Decl(ambientConstLiterals.ts, 15, 5))
>c10 : Symbol(c10, Decl(ambientConstLiterals.ts, 16, 5))

const c11 = "abc" + "def";
>c11 : Symbol(c11, Decl(ambientConstLiterals.ts, 16, 5))
>c11 : Symbol(c11, Decl(ambientConstLiterals.ts, 17, 5))

const c12 = 123 + 456;
>c12 : Symbol(c12, Decl(ambientConstLiterals.ts, 17, 5))
>c12 : Symbol(c12, Decl(ambientConstLiterals.ts, 18, 5))

const c13 = Math.random() > 0.5 ? "abc" : "def";
>c13 : Symbol(c13, Decl(ambientConstLiterals.ts, 18, 5))
>c13 : Symbol(c13, Decl(ambientConstLiterals.ts, 19, 5))
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))

const c14 = Math.random() > 0.5 ? 123 : 456;
>c14 : Symbol(c14, Decl(ambientConstLiterals.ts, 19, 5))
>c14 : Symbol(c14, Decl(ambientConstLiterals.ts, 20, 5))
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
Expand Down
9 changes: 8 additions & 1 deletion tests/baselines/reference/ambientConstLiterals.types
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ function f<T>(x: T): T {
>x : T
}

enum E { A, B, C }
enum E { A, B, C, "non identifier" }
>E : E
>A : E.A
>B : E.B
>C : E.C
>"non identifier" : E.non identifier

const c1 = "abc";
>c1 : "abc"
Expand Down Expand Up @@ -52,6 +53,12 @@ const c8 = E.A;
>E : typeof E
>A : E.A

const c8b = E["non identifier"];
>c8b : E.non identifier
>E["non identifier"] : E.non identifier
>E : typeof E
>"non identifier" : "non identifier"

const c9 = { x: "abc" };
>c9 : { x: string; }
>{ x: "abc" } : { x: string; }
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/ambientEnum1.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
>E1 : E1

y = 4.23
>y : E1
>y : E1.y
>4.23 : 4.23
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ declare enum E {
>E : E

e = 3
>e : E
>e : E.e
>3 : 3
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ declare enum E {
>E : E

e = -3 // Negative
>e : E
>e : E.e
>-3 : -3
>3 : 3
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ declare enum E {
>E : E

e = 3.3 // Decimal
>e : E
>e : E.e
>3.3 : 3.3
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ declare enum E {
>E : E

e = 0xA
>e : E
>e : E.e
>0xA : 10
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ declare enum E {
>E : E

e = -0xA
>e : E
>e : E.e
>-0xA : -10
>0xA : 10
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare module M {
>E : E

e = 3
>e : E
>e : E.e
>3 : 3
}
}
2 changes: 1 addition & 1 deletion tests/baselines/reference/ambientErrors.types
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ declare enum E1 {
>E1 : E1

y = 4.23
>y : E1
>y : E1.y
>4.23 : 4.23
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ var r3 = foo3(a); // any

enum E { A }
>E : E
>A : E
>A : E.A

declare function foo14(x: E): E;
>foo14 : { (x: E): E; (x: any): any; }
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/anyAssignableToEveryType.types
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var ai: I;

enum E { A }
>E : E
>A : E
>A : E.A

var ae: E;
>ae : E
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/anyAssignableToEveryType2.types
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ interface I13 {

enum E { A }
>E : E
>A : E
>A : E.A

interface I14 {
[x: string]: E;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/assignAnyToEveryType.types
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ enum E {
>E : E

A
>A : E
>A : E.A
}

var g: E = x;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/assignEveryTypeToAny.types
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ enum E {
>E : E

A
>A : E
>A : E.A
}

x = E.A;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ enum E {
>E : E

A
>A : E
>A : E.A
}
E = undefined; // Error
>E = undefined : undefined
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/assignments.types
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ C = null; // Error

enum E { A }
>E : E
>A : E
>A : E.A

E = null; // Error
>E = null : null
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/asyncEnum_es5.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ async enum E {
>E : E

Value
>Value : E
>Value : E.Value
}
2 changes: 1 addition & 1 deletion tests/baselines/reference/asyncEnum_es6.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ async enum E {
>E : E

Value
>Value : E
>Value : E.Value
}
2 changes: 1 addition & 1 deletion tests/baselines/reference/augmentedTypesClass.types
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ class c4 { public foo() { } }

enum c4 { One } // error
>c4 : c4
>One : c4
>One : c4.One

2 changes: 1 addition & 1 deletion tests/baselines/reference/augmentedTypesClass2.types
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class c33 {
}
enum c33 { One };
>c33 : c33
>One : c33
>One : c33.One

// class then import
class c44 {
Expand Down
Loading