Support classes without names in our AST. Report any issues with this at 'check' time.

This commit is contained in:
unknown 2015-04-01 17:20:20 -07:00
parent 22a87fb9ca
commit ba8be9eef4
20 changed files with 67 additions and 32 deletions

View file

@ -9757,7 +9757,12 @@ module ts {
function checkClassDeclaration(node: ClassDeclaration) {
// Grammar checking
if (node.parent.kind !== SyntaxKind.ModuleBlock && node.parent.kind !== SyntaxKind.SourceFile) {
grammarErrorOnNode(node, Diagnostics.class_declarations_are_only_supported_directly_inside_a_module_or_as_a_top_level_declaration);
grammarErrorOnFirstToken(node, Diagnostics.class_declarations_are_only_supported_directly_inside_a_module_or_as_a_top_level_declaration);
}
// node.flags & NodeFlags.Default || kind === SyntaxKind.ClassExpression
if (!node.name && !(node.flags & NodeFlags.Default)) {
grammarErrorOnNode(node, Diagnostics.A_class_declaration_without_the_default_modifier_must_have_a_name);
}
checkGrammarClassDeclarationHeritageClauses(node);

View file

@ -167,6 +167,7 @@ module ts {
Decorators_cannot_be_applied_to_multiple_get_Slashset_accessors_of_the_same_name: { code: 1207, category: DiagnosticCategory.Error, key: "Decorators cannot be applied to multiple get/set accessors of the same name." },
Cannot_compile_non_external_modules_when_the_separateCompilation_flag_is_provided: { code: 1208, category: DiagnosticCategory.Error, key: "Cannot compile non-external modules when the '--separateCompilation' flag is provided." },
Ambient_const_enums_are_not_allowed_when_the_separateCompilation_flag_is_provided: { code: 1209, category: DiagnosticCategory.Error, key: "Ambient const enums are not allowed when the '--separateCompilation' flag is provided." },
A_class_declaration_without_the_default_modifier_must_have_a_name: { code: 1210, category: DiagnosticCategory.Error, key: "A class declaration without the 'default' modifier must have a name" },
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },

View file

@ -659,6 +659,10 @@
"category": "Error",
"code": 1209
},
"A class declaration without the 'default' modifier must have a name": {
"category": "Error",
"code": 1210
},
"Duplicate identifier '{0}'.": {
"category": "Error",
"code": 2300

View file

@ -4764,7 +4764,7 @@ module ts {
node.decorators = decorators;
setModifiers(node, modifiers);
parseExpected(SyntaxKind.ClassKeyword);
node.name = node.flags & NodeFlags.Default ? parseOptionalIdentifier() : parseIdentifier();
node.name = parseOptionalIdentifier();
node.typeParameters = parseTypeParameters();
node.heritageClauses = parseHeritageClauses(/*isClassHeritageClause:*/ true);

View file

@ -418,10 +418,10 @@ module ts.NavigationBar {
}
function createFunctionItem(node: FunctionDeclaration) {
if ((node.name || node.flags & NodeFlags.Default) && node.body && node.body.kind === SyntaxKind.Block) {
if (node.body && node.body.kind === SyntaxKind.Block) {
let childItems = getItemsWorker(sortNodes((<Block>node.body).statements), createChildItem);
return getNavigationBarItem((!node.name && node.flags & NodeFlags.Default) ? "default": node.name.text ,
return getNavigationBarItem(!node.name ? "default": node.name.text ,
ts.ScriptElementKind.functionElement,
getNodeModifiers(node),
[getNodeSpan(node)],
@ -470,7 +470,7 @@ module ts.NavigationBar {
childItems = getItemsWorker(sortNodes(nodes), createChildItem);
}
var nodeName = !node.name && (node.flags & NodeFlags.Default) ? "default" : node.name.text;
var nodeName = !node.name ? "default" : node.name.text;
return getNavigationBarItem(
nodeName,

View file

@ -0,0 +1,9 @@
tests/cases/compiler/anonymousClassExpression1.ts(2,19): error TS9003: 'class' expressions are not currently supported.
==== tests/cases/compiler/anonymousClassExpression1.ts (1 errors) ====
function f() {
return typeof class {} === "function";
~~~~~
!!! error TS9003: 'class' expressions are not currently supported.
}

View file

@ -0,0 +1,13 @@
//// [anonymousClassExpression1.ts]
function f() {
return typeof class {} === "function";
}
//// [anonymousClassExpression1.js]
function f() {
return typeof (function () {
function () {
}
return ;
})() === "function";
}

View file

@ -1,4 +1,4 @@
tests/cases/compiler/classDeclarationBlockScoping1.ts(5,11): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
tests/cases/compiler/classDeclarationBlockScoping1.ts(5,5): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
==== tests/cases/compiler/classDeclarationBlockScoping1.ts (1 errors) ====
@ -7,7 +7,7 @@ tests/cases/compiler/classDeclarationBlockScoping1.ts(5,11): error TS9004: 'clas
{
class C {
~
~~~~~
!!! error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
}
}

View file

@ -1,16 +1,16 @@
tests/cases/compiler/classDeclarationBlockScoping2.ts(2,11): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
tests/cases/compiler/classDeclarationBlockScoping2.ts(5,15): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
tests/cases/compiler/classDeclarationBlockScoping2.ts(2,5): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
tests/cases/compiler/classDeclarationBlockScoping2.ts(5,9): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
==== tests/cases/compiler/classDeclarationBlockScoping2.ts (2 errors) ====
function f() {
class C {}
~
~~~~~
!!! error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
var c1 = C;
{
class C {}
~
~~~~~
!!! error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
var c2 = C;
}

View file

@ -1,10 +1,10 @@
tests/cases/compiler/classExpressionTest1.ts(2,11): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
tests/cases/compiler/classExpressionTest1.ts(2,5): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
==== tests/cases/compiler/classExpressionTest1.ts (1 errors) ====
function M() {
class C<X> {
~
~~~~~
!!! error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
f<T>() {
var t: T;

View file

@ -1,9 +1,9 @@
tests/cases/conformance/classes/classDeclarations/classInsideBlock.ts(2,11): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
tests/cases/conformance/classes/classDeclarations/classInsideBlock.ts(2,5): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
==== tests/cases/conformance/classes/classDeclarations/classInsideBlock.ts (1 errors) ====
function foo() {
class C { }
~
~~~~~
!!! error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
}

View file

@ -1,4 +1,4 @@
tests/cases/conformance/classes/classDeclarations/classWithPredefinedTypesAsNames2.ts(3,7): error TS1003: Identifier expected.
tests/cases/conformance/classes/classDeclarations/classWithPredefinedTypesAsNames2.ts(3,7): error TS1005: '{' expected.
==== tests/cases/conformance/classes/classDeclarations/classWithPredefinedTypesAsNames2.ts (1 errors) ====
@ -6,4 +6,4 @@ tests/cases/conformance/classes/classDeclarations/classWithPredefinedTypesAsName
class void {}
~~~~
!!! error TS1003: Identifier expected.
!!! error TS1005: '{' expected.

View file

@ -5,9 +5,9 @@ class void {}
//// [classWithPredefinedTypesAsNames2.js]
// classes cannot use predefined types as names
var = (function () {
function () {
var default_1 = (function () {
function default_1() {
}
return ;
return default_1;
})();
void {};

View file

@ -1,4 +1,4 @@
tests/cases/conformance/types/specifyingTypes/predefinedTypes/objectTypesWithPredefinedTypesAsName2.ts(3,7): error TS1003: Identifier expected.
tests/cases/conformance/types/specifyingTypes/predefinedTypes/objectTypesWithPredefinedTypesAsName2.ts(3,7): error TS1005: '{' expected.
==== tests/cases/conformance/types/specifyingTypes/predefinedTypes/objectTypesWithPredefinedTypesAsName2.ts (1 errors) ====
@ -6,4 +6,4 @@ tests/cases/conformance/types/specifyingTypes/predefinedTypes/objectTypesWithPre
class void {} // parse error unlike the others
~~~~
!!! error TS1003: Identifier expected.
!!! error TS1005: '{' expected.

View file

@ -5,9 +5,9 @@ class void {} // parse error unlike the others
//// [objectTypesWithPredefinedTypesAsName2.js]
// it is an error to use a predefined type as a type name
var = (function () {
function () {
var default_1 = (function () {
function default_1() {
}
return ;
return default_1;
})();
void {}; // parse error unlike the others

View file

@ -1,6 +1,6 @@
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInvalidIdentifiersInVariableStatements1.ts(1,5): error TS1134: Variable declaration expected.
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInvalidIdentifiersInVariableStatements1.ts(3,5): error TS1134: Variable declaration expected.
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInvalidIdentifiersInVariableStatements1.ts(3,10): error TS1003: Identifier expected.
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInvalidIdentifiersInVariableStatements1.ts(3,10): error TS1005: '{' expected.
==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInvalidIdentifiersInVariableStatements1.ts (3 errors) ====
@ -12,6 +12,6 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInv
~~~~~
!!! error TS1134: Variable declaration expected.
~
!!! error TS1003: Identifier expected.
!!! error TS1005: '{' expected.
var bar;

View file

@ -9,10 +9,10 @@ var bar;
var ;
var foo;
var ;
var = (function () {
function () {
var default_1 = (function () {
function default_1() {
}
return ;
return default_1;
})();
;
var bar;

View file

@ -0,0 +1,3 @@
function f() {
return typeof class {} === "function";
}

View file

@ -8,5 +8,5 @@ edit.insertLine("module A");
edit.insert("export class ");
// should not crash
verify.getScriptLexicalStructureListCount(1);
verify.getScriptLexicalStructureListCount(2);

View file

@ -8,4 +8,4 @@
// The class is unnamed, so its method is not included either.
verify.getScriptLexicalStructureListCount(0);
verify.getScriptLexicalStructureListCount(2);