31 lines
1.1 KiB
Plaintext
Raw Normal View History

2021-01-27 13:42:22 +01:00
When a non-existent variable is referenced a ``++ReferenceError++`` is raised.
2020-06-30 12:48:39 +02:00
Due to the dynamic nature of JavaScript this can happen in a number of scenarios:
2020-06-30 12:48:39 +02:00
* When typo was made in a symbol's name.
2021-01-27 13:42:22 +01:00
* When using variable declared with ``++let++`` or ``++const++`` before declaration (unlike ``++var++``-declarations, they are not hoisted to the top of the scope).
* Due to confusion with scopes of ``++let++``- and ``++const++``-declarations (they have block scope, unlike ``++var++``-declarations, having function scope).
* When accessing a property in the wrong scope (e.g. forgetting to specify ``++this.++``).
2020-06-30 12:48:39 +02:00
2021-01-27 13:42:22 +01:00
This rule does not raise issues on global variables which are defined with ``++sonar.javascript.globals++`` and ``++sonar.javascript.environments++`` properties.
2020-06-30 12:48:39 +02:00
== Noncompliant Code Example
----
var john = {
firstName: "john",
show: function() { console.log(firstName); } // Noncompliant: firstName is not defined
}
john.show();
----
== Compliant Solution
----
var john = {
firstName: "john",
show: function() { console.log(this.firstName); }
}
john.show();
----