53 lines
1.4 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-01-27 13:42:22 +01:00
When a non-existent variable is referenced a ``++ReferenceError++`` is raised.
2021-02-02 15:02:10 +01:00
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
2020-06-30 12:48:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2020-06-30 12:48:39 +02:00
----
var john = {
firstName: "john",
show: function() { console.log(firstName); } // Noncompliant: firstName is not defined
}
john.show();
----
=== Compliant solution
2020-06-30 12:48:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2020-06-30 12:48:39 +02:00
----
var john = {
firstName: "john",
show: function() { console.log(this.firstName); }
}
john.show();
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
include::../highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]