Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba8be9e

Browse files
author
unknown
committed
Support classes without names in our AST. Report any issues with this at 'check' time.
1 parent 22a87fb commit ba8be9e

20 files changed

+67
-32
lines changed

src/compiler/checker.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -9757,7 +9757,12 @@ module ts {
97579757
function checkClassDeclaration(node: ClassDeclaration) {
97589758
// Grammar checking
97599759
if (node.parent.kind !== SyntaxKind.ModuleBlock && node.parent.kind !== SyntaxKind.SourceFile) {
9760-
grammarErrorOnNode(node, Diagnostics.class_declarations_are_only_supported_directly_inside_a_module_or_as_a_top_level_declaration);
9760+
grammarErrorOnFirstToken(node, Diagnostics.class_declarations_are_only_supported_directly_inside_a_module_or_as_a_top_level_declaration);
9761+
}
9762+
9763+
// node.flags & NodeFlags.Default || kind === SyntaxKind.ClassExpression
9764+
if (!node.name && !(node.flags & NodeFlags.Default)) {
9765+
grammarErrorOnNode(node, Diagnostics.A_class_declaration_without_the_default_modifier_must_have_a_name);
97619766
}
97629767

97639768
checkGrammarClassDeclarationHeritageClauses(node);

src/compiler/diagnosticInformationMap.generated.ts

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ module ts {
167167
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." },
168168
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." },
169169
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." },
170+
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" },
170171
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
171172
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." },
172173
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,10 @@
659659
"category": "Error",
660660
"code": 1209
661661
},
662+
"A class declaration without the 'default' modifier must have a name": {
663+
"category": "Error",
664+
"code": 1210
665+
},
662666
"Duplicate identifier '{0}'.": {
663667
"category": "Error",
664668
"code": 2300

src/compiler/parser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4764,7 +4764,7 @@ module ts {
47644764
node.decorators = decorators;
47654765
setModifiers(node, modifiers);
47664766
parseExpected(SyntaxKind.ClassKeyword);
4767-
node.name = node.flags & NodeFlags.Default ? parseOptionalIdentifier() : parseIdentifier();
4767+
node.name = parseOptionalIdentifier();
47684768
node.typeParameters = parseTypeParameters();
47694769
node.heritageClauses = parseHeritageClauses(/*isClassHeritageClause:*/ true);
47704770

src/services/navigationBar.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,10 @@ module ts.NavigationBar {
418418
}
419419

420420
function createFunctionItem(node: FunctionDeclaration) {
421-
if ((node.name || node.flags & NodeFlags.Default) && node.body && node.body.kind === SyntaxKind.Block) {
421+
if (node.body && node.body.kind === SyntaxKind.Block) {
422422
let childItems = getItemsWorker(sortNodes((<Block>node.body).statements), createChildItem);
423423

424-
return getNavigationBarItem((!node.name && node.flags & NodeFlags.Default) ? "default": node.name.text ,
424+
return getNavigationBarItem(!node.name ? "default": node.name.text ,
425425
ts.ScriptElementKind.functionElement,
426426
getNodeModifiers(node),
427427
[getNodeSpan(node)],
@@ -470,7 +470,7 @@ module ts.NavigationBar {
470470
childItems = getItemsWorker(sortNodes(nodes), createChildItem);
471471
}
472472

473-
var nodeName = !node.name && (node.flags & NodeFlags.Default) ? "default" : node.name.text;
473+
var nodeName = !node.name ? "default" : node.name.text;
474474

475475
return getNavigationBarItem(
476476
nodeName,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/compiler/anonymousClassExpression1.ts(2,19): error TS9003: 'class' expressions are not currently supported.
2+
3+
4+
==== tests/cases/compiler/anonymousClassExpression1.ts (1 errors) ====
5+
function f() {
6+
return typeof class {} === "function";
7+
~~~~~
8+
!!! error TS9003: 'class' expressions are not currently supported.
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [anonymousClassExpression1.ts]
2+
function f() {
3+
return typeof class {} === "function";
4+
}
5+
6+
//// [anonymousClassExpression1.js]
7+
function f() {
8+
return typeof (function () {
9+
function () {
10+
}
11+
return ;
12+
})() === "function";
13+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/classDeclarationBlockScoping1.ts(5,11): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
1+
tests/cases/compiler/classDeclarationBlockScoping1.ts(5,5): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
22

33

44
==== tests/cases/compiler/classDeclarationBlockScoping1.ts (1 errors) ====
@@ -7,7 +7,7 @@ tests/cases/compiler/classDeclarationBlockScoping1.ts(5,11): error TS9004: 'clas
77

88
{
99
class C {
10-
~
10+
~~~~~
1111
!!! error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
1212
}
1313
}

tests/baselines/reference/classDeclarationBlockScoping2.errors.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
tests/cases/compiler/classDeclarationBlockScoping2.ts(2,11): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
2-
tests/cases/compiler/classDeclarationBlockScoping2.ts(5,15): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
1+
tests/cases/compiler/classDeclarationBlockScoping2.ts(2,5): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
2+
tests/cases/compiler/classDeclarationBlockScoping2.ts(5,9): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
33

44

55
==== tests/cases/compiler/classDeclarationBlockScoping2.ts (2 errors) ====
66
function f() {
77
class C {}
8-
~
8+
~~~~~
99
!!! error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
1010
var c1 = C;
1111
{
1212
class C {}
13-
~
13+
~~~~~
1414
!!! error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
1515
var c2 = C;
1616
}

tests/baselines/reference/classExpressionTest1.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
tests/cases/compiler/classExpressionTest1.ts(2,11): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
1+
tests/cases/compiler/classExpressionTest1.ts(2,5): error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
22

33

44
==== tests/cases/compiler/classExpressionTest1.ts (1 errors) ====
55
function M() {
66
class C<X> {
7-
~
7+
~~~~~
88
!!! error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
99
f<T>() {
1010
var t: T;
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
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.
1+
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.
22

33

44
==== tests/cases/conformance/classes/classDeclarations/classInsideBlock.ts (1 errors) ====
55
function foo() {
66
class C { }
7-
~
7+
~~~~~
88
!!! error TS9004: 'class' declarations are only supported directly inside a module or as a top level declaration.
99
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
tests/cases/conformance/classes/classDeclarations/classWithPredefinedTypesAsNames2.ts(3,7): error TS1003: Identifier expected.
1+
tests/cases/conformance/classes/classDeclarations/classWithPredefinedTypesAsNames2.ts(3,7): error TS1005: '{' expected.
22

33

44
==== tests/cases/conformance/classes/classDeclarations/classWithPredefinedTypesAsNames2.ts (1 errors) ====
55
// classes cannot use predefined types as names
66

77
class void {}
88
~~~~
9-
!!! error TS1003: Identifier expected.
9+
!!! error TS1005: '{' expected.

tests/baselines/reference/classWithPredefinedTypesAsNames2.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ class void {}
55

66
//// [classWithPredefinedTypesAsNames2.js]
77
// classes cannot use predefined types as names
8-
var = (function () {
9-
function () {
8+
var default_1 = (function () {
9+
function default_1() {
1010
}
11-
return ;
11+
return default_1;
1212
})();
1313
void {};
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
tests/cases/conformance/types/specifyingTypes/predefinedTypes/objectTypesWithPredefinedTypesAsName2.ts(3,7): error TS1003: Identifier expected.
1+
tests/cases/conformance/types/specifyingTypes/predefinedTypes/objectTypesWithPredefinedTypesAsName2.ts(3,7): error TS1005: '{' expected.
22

33

44
==== tests/cases/conformance/types/specifyingTypes/predefinedTypes/objectTypesWithPredefinedTypesAsName2.ts (1 errors) ====
55
// it is an error to use a predefined type as a type name
66

77
class void {} // parse error unlike the others
88
~~~~
9-
!!! error TS1003: Identifier expected.
9+
!!! error TS1005: '{' expected.

tests/baselines/reference/objectTypesWithPredefinedTypesAsName2.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ class void {} // parse error unlike the others
55

66
//// [objectTypesWithPredefinedTypesAsName2.js]
77
// it is an error to use a predefined type as a type name
8-
var = (function () {
9-
function () {
8+
var default_1 = (function () {
9+
function default_1() {
1010
}
11-
return ;
11+
return default_1;
1212
})();
1313
void {}; // parse error unlike the others

tests/baselines/reference/parserInvalidIdentifiersInVariableStatements1.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInvalidIdentifiersInVariableStatements1.ts(1,5): error TS1134: Variable declaration expected.
22
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInvalidIdentifiersInVariableStatements1.ts(3,5): error TS1134: Variable declaration expected.
3-
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInvalidIdentifiersInVariableStatements1.ts(3,10): error TS1003: Identifier expected.
3+
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInvalidIdentifiersInVariableStatements1.ts(3,10): error TS1005: '{' expected.
44

55

66
==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInvalidIdentifiersInVariableStatements1.ts (3 errors) ====
@@ -12,6 +12,6 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/VariableLists/parserInv
1212
~~~~~
1313
!!! error TS1134: Variable declaration expected.
1414
~
15-
!!! error TS1003: Identifier expected.
15+
!!! error TS1005: '{' expected.
1616
var bar;
1717

tests/baselines/reference/parserInvalidIdentifiersInVariableStatements1.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ var bar;
99
var ;
1010
var foo;
1111
var ;
12-
var = (function () {
13-
function () {
12+
var default_1 = (function () {
13+
function default_1() {
1414
}
15-
return ;
15+
return default_1;
1616
})();
1717
;
1818
var bar;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function f() {
2+
return typeof class {} === "function";
3+
}

tests/cases/fourslash/scriptLexicalStructureItems2.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ edit.insertLine("module A");
88
edit.insert("export class ");
99

1010
// should not crash
11-
verify.getScriptLexicalStructureListCount(1);
11+
verify.getScriptLexicalStructureListCount(2);
1212

tests/cases/fourslash/scriptLexicalStructureMissingName2.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99

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

0 commit comments

Comments
 (0)