Skip to content

Commit

Permalink
Merge branch 'master' into rule/S5973-add-javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
ilia-kebets-sonarsource authored Nov 29, 2023
2 parents 975ec98 + 20f66a3 commit 382d88e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 6 deletions.
4 changes: 4 additions & 0 deletions frontend/public/covered_rules.json
Original file line number Diff line number Diff line change
Expand Up @@ -3605,6 +3605,8 @@
"S6853": "SonarJS 10.10.0.24774",
"S6854": "SonarJS 10.10.0.24774",
"S6855": "SonarJS 10.10.0.24774",
"S6859": "SonarJS master",
"S6861": "SonarJS master",
"S878": "SonarJS 3.3.0.5702",
"S881": "SonarJS 3.3.0.5702",
"S888": "SonarJS 3.3.0.5702",
Expand Down Expand Up @@ -5807,6 +5809,8 @@
"S6853": "SonarJS 10.10.0.24774",
"S6854": "SonarJS 10.10.0.24774",
"S6855": "SonarJS 10.10.0.24774",
"S6859": "SonarJS master",
"S6861": "SonarJS master",
"S878": "SonarJS 6.0.0.9595",
"S881": "SonarJS 6.1.0.11503",
"S888": "SonarJS 6.2.0.12043",
Expand Down
6 changes: 0 additions & 6 deletions rules/S3743/cfamily/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ void init() noexcept(true) { // compliant because ...

Destructors are not handled by this rule because there is a specific rule about exceptions in destructors (see ExceptionInDestructor).


== Resources

* https://www.hlsl.co.uk/blog/2017/12/1/c-noexcept-and-move-constructors-effect-on-performance-in-stl-containers[{cpp} noexcept and move constructors effect on performance in STL containers]


ifdef::env-github,rspecator-view[]

'''
Expand Down
24 changes: 24 additions & 0 deletions rules/S6861/javascript/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"title": "Mutable variables should not be exported",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6861",
"sqKey": "S6861",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "infeasible",
"code": {
"impacts": {
"MAINTAINABILITY": "HIGH",
"RELIABILITY": "MEDIUM"
},
"attribute": "CONVENTIONAL"
}
}
51 changes: 51 additions & 0 deletions rules/S6861/javascript/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
== Why is this an issue?

In JavaScript, a mutable variable is one whose value can be changed after it has been initially set. This is in contrast to immutable variables, whose values cannot be changed once they are set.

Exporting mutable variables can lead to unpredictable behavior and bugs in your code. This is because any module that imports the variable can change its value. If multiple modules import and change the value of the same variable, it can become difficult to track what the current value of the variable is and which module changed it last.

== How to fix it

If the value of the variable does not need to change, you can declare it as a constant using the ``++const++`` keyword. Alternatively, if you have a group of related variables that need to be mutable, consider using a class to encapsulate them. You can then export an instance of the class, or a factory function that creates instances of the class.

=== Code examples

==== Noncompliant code example

[source,javascript]
----
let mutableVar = "initial value";
export { mutableVar }; // Noncompliant
----

==== Compliant solution

[source,javascript]
----
const immutableVar = "constant value";
export { immutableVar };
----

or

[source,javascript]
----
class MyClass {
constructor() {
this.mutableVar = "initial value";
}
}
export function createMyClass() {
return new MyClass();
}
----

== Resources
=== Documentation

* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let[let]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const[const]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Glossary/Mutable[Mutable]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Glossary/Immutable[Immutable]
2 changes: 2 additions & 0 deletions rules/S6861/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}

0 comments on commit 382d88e

Please sign in to comment.