-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
…like a built in Symbol
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7487,7 +7487,7 @@ module ts { | |
|
||
function checkPropertyDeclaration(node: PropertyDeclaration) { | ||
// Grammar checking | ||
checkGrammarModifiers(node) || checkGrammarProperty(node); | ||
checkGrammarModifiers(node) || checkGrammarProperty(node) || checkGrammarComputedPropertyName(node.name); | ||
|
||
checkVariableLikeDeclaration(node); | ||
} | ||
|
@@ -10771,8 +10771,8 @@ module ts { | |
} | ||
} | ||
|
||
function checkGrammarForDisallowedComputedProperty(node: DeclarationName, message: DiagnosticMessage) { | ||
if (node.kind === SyntaxKind.ComputedPropertyName) { | ||
function checkGrammarForNonSymbolComputedProperty(node: DeclarationName, message: DiagnosticMessage) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
JsonFreeman
Author
Contributor
|
||
if (node.kind === SyntaxKind.ComputedPropertyName && !isWellKnownSymbolSyntactically((<ComputedPropertyName>node).expression)) { | ||
return grammarErrorOnNode(node, message); | ||
} | ||
} | ||
|
@@ -10803,17 +10803,17 @@ module ts { | |
// and accessors are not allowed in ambient contexts in general, | ||
// so this error only really matters for methods. | ||
if (isInAmbientContext(node)) { | ||
return checkGrammarForDisallowedComputedProperty(node.name, Diagnostics.A_computed_property_name_in_an_ambient_context_must_directly_refer_to_a_built_in_Symbol); | ||
return checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_an_ambient_context_must_directly_refer_to_a_built_in_Symbol); | ||
} | ||
else if (!node.body) { | ||
return checkGrammarForDisallowedComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_method_overload_must_directly_refer_to_a_built_in_Symbol); | ||
return checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_method_overload_must_directly_refer_to_a_built_in_Symbol); | ||
} | ||
} | ||
else if (node.parent.kind === SyntaxKind.InterfaceDeclaration) { | ||
return checkGrammarForDisallowedComputedProperty(node.name, Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_Symbol); | ||
return checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_Symbol); | ||
} | ||
else if (node.parent.kind === SyntaxKind.TypeLiteral) { | ||
return checkGrammarForDisallowedComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_type_literal_must_directly_refer_to_a_built_in_Symbol); | ||
return checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_type_literal_must_directly_refer_to_a_built_in_Symbol); | ||
} | ||
} | ||
|
||
|
@@ -11084,17 +11084,17 @@ module ts { | |
function checkGrammarProperty(node: PropertyDeclaration) { | ||
if (node.parent.kind === SyntaxKind.ClassDeclaration) { | ||
if (checkGrammarForInvalidQuestionMark(node, node.questionToken, Diagnostics.A_class_member_cannot_be_declared_optional) || | ||
checkGrammarForDisallowedComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_class_property_declaration_must_directly_refer_to_a_built_in_Symbol)) { | ||
checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_class_property_declaration_must_directly_refer_to_a_built_in_Symbol)) { | ||
return true; | ||
} | ||
} | ||
else if (node.parent.kind === SyntaxKind.InterfaceDeclaration) { | ||
if (checkGrammarForDisallowedComputedProperty(node.name, Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_Symbol)) { | ||
if (checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_Symbol)) { | ||
return true; | ||
} | ||
} | ||
else if (node.parent.kind === SyntaxKind.TypeLiteral) { | ||
if (checkGrammarForDisallowedComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_type_literal_must_directly_refer_to_a_built_in_Symbol)) { | ||
if (checkGrammarForNonSymbolComputedProperty(node.name, Diagnostics.A_computed_property_name_in_a_type_literal_must_directly_refer_to_a_built_in_Symbol)) { | ||
return true; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -835,11 +835,21 @@ module ts { | |
return SyntaxKind.FirstTriviaToken <= token && token <= SyntaxKind.LastTriviaToken; | ||
} | ||
|
||
export function isWellKnownSymbolSyntactically(node: Node): boolean { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
JsonFreeman
Author
Contributor
|
||
return node.kind === SyntaxKind.PropertyAccessExpression && isESSymbolIdentifier((<PropertyAccessExpression>node).expression); | ||
} | ||
|
||
export function isESSymbolTypeNode(node: Node): boolean { | ||
return node.kind === SyntaxKind.TypeReference && | ||
(<TypeReferenceNode>node).typeArguments === undefined && | ||
(<TypeReferenceNode>node).typeName.kind === SyntaxKind.Identifier && | ||
(<Identifier>(<TypeReferenceNode>node).typeName).text === "Symbol"; | ||
isESSymbolIdentifier((<TypeReferenceNode>node).typeName); | ||
} | ||
|
||
/** | ||
* Includes the word "Symbol" with unicode escapes | ||
*/ | ||
export function isESSymbolIdentifier(node: Node): boolean { | ||
return node.kind === SyntaxKind.Identifier && (<Identifier>node).text === "Symbol"; | ||
} | ||
|
||
export function isModifier(token: SyntaxKind): boolean { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty1.ts(2,5): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty1.ts(2,6): error TS2304: Cannot find name 'Symbol'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty1.ts (2 errors) ==== | ||
interface I { | ||
[Symbol.iterator]: string; | ||
~~~~~~~~~~~~~~~~~ | ||
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
~~~~~~ | ||
!!! error TS2304: Cannot find name 'Symbol'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty2.ts(2,5): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty2.ts(2,6): error TS2304: Cannot find name 'Symbol'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty2.ts (2 errors) ==== | ||
interface I { | ||
[Symbol.unscopables](): string; | ||
~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
~~~~~~ | ||
!!! error TS2304: Cannot find name 'Symbol'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty3.ts(2,5): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty3.ts(2,6): error TS2304: Cannot find name 'Symbol'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty3.ts (2 errors) ==== | ||
declare class C { | ||
[Symbol.unscopables](): string; | ||
~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
~~~~~~ | ||
!!! error TS2304: Cannot find name 'Symbol'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty4.ts(2,5): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty4.ts(2,6): error TS2304: Cannot find name 'Symbol'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty4.ts (2 errors) ==== | ||
declare class C { | ||
[Symbol.isRegExp]: string; | ||
~~~~~~~~~~~~~~~~~ | ||
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
~~~~~~ | ||
!!! error TS2304: Cannot find name 'Symbol'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty5.ts(2,5): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty5.ts(2,6): error TS2304: Cannot find name 'Symbol'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty5.ts (2 errors) ==== | ||
class C { | ||
[Symbol.isRegExp]: string; | ||
~~~~~~~~~~~~~~~~~ | ||
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
~~~~~~ | ||
!!! error TS2304: Cannot find name 'Symbol'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty6.ts(2,5): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty6.ts(2,6): error TS2304: Cannot find name 'Symbol'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty6.ts (2 errors) ==== | ||
class C { | ||
[Symbol.toStringTag]: string = ""; | ||
~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
~~~~~~ | ||
!!! error TS2304: Cannot find name 'Symbol'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty7.ts(2,5): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty7.ts(2,6): error TS2304: Cannot find name 'Symbol'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty7.ts (2 errors) ==== | ||
class C { | ||
[Symbol.toStringTag](): void { } | ||
~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
~~~~~~ | ||
!!! error TS2304: Cannot find name 'Symbol'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty8.ts(2,5): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty8.ts(2,6): error TS2304: Cannot find name 'Symbol'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty8.ts (2 errors) ==== | ||
var x: { | ||
[Symbol.toPrimitive](): string | ||
~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
~~~~~~ | ||
!!! error TS2304: Cannot find name 'Symbol'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty9.ts(2,5): error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty9.ts(2,6): error TS2304: Cannot find name 'Symbol'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript5/Symbols/parserES5SymbolProperty9.ts (2 errors) ==== | ||
var x: { | ||
[Symbol.toPrimitive]: string | ||
~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS1167: Computed property names are only available when targeting ECMAScript 6 and higher. | ||
~~~~~~ | ||
!!! error TS2304: Cannot find name 'Symbol'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty1.ts(2,5): error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty1.ts (1 errors) ==== | ||
interface I { | ||
[Symbol.iterator]: string; | ||
~~~~~~~~~~~~~~~~~ | ||
!!! error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//// [parserSymbolProperty1.ts] | ||
interface I { | ||
[Symbol.iterator]: string; | ||
} | ||
|
||
//// [parserSymbolProperty1.js] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty2.ts(2,5): error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty2.ts (1 errors) ==== | ||
interface I { | ||
[Symbol.unscopables](): string; | ||
~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//// [parserSymbolProperty2.ts] | ||
interface I { | ||
[Symbol.unscopables](): string; | ||
} | ||
|
||
//// [parserSymbolProperty2.js] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty3.ts(2,5): error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty3.ts (1 errors) ==== | ||
declare class C { | ||
[Symbol.unscopables](): string; | ||
~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//// [parserSymbolProperty3.ts] | ||
declare class C { | ||
[Symbol.unscopables](): string; | ||
} | ||
|
||
//// [parserSymbolProperty3.js] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty4.ts(2,5): error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty4.ts (1 errors) ==== | ||
declare class C { | ||
[Symbol.isRegExp]: string; | ||
~~~~~~~~~~~~~~~~~ | ||
!!! error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//// [parserSymbolProperty4.ts] | ||
declare class C { | ||
[Symbol.isRegExp]: string; | ||
} | ||
|
||
//// [parserSymbolProperty4.js] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty5.ts(2,5): error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty5.ts (1 errors) ==== | ||
class C { | ||
[Symbol.isRegExp]: string; | ||
~~~~~~~~~~~~~~~~~ | ||
!!! error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//// [parserSymbolProperty5.ts] | ||
class C { | ||
[Symbol.isRegExp]: string; | ||
} | ||
|
||
//// [parserSymbolProperty5.js] | ||
var C = (function () { | ||
function C() { | ||
} | ||
return C; | ||
})(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty6.ts(2,5): error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty6.ts (1 errors) ==== | ||
class C { | ||
[Symbol.toStringTag]: string = ""; | ||
~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//// [parserSymbolProperty6.ts] | ||
class C { | ||
[Symbol.toStringTag]: string = ""; | ||
} | ||
|
||
//// [parserSymbolProperty6.js] | ||
var C = (function () { | ||
function C() { | ||
this[Symbol.toStringTag] = ""; | ||
} | ||
return C; | ||
})(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty7.ts(2,5): error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty7.ts (1 errors) ==== | ||
class C { | ||
[Symbol.toStringTag](): void { } | ||
~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//// [parserSymbolProperty7.ts] | ||
class C { | ||
[Symbol.toStringTag](): void { } | ||
} | ||
|
||
//// [parserSymbolProperty7.js] | ||
var C = (function () { | ||
function C() { | ||
} | ||
C.prototype[Symbol.toStringTag] = function () { | ||
}; | ||
return C; | ||
})(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty8.ts(2,5): error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty8.ts (1 errors) ==== | ||
var x: { | ||
[Symbol.toPrimitive](): string | ||
~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//// [parserSymbolProperty8.ts] | ||
var x: { | ||
[Symbol.toPrimitive](): string | ||
} | ||
|
||
//// [parserSymbolProperty8.js] | ||
var x; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty9.ts(2,5): error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
|
||
|
||
==== tests/cases/conformance/parser/ecmascript6/Symbols/parserSymbolProperty9.ts (1 errors) ==== | ||
var x: { | ||
[Symbol.toPrimitive]: string | ||
~~~~~~~~~~~~~~~~~~~~ | ||
!!! error TS2464: A computed property name must be of type 'string', 'number', or 'any'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//// [parserSymbolProperty9.ts] | ||
var x: { | ||
[Symbol.toPrimitive]: string | ||
} | ||
|
||
//// [parserSymbolProperty9.js] | ||
var x; |
I find it slightly off that we have to pass in the message for check Grammar. Why don't we move the entire if /else inside?