All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.javascript.rules.javascript.S6861.html Maven / Gradle / Ivy

There is a newer version: 10.17.0.28100
Show newest version

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

let mutableVar = "initial value";

export { mutableVar }; // Noncompliant

Compliant solution

const immutableVar = "constant value";
export { immutableVar };

or

class MyClass {
  constructor() {
    this.mutableVar = "initial value";
  }
}

export function createMyClass() {
  return new MyClass();
}

Resources

Documentation





© 2015 - 2024 Weber Informatics LLC | Privacy Policy