From 40ba1be0a98e2634e009662900d995ee3a218776 Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 6 Oct 2015 19:46:46 -0700 Subject: [PATCH] Address PR feedback Conflicts: src/compiler/diagnosticInformationMap.generated.ts src/compiler/diagnosticMessages.json src/compiler/types.ts --- src/compiler/checker.ts | 17 ++++++++++++----- src/compiler/diagnosticMessages.json | 4 ++++ src/compiler/types.ts | 2 ++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index eef80e19776..3c9b32a238f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -7251,9 +7251,8 @@ namespace ts { // (()=>this); // No Error // super(); // } - if ((container).hasSeenSuperBeforeThis === undefined) { - (container).hasSeenSuperBeforeThis = false; - } + let nodeLinks = getNodeLinks(container); + nodeLinks.flags |= NodeCheckFlags.HasSeenThisCall; } // Now skip arrow functions to get the "real" owner of 'this'. @@ -10200,6 +10199,14 @@ namespace ts { const signature = getResolvedSignature(node); if (node.expression.kind === SyntaxKind.SuperKeyword) { + let containgFunction = getContainingFunction(node.expression); + + if (containgFunction && containgFunction.kind === SyntaxKind.Constructor) { + let nodeLinks = getNodeLinks(containgFunction); + if (!(nodeLinks.flags & NodeCheckFlags.HasSeenThisCall)) { + nodeLinks.flags |= NodeCheckFlags.HasSeenSuperBeforeThis; + } + } return voidType; } if (node.kind === SyntaxKind.NewExpression) { @@ -11828,9 +11835,9 @@ namespace ts { markThisReferencesAsErrors(superCallStatement.expression); } } - else if (!node.hasSeenSuperBeforeThis) { + else if (!(getNodeCheckFlags(node) & NodeCheckFlags.HasSeenSuperBeforeThis)){ // In ES6, super inside constructor of class-declaration has to precede "this" accessing - error(superCallStatement, Diagnostics.super_has_to_be_called_before_this_accessing); + error(superCallStatement, Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class); } } else if (baseConstructorType !== nullType) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 27ce11f5286..ca9ad4fd775 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2642,5 +2642,9 @@ "JSX element '{0}' has no corresponding closing tag.": { "category": "Error", "code": 17008 + }, + "'super' must be called before accessing 'this' in the constructor of a derived class.": { + "category": "Error", + "code": 17008 } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 6963797d601..0c37a12ecdb 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2068,6 +2068,8 @@ namespace ts { LoopWithCapturedBlockScopedBinding = 0x00010000, // Loop that contains block scoped variable captured in closure CapturedBlockScopedBinding = 0x00020000, // Block-scoped binding that is captured in some function BlockScopedBindingInLoop = 0x00040000, // Block-scoped binding with declaration nested inside iteration statement + HasSeenSuperBeforeThis = 0x00080000, // Set during the binding if the 'super' is used before 'this' in constructor function + HasSeenThisCall = 0x00100000, // Set during the binding when encounter 'this' } /* @internal */