From ac8cd23e5be277b324cdbe4e7605caa28bafa105 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 21 Jun 2023 16:42:01 +0200 Subject: [PATCH] Create rule S6647: Unnecessary constructors should be removed (#2243) https://github.com/SonarSource/SonarJS/issues/3906 --- rules/S6647/javascript/metadata.json | 17 ++++++++++++++++ rules/S6647/javascript/rule.adoc | 30 ++++++++++++++++++++++++++++ rules/S6647/metadata.json | 2 ++ 3 files changed, 49 insertions(+) create mode 100644 rules/S6647/javascript/metadata.json create mode 100644 rules/S6647/javascript/rule.adoc create mode 100644 rules/S6647/metadata.json diff --git a/rules/S6647/javascript/metadata.json b/rules/S6647/javascript/metadata.json new file mode 100644 index 0000000000..c2288bbd05 --- /dev/null +++ b/rules/S6647/javascript/metadata.json @@ -0,0 +1,17 @@ +{ + "title": "Unnecessary constructors should be removed", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + ], + "defaultSeverity": "Minor", + "ruleSpecification": "RSPEC-6647", + "sqKey": "S6647", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "covered" +} diff --git a/rules/S6647/javascript/rule.adoc b/rules/S6647/javascript/rule.adoc new file mode 100644 index 0000000000..69ab83320a --- /dev/null +++ b/rules/S6647/javascript/rule.adoc @@ -0,0 +1,30 @@ +== Why is this an issue? + +If the class declaration does not include a constructor, one is automatically created, so there is no need to provide an empty constructor, or one that just delegates to the parent class. + +[source,javascript] +---- +class Foo { + constructor() {} // Noncompliant, empty +} + +class Bar extends Foo { + constructor(params) { // Noncompliant, just delegates to the parent + super(params); + } +} +---- + +Instead, you can safely remove the empty constructor without affecting the functionality. + +[source,javascript] +---- +class Foo {} + +class Bar extends Foo {} +---- + +== Resources +=== Documentation + +* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor[MDN - constructor] \ No newline at end of file diff --git a/rules/S6647/metadata.json b/rules/S6647/metadata.json new file mode 100644 index 0000000000..2c63c08510 --- /dev/null +++ b/rules/S6647/metadata.json @@ -0,0 +1,2 @@ +{ +}