From a264be5afaa8ac6f27f827303b789618a1dd068f Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 18 Jun 2015 10:25:23 -0700 Subject: [PATCH] Actually check types in checkClassExpression --- src/compiler/checker.ts | 21 ++++++++------------- src/compiler/utilities.ts | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 232a3db419a..76bc4e9a94e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1402,15 +1402,8 @@ namespace ts { * for the name of the symbol if it is available to match how the user inputted the name. */ function appendSymbolNameOnly(symbol: Symbol, writer: SymbolWriter): void { - if (symbol.declarations && symbol.declarations.length > 0) { - let declaration = symbol.declarations[0]; - if (declaration.name) { - writer.writeSymbol(declarationNameToString(declaration.name), symbol); - return; - } - } - - writer.writeSymbol(symbol.name, symbol); + let name = symbol.declarations && symbol.declarations.length > 0 ? declarationToString(symbol.declarations[0]) : symbol.name; + writer.writeSymbol(name, symbol); } /** @@ -10565,9 +10558,8 @@ namespace ts { } function checkClassExpression(node: ClassExpression): Type { - grammarErrorOnNode(node, Diagnostics.class_expressions_are_not_currently_supported); - forEach(node.members, checkSourceElement); - return unknownType; + checkClassLikeDeclaration(node); + return getTypeOfSymbol(getSymbolOfNode(node)); } function checkClassDeclaration(node: ClassDeclaration) { @@ -10575,7 +10567,10 @@ namespace ts { if (!node.name && !(node.flags & NodeFlags.Default)) { grammarErrorOnFirstToken(node, Diagnostics.A_class_declaration_without_the_default_modifier_must_have_a_name); } + checkClassLikeDeclaration(node); + } + function checkClassLikeDeclaration(node: ClassLikeDeclaration) { checkGrammarClassDeclarationHeritageClauses(node); checkDecorators(node); if (node.name) { @@ -12921,7 +12916,7 @@ namespace ts { } } - function checkGrammarClassDeclarationHeritageClauses(node: ClassDeclaration) { + function checkGrammarClassDeclarationHeritageClauses(node: ClassLikeDeclaration) { let seenExtendsClause = false; let seenImplementsClause = false; diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 7f9888c73d3..f50f280d861 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -247,6 +247,19 @@ namespace ts { declaration.parent.kind === SyntaxKind.CatchClause; } + export function declarationToString(declaration: Declaration) { + if (!declaration.name) { + switch (declaration.kind) { + case SyntaxKind.ClassExpression: + return "(Anonymous class)"; + case SyntaxKind.FunctionExpression: + case SyntaxKind.ArrowFunction: + return "(Anonymous function)"; + } + } + return declarationNameToString(declaration.name); + } + // Return display name of an identifier // Computed property names will just be emitted as "[]", where is the source // text of the expression in the computed property. @@ -1234,7 +1247,7 @@ namespace ts { return heritageClause && heritageClause.types.length > 0 ? heritageClause.types[0] : undefined; } - export function getClassImplementsHeritageClauseElements(node: ClassDeclaration) { + export function getClassImplementsHeritageClauseElements(node: ClassLikeDeclaration) { let heritageClause = getHeritageClause(node.heritageClauses, SyntaxKind.ImplementsKeyword); return heritageClause ? heritageClause.types : undefined; }