Skip to content

Commit

Permalink
Create global Symbol type
Browse files Browse the repository at this point in the history
  • Loading branch information
JsonFreeman committed Feb 7, 2015
1 parent 30892af commit 9cb38fb
Show file tree
Hide file tree
Showing 29 changed files with 427 additions and 31 deletions.
81 changes: 58 additions & 23 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ module ts {
var globals: SymbolTable = {};

var globalArraySymbol: Symbol;
var globalESSymbolConstructorSymbol: Symbol;

var globalObjectType: ObjectType;
var globalFunctionType: ObjectType;
Expand All @@ -93,6 +94,8 @@ module ts {
var globalBooleanType: ObjectType;
var globalRegExpType: ObjectType;
var globalTemplateStringsArrayType: ObjectType;
var globalESSymbolType: ObjectType;
var globalESSymbolConstructorType: ObjectType;

var anyArrayType: Type;

Expand Down Expand Up @@ -3005,12 +3008,20 @@ module ts {
return <ObjectType>type;
}

function getGlobalSymbol(name: string): Symbol {
return resolveName(undefined, name, SymbolFlags.Type, Diagnostics.Cannot_find_global_type_0, name);
function getGlobalValueSymbol(name: string): Symbol {
return getGlobalSymbol(name, SymbolFlags.Value, Diagnostics.Cannot_find_global_value_0);
}

function getGlobalTypeSymbol(name: string): Symbol {
return getGlobalSymbol(name, SymbolFlags.Type, Diagnostics.Cannot_find_global_type_0);
}

function getGlobalSymbol(name: string, meaning: SymbolFlags, diagnostic: DiagnosticMessage): Symbol {

This comment has been minimized.

Copy link
@yuit

yuit Feb 10, 2015

Contributor

what is the different between this function and the two above ?

return resolveName(undefined, name, meaning, diagnostic, name);
}

function getGlobalType(name: string): ObjectType {
return getTypeOfGlobalSymbol(getGlobalSymbol(name), 0);
return getTypeOfGlobalSymbol(getGlobalTypeSymbol(name), 0);
}

function createArrayType(elementType: Type): Type {
Expand Down Expand Up @@ -5481,7 +5492,7 @@ module ts {
function isNumericComputedName(name: ComputedPropertyName): boolean {
// It seems odd to consider an expression of type Any to result in a numeric name,
// but this behavior is consistent with checkIndexedAccess
return isTypeOfKind(checkComputedPropertyName(name), TypeFlags.Any | TypeFlags.NumberLike);
return isTypeOfKind(checkComputedPropertyName(name), TypeFlags.Any | TypeFlags.NumberLike, /*includeESSymbols*/ false);
}

function isNumericLiteralName(name: string) {
Expand Down Expand Up @@ -5514,9 +5525,9 @@ module ts {
if (!links.resolvedType) {
links.resolvedType = checkExpression(node.expression);

// This will allow types number, string, or any. It will also allow enums, the unknown
// This will allow types number, string, Symbol or any. It will also allow enums, the unknown
// type, and any union of these types (like string | number).
if (!isTypeOfKind(links.resolvedType, TypeFlags.Any | TypeFlags.NumberLike | TypeFlags.StringLike)) {
if (!isTypeOfKind(links.resolvedType, TypeFlags.Any | TypeFlags.NumberLike | TypeFlags.StringLike, /*includeESSymbols*/ true)) {
error(node, Diagnostics.A_computed_property_name_must_be_of_type_string_number_Symbol_or_any);
}
}
Expand Down Expand Up @@ -5781,10 +5792,10 @@ module ts {
}

// Check for compatible indexer types.
if (isTypeOfKind(indexType, TypeFlags.Any | TypeFlags.StringLike | TypeFlags.NumberLike)) {
if (isTypeOfKind(indexType, TypeFlags.Any | TypeFlags.StringLike | TypeFlags.NumberLike, /*includeESSymbols*/ true)) {

// Try to use a number indexer.
if (isTypeOfKind(indexType, TypeFlags.Any | TypeFlags.NumberLike)) {
if (isTypeOfKind(indexType, TypeFlags.Any | TypeFlags.NumberLike, /*includeESSymbols*/ false)) {
var numberIndexType = getIndexTypeOfType(objectType, IndexKind.Number);
if (numberIndexType) {
return numberIndexType;
Expand Down Expand Up @@ -6722,7 +6733,7 @@ module ts {
}

function checkArithmeticOperandType(operand: Node, type: Type, diagnostic: DiagnosticMessage): boolean {
if (!isTypeOfKind(type, TypeFlags.Any | TypeFlags.NumberLike)) {
if (!isTypeOfKind(type, TypeFlags.Any | TypeFlags.NumberLike, /*includeESSymbols*/ false)) {
error(operand, diagnostic);
return false;
}
Expand Down Expand Up @@ -6874,19 +6885,28 @@ module ts {
}

// Return true if type has the given flags, or is a union type composed of types that all have those flags
function isTypeOfKind(type: Type, kind: TypeFlags): boolean {
// If include includeESSymbols is true, then check if the type (or union constituents) is an ESSymbol
// if it does not match the kind. This is necessary because ESSymbol has no corresponding flag.
function isTypeOfKind(type: Type, kind: TypeFlags, includeESSymbols: boolean): boolean {
if (type.flags & kind) {
return true;
}
if (type.flags & TypeFlags.Union) {
var types = (<UnionType>type).types;
for (var i = 0; i < types.length; i++) {
if (!(types[i].flags & kind)) {
return false;
if (types[i].flags & kind) {
continue;
}
if (includeESSymbols && types[i] === globalESSymbolType) {
continue;
}
return false;
}
return true;
}
if (includeESSymbols) {
return type === globalESSymbolType;
}
return false;
}

Expand All @@ -6904,7 +6924,11 @@ module ts {
// and the right operand to be of type Any or a subtype of the 'Function' interface type.
// The result is always of the Boolean primitive type.
// NOTE: do not raise error if leftType is unknown as related error was already reported
if (isTypeOfKind(leftType, TypeFlags.Primitive)) {
//
// The reason for globalESSymbolType !== unknownType is that if the type is unknownType, we don't want to error.
// If the globalESSymbolType is also unknownType, then by including globalESSymbolType, we will error
// on unknownType, because transitively, type will be the same as globalESSymbolType.
if (isTypeOfKind(leftType, TypeFlags.Primitive, /*includeESSymbols*/ globalESSymbolType !== unknownType)) {
error(node.left, Diagnostics.The_left_hand_side_of_an_instanceof_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
}
// NOTE: do not raise error if right is unknown as related error was already reported
Expand All @@ -6919,10 +6943,10 @@ module ts {
// The in operator requires the left operand to be of type Any, the String primitive type, or the Number primitive type,
// and the right operand to be of type Any, an object type, or a type parameter type.
// The result is always of the Boolean primitive type.
if (!isTypeOfKind(leftType, TypeFlags.Any | TypeFlags.StringLike | TypeFlags.NumberLike)) {
if (!isTypeOfKind(leftType, TypeFlags.Any | TypeFlags.StringLike | TypeFlags.NumberLike, /*includeESSymbols*/ true)) {
error(node.left, Diagnostics.The_left_hand_side_of_an_in_expression_must_be_of_types_any_string_or_number);
}
if (!isTypeOfKind(rightType, TypeFlags.Any | TypeFlags.ObjectType | TypeFlags.TypeParameter)) {
if (!isTypeOfKind(rightType, TypeFlags.Any | TypeFlags.ObjectType | TypeFlags.TypeParameter, /*includeESSymbols*/ false)) {
error(node.right, Diagnostics.The_right_hand_side_of_an_in_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
}
return booleanType;
Expand Down Expand Up @@ -7083,12 +7107,12 @@ module ts {
if (rightType.flags & (TypeFlags.Undefined | TypeFlags.Null)) rightType = leftType;

var resultType: Type;
if (isTypeOfKind(leftType, TypeFlags.NumberLike) && isTypeOfKind(rightType, TypeFlags.NumberLike)) {
if (isTypeOfKind(leftType, TypeFlags.NumberLike, /*includeESSymbols*/ false) && isTypeOfKind(rightType, TypeFlags.NumberLike, /*includeESSymbols*/ false)) {
// Operands of an enum type are treated as having the primitive type Number.
// If both operands are of the Number primitive type, the result is of the Number primitive type.
resultType = numberType;
}
else if (isTypeOfKind(leftType, TypeFlags.StringLike) || isTypeOfKind(rightType, TypeFlags.StringLike)) {
else if (isTypeOfKind(leftType, TypeFlags.StringLike, /*includeESSymbols*/ false) || isTypeOfKind(rightType, TypeFlags.StringLike, /*includeESSymbols*/ false)) {
// If one or both operands are of the String primitive type, the result is of the String primitive type.
resultType = stringType;
}
Expand Down Expand Up @@ -8454,7 +8478,7 @@ module ts {
// and Expr must be an expression of type Any, an object type, or a type parameter type.
var varExpr = <Expression>node.initializer;
var exprType = checkExpression(varExpr);
if (exprType !== anyType && exprType !== stringType) {
if (!isTypeOfKind(exprType, TypeFlags.Any | TypeFlags.StringLike, /*includeESSymbols*/ true)) {
error(varExpr, Diagnostics.The_left_hand_side_of_a_for_in_statement_must_be_of_type_string_or_any);
}
else {
Expand All @@ -8466,7 +8490,7 @@ module ts {
var exprType = checkExpression(node.expression);
// unknownType is returned i.e. if node.expression is identifier whose name cannot be resolved
// in this case error about missing name is already reported - do not report extra one
if (!isTypeOfKind(exprType, TypeFlags.Any | TypeFlags.ObjectType | TypeFlags.TypeParameter)) {
if (!isTypeOfKind(exprType, TypeFlags.Any | TypeFlags.ObjectType | TypeFlags.TypeParameter, /*includeESSymbols*/ false)) {
error(node.expression, Diagnostics.The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter);
}

Expand Down Expand Up @@ -10251,19 +10275,30 @@ module ts {
getSymbolLinks(unknownSymbol).type = unknownType;
globals[undefinedSymbol.name] = undefinedSymbol;
// Initialize special types
globalArraySymbol = getGlobalSymbol("Array");
globalArraySymbol = getGlobalTypeSymbol("Array");
globalArrayType = getTypeOfGlobalSymbol(globalArraySymbol, 1);
globalObjectType = getGlobalType("Object");
globalFunctionType = getGlobalType("Function");
globalStringType = getGlobalType("String");
globalNumberType = getGlobalType("Number");
globalBooleanType = getGlobalType("Boolean");
globalRegExpType = getGlobalType("RegExp");

// If we're in ES6 mode, load the TemplateStringsArray.
// Otherwise, default to 'unknown' for the purposes of type checking in LS scenarios.
globalTemplateStringsArrayType = languageVersion >= ScriptTarget.ES6
? getGlobalType("TemplateStringsArray")
: unknownType;
if (languageVersion >= ScriptTarget.ES6) {
globalTemplateStringsArrayType = getGlobalType("TemplateStringsArray");
globalESSymbolType = getGlobalType("Symbol");
globalESSymbolConstructorSymbol = getGlobalValueSymbol("Symbol");
globalESSymbolConstructorType = getTypeOfGlobalSymbol(globalESSymbolConstructorSymbol, /*arity*/ 0);
}
else {
globalTemplateStringsArrayType = unknownType;
globalESSymbolType = unknownType;
globalESSymbolConstructorSymbol = unknownSymbol;
globalESSymbolConstructorType = unknownType;
}

anyArrayType = createArrayType(anyType);
}

Expand Down
1 change: 1 addition & 0 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ module ts {
this_cannot_be_referenced_in_a_computed_property_name: { code: 2465, category: DiagnosticCategory.Error, key: "'this' cannot be referenced in a computed property name." },
super_cannot_be_referenced_in_a_computed_property_name: { code: 2466, category: DiagnosticCategory.Error, key: "'super' cannot be referenced in a computed property name." },
A_computed_property_name_cannot_reference_a_type_parameter_from_its_containing_type: { code: 2466, category: DiagnosticCategory.Error, key: "A computed property name cannot reference a type parameter from its containing type." },
Cannot_find_global_value_0: { code: 2468, category: DiagnosticCategory.Error, key: "Cannot find global value '{0}'." },
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." },
Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." },
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,10 @@
"category": "Error",
"code": 2466
},
"Cannot find global value '{0}'.": {
"category": "Error",
"code": 2468
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
5 changes: 1 addition & 4 deletions tests/baselines/reference/parserForStatement3.errors.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement3.ts(1,5): error TS2304: Cannot find name 'd'.
tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement3.ts(1,5): error TS2405: The left-hand side of a 'for...in' statement must be of type 'string' or 'any'.
tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement3.ts(1,10): error TS2304: Cannot find name '_'.
tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement3.ts(1,15): error TS2304: Cannot find name 'a'.
tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement3.ts(1,18): error TS2304: Cannot find name '_'.
tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement3.ts(1,23): error TS2304: Cannot find name 'a'.
tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement3.ts(1,30): error TS2304: Cannot find name 'b'.


==== tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement3.ts (7 errors) ====
==== tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement3.ts (6 errors) ====
for(d in _.jh[a]=_.jh[a]||[],b);
~
!!! error TS2304: Cannot find name 'd'.
~
!!! error TS2405: The left-hand side of a 'for...in' statement must be of type 'string' or 'any'.
~
!!! error TS2304: Cannot find name '_'.
~
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/parserForStatement6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement6.ts(1,6): error TS2304: Cannot find name 'foo'.
tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement6.ts(1,6): error TS2405: The left-hand side of a 'for...in' statement must be of type 'string' or 'any'.
tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement6.ts(1,6): error TS2406: Invalid left-hand side in 'for...in' statement.
tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement6.ts(1,15): error TS2304: Cannot find name 'b'.


Expand All @@ -8,7 +8,7 @@ tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement6.ts(1,1
~~~
!!! error TS2304: Cannot find name 'foo'.
~~~~~
!!! error TS2405: The left-hand side of a 'for...in' statement must be of type 'string' or 'any'.
!!! error TS2406: Invalid left-hand side in 'for...in' statement.
~
!!! error TS2304: Cannot find name 'b'.
}
4 changes: 2 additions & 2 deletions tests/baselines/reference/parserForStatement7.errors.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement7.ts(1,6): error TS2405: The left-hand side of a 'for...in' statement must be of type 'string' or 'any'.
tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement7.ts(1,6): error TS2406: Invalid left-hand side in 'for...in' statement.
tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement7.ts(1,10): error TS2304: Cannot find name 'foo'.
tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement7.ts(1,19): error TS2304: Cannot find name 'b'.


==== tests/cases/conformance/parser/ecmascript5/Statements/parserForStatement7.ts (3 errors) ====
for (new foo() in b) {
~~~~~~~~~
!!! error TS2405: The left-hand side of a 'for...in' statement must be of type 'string' or 'any'.
!!! error TS2406: Invalid left-hand side in 'for...in' statement.
~~~
!!! error TS2304: Cannot find name 'foo'.
~
Expand Down
20 changes: 20 additions & 0 deletions tests/baselines/reference/symbolProperty1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [symbolProperty1.ts]
var s: Symbol;
var x = {
[s]: 0,
[s]() { },
get [s]() {
return 0;
}
}

//// [symbolProperty1.js]
var s;
var x = {
[s]: 0,
[s]() {
},
get [s]() {
return 0;
}
};
21 changes: 21 additions & 0 deletions tests/baselines/reference/symbolProperty1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/conformance/es6/Symbols/symbolProperty1.ts ===
var s: Symbol;
>s : Symbol
>Symbol : Symbol

var x = {
>x : {}
>{ [s]: 0, [s]() { }, get [s]() { return 0; }} : {}

[s]: 0,
>s : Symbol

[s]() { },
>s : Symbol

get [s]() {
>s : Symbol

return 0;
}
}
20 changes: 20 additions & 0 deletions tests/baselines/reference/symbolProperty2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//// [symbolProperty2.ts]
var s = Symbol();
var x = {
[s]: 0,
[s]() { },
get [s]() {
return 0;
}
}

//// [symbolProperty2.js]
var s = Symbol();
var x = {
[s]: 0,
[s]() {
},
get [s]() {
return 0;
}
};
22 changes: 22 additions & 0 deletions tests/baselines/reference/symbolProperty2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/conformance/es6/Symbols/symbolProperty2.ts ===
var s = Symbol();
>s : Symbol
>Symbol() : Symbol
>Symbol : SymbolConstructor

var x = {
>x : {}
>{ [s]: 0, [s]() { }, get [s]() { return 0; }} : {}

[s]: 0,
>s : Symbol

[s]() { },
>s : Symbol

get [s]() {
>s : Symbol

return 0;
}
}
20 changes: 20 additions & 0 deletions tests/baselines/reference/symbolProperty3.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
tests/cases/conformance/es6/Symbols/symbolProperty3.ts(3,5): error TS2464: A computed property name must be of type 'string', 'number', 'Symbol', or 'any'.
tests/cases/conformance/es6/Symbols/symbolProperty3.ts(4,5): error TS2464: A computed property name must be of type 'string', 'number', 'Symbol', or 'any'.
tests/cases/conformance/es6/Symbols/symbolProperty3.ts(5,9): error TS2464: A computed property name must be of type 'string', 'number', 'Symbol', or 'any'.


==== tests/cases/conformance/es6/Symbols/symbolProperty3.ts (3 errors) ====
var s = Symbol;
var x = {
[s]: 0,
~~~
!!! error TS2464: A computed property name must be of type 'string', 'number', 'Symbol', or 'any'.
[s]() { },
~~~
!!! error TS2464: A computed property name must be of type 'string', 'number', 'Symbol', or 'any'.
get [s]() {
~~~
!!! error TS2464: A computed property name must be of type 'string', 'number', 'Symbol', or 'any'.
return 0;
}
}
Loading

0 comments on commit 9cb38fb

Please sign in to comment.