Modify rule S4124: Adapt to LaYC (#2692)
Co-authored-by: Yassin Kammoun <52890329+yassin-kammoun-sonarsource@users.noreply.github.com>
This commit is contained in:
parent
4387797de4
commit
59516e9a7e
@ -1,31 +1,42 @@
|
|||||||
== Why is this an issue?
|
== Why is this an issue?
|
||||||
|
|
||||||
Declare a constructor inside an ``++interface++``, and you will get a simple method with the name "constructor". The same thing will happen if you create a ``++new++`` method inside the ``++interface++``: you'll get a simple method named "new".
|
JavaScript and TypeScript classes may define a `constructor` method that is executed when a new instance is created. TypeScript allows interfaces that describe a static class object to define a `new()` method. Using these terms to name methods in other contexts can lead to confusion and make the code unclear and harder to understand.
|
||||||
|
|
||||||
|
This rule reports when:
|
||||||
|
|
||||||
|
* A class defines a method named `new`. The `new` keyword is used to create new instances of the class. If a method with the same name is defined, it can be unclear whether the method is intended to create new instances or perform some other action.
|
||||||
|
* An interface defines a method named `constructor`. The constructor method is used to define the constructor function for a class that implements the interface. If a method with the same name is defined in the interface, it can be unclear whether the method is intended to define the constructor function or perform some other action.
|
||||||
|
|
||||||
|
|
||||||
Instead, the intent was probably to specify that the type did not originate from a TypeScript file. In such cases, just use the ``++declare class++`` syntax.
|
[source,javascript,diff-id=1,diff-type=noncompliant]
|
||||||
|
|
||||||
|
|
||||||
=== Noncompliant code example
|
|
||||||
|
|
||||||
[source,javascript]
|
|
||||||
----
|
----
|
||||||
interface TypeDeclaredElsewhere {
|
interface I {
|
||||||
someMethod(): number;
|
constructor(): void; // Noncompliant
|
||||||
new(b: boolean): TypeDeclaredElsewhere; // Noncompliant
|
new(): I;
|
||||||
constructor(b: boolean): void; // Noncompliant
|
}
|
||||||
|
|
||||||
|
declare class C {
|
||||||
|
constructor();
|
||||||
|
new(): C; // Noncompliant
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
Do not define methods named `constructor` on TypeScript interfaces. Similarly, avoid defining class methods called `new`.
|
||||||
|
|
||||||
=== Compliant solution
|
[source,javascript,diff-id=1,diff-type=compliant]
|
||||||
|
|
||||||
[source,javascript]
|
|
||||||
----
|
----
|
||||||
declare class TypeDeclaredElsewhere {
|
interface I {
|
||||||
someMethod(): number;
|
new(): I;
|
||||||
constructor(b: boolean);
|
}
|
||||||
|
|
||||||
|
declare class C {
|
||||||
|
constructor();
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
== Resources
|
||||||
|
=== Documentation
|
||||||
|
* https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#interfaces[TypeScript - Interfaces]
|
||||||
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes[MDN - Classes]
|
||||||
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor[MDN - `constructor`]
|
||||||
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new[MDN - `new` operator]
|
Loading…
x
Reference in New Issue
Block a user