Skip to content

Class expressions #3568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ namespace ts {
function getStrictModeIdentifierMessage(node: Node) {
// Provide specialized messages to help the user understand why we think they're in
// strict mode.
if (getAncestor(node, SyntaxKind.ClassDeclaration) || getAncestor(node, SyntaxKind.ClassExpression)) {
if (getContainingClass(node)) {
return Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode;
}

Expand Down Expand Up @@ -688,7 +688,7 @@ namespace ts {
function getStrictModeEvalOrArgumentsMessage(node: Node) {
// Provide specialized messages to help the user understand why we think they're in
// strict mode.
if (getAncestor(node, SyntaxKind.ClassDeclaration) || getAncestor(node, SyntaxKind.ClassExpression)) {
if (getContainingClass(node)) {
return Diagnostics.Invalid_use_of_0_Class_definitions_are_automatically_in_strict_mode;
}

Expand Down Expand Up @@ -1031,7 +1031,7 @@ namespace ts {
// containing class.
if (node.flags & NodeFlags.AccessibilityModifier &&
node.parent.kind === SyntaxKind.Constructor &&
(node.parent.parent.kind === SyntaxKind.ClassDeclaration || node.parent.parent.kind === SyntaxKind.ClassExpression)) {
isClassLike(node.parent.parent)) {

let classDeclaration = <ClassLikeDeclaration>node.parent.parent;
declareSymbol(classDeclaration.symbol.members, classDeclaration.symbol, node, SymbolFlags.Property, SymbolFlags.PropertyExcludes);
Expand Down
176 changes: 79 additions & 97 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,5 @@ namespace ts {
enum_declarations_can_only_be_used_in_a_ts_file: { code: 8015, category: DiagnosticCategory.Error, key: "'enum declarations' can only be used in a .ts file." },
type_assertion_expressions_can_only_be_used_in_a_ts_file: { code: 8016, category: DiagnosticCategory.Error, key: "'type assertion expressions' can only be used in a .ts file." },
decorators_can_only_be_used_in_a_ts_file: { code: 8017, category: DiagnosticCategory.Error, key: "'decorators' can only be used in a .ts file." },
Only_identifiers_Slashqualified_names_with_optional_type_arguments_are_currently_supported_in_a_class_extends_clauses: { code: 9002, category: DiagnosticCategory.Error, key: "Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clauses." },
class_expressions_are_not_currently_supported: { code: 9003, category: DiagnosticCategory.Error, key: "'class' expressions are not currently supported." },
};
}
9 changes: 0 additions & 9 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2259,14 +2259,5 @@
"'decorators' can only be used in a .ts file.": {
"category": "Error",
"code": 8017
},

"Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clauses.": {
"category": "Error",
"code": 9002
},
"'class' expressions are not currently supported.": {
"category": "Error",
"code": 9003
}
}
7 changes: 6 additions & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
return makeUniqueName("default");
}

function generateNameForClassExpression() {
return makeUniqueName("class");
}

function generateNameForNode(node: Node) {
switch (node.kind) {
case SyntaxKind.Identifier:
Expand All @@ -276,9 +280,10 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
return generateNameForImportOrExportDeclaration(<ImportDeclaration | ExportDeclaration>node);
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
case SyntaxKind.ExportAssignment:
return generateNameForExportDefault();
case SyntaxKind.ClassExpression:
return generateNameForClassExpression();
}
}

Expand Down
35 changes: 17 additions & 18 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ namespace ts {
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.TypeAliasDeclaration:
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
// These are not allowed inside a generator now, but eventually they may be allowed
// as local types. Regardless, any yield statements contained within them should be
// skipped in this traversal.
Expand Down Expand Up @@ -579,26 +580,15 @@ namespace ts {
return true;
}
}

return false;
}

export function isAccessor(node: Node): boolean {
if (node) {
switch (node.kind) {
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
return true;
}
}

return false;
return node && (node.kind === SyntaxKind.GetAccessor || node.kind === SyntaxKind.SetAccessor);
}

export function isClassLike(node: Node): boolean {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can now make this return node is ClassLikeDeclaration

if (node) {
return node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.ClassExpression;
}
return node && (node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.ClassExpression);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this !!node so this never returns undefined?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to, undefined is just as good.

}

export function isFunctionLike(node: Node): boolean {
Expand All @@ -620,7 +610,6 @@ namespace ts {
return true;
}
}

return false;
}

Expand All @@ -641,6 +630,15 @@ namespace ts {
}
}

export function getContainingClass(node: Node): ClassLikeDeclaration {
while (true) {
node = node.parent;
if (!node || isClassLike(node)) {
return <ClassLikeDeclaration>node;
}
}
}

export function getThisContainer(node: Node, includeArrowFunctions: boolean): Node {
while (true) {
node = node.parent;
Expand All @@ -653,7 +651,7 @@ namespace ts {
// then the computed property is not a 'this' container.
// A computed property name in a class needs to be a this container
// so that we can error on it.
if (node.parent.parent.kind === SyntaxKind.ClassDeclaration) {
if (isClassLike(node.parent.parent)) {
return node;
}
// If this is a computed property, then the parent should not
Expand Down Expand Up @@ -708,7 +706,7 @@ namespace ts {
// then the computed property is not a 'super' container.
// A computed property name in a class needs to be a super container
// so that we can error on it.
if (node.parent.parent.kind === SyntaxKind.ClassDeclaration) {
if (isClassLike(node.parent.parent)) {
return node;
}
// If this is a computed property, then the parent should not
Expand Down Expand Up @@ -1087,6 +1085,7 @@ namespace ts {
case SyntaxKind.ArrowFunction:
case SyntaxKind.BindingElement:
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
case SyntaxKind.Constructor:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.EnumMember:
Expand Down Expand Up @@ -1235,7 +1234,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;
}
Expand Down Expand Up @@ -1917,7 +1916,7 @@ namespace ts {
export function isExpressionWithTypeArgumentsInClassExtendsClause(node: Node): boolean {
return node.kind === SyntaxKind.ExpressionWithTypeArguments &&
(<HeritageClause>node.parent).token === SyntaxKind.ExtendsKeyword &&
node.parent.parent.kind === SyntaxKind.ClassDeclaration;
isClassLike(node.parent.parent);
}

// Returns false if this heritage clause element's expression contains something unsupported
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions tests/baselines/reference/anonymousClassExpression1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ function f() {
//// [anonymousClassExpression1.js]
function f() {
return typeof (function () {
function default_1() {
function class_1() {
}
return default_1;
return class_1;
})() === "function";
}
6 changes: 6 additions & 0 deletions tests/baselines/reference/anonymousClassExpression1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=== tests/cases/compiler/anonymousClassExpression1.ts ===
function f() {
>f : Symbol(f, Decl(anonymousClassExpression1.ts, 0, 0))

return typeof class {} === "function";
}
10 changes: 10 additions & 0 deletions tests/baselines/reference/anonymousClassExpression1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
=== tests/cases/compiler/anonymousClassExpression1.ts ===
function f() {
>f : () => boolean

return typeof class {} === "function";
>typeof class {} === "function" : boolean
>typeof class {} : string
>class {} : typeof (Anonymous class)
>"function" : string
}
24 changes: 0 additions & 24 deletions tests/baselines/reference/classExpression.errors.txt

This file was deleted.

23 changes: 23 additions & 0 deletions tests/baselines/reference/classExpression.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/conformance/classes/classExpression.ts ===
var x = class C {
>x : Symbol(x, Decl(classExpression.ts, 0, 3))
>C : Symbol(C, Decl(classExpression.ts, 0, 7))
}

var y = {
>y : Symbol(y, Decl(classExpression.ts, 3, 3))

foo: class C2 {
>foo : Symbol(foo, Decl(classExpression.ts, 3, 9))
>C2 : Symbol(C2, Decl(classExpression.ts, 4, 8))
}
}

module M {
>M : Symbol(M, Decl(classExpression.ts, 6, 1))

var z = class C4 {
>z : Symbol(z, Decl(classExpression.ts, 9, 7))
>C4 : Symbol(C4, Decl(classExpression.ts, 9, 11))
}
}
27 changes: 27 additions & 0 deletions tests/baselines/reference/classExpression.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/conformance/classes/classExpression.ts ===
var x = class C {
>x : typeof C
>class C {} : typeof C
>C : typeof C
}

var y = {
>y : { foo: typeof C2; }
>{ foo: class C2 { }} : { foo: typeof C2; }

foo: class C2 {
>foo : typeof C2
>class C2 { } : typeof C2
>C2 : typeof C2
}
}

module M {
>M : typeof M

var z = class C4 {
>z : typeof C4
>class C4 { } : typeof C4
>C4 : typeof C4
}
}
7 changes: 0 additions & 7 deletions tests/baselines/reference/classExpression1.errors.txt

This file was deleted.

5 changes: 5 additions & 0 deletions tests/baselines/reference/classExpression1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
=== tests/cases/conformance/classes/classExpressions/classExpression1.ts ===
var v = class C {};
>v : Symbol(v, Decl(classExpression1.ts, 0, 3))
>C : Symbol(C, Decl(classExpression1.ts, 0, 7))

6 changes: 6 additions & 0 deletions tests/baselines/reference/classExpression1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
=== tests/cases/conformance/classes/classExpressions/classExpression1.ts ===
var v = class C {};
>v : typeof C
>class C {} : typeof C
>C : typeof C

8 changes: 0 additions & 8 deletions tests/baselines/reference/classExpression2.errors.txt

This file was deleted.

5 changes: 5 additions & 0 deletions tests/baselines/reference/classExpression2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ class D { }
var v = class C extends D {};

//// [classExpression2.js]
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var D = (function () {
function D() {
}
Expand Down
9 changes: 9 additions & 0 deletions tests/baselines/reference/classExpression2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== tests/cases/conformance/classes/classExpressions/classExpression2.ts ===
class D { }
>D : Symbol(D, Decl(classExpression2.ts, 0, 0))

var v = class C extends D {};
>v : Symbol(v, Decl(classExpression2.ts, 1, 3))
>C : Symbol(C, Decl(classExpression2.ts, 1, 7))
>D : Symbol(D, Decl(classExpression2.ts, 0, 0))

10 changes: 10 additions & 0 deletions tests/baselines/reference/classExpression2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
=== tests/cases/conformance/classes/classExpressions/classExpression2.ts ===
class D { }
>D : D

var v = class C extends D {};
>v : typeof C
>class C extends D {} : typeof C
>C : typeof C
>D : D

Loading