Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions tsc/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2936,15 +2936,15 @@ func (c *Checker) checkConstructorDeclaration(node *ast.Node) {
if classExtendsNull {
c.error(superCall, diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null)
}
// A super call must be root-level in a constructor if both of the following are true:
// - The containing class is a derived class.
// - The constructor declares parameter properties
// or the containing class declares instance member variables with initializers.
superCallShouldBeRootLevel := !c.emitStandardClassFields &&
(core.Some(node.Parent.Members(), isInstancePropertyWithInitializerOrPrivateIdentifierProperty) ||
core.Some(node.Parameters(), func(p *ast.Node) bool {
return ast.HasSyntacticModifier(p, ast.ModifierFlagsParameterPropertyModifier)
}))
// A super call must be root-level in a derived constructor if either condition holds:
// - The constructor declares parameter properties, which require initialization on this.
// - Class fields are downleveled and the class declares instance property initializers
// or private identifiers.
superCallShouldBeRootLevel := (!c.emitStandardClassFields &&
core.Some(node.Parent.Members(), isInstancePropertyWithInitializerOrPrivateIdentifierProperty)) ||
core.Some(node.Parameters(), func(p *ast.Node) bool {
return ast.HasSyntacticModifier(p, ast.ModifierFlagsParameterPropertyModifier)
})
if superCallShouldBeRootLevel {
// Until we have better flow analysis, it is an error to place the super call within any kind of block or conditional
// See GH #8277
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
parameterPropertiesRequireRootLevelSuper.ts(5,20): error TS2401: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
parameterPropertiesRequireRootLevelSuper.ts(12,31): error TS2401: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
parameterPropertiesRequireRootLevelSuper.ts(19,13): error TS2401: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
parameterPropertiesRequireRootLevelSuper.ts(43,24): error TS2401: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
parameterPropertiesRequireRootLevelSuper.ts(49,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties, parameter properties, or private identifiers.
parameterPropertiesRequireRootLevelSuper.ts(50,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.


==== parameterPropertiesRequireRootLevelSuper.ts (6 errors) ====
class Base {}

class ConditionalParameterProperty extends Base {
constructor(public value: number) {
if (value) super();
~~~~~~~
!!! error TS2401: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
else super();
}
}

class ReturnedSuperParameterProperty extends Array<number> {
constructor(public value?: number) {
return !value ? [] : (super() as any);
~~~~~~~
!!! error TS2401: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
}
}

class BlockParameterProperty extends Base {
constructor(readonly value: number) {
{
super();
~~~~~~~
!!! error TS2401: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
}
}
}

class RootLevelParameterProperty extends Base {
constructor(public value: number) {
const adjusted = value + 1;
super();
this.value = adjusted;
}
}

class ConditionalPlainParameter extends Base {
constructor(value: number) {
if (value) super();
else super();
}
}

class ConditionalFieldInitializer extends Base {
value = 1;

constructor(condition: boolean) {
if (condition) super();
~~~~~~~
!!! error TS2401: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
else super();
}
}

class AccessBeforeSuperParameterProperty extends Base {
constructor(public value: number) {
~~~~~~~~~~~
!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties, parameter properties, or private identifiers.
this.value;
~~~~
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
super();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//// [tests/cases/compiler/parameterPropertiesRequireRootLevelSuper.ts] ////

//// [parameterPropertiesRequireRootLevelSuper.ts]
class Base {}

class ConditionalParameterProperty extends Base {
constructor(public value: number) {
if (value) super();
else super();
}
}

class ReturnedSuperParameterProperty extends Array<number> {
constructor(public value?: number) {
return !value ? [] : (super() as any);
}
}

class BlockParameterProperty extends Base {
constructor(readonly value: number) {
{
super();
}
}
}

class RootLevelParameterProperty extends Base {
constructor(public value: number) {
const adjusted = value + 1;
super();
this.value = adjusted;
}
}

class ConditionalPlainParameter extends Base {
constructor(value: number) {
if (value) super();
else super();
}
}

class ConditionalFieldInitializer extends Base {
value = 1;

constructor(condition: boolean) {
if (condition) super();
else super();
}
}

class AccessBeforeSuperParameterProperty extends Base {
constructor(public value: number) {
this.value;
super();
}
}


//// [parameterPropertiesRequireRootLevelSuper.js]
"use strict";
class Base {
}
class ConditionalParameterProperty extends Base {
constructor(value) {
this.value = value;
if (value)
super();
else
super();
}
}
class ReturnedSuperParameterProperty extends Array {
constructor(value) {
this.value = value;
return !value ? [] : super();
}
}
class BlockParameterProperty extends Base {
constructor(value) {
this.value = value;
{
super();
}
}
}
class RootLevelParameterProperty extends Base {
constructor(value) {
const adjusted = value + 1;
super();
this.value = value;
this.value = adjusted;
}
}
class ConditionalPlainParameter extends Base {
constructor(value) {
if (value)
super();
else
super();
}
}
class ConditionalFieldInitializer extends Base {
constructor(condition) {
this.value = 1;
if (condition)
super();
else
super();
}
}
class AccessBeforeSuperParameterProperty extends Base {
constructor(value) {
this.value;
super();
this.value = value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
parameterPropertiesRequireRootLevelSuper.ts(5,20): error TS2401: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
parameterPropertiesRequireRootLevelSuper.ts(12,31): error TS2401: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
parameterPropertiesRequireRootLevelSuper.ts(19,13): error TS2401: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
parameterPropertiesRequireRootLevelSuper.ts(43,24): error TS2401: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
parameterPropertiesRequireRootLevelSuper.ts(49,5): error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties, parameter properties, or private identifiers.
parameterPropertiesRequireRootLevelSuper.ts(50,9): error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.


==== parameterPropertiesRequireRootLevelSuper.ts (6 errors) ====
class Base {}

class ConditionalParameterProperty extends Base {
constructor(public value: number) {
if (value) super();
~~~~~~~
!!! error TS2401: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
else super();
}
}

class ReturnedSuperParameterProperty extends Array<number> {
constructor(public value?: number) {
return !value ? [] : (super() as any);
~~~~~~~
!!! error TS2401: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
}
}

class BlockParameterProperty extends Base {
constructor(readonly value: number) {
{
super();
~~~~~~~
!!! error TS2401: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
}
}
}

class RootLevelParameterProperty extends Base {
constructor(public value: number) {
const adjusted = value + 1;
super();
this.value = adjusted;
}
}

class ConditionalPlainParameter extends Base {
constructor(value: number) {
if (value) super();
else super();
}
}

class ConditionalFieldInitializer extends Base {
value = 1;

constructor(condition: boolean) {
if (condition) super();
~~~~~~~
!!! error TS2401: A 'super' call must be a root-level statement within a constructor of a derived class that contains initialized properties, parameter properties, or private identifiers.
else super();
}
}

class AccessBeforeSuperParameterProperty extends Base {
constructor(public value: number) {
~~~~~~~~~~~
!!! error TS2376: A 'super' call must be the first statement in the constructor to refer to 'super' or 'this' when a derived class contains initialized properties, parameter properties, or private identifiers.
this.value;
~~~~
!!! error TS17009: 'super' must be called before accessing 'this' in the constructor of a derived class.
super();
}
}

Loading