From e15eab9d9961cbd978389e2e0504e366e648f020 Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 15 Sep 2015 15:11:04 -0700 Subject: [PATCH] Add basic check for use super before this Conflicts: src/compiler/diagnosticInformationMap.generated.ts src/compiler/diagnosticMessages.json src/compiler/types.ts --- src/compiler/checker.ts | 13 +++++++++++++ src/compiler/diagnosticMessages.json | 4 ++++ src/compiler/types.ts | 1 + 3 files changed, 18 insertions(+) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 7f20c542128..5238a4780e2 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6866,6 +6866,11 @@ namespace ts { // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks break; case SyntaxKind.Constructor: + // TODO(yuisu): Comments + if ((container).hasSeenSuperBeforeThis === undefined) { + (container).hasSeenSuperBeforeThis = false; + } + if (isInConstructorArgumentInitializer(node, container)) { error(node, Diagnostics.this_cannot_be_referenced_in_constructor_arguments); // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks @@ -9588,6 +9593,10 @@ namespace ts { const signature = getResolvedSignature(node); if (node.expression.kind === SyntaxKind.SuperKeyword) { + let containgFunction = getContainingFunction(node.expression); + if (containgFunction && containgFunction.kind === SyntaxKind.Constructor && (containgFunction).hasSeenSuperBeforeThis === undefined) { + (containgFunction).hasSeenSuperBeforeThis = true; + } return voidType; } if (node.kind === SyntaxKind.NewExpression) { @@ -11182,6 +11191,10 @@ namespace ts { markThisReferencesAsErrors(superCallStatement.expression); } } + else if (!node.hasSeenSuperBeforeThis) { + // TODO: comment + error(node, Diagnostics.super_has_to_be_called_before_this_accessing); + } } else if (baseConstructorType !== nullType) { error(node, Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call); diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 62661706373..51a4e2c6c8f 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2539,5 +2539,9 @@ "A type assertion expression is not allowed in the left-hand side of an exponentiation expression. Consider enclosing the expression in parentheses.": { "category": "Error", "code": 17007 + }, + "'super' has to be called before 'this' accessing.": { + "category": "Error", + "code": 17006 } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index f8b26ddaa6d..5a216f8bbcd 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -695,6 +695,7 @@ namespace ts { // @kind(SyntaxKind.Constructor) export interface ConstructorDeclaration extends FunctionLikeDeclaration, ClassElement { body?: FunctionBody; + hasSeenSuperBeforeThis: boolean; // TODDO (yuisu): comment } // For when we encounter a semicolon in a class declaration. ES6 allows these as class elements.