2023-05-03 11:06:20 +02:00
|
|
|
== Why is this an issue?
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
There are situations where ``++super()++`` must be invoked and situations where ``++super()++`` cannot be invoked.
|
|
|
|
|
|
|
|
|
|
|
|
The basic rule is: a constructor in a non-derived class cannot invoke ``++super()++``; a constructor in a derived class must invoke ``++super()++``.
|
|
|
|
|
|
|
|
|
|
|
|
Furthermore:
|
|
|
|
|
|
|
|
* ``++super()++`` must be invoked before the ``++this++`` and ``++super++`` keywords can be used.
|
|
|
|
* ``++super()++`` must be invoked with the same number of arguments as the base class' constructor.
|
|
|
|
* ``++super()++`` can only be invoked in a constructor - not in any other method.
|
|
|
|
* ``++super()++`` cannot be invoked multiple times in the same constructor.
|
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
Some issues are not raised if the base class is not defined in the same file as the current class. This is a known limitation for this rule.
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2020-12-21 15:38:52 +01:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Noncompliant code example
|
2020-12-21 15:38:52 +01:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,javascript]
|
2020-12-21 15:38:52 +01:00
|
|
|
----
|
|
|
|
class Dog extends Animal {
|
|
|
|
constructor(name) {
|
|
|
|
super();
|
|
|
|
this.name = name;
|
|
|
|
super(); // Noncompliant
|
|
|
|
super.doSomething();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
2021-04-28 18:08:03 +02:00
|
|
|
|
2023-05-03 11:06:20 +02:00
|
|
|
=== Compliant solution
|
2020-12-21 15:38:52 +01:00
|
|
|
|
2022-02-04 17:28:24 +01:00
|
|
|
[source,javascript]
|
2020-12-21 15:38:52 +01:00
|
|
|
----
|
|
|
|
class Dog extends Animal {
|
|
|
|
constructor(name) {
|
|
|
|
super();
|
|
|
|
this.name = name;
|
|
|
|
super.doSomething();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2021-04-28 18:08:03 +02:00
|
|
|
|
|
|
|
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-09-20 15:38:42 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
== Implementation Specification
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== Message
|
|
|
|
|
|
|
|
Lacked a call of 'super()' in some code paths.
|
|
|
|
|
|
|
|
Expected to call 'super()'.
|
|
|
|
|
|
|
|
Unexpected duplicate 'super()'.
|
|
|
|
|
|
|
|
Unexpected 'super()' because 'super' is not a constructor.
|
|
|
|
|
|
|
|
Unexpected 'super()'.
|
|
|
|
|
|
|
|
'super'|'this' is not allowed before 'super()'.
|
|
|
|
|
2021-09-20 15:38:42 +02:00
|
|
|
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
2023-05-25 14:18:12 +02:00
|
|
|
=== supercedes: S3833
|
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|