From 55254be2323b56f58e0b9517dd609a2e0773b3ed Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 15 Sep 2015 15:11:04 -0700 Subject: [PATCH 1/8] Add basic check for use super before this --- src/compiler/checker.ts | 15 ++++++++++++++- .../diagnosticInformationMap.generated.ts | 1 + src/compiler/diagnosticMessages.json | 4 ++++ src/compiler/types.ts | 1 + 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index df432808f3008..2ae81f3156069 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6422,6 +6422,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 @@ -9034,6 +9039,10 @@ namespace ts { let 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) { @@ -10560,12 +10569,16 @@ namespace ts { } if (!superCallStatement) { error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties); - } + } else { // In such a required super call, it is a compile-time error for argument expressions to reference this. 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/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index af48e67d964d7..c6d175f91ff7c 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -613,5 +613,6 @@ namespace ts { JSX_attribute_expected: { code: 17003, category: DiagnosticCategory.Error, key: "JSX attribute expected." }, Cannot_use_JSX_unless_the_jsx_flag_is_provided: { code: 17004, category: DiagnosticCategory.Error, key: "Cannot use JSX unless the '--jsx' flag is provided." }, A_constructor_cannot_contain_a_super_call_when_its_class_extends_null: { code: 17005, category: DiagnosticCategory.Error, key: "A constructor cannot contain a 'super' call when its class extends 'null'" }, + super_has_to_be_called_before_this_accessing: { code: 17006, category: DiagnosticCategory.Error, key: "'super' has to be called before 'this' accessing." }, }; } \ No newline at end of file diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 7abec7b7c43cf..20bfbf698d52b 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2446,5 +2446,9 @@ "A constructor cannot contain a 'super' call when its class extends 'null'": { "category": "Error", "code": 17005 + }, + "'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 265fa5c86fbcf..f4d3a6fa65bdb 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -619,6 +619,7 @@ namespace ts { export interface ConstructorDeclaration extends FunctionLikeDeclaration, ClassElement { body?: Block; + hasSeenSuperBeforeThis: boolean; // TODDO (yuisu): comment } // For when we encounter a semicolon in a class declaration. ES6 allows these as class elements. From e73a6f8eea9b7c31d96a599a9701f7c2e2ebf451 Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 15 Sep 2015 16:42:04 -0700 Subject: [PATCH 2/8] Fix where to report on error. Move from constructor node to just super node --- src/compiler/checker.ts | 59 ++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2ae81f3156069..21862c4a24b3b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6404,6 +6404,22 @@ namespace ts { let container = getThisContainer(node, /* includeArrowFunctions */ true); let needToCaptureLexicalThis = false; + + if (container.kind === SyntaxKind.Constructor) { + // Keep track of whether we have seen "super" before encounter "this" so that + // we can report appropriate error later in checkConstructorDeclaration + // We have to do the check here to make sure we won't give false error when + // "this" is used in arrow functions + // For example: + // constructor() { + // (()=>this); // No Error + // super(); + // } + if ((container).hasSeenSuperBeforeThis === undefined) { + (container).hasSeenSuperBeforeThis = false; + } + } + // Now skip arrow functions to get the "real" owner of 'this'. if (container.kind === SyntaxKind.ArrowFunction) { container = getThisContainer(container, /* includeArrowFunctions */ false); @@ -6422,11 +6438,6 @@ 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 @@ -10538,8 +10549,25 @@ namespace ts { let containingClassSymbol = getSymbolOfNode(containingClassDecl); let containingClassInstanceType = getDeclaredTypeOfSymbol(containingClassSymbol); let baseConstructorType = getBaseConstructorTypeOfClass(containingClassInstanceType); + let statements = (node.body).statements; + let superCallStatement: ExpressionStatement; + let isSuperCallFirstStatment: boolean; - if (containsSuperCall(node.body)) { + for (let statement of statements) { + if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCallExpression((statement).expression)) { + superCallStatement = statement; + if (isSuperCallFirstStatment === undefined) { + isSuperCallFirstStatment = true; + } + } + else if (isSuperCallFirstStatment === undefined && !isPrologueDirective(statement)) { + isSuperCallFirstStatment = false; + } + } + + // The main different between looping through each statement in constructor and calling containsSuperCall is that, + // containsSuperCall will consider "super" inside computed-property for inner class declaration + if (superCallStatement || containsSuperCall(node.body)) { if (baseConstructorType === nullType) { error(node, Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null); } @@ -10556,19 +10584,8 @@ namespace ts { // Skip past any prologue directives to find the first statement // to ensure that it was a super call. if (superCallShouldBeFirst) { - let statements = (node.body).statements; - let superCallStatement: ExpressionStatement; - for (let statement of statements) { - if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCallExpression((statement).expression)) { - superCallStatement = statement; - break; - } - if (!isPrologueDirective(statement)) { - break; - } - } - if (!superCallStatement) { - error(node, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties); + if (!isSuperCallFirstStatment) { + error(superCallStatement, Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties); } else { // In such a required super call, it is a compile-time error for argument expressions to reference this. @@ -10576,8 +10593,8 @@ namespace ts { } } else if (!node.hasSeenSuperBeforeThis) { - // TODO: comment - error(node, Diagnostics.super_has_to_be_called_before_this_accessing); + // In ES6, super inside constructor of class-declaration has to precede "this" accessing + error(superCallStatement, Diagnostics.super_has_to_be_called_before_this_accessing); } } else if (baseConstructorType !== nullType) { From 07f8ddf06c85e9c3c3070b38f433da130a0db500 Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 15 Sep 2015 17:17:30 -0700 Subject: [PATCH 3/8] Add tests --- .../checkSuperCallBeforeThisAccessing1.ts | 10 ++++++++++ .../checkSuperCallBeforeThisAccessing2.ts | 10 ++++++++++ .../checkSuperCallBeforeThisAccessing3.ts | 15 +++++++++++++++ .../checkSuperCallBeforeThisAccessing4.ts | 19 +++++++++++++++++++ .../checkSuperCallBeforeThisAccessing5.ts | 7 +++++++ .../checkSuperCallBeforeThisAccessing6.ts | 10 ++++++++++ .../checkSuperCallBeforeThisAccessing7.ts | 9 +++++++++ 7 files changed, 80 insertions(+) create mode 100644 tests/cases/compiler/checkSuperCallBeforeThisAccessing1.ts create mode 100644 tests/cases/compiler/checkSuperCallBeforeThisAccessing2.ts create mode 100644 tests/cases/compiler/checkSuperCallBeforeThisAccessing3.ts create mode 100644 tests/cases/compiler/checkSuperCallBeforeThisAccessing4.ts create mode 100644 tests/cases/compiler/checkSuperCallBeforeThisAccessing5.ts create mode 100644 tests/cases/compiler/checkSuperCallBeforeThisAccessing6.ts create mode 100644 tests/cases/compiler/checkSuperCallBeforeThisAccessing7.ts diff --git a/tests/cases/compiler/checkSuperCallBeforeThisAccessing1.ts b/tests/cases/compiler/checkSuperCallBeforeThisAccessing1.ts new file mode 100644 index 0000000000000..e8dd78ef54651 --- /dev/null +++ b/tests/cases/compiler/checkSuperCallBeforeThisAccessing1.ts @@ -0,0 +1,10 @@ +class Based { } +class Derived extends Based { + public x: number; + constructor() { + super(); + this; + this.x = 10; + var that = this; + } +} \ No newline at end of file diff --git a/tests/cases/compiler/checkSuperCallBeforeThisAccessing2.ts b/tests/cases/compiler/checkSuperCallBeforeThisAccessing2.ts new file mode 100644 index 0000000000000..e6545f242889c --- /dev/null +++ b/tests/cases/compiler/checkSuperCallBeforeThisAccessing2.ts @@ -0,0 +1,10 @@ +class Based { } +class Derived extends Based { + public x: number; + constructor() { + this.x = 100; + super(); + this.x = 10; + var that = this; + } +} \ No newline at end of file diff --git a/tests/cases/compiler/checkSuperCallBeforeThisAccessing3.ts b/tests/cases/compiler/checkSuperCallBeforeThisAccessing3.ts new file mode 100644 index 0000000000000..fd9de120b057a --- /dev/null +++ b/tests/cases/compiler/checkSuperCallBeforeThisAccessing3.ts @@ -0,0 +1,15 @@ +class Based { } +class Derived extends Based { + public x: number; + constructor() { + class innver { + public y: boolean; + constructor() { + this.y = true; + } + } + super(); + this.x = 10; + var that = this; + } +} \ No newline at end of file diff --git a/tests/cases/compiler/checkSuperCallBeforeThisAccessing4.ts b/tests/cases/compiler/checkSuperCallBeforeThisAccessing4.ts new file mode 100644 index 0000000000000..1fdee00c2f861 --- /dev/null +++ b/tests/cases/compiler/checkSuperCallBeforeThisAccessing4.ts @@ -0,0 +1,19 @@ +class Based { } +class Derived extends Based { + public x: number; + constructor() { + (() => { + this; // No error + }); + () => { + this; // No error + }; + (() => { + this; // No error + })(); + super(); + super(); + this.x = 10; + var that = this; + } +} \ No newline at end of file diff --git a/tests/cases/compiler/checkSuperCallBeforeThisAccessing5.ts b/tests/cases/compiler/checkSuperCallBeforeThisAccessing5.ts new file mode 100644 index 0000000000000..65d1db6df7522 --- /dev/null +++ b/tests/cases/compiler/checkSuperCallBeforeThisAccessing5.ts @@ -0,0 +1,7 @@ +class Based { constructor(...arg) { } } +class Derived extends Based { + public x: number; + constructor() { + super(this.x); + } +} \ No newline at end of file diff --git a/tests/cases/compiler/checkSuperCallBeforeThisAccessing6.ts b/tests/cases/compiler/checkSuperCallBeforeThisAccessing6.ts new file mode 100644 index 0000000000000..2cdcb9ad3a810 --- /dev/null +++ b/tests/cases/compiler/checkSuperCallBeforeThisAccessing6.ts @@ -0,0 +1,10 @@ +class Base { + constructor(...arg) { + } +} +class Super extends Base { + constructor() { + (() => this); // No Error + super(); + } +} \ No newline at end of file diff --git a/tests/cases/compiler/checkSuperCallBeforeThisAccessing7.ts b/tests/cases/compiler/checkSuperCallBeforeThisAccessing7.ts new file mode 100644 index 0000000000000..1c4af98f0a294 --- /dev/null +++ b/tests/cases/compiler/checkSuperCallBeforeThisAccessing7.ts @@ -0,0 +1,9 @@ +class Base { + constructor(func: ()=>Base) { + } +} +class Super extends Base { + constructor() { + super((() => this)); // No error + } +} \ No newline at end of file From e0b6f7aafb26f1f585f75567443f2a88fa6f5c0c Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 15 Sep 2015 17:19:03 -0700 Subject: [PATCH 4/8] Update baselines --- .../checkSuperCallBeforeThisAccessing1.js | 33 +++++++++++ ...checkSuperCallBeforeThisAccessing1.symbols | 28 ++++++++++ .../checkSuperCallBeforeThisAccessing1.types | 31 ++++++++++ ...ckSuperCallBeforeThisAccessing2.errors.txt | 16 ++++++ .../checkSuperCallBeforeThisAccessing2.js | 33 +++++++++++ .../checkSuperCallBeforeThisAccessing3.js | 43 ++++++++++++++ ...checkSuperCallBeforeThisAccessing3.symbols | 38 +++++++++++++ .../checkSuperCallBeforeThisAccessing3.types | 43 ++++++++++++++ .../checkSuperCallBeforeThisAccessing4.js | 52 +++++++++++++++++ ...checkSuperCallBeforeThisAccessing4.symbols | 43 ++++++++++++++ .../checkSuperCallBeforeThisAccessing4.types | 56 +++++++++++++++++++ ...ckSuperCallBeforeThisAccessing5.errors.txt | 13 +++++ .../checkSuperCallBeforeThisAccessing5.js | 31 ++++++++++ .../checkSuperCallBeforeThisAccessing6.js | 36 ++++++++++++ ...checkSuperCallBeforeThisAccessing6.symbols | 20 +++++++ .../checkSuperCallBeforeThisAccessing6.types | 23 ++++++++ .../checkSuperCallBeforeThisAccessing7.js | 30 ++++++++++ ...checkSuperCallBeforeThisAccessing7.symbols | 19 +++++++ .../checkSuperCallBeforeThisAccessing7.types | 22 ++++++++ ...ckSuperCallBeforeThisAccessing8.errors.txt | 16 ++++++ .../checkSuperCallBeforeThisAccessing8.js | 35 ++++++++++++ .../reference/classUpdateTests.errors.txt | 18 ++---- ...derivedClassParameterProperties.errors.txt | 45 ++++++--------- .../derivedClassParameterProperties.js | 4 +- ...rivedClassSuperCallsWithThisArg.errors.txt | 5 +- .../strictModeInConstructor.errors.txt | 10 +--- .../thisInInvalidContexts.errors.txt | 7 ++- .../reference/thisInInvalidContexts.js | 4 +- ...InInvalidContextsExternalModule.errors.txt | 7 ++- .../thisInInvalidContextsExternalModule.js | 4 +- .../reference/thisInSuperCall.errors.txt | 7 ++- tests/baselines/reference/thisInSuperCall.js | 4 +- .../reference/thisInSuperCall2.errors.txt | 7 ++- tests/baselines/reference/thisInSuperCall2.js | 4 +- tests/cases/compiler/thisInSuperCall.ts | 2 +- tests/cases/compiler/thisInSuperCall2.ts | 2 +- .../derivedClassParameterProperties.ts | 2 +- .../thisKeyword/thisInInvalidContexts.ts | 2 +- .../thisInInvalidContextsExternalModule.ts | 2 +- 39 files changed, 726 insertions(+), 71 deletions(-) create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing1.js create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing1.symbols create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing1.types create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing2.errors.txt create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing2.js create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing3.js create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing3.symbols create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing3.types create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing4.js create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing4.symbols create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing4.types create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing5.errors.txt create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing5.js create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing6.js create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing6.symbols create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing6.types create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing7.js create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing7.symbols create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing7.types create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing8.errors.txt create mode 100644 tests/baselines/reference/checkSuperCallBeforeThisAccessing8.js diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.js new file mode 100644 index 0000000000000..ec6e6e39ee8aa --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.js @@ -0,0 +1,33 @@ +//// [checkSuperCallBeforeThisAccessing1.ts] +class Based { } +class Derived extends Based { + public x: number; + constructor() { + super(); + this; + this.x = 10; + var that = this; + } +} + +//// [checkSuperCallBeforeThisAccessing1.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 Based = (function () { + function Based() { + } + return Based; +})(); +var Derived = (function (_super) { + __extends(Derived, _super); + function Derived() { + _super.call(this); + this; + this.x = 10; + var that = this; + } + return Derived; +})(Based); diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.symbols b/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.symbols new file mode 100644 index 0000000000000..008d1156b564d --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.symbols @@ -0,0 +1,28 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing1.ts === +class Based { } +>Based : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing1.ts, 0, 0)) + +class Derived extends Based { +>Derived : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing1.ts, 0, 15)) +>Based : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing1.ts, 0, 0)) + + public x: number; +>x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing1.ts, 1, 29)) + + constructor() { + super(); +>super : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing1.ts, 0, 0)) + + this; +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing1.ts, 0, 15)) + + this.x = 10; +>this.x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing1.ts, 1, 29)) +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing1.ts, 0, 15)) +>x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing1.ts, 1, 29)) + + var that = this; +>that : Symbol(that, Decl(checkSuperCallBeforeThisAccessing1.ts, 7, 11)) +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing1.ts, 0, 15)) + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.types b/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.types new file mode 100644 index 0000000000000..f6114d2899794 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing1.types @@ -0,0 +1,31 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing1.ts === +class Based { } +>Based : Based + +class Derived extends Based { +>Derived : Derived +>Based : Based + + public x: number; +>x : number + + constructor() { + super(); +>super() : void +>super : typeof Based + + this; +>this : Derived + + this.x = 10; +>this.x = 10 : number +>this.x : number +>this : Derived +>x : number +>10 : number + + var that = this; +>that : Derived +>this : Derived + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.errors.txt b/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.errors.txt new file mode 100644 index 0000000000000..2c9473976a461 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.errors.txt @@ -0,0 +1,16 @@ +tests/cases/compiler/checkSuperCallBeforeThisAccessing2.ts(6,9): error TS17006: 'super' has to be called before 'this' accessing. + + +==== tests/cases/compiler/checkSuperCallBeforeThisAccessing2.ts (1 errors) ==== + class Based { } + class Derived extends Based { + public x: number; + constructor() { + this.x = 100; + super(); + ~~~~~~~~ +!!! error TS17006: 'super' has to be called before 'this' accessing. + this.x = 10; + var that = this; + } + } \ No newline at end of file diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.js new file mode 100644 index 0000000000000..eb58afd820b80 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.js @@ -0,0 +1,33 @@ +//// [checkSuperCallBeforeThisAccessing2.ts] +class Based { } +class Derived extends Based { + public x: number; + constructor() { + this.x = 100; + super(); + this.x = 10; + var that = this; + } +} + +//// [checkSuperCallBeforeThisAccessing2.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 Based = (function () { + function Based() { + } + return Based; +})(); +var Derived = (function (_super) { + __extends(Derived, _super); + function Derived() { + this.x = 100; + _super.call(this); + this.x = 10; + var that = this; + } + return Derived; +})(Based); diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.js new file mode 100644 index 0000000000000..2c5652cf95fa7 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.js @@ -0,0 +1,43 @@ +//// [checkSuperCallBeforeThisAccessing3.ts] +class Based { } +class Derived extends Based { + public x: number; + constructor() { + class innver { + public y: boolean; + constructor() { + this.y = true; + } + } + super(); + this.x = 10; + var that = this; + } +} + +//// [checkSuperCallBeforeThisAccessing3.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 Based = (function () { + function Based() { + } + return Based; +})(); +var Derived = (function (_super) { + __extends(Derived, _super); + function Derived() { + var innver = (function () { + function innver() { + this.y = true; + } + return innver; + })(); + _super.call(this); + this.x = 10; + var that = this; + } + return Derived; +})(Based); diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.symbols b/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.symbols new file mode 100644 index 0000000000000..8dd68fc3b7494 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.symbols @@ -0,0 +1,38 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing3.ts === +class Based { } +>Based : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing3.ts, 0, 0)) + +class Derived extends Based { +>Derived : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing3.ts, 0, 15)) +>Based : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing3.ts, 0, 0)) + + public x: number; +>x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing3.ts, 1, 29)) + + constructor() { + class innver { +>innver : Symbol(innver, Decl(checkSuperCallBeforeThisAccessing3.ts, 3, 19)) + + public y: boolean; +>y : Symbol(y, Decl(checkSuperCallBeforeThisAccessing3.ts, 4, 22)) + + constructor() { + this.y = true; +>this.y : Symbol(y, Decl(checkSuperCallBeforeThisAccessing3.ts, 4, 22)) +>this : Symbol(innver, Decl(checkSuperCallBeforeThisAccessing3.ts, 3, 19)) +>y : Symbol(y, Decl(checkSuperCallBeforeThisAccessing3.ts, 4, 22)) + } + } + super(); +>super : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing3.ts, 0, 0)) + + this.x = 10; +>this.x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing3.ts, 1, 29)) +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing3.ts, 0, 15)) +>x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing3.ts, 1, 29)) + + var that = this; +>that : Symbol(that, Decl(checkSuperCallBeforeThisAccessing3.ts, 12, 11)) +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing3.ts, 0, 15)) + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.types b/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.types new file mode 100644 index 0000000000000..45077a6fe5ef8 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing3.types @@ -0,0 +1,43 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing3.ts === +class Based { } +>Based : Based + +class Derived extends Based { +>Derived : Derived +>Based : Based + + public x: number; +>x : number + + constructor() { + class innver { +>innver : innver + + public y: boolean; +>y : boolean + + constructor() { + this.y = true; +>this.y = true : boolean +>this.y : boolean +>this : innver +>y : boolean +>true : boolean + } + } + super(); +>super() : void +>super : typeof Based + + this.x = 10; +>this.x = 10 : number +>this.x : number +>this : Derived +>x : number +>10 : number + + var that = this; +>that : Derived +>this : Derived + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.js new file mode 100644 index 0000000000000..eacec00c43878 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.js @@ -0,0 +1,52 @@ +//// [checkSuperCallBeforeThisAccessing4.ts] +class Based { } +class Derived extends Based { + public x: number; + constructor() { + (() => { + this; // No error + }); + () => { + this; // No error + }; + (() => { + this; // No error + })(); + super(); + super(); + this.x = 10; + var that = this; + } +} + +//// [checkSuperCallBeforeThisAccessing4.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 Based = (function () { + function Based() { + } + return Based; +})(); +var Derived = (function (_super) { + __extends(Derived, _super); + function Derived() { + var _this = this; + (function () { + _this; // No error + }); + (function () { + _this; // No error + }); + (function () { + _this; // No error + })(); + _super.call(this); + _super.call(this); + this.x = 10; + var that = this; + } + return Derived; +})(Based); diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.symbols b/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.symbols new file mode 100644 index 0000000000000..7101280358dbe --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.symbols @@ -0,0 +1,43 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing4.ts === +class Based { } +>Based : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 0)) + +class Derived extends Based { +>Derived : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 15)) +>Based : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 0)) + + public x: number; +>x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing4.ts, 1, 29)) + + constructor() { + (() => { + this; // No error +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 15)) + + }); + () => { + this; // No error +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 15)) + + }; + (() => { + this; // No error +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 15)) + + })(); + super(); +>super : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 0)) + + super(); +>super : Symbol(Based, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 0)) + + this.x = 10; +>this.x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing4.ts, 1, 29)) +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 15)) +>x : Symbol(x, Decl(checkSuperCallBeforeThisAccessing4.ts, 1, 29)) + + var that = this; +>that : Symbol(that, Decl(checkSuperCallBeforeThisAccessing4.ts, 16, 11)) +>this : Symbol(Derived, Decl(checkSuperCallBeforeThisAccessing4.ts, 0, 15)) + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.types b/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.types new file mode 100644 index 0000000000000..965d09a410918 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing4.types @@ -0,0 +1,56 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing4.ts === +class Based { } +>Based : Based + +class Derived extends Based { +>Derived : Derived +>Based : Based + + public x: number; +>x : number + + constructor() { + (() => { +>(() => { this; // No error }) : () => void +>() => { this; // No error } : () => void + + this; // No error +>this : Derived + + }); + () => { +>() => { this; // No error } : () => void + + this; // No error +>this : Derived + + }; + (() => { +>(() => { this; // No error })() : void +>(() => { this; // No error }) : () => void +>() => { this; // No error } : () => void + + this; // No error +>this : Derived + + })(); + super(); +>super() : void +>super : typeof Based + + super(); +>super() : void +>super : typeof Based + + this.x = 10; +>this.x = 10 : number +>this.x : number +>this : Derived +>x : number +>10 : number + + var that = this; +>that : Derived +>this : Derived + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing5.errors.txt b/tests/baselines/reference/checkSuperCallBeforeThisAccessing5.errors.txt new file mode 100644 index 0000000000000..fa5cfd524cb25 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing5.errors.txt @@ -0,0 +1,13 @@ +tests/cases/compiler/checkSuperCallBeforeThisAccessing5.ts(5,9): error TS17006: 'super' has to be called before 'this' accessing. + + +==== tests/cases/compiler/checkSuperCallBeforeThisAccessing5.ts (1 errors) ==== + class Based { constructor(...arg) { } } + class Derived extends Based { + public x: number; + constructor() { + super(this.x); + ~~~~~~~~~~~~~~ +!!! error TS17006: 'super' has to be called before 'this' accessing. + } + } \ No newline at end of file diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing5.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing5.js new file mode 100644 index 0000000000000..489c7200050e8 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing5.js @@ -0,0 +1,31 @@ +//// [checkSuperCallBeforeThisAccessing5.ts] +class Based { constructor(...arg) { } } +class Derived extends Based { + public x: number; + constructor() { + super(this.x); + } +} + +//// [checkSuperCallBeforeThisAccessing5.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 Based = (function () { + function Based() { + var arg = []; + for (var _i = 0; _i < arguments.length; _i++) { + arg[_i - 0] = arguments[_i]; + } + } + return Based; +})(); +var Derived = (function (_super) { + __extends(Derived, _super); + function Derived() { + _super.call(this, this.x); + } + return Derived; +})(Based); diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.js new file mode 100644 index 0000000000000..6ad9773bc5f39 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.js @@ -0,0 +1,36 @@ +//// [checkSuperCallBeforeThisAccessing6.ts] +class Base { + constructor(...arg) { + } +} +class Super extends Base { + constructor() { + (() => this); // No Error + super(); + } +} + +//// [checkSuperCallBeforeThisAccessing6.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 Base = (function () { + function Base() { + var arg = []; + for (var _i = 0; _i < arguments.length; _i++) { + arg[_i - 0] = arguments[_i]; + } + } + return Base; +})(); +var Super = (function (_super) { + __extends(Super, _super); + function Super() { + var _this = this; + (function () { return _this; }); // No Error + _super.call(this); + } + return Super; +})(Base); diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.symbols b/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.symbols new file mode 100644 index 0000000000000..84bac5775ec25 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.symbols @@ -0,0 +1,20 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing6.ts === +class Base { +>Base : Symbol(Base, Decl(checkSuperCallBeforeThisAccessing6.ts, 0, 0)) + + constructor(...arg) { +>arg : Symbol(arg, Decl(checkSuperCallBeforeThisAccessing6.ts, 1, 16)) + } +} +class Super extends Base { +>Super : Symbol(Super, Decl(checkSuperCallBeforeThisAccessing6.ts, 3, 1)) +>Base : Symbol(Base, Decl(checkSuperCallBeforeThisAccessing6.ts, 0, 0)) + + constructor() { + (() => this); // No Error +>this : Symbol(Super, Decl(checkSuperCallBeforeThisAccessing6.ts, 3, 1)) + + super(); +>super : Symbol(Base, Decl(checkSuperCallBeforeThisAccessing6.ts, 0, 0)) + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.types b/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.types new file mode 100644 index 0000000000000..8e2d285d85b98 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing6.types @@ -0,0 +1,23 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing6.ts === +class Base { +>Base : Base + + constructor(...arg) { +>arg : any[] + } +} +class Super extends Base { +>Super : Super +>Base : Base + + constructor() { + (() => this); // No Error +>(() => this) : () => Super +>() => this : () => Super +>this : Super + + super(); +>super() : void +>super : typeof Base + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.js new file mode 100644 index 0000000000000..eafc662085ff9 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.js @@ -0,0 +1,30 @@ +//// [checkSuperCallBeforeThisAccessing7.ts] +class Base { + constructor(func: ()=>Base) { + } +} +class Super extends Base { + constructor() { + super((() => this)); // No error + } +} + +//// [checkSuperCallBeforeThisAccessing7.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 Base = (function () { + function Base(func) { + } + return Base; +})(); +var Super = (function (_super) { + __extends(Super, _super); + function Super() { + var _this = this; + _super.call(this, (function () { return _this; })); // No error + } + return Super; +})(Base); diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.symbols b/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.symbols new file mode 100644 index 0000000000000..3611429d2b272 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.symbols @@ -0,0 +1,19 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing7.ts === +class Base { +>Base : Symbol(Base, Decl(checkSuperCallBeforeThisAccessing7.ts, 0, 0)) + + constructor(func: ()=>Base) { +>func : Symbol(func, Decl(checkSuperCallBeforeThisAccessing7.ts, 1, 16)) +>Base : Symbol(Base, Decl(checkSuperCallBeforeThisAccessing7.ts, 0, 0)) + } +} +class Super extends Base { +>Super : Symbol(Super, Decl(checkSuperCallBeforeThisAccessing7.ts, 3, 1)) +>Base : Symbol(Base, Decl(checkSuperCallBeforeThisAccessing7.ts, 0, 0)) + + constructor() { + super((() => this)); // No error +>super : Symbol(Base, Decl(checkSuperCallBeforeThisAccessing7.ts, 0, 0)) +>this : Symbol(Super, Decl(checkSuperCallBeforeThisAccessing7.ts, 3, 1)) + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.types b/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.types new file mode 100644 index 0000000000000..fa5e7dbe857c4 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing7.types @@ -0,0 +1,22 @@ +=== tests/cases/compiler/checkSuperCallBeforeThisAccessing7.ts === +class Base { +>Base : Base + + constructor(func: ()=>Base) { +>func : () => Base +>Base : Base + } +} +class Super extends Base { +>Super : Super +>Base : Base + + constructor() { + super((() => this)); // No error +>super((() => this)) : void +>super : typeof Base +>(() => this) : () => Super +>() => this : () => Super +>this : Super + } +} diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.errors.txt b/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.errors.txt new file mode 100644 index 0000000000000..f7c763ee708ae --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.errors.txt @@ -0,0 +1,16 @@ +tests/cases/compiler/checkSuperCallBeforeThisAccessing8.ts(8,9): error TS17006: 'super' has to be called before 'this' accessing. + + +==== tests/cases/compiler/checkSuperCallBeforeThisAccessing8.ts (1 errors) ==== + class Base { + constructor(...arg) { + } + } + class Super extends Base { + constructor() { + var that = this; + super(); + ~~~~~~~~ +!!! error TS17006: 'super' has to be called before 'this' accessing. + } + } \ No newline at end of file diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.js b/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.js new file mode 100644 index 0000000000000..4675d328b38a2 --- /dev/null +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.js @@ -0,0 +1,35 @@ +//// [checkSuperCallBeforeThisAccessing8.ts] +class Base { + constructor(...arg) { + } +} +class Super extends Base { + constructor() { + var that = this; + super(); + } +} + +//// [checkSuperCallBeforeThisAccessing8.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 Base = (function () { + function Base() { + var arg = []; + for (var _i = 0; _i < arguments.length; _i++) { + arg[_i - 0] = arguments[_i]; + } + } + return Base; +})(); +var Super = (function (_super) { + __extends(Super, _super); + function Super() { + var that = this; + _super.call(this); + } + return Super; +})(Base); diff --git a/tests/baselines/reference/classUpdateTests.errors.txt b/tests/baselines/reference/classUpdateTests.errors.txt index 59e9bc9ced182..559657564101a 100644 --- a/tests/baselines/reference/classUpdateTests.errors.txt +++ b/tests/baselines/reference/classUpdateTests.errors.txt @@ -1,11 +1,11 @@ tests/cases/compiler/classUpdateTests.ts(34,2): error TS2377: Constructors for derived classes must contain a 'super' call. tests/cases/compiler/classUpdateTests.ts(43,18): error TS2335: 'super' can only be referenced in a derived class. -tests/cases/compiler/classUpdateTests.ts(57,2): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. +tests/cases/compiler/classUpdateTests.ts(59,3): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. tests/cases/compiler/classUpdateTests.ts(63,7): error TS2415: Class 'L' incorrectly extends base class 'G'. Property 'p1' is private in type 'L' but not in type 'G'. tests/cases/compiler/classUpdateTests.ts(69,7): error TS2415: Class 'M' incorrectly extends base class 'G'. Property 'p1' is private in type 'M' but not in type 'G'. -tests/cases/compiler/classUpdateTests.ts(70,2): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. +tests/cases/compiler/classUpdateTests.ts(72,3): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. tests/cases/compiler/classUpdateTests.ts(93,3): error TS1128: Declaration or statement expected. tests/cases/compiler/classUpdateTests.ts(95,1): error TS1128: Declaration or statement expected. tests/cases/compiler/classUpdateTests.ts(99,3): error TS1128: Declaration or statement expected. @@ -80,14 +80,11 @@ tests/cases/compiler/classUpdateTests.ts(113,1): error TS1128: Declaration or st class K extends G { constructor(public p1:number) { // ERROR - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var i = 0; - ~~~~~~~~~~~~ super(); - ~~~~~~~~~~ - } - ~~ + ~~~~~~~~ !!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. + } } class L extends G { @@ -104,14 +101,11 @@ tests/cases/compiler/classUpdateTests.ts(113,1): error TS1128: Declaration or st !!! error TS2415: Class 'M' incorrectly extends base class 'G'. !!! error TS2415: Property 'p1' is private in type 'M' but not in type 'G'. constructor(private p1:number) { // ERROR - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var i = 0; - ~~~~~~~~~~~~ super(); - ~~~~~~~~~~ - } - ~~ + ~~~~~~~~ !!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. + } } // diff --git a/tests/baselines/reference/derivedClassParameterProperties.errors.txt b/tests/baselines/reference/derivedClassParameterProperties.errors.txt index 0dba0d24cb0ff..7b57951d4935d 100644 --- a/tests/baselines/reference/derivedClassParameterProperties.errors.txt +++ b/tests/baselines/reference/derivedClassParameterProperties.errors.txt @@ -1,10 +1,11 @@ -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(15,5): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(30,5): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(56,5): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(79,5): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(17,9): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(32,9): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(49,9): error TS17006: 'super' has to be called before 'this' accessing. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(59,9): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(82,9): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. -==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts (4 errors) ==== +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts (5 errors) ==== // ordering of super calls in derived constructors matters depending on other class contents class Base { @@ -20,14 +21,11 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP class Derived2 extends Base { constructor(public y: string) { - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var a = 1; - ~~~~~~~~~~~~~~~~~~ super(); // error - ~~~~~~~~~~~~~~~~~~~~~~~~~ - } - ~~~~~ + ~~~~~~~~ !!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. + } } class Derived3 extends Base { @@ -40,14 +38,11 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP class Derived4 extends Base { a = 1; constructor(y: string) { - ~~~~~~~~~~~~~~~~~~~~~~~~ var b = 2; - ~~~~~~~~~~~~~~~~~~ super(); // error - ~~~~~~~~~~~~~~~~~~~~~~~~~ - } - ~~~~~ + ~~~~~~~~ !!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. + } } class Derived5 extends Base { @@ -63,7 +58,9 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP constructor(y: string) { this.a = 1; var b = 2; - super(); // ok + super(); // error: "super" has to be called before "this" accessing + ~~~~~~~~ +!!! error TS17006: 'super' has to be called before 'this' accessing. } } @@ -71,16 +68,12 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP a = 1; b: number; constructor(y: string) { - ~~~~~~~~~~~~~~~~~~~~~~~~ this.a = 3; - ~~~~~~~~~~~~~~~~~~~ this.b = 3; - ~~~~~~~~~~~~~~~~~~~ super(); // error - ~~~~~~~~~~~~~~~~~~~~~~~~~ - } - ~~~~~ + ~~~~~~~~ !!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. + } } class Derived8 extends Base { @@ -100,16 +93,12 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP a = 1; b: number; constructor(y: string) { - ~~~~~~~~~~~~~~~~~~~~~~~~ this.a = 3; - ~~~~~~~~~~~~~~~~~~~ this.b = 3; - ~~~~~~~~~~~~~~~~~~~ super(); // error - ~~~~~~~~~~~~~~~~~~~~~~~~~ - } - ~~~~~ + ~~~~~~~~ !!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. + } } class Derived10 extends Base2 { diff --git a/tests/baselines/reference/derivedClassParameterProperties.js b/tests/baselines/reference/derivedClassParameterProperties.js index 55c0a312b7163..0d4a476af12a7 100644 --- a/tests/baselines/reference/derivedClassParameterProperties.js +++ b/tests/baselines/reference/derivedClassParameterProperties.js @@ -47,7 +47,7 @@ class Derived6 extends Base { constructor(y: string) { this.a = 1; var b = 2; - super(); // ok + super(); // error: "super" has to be called before "this" accessing } } @@ -155,7 +155,7 @@ var Derived6 = (function (_super) { function Derived6(y) { this.a = 1; var b = 2; - _super.call(this); // ok + _super.call(this); // error: "super" has to be called before "this" accessing } return Derived6; })(Base); diff --git a/tests/baselines/reference/derivedClassSuperCallsWithThisArg.errors.txt b/tests/baselines/reference/derivedClassSuperCallsWithThisArg.errors.txt index ef43d1d6f528d..d8b724e3d0f1a 100644 --- a/tests/baselines/reference/derivedClassSuperCallsWithThisArg.errors.txt +++ b/tests/baselines/reference/derivedClassSuperCallsWithThisArg.errors.txt @@ -1,8 +1,9 @@ +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperCallsWithThisArg.ts(8,9): error TS17006: 'super' has to be called before 'this' accessing. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperCallsWithThisArg.ts(14,15): error TS2332: 'this' cannot be referenced in current location. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperCallsWithThisArg.ts(20,21): error TS2332: 'this' cannot be referenced in current location. -==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperCallsWithThisArg.ts (2 errors) ==== +==== tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperCallsWithThisArg.ts (3 errors) ==== class Base { x: string; constructor(a) { } @@ -11,6 +12,8 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS class Derived extends Base { constructor() { super(this); // ok + ~~~~~~~~~~~~ +!!! error TS17006: 'super' has to be called before 'this' accessing. } } diff --git a/tests/baselines/reference/strictModeInConstructor.errors.txt b/tests/baselines/reference/strictModeInConstructor.errors.txt index 9a80732842afe..3c38530cc12d7 100644 --- a/tests/baselines/reference/strictModeInConstructor.errors.txt +++ b/tests/baselines/reference/strictModeInConstructor.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/strictModeInConstructor.ts(27,5): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. +tests/cases/compiler/strictModeInConstructor.ts(29,9): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. ==== tests/cases/compiler/strictModeInConstructor.ts (1 errors) ==== @@ -29,16 +29,12 @@ tests/cases/compiler/strictModeInConstructor.ts(27,5): error TS2376: A 'super' c public s: number = 9; constructor () { - ~~~~~~~~~~~~~~~~ var x = 1; // Error - ~~~~~~~~~~~~~~~~~~~~~~~~~~~ super(); - ~~~~~~~~~~~~~~~~ + ~~~~~~~~ +!!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. "use strict"; - ~~~~~~~~~~~~~~~~~~~~~ } - ~~~~~ -!!! error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. } class Bs extends A { diff --git a/tests/baselines/reference/thisInInvalidContexts.errors.txt b/tests/baselines/reference/thisInInvalidContexts.errors.txt index d6f24cbcfdce3..fb2c706bd6e0d 100644 --- a/tests/baselines/reference/thisInInvalidContexts.errors.txt +++ b/tests/baselines/reference/thisInInvalidContexts.errors.txt @@ -1,4 +1,5 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(3,16): error TS2334: 'this' cannot be referenced in a static property initializer. +tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(14,9): error TS17006: 'super' has to be called before 'this' accessing. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(22,15): error TS2332: 'this' cannot be referenced in current location. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(28,13): error TS2331: 'this' cannot be referenced in a module or namespace body. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(38,25): error TS2507: Type 'any' is not a constructor function type. @@ -6,7 +7,7 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(44,9): tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(45,9): error TS2332: 'this' cannot be referenced in current location. -==== tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts (6 errors) ==== +==== tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts (7 errors) ==== //'this' in static member initializer class ErrClass1 { static t = this; // Error @@ -22,7 +23,9 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(45,9): t; //'this' in optional super call constructor() { - super(this); // OK + super(this); // Error + ~~~~~~~~~~~~ +!!! error TS17006: 'super' has to be called before 'this' accessing. } } diff --git a/tests/baselines/reference/thisInInvalidContexts.js b/tests/baselines/reference/thisInInvalidContexts.js index 84b964046832d..fbdf37b86db0e 100644 --- a/tests/baselines/reference/thisInInvalidContexts.js +++ b/tests/baselines/reference/thisInInvalidContexts.js @@ -12,7 +12,7 @@ class ClassWithNoInitializer extends BaseErrClass { t; //'this' in optional super call constructor() { - super(this); // OK + super(this); // Error } } @@ -70,7 +70,7 @@ var ClassWithNoInitializer = (function (_super) { __extends(ClassWithNoInitializer, _super); //'this' in optional super call function ClassWithNoInitializer() { - _super.call(this, this); // OK + _super.call(this, this); // Error } return ClassWithNoInitializer; })(BaseErrClass); diff --git a/tests/baselines/reference/thisInInvalidContextsExternalModule.errors.txt b/tests/baselines/reference/thisInInvalidContextsExternalModule.errors.txt index df47901d771c3..9b1617dbc397d 100644 --- a/tests/baselines/reference/thisInInvalidContextsExternalModule.errors.txt +++ b/tests/baselines/reference/thisInInvalidContextsExternalModule.errors.txt @@ -1,4 +1,5 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(3,16): error TS2334: 'this' cannot be referenced in a static property initializer. +tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(14,9): error TS17006: 'super' has to be called before 'this' accessing. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(22,15): error TS2332: 'this' cannot be referenced in current location. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(28,13): error TS2331: 'this' cannot be referenced in a module or namespace body. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(38,25): error TS2507: Type 'any' is not a constructor function type. @@ -7,7 +8,7 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalMod tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(48,1): error TS1148: Cannot compile modules unless the '--module' flag is provided. -==== tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts (7 errors) ==== +==== tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts (8 errors) ==== //'this' in static member initializer class ErrClass1 { static t = this; // Error @@ -23,7 +24,9 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalMod t; //'this' in optional super call constructor() { - super(this); // OK + super(this); // error: "super" has to be called before "this" accessing + ~~~~~~~~~~~~ +!!! error TS17006: 'super' has to be called before 'this' accessing. } } diff --git a/tests/baselines/reference/thisInInvalidContextsExternalModule.js b/tests/baselines/reference/thisInInvalidContextsExternalModule.js index 7de94be9420c1..2323c0db95c3c 100644 --- a/tests/baselines/reference/thisInInvalidContextsExternalModule.js +++ b/tests/baselines/reference/thisInInvalidContextsExternalModule.js @@ -12,7 +12,7 @@ class ClassWithNoInitializer extends BaseErrClass { t; //'this' in optional super call constructor() { - super(this); // OK + super(this); // error: "super" has to be called before "this" accessing } } @@ -70,7 +70,7 @@ var ClassWithNoInitializer = (function (_super) { __extends(ClassWithNoInitializer, _super); //'this' in optional super call function ClassWithNoInitializer() { - _super.call(this, this); // OK + _super.call(this, this); // error: "super" has to be called before "this" accessing } return ClassWithNoInitializer; })(BaseErrClass); diff --git a/tests/baselines/reference/thisInSuperCall.errors.txt b/tests/baselines/reference/thisInSuperCall.errors.txt index 57255aa958235..d0be13d8adc8e 100644 --- a/tests/baselines/reference/thisInSuperCall.errors.txt +++ b/tests/baselines/reference/thisInSuperCall.errors.txt @@ -1,15 +1,18 @@ +tests/cases/compiler/thisInSuperCall.ts(7,9): error TS17006: 'super' has to be called before 'this' accessing. tests/cases/compiler/thisInSuperCall.ts(14,15): error TS2332: 'this' cannot be referenced in current location. tests/cases/compiler/thisInSuperCall.ts(20,15): error TS2332: 'this' cannot be referenced in current location. -==== tests/cases/compiler/thisInSuperCall.ts (2 errors) ==== +==== tests/cases/compiler/thisInSuperCall.ts (3 errors) ==== class Base { constructor(x: any) {} } class Foo extends Base { constructor() { - super(this); // no error + super(this); // error: "super" has to be called before "this" accessing + ~~~~~~~~~~~~ +!!! error TS17006: 'super' has to be called before 'this' accessing. } } diff --git a/tests/baselines/reference/thisInSuperCall.js b/tests/baselines/reference/thisInSuperCall.js index f7d7ca4e4fd65..0bcaf35fcbf5d 100644 --- a/tests/baselines/reference/thisInSuperCall.js +++ b/tests/baselines/reference/thisInSuperCall.js @@ -5,7 +5,7 @@ class Base { class Foo extends Base { constructor() { - super(this); // no error + super(this); // error: "super" has to be called before "this" accessing } } @@ -36,7 +36,7 @@ var Base = (function () { var Foo = (function (_super) { __extends(Foo, _super); function Foo() { - _super.call(this, this); // no error + _super.call(this, this); // error: "super" has to be called before "this" accessing } return Foo; })(Base); diff --git a/tests/baselines/reference/thisInSuperCall2.errors.txt b/tests/baselines/reference/thisInSuperCall2.errors.txt index d3a94f79487ce..0559f83eb906d 100644 --- a/tests/baselines/reference/thisInSuperCall2.errors.txt +++ b/tests/baselines/reference/thisInSuperCall2.errors.txt @@ -1,7 +1,8 @@ +tests/cases/compiler/thisInSuperCall2.ts(8,9): error TS17006: 'super' has to be called before 'this' accessing. tests/cases/compiler/thisInSuperCall2.ts(16,15): error TS2332: 'this' cannot be referenced in current location. -==== tests/cases/compiler/thisInSuperCall2.ts (1 errors) ==== +==== tests/cases/compiler/thisInSuperCall2.ts (2 errors) ==== class Base { constructor(a: any) {} } @@ -9,7 +10,9 @@ tests/cases/compiler/thisInSuperCall2.ts(16,15): error TS2332: 'this' cannot be class Foo extends Base { public x: number; constructor() { - super(this); // no error + super(this); // error: "super" has to be called before "this" accessing + ~~~~~~~~~~~~ +!!! error TS17006: 'super' has to be called before 'this' accessing. } } diff --git a/tests/baselines/reference/thisInSuperCall2.js b/tests/baselines/reference/thisInSuperCall2.js index a9bbeda9fe798..8957a08921022 100644 --- a/tests/baselines/reference/thisInSuperCall2.js +++ b/tests/baselines/reference/thisInSuperCall2.js @@ -6,7 +6,7 @@ class Base { class Foo extends Base { public x: number; constructor() { - super(this); // no error + super(this); // error: "super" has to be called before "this" accessing } } @@ -33,7 +33,7 @@ var Base = (function () { var Foo = (function (_super) { __extends(Foo, _super); function Foo() { - _super.call(this, this); // no error + _super.call(this, this); // error: "super" has to be called before "this" accessing } return Foo; })(Base); diff --git a/tests/cases/compiler/thisInSuperCall.ts b/tests/cases/compiler/thisInSuperCall.ts index 6a54e9ac6f12e..b7df7cbafad74 100644 --- a/tests/cases/compiler/thisInSuperCall.ts +++ b/tests/cases/compiler/thisInSuperCall.ts @@ -4,7 +4,7 @@ class Base { class Foo extends Base { constructor() { - super(this); // no error + super(this); // error: "super" has to be called before "this" accessing } } diff --git a/tests/cases/compiler/thisInSuperCall2.ts b/tests/cases/compiler/thisInSuperCall2.ts index 5de7c34cf3252..2869ab9a80553 100644 --- a/tests/cases/compiler/thisInSuperCall2.ts +++ b/tests/cases/compiler/thisInSuperCall2.ts @@ -5,7 +5,7 @@ class Base { class Foo extends Base { public x: number; constructor() { - super(this); // no error + super(this); // error: "super" has to be called before "this" accessing } } diff --git a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts index edfd7ab4f8c17..a054ed0f6831b 100644 --- a/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts +++ b/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts @@ -46,7 +46,7 @@ class Derived6 extends Base { constructor(y: string) { this.a = 1; var b = 2; - super(); // ok + super(); // error: "super" has to be called before "this" accessing } } diff --git a/tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts b/tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts index 0afbc45004087..2b929318c13fe 100644 --- a/tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts +++ b/tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts @@ -11,7 +11,7 @@ class ClassWithNoInitializer extends BaseErrClass { t; //'this' in optional super call constructor() { - super(this); // OK + super(this); // Error } } diff --git a/tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts b/tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts index 513e56977df5e..e3d8ce9f5fcd2 100644 --- a/tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts +++ b/tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts @@ -11,7 +11,7 @@ class ClassWithNoInitializer extends BaseErrClass { t; //'this' in optional super call constructor() { - super(this); // OK + super(this); // error: "super" has to be called before "this" accessing } } From 6e4574faf0c8d57dc37f0e6759986b56d6d55bed Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 15 Sep 2015 17:19:19 -0700 Subject: [PATCH 5/8] Add tests --- .../compiler/checkSuperCallBeforeThisAccessing8.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 tests/cases/compiler/checkSuperCallBeforeThisAccessing8.ts diff --git a/tests/cases/compiler/checkSuperCallBeforeThisAccessing8.ts b/tests/cases/compiler/checkSuperCallBeforeThisAccessing8.ts new file mode 100644 index 0000000000000..230b40fc24df6 --- /dev/null +++ b/tests/cases/compiler/checkSuperCallBeforeThisAccessing8.ts @@ -0,0 +1,10 @@ +class Base { + constructor(...arg) { + } +} +class Super extends Base { + constructor() { + var that = this; + super(); + } +} \ No newline at end of file From 3be5161152d678789bb65bdc3fd3b8789d097c52 Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 6 Oct 2015 19:46:46 -0700 Subject: [PATCH 6/8] Address PR feedback --- src/compiler/checker.ts | 17 +++++---- .../diagnosticInformationMap.generated.ts | 2 +- src/compiler/diagnosticMessages.json | 2 +- src/compiler/types.ts | 37 ++++++++++--------- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 21862c4a24b3b..7f91cd5ef6cb1 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -6415,9 +6415,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'. @@ -9051,8 +9050,12 @@ namespace ts { let 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; + + if (containgFunction && containgFunction.kind === SyntaxKind.Constructor) { + let nodeLinks = getNodeLinks(containgFunction); + if (!(nodeLinks.flags & NodeCheckFlags.HasSeenThisCall)) { + nodeLinks.flags |= NodeCheckFlags.HasSeenSuperBeforeThis; + } } return voidType; } @@ -10592,9 +10595,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/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index c6d175f91ff7c..902b027e1502a 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -613,6 +613,6 @@ namespace ts { JSX_attribute_expected: { code: 17003, category: DiagnosticCategory.Error, key: "JSX attribute expected." }, Cannot_use_JSX_unless_the_jsx_flag_is_provided: { code: 17004, category: DiagnosticCategory.Error, key: "Cannot use JSX unless the '--jsx' flag is provided." }, A_constructor_cannot_contain_a_super_call_when_its_class_extends_null: { code: 17005, category: DiagnosticCategory.Error, key: "A constructor cannot contain a 'super' call when its class extends 'null'" }, - super_has_to_be_called_before_this_accessing: { code: 17006, category: DiagnosticCategory.Error, key: "'super' has to be called before 'this' accessing." }, + super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class: { code: 17006, category: DiagnosticCategory.Error, key: "'super' must be called before accessing 'this' in the constructor of a derived class." }, }; } \ No newline at end of file diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 20bfbf698d52b..94d63f9de62c9 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2447,7 +2447,7 @@ "category": "Error", "code": 17005 }, - "'super' has to be called before 'this' accessing.": { + "'super' must be called before accessing 'this' in the constructor of a derived class.": { "category": "Error", "code": 17006 } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index f4d3a6fa65bdb..79c15054d3a30 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -619,7 +619,6 @@ namespace ts { export interface ConstructorDeclaration extends FunctionLikeDeclaration, ClassElement { body?: Block; - hasSeenSuperBeforeThis: boolean; // TODDO (yuisu): comment } // For when we encounter a semicolon in a class declaration. ES6 allows these as class elements. @@ -1245,7 +1244,7 @@ namespace ts { moduleName: string; referencedFiles: FileReference[]; languageVariant: LanguageVariant; - + // this map is used by transpiler to supply alternative names for dependencies (i.e. in case of bundling) /* @internal */ renamedDependencies?: Map; @@ -1313,12 +1312,12 @@ namespace ts { } export interface Program extends ScriptReferenceHost { - + /** * Get a list of root file names that were passed to a 'createProgram' */ getRootFileNames(): string[] - + /** * Get a list of files in the program */ @@ -1599,7 +1598,7 @@ namespace ts { getConstantValue(node: EnumMember | PropertyAccessExpression | ElementAccessExpression): number; getBlockScopedVariableId(node: Identifier): number; getReferencedValueDeclaration(reference: Identifier): Declaration; - getTypeReferenceSerializationKind(typeName: EntityName): TypeReferenceSerializationKind; + getTypeReferenceSerializationKind(typeName: EntityName): TypeReferenceSerializationKind; isOptionalParameter(node: ParameterDeclaration): boolean; } @@ -1745,6 +1744,8 @@ namespace ts { EnumValuesComputed = 0x00002000, BlockScopedBindingInLoop = 0x00004000, LexicalModuleMergesWithClass= 0x00008000, // Instantiated lexical module declaration is merged with a previous class declaration. + HasSeenSuperBeforeThis = 0x00010000, // Set during the binding if the 'super' is used before 'this' in constructor function + HasSeenThisCall = 0x00020000, // Set during the binding when encounter 'this' } /* @internal */ @@ -2012,12 +2013,12 @@ namespace ts { Error, Message, } - + export const enum ModuleResolutionKind { Classic = 1, NodeJs = 2 } - + export interface CompilerOptions { allowNonTsExtensions?: boolean; charset?: string; @@ -2260,20 +2261,20 @@ namespace ts { byteOrderMark = 0xFEFF, tab = 0x09, // \t verticalTab = 0x0B, // \v - } - + } + export interface ModuleResolutionHost { fileExists(fileName: string): boolean; // readFile function is used to read arbitrary text files on disk, i.e. when resolution procedure needs the content of 'package.json' - // to determine location of bundled typings for node module + // to determine location of bundled typings for node module readFile(fileName: string): string; } - + export interface ResolvedModule { resolvedFileName: string; failedLookupLocations: string[]; } - + export type ModuleNameResolver = (moduleName: string, containingFile: string, options: CompilerOptions, host: ModuleResolutionHost) => ResolvedModule; export interface CompilerHost extends ModuleResolutionHost { @@ -2285,13 +2286,13 @@ namespace ts { getCanonicalFileName(fileName: string): string; useCaseSensitiveFileNames(): boolean; getNewLine(): string; - + /* - * CompilerHost must either implement resolveModuleNames (in case if it wants to be completely in charge of - * module name resolution) or provide implementation for methods from ModuleResolutionHost (in this case compiler + * CompilerHost must either implement resolveModuleNames (in case if it wants to be completely in charge of + * module name resolution) or provide implementation for methods from ModuleResolutionHost (in this case compiler * will appply built-in module resolution logic and use members of ModuleResolutionHost to ask host specific questions). - * If resolveModuleNames is implemented then implementation for members from ModuleResolutionHost can be just - * 'throw new Error("NotImplemented")' + * If resolveModuleNames is implemented then implementation for members from ModuleResolutionHost can be just + * 'throw new Error("NotImplemented")' */ resolveModuleNames?(moduleNames: string[], containingFile: string): string[]; } @@ -2324,7 +2325,7 @@ namespace ts { // operation caused diagnostics to be returned by storing and comparing the return value // of this method before/after the operation is performed. getModificationCount(): number; - + /* @internal */ reattachFileDiagnostics(newFile: SourceFile): void; } } From 70a6fe0eca90087c987eaa67e78afa0718598efb Mon Sep 17 00:00:00 2001 From: Yui T Date: Tue, 6 Oct 2015 19:47:17 -0700 Subject: [PATCH 7/8] Update baseline for new message --- .../reference/checkSuperCallBeforeThisAccessing2.errors.txt | 4 ++-- .../reference/checkSuperCallBeforeThisAccessing5.errors.txt | 4 ++-- .../reference/checkSuperCallBeforeThisAccessing8.errors.txt | 4 ++-- .../reference/derivedClassParameterProperties.errors.txt | 4 ++-- .../reference/derivedClassSuperCallsWithThisArg.errors.txt | 4 ++-- tests/baselines/reference/thisInInvalidContexts.errors.txt | 4 ++-- .../reference/thisInInvalidContextsExternalModule.errors.txt | 4 ++-- tests/baselines/reference/thisInSuperCall.errors.txt | 4 ++-- tests/baselines/reference/thisInSuperCall2.errors.txt | 4 ++-- 9 files changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.errors.txt b/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.errors.txt index 2c9473976a461..6fdb2e3e3943f 100644 --- a/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.errors.txt +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing2.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/checkSuperCallBeforeThisAccessing2.ts(6,9): error TS17006: 'super' has to be called before 'this' accessing. +tests/cases/compiler/checkSuperCallBeforeThisAccessing2.ts(6,9): error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. ==== tests/cases/compiler/checkSuperCallBeforeThisAccessing2.ts (1 errors) ==== @@ -9,7 +9,7 @@ tests/cases/compiler/checkSuperCallBeforeThisAccessing2.ts(6,9): error TS17006: this.x = 100; super(); ~~~~~~~~ -!!! error TS17006: 'super' has to be called before 'this' accessing. +!!! error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. this.x = 10; var that = this; } diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing5.errors.txt b/tests/baselines/reference/checkSuperCallBeforeThisAccessing5.errors.txt index fa5cfd524cb25..edd7745a03cb4 100644 --- a/tests/baselines/reference/checkSuperCallBeforeThisAccessing5.errors.txt +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing5.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/checkSuperCallBeforeThisAccessing5.ts(5,9): error TS17006: 'super' has to be called before 'this' accessing. +tests/cases/compiler/checkSuperCallBeforeThisAccessing5.ts(5,9): error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. ==== tests/cases/compiler/checkSuperCallBeforeThisAccessing5.ts (1 errors) ==== @@ -8,6 +8,6 @@ tests/cases/compiler/checkSuperCallBeforeThisAccessing5.ts(5,9): error TS17006: constructor() { super(this.x); ~~~~~~~~~~~~~~ -!!! error TS17006: 'super' has to be called before 'this' accessing. +!!! error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. } } \ No newline at end of file diff --git a/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.errors.txt b/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.errors.txt index f7c763ee708ae..39db87d41a1d8 100644 --- a/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.errors.txt +++ b/tests/baselines/reference/checkSuperCallBeforeThisAccessing8.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/checkSuperCallBeforeThisAccessing8.ts(8,9): error TS17006: 'super' has to be called before 'this' accessing. +tests/cases/compiler/checkSuperCallBeforeThisAccessing8.ts(8,9): error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. ==== tests/cases/compiler/checkSuperCallBeforeThisAccessing8.ts (1 errors) ==== @@ -11,6 +11,6 @@ tests/cases/compiler/checkSuperCallBeforeThisAccessing8.ts(8,9): error TS17006: var that = this; super(); ~~~~~~~~ -!!! error TS17006: 'super' has to be called before 'this' accessing. +!!! error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. } } \ No newline at end of file diff --git a/tests/baselines/reference/derivedClassParameterProperties.errors.txt b/tests/baselines/reference/derivedClassParameterProperties.errors.txt index 7b57951d4935d..c5d853dec56d2 100644 --- a/tests/baselines/reference/derivedClassParameterProperties.errors.txt +++ b/tests/baselines/reference/derivedClassParameterProperties.errors.txt @@ -1,6 +1,6 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(17,9): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(32,9): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(49,9): error TS17006: 'super' has to be called before 'this' accessing. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(49,9): error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(59,9): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts(82,9): error TS2376: A 'super' call must be the first statement in the constructor when a class contains initialized properties or has parameter properties. @@ -60,7 +60,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassP var b = 2; super(); // error: "super" has to be called before "this" accessing ~~~~~~~~ -!!! error TS17006: 'super' has to be called before 'this' accessing. +!!! error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. } } diff --git a/tests/baselines/reference/derivedClassSuperCallsWithThisArg.errors.txt b/tests/baselines/reference/derivedClassSuperCallsWithThisArg.errors.txt index d8b724e3d0f1a..e72dbe293e9ed 100644 --- a/tests/baselines/reference/derivedClassSuperCallsWithThisArg.errors.txt +++ b/tests/baselines/reference/derivedClassSuperCallsWithThisArg.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperCallsWithThisArg.ts(8,9): error TS17006: 'super' has to be called before 'this' accessing. +tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperCallsWithThisArg.ts(8,9): error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperCallsWithThisArg.ts(14,15): error TS2332: 'this' cannot be referenced in current location. tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperCallsWithThisArg.ts(20,21): error TS2332: 'this' cannot be referenced in current location. @@ -13,7 +13,7 @@ tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassS constructor() { super(this); // ok ~~~~~~~~~~~~ -!!! error TS17006: 'super' has to be called before 'this' accessing. +!!! error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. } } diff --git a/tests/baselines/reference/thisInInvalidContexts.errors.txt b/tests/baselines/reference/thisInInvalidContexts.errors.txt index fb2c706bd6e0d..0bcf7784025f5 100644 --- a/tests/baselines/reference/thisInInvalidContexts.errors.txt +++ b/tests/baselines/reference/thisInInvalidContexts.errors.txt @@ -1,5 +1,5 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(3,16): error TS2334: 'this' cannot be referenced in a static property initializer. -tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(14,9): error TS17006: 'super' has to be called before 'this' accessing. +tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(14,9): error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(22,15): error TS2332: 'this' cannot be referenced in current location. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(28,13): error TS2331: 'this' cannot be referenced in a module or namespace body. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(38,25): error TS2507: Type 'any' is not a constructor function type. @@ -25,7 +25,7 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContexts.ts(45,9): constructor() { super(this); // Error ~~~~~~~~~~~~ -!!! error TS17006: 'super' has to be called before 'this' accessing. +!!! error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. } } diff --git a/tests/baselines/reference/thisInInvalidContextsExternalModule.errors.txt b/tests/baselines/reference/thisInInvalidContextsExternalModule.errors.txt index 9b1617dbc397d..3cc06b92f365a 100644 --- a/tests/baselines/reference/thisInInvalidContextsExternalModule.errors.txt +++ b/tests/baselines/reference/thisInInvalidContextsExternalModule.errors.txt @@ -1,5 +1,5 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(3,16): error TS2334: 'this' cannot be referenced in a static property initializer. -tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(14,9): error TS17006: 'super' has to be called before 'this' accessing. +tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(14,9): error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(22,15): error TS2332: 'this' cannot be referenced in current location. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(28,13): error TS2331: 'this' cannot be referenced in a module or namespace body. tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalModule.ts(38,25): error TS2507: Type 'any' is not a constructor function type. @@ -26,7 +26,7 @@ tests/cases/conformance/expressions/thisKeyword/thisInInvalidContextsExternalMod constructor() { super(this); // error: "super" has to be called before "this" accessing ~~~~~~~~~~~~ -!!! error TS17006: 'super' has to be called before 'this' accessing. +!!! error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. } } diff --git a/tests/baselines/reference/thisInSuperCall.errors.txt b/tests/baselines/reference/thisInSuperCall.errors.txt index d0be13d8adc8e..ea4b6fd742a3e 100644 --- a/tests/baselines/reference/thisInSuperCall.errors.txt +++ b/tests/baselines/reference/thisInSuperCall.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/thisInSuperCall.ts(7,9): error TS17006: 'super' has to be called before 'this' accessing. +tests/cases/compiler/thisInSuperCall.ts(7,9): error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/compiler/thisInSuperCall.ts(14,15): error TS2332: 'this' cannot be referenced in current location. tests/cases/compiler/thisInSuperCall.ts(20,15): error TS2332: 'this' cannot be referenced in current location. @@ -12,7 +12,7 @@ tests/cases/compiler/thisInSuperCall.ts(20,15): error TS2332: 'this' cannot be r constructor() { super(this); // error: "super" has to be called before "this" accessing ~~~~~~~~~~~~ -!!! error TS17006: 'super' has to be called before 'this' accessing. +!!! error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. } } diff --git a/tests/baselines/reference/thisInSuperCall2.errors.txt b/tests/baselines/reference/thisInSuperCall2.errors.txt index 0559f83eb906d..7130ee8deb885 100644 --- a/tests/baselines/reference/thisInSuperCall2.errors.txt +++ b/tests/baselines/reference/thisInSuperCall2.errors.txt @@ -1,4 +1,4 @@ -tests/cases/compiler/thisInSuperCall2.ts(8,9): error TS17006: 'super' has to be called before 'this' accessing. +tests/cases/compiler/thisInSuperCall2.ts(8,9): error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. tests/cases/compiler/thisInSuperCall2.ts(16,15): error TS2332: 'this' cannot be referenced in current location. @@ -12,7 +12,7 @@ tests/cases/compiler/thisInSuperCall2.ts(16,15): error TS2332: 'this' cannot be constructor() { super(this); // error: "super" has to be called before "this" accessing ~~~~~~~~~~~~ -!!! error TS17006: 'super' has to be called before 'this' accessing. +!!! error TS17006: 'super' must be called before accessing 'this' in the constructor of a derived class. } } From 7b6dc92b3826597d5443dcfa863c0d64e7a8719a Mon Sep 17 00:00:00 2001 From: Yui T Date: Wed, 7 Oct 2015 18:34:48 -0700 Subject: [PATCH 8/8] Fix tslint error --- src/compiler/checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index d2eccc7540875..a0c4d2143f681 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10860,7 +10860,7 @@ namespace ts { markThisReferencesAsErrors(superCallStatement.expression); } } - else if (!(getNodeCheckFlags(node) & NodeCheckFlags.HasSeenSuperBeforeThis)){ + else if (!(getNodeCheckFlags(node) & NodeCheckFlags.HasSeenSuperBeforeThis)) { // In ES6, super inside constructor of class-declaration has to precede "this" accessing error(superCallStatement, Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class); }