org.sonar.l10n.javascript.rules.javascript.S2137.html Maven / Gradle / Ivy
Why is this an issue?
JavaScript has special identifiers that, while not reserved, still should not be used as identifiers. They form the JavaScript standard built-in
objects and global properties. They are available in all environments. Some examples are:
- Global objects:
Object
, Function
, Error
, …
- Global object function properties:
eval()
, isNan()
, parseFloat()
, decodeURI()
, …
- Global object value properties:
undefined
, NaN
, Infinity
- Identifiers with special meanings:
arguments
These words should not be bound or assigned, because doing so would overwrite the original definitions of these identifiers. What’s more, assigning
or binding some of these names will generate an error in JavaScript strict mode code.
Noncompliant code example
eval = 17; // Noncompliant
arguments++; // Noncompliant
++eval; // Noncompliant
const obj = { set p(arguments) { } }; // Noncompliant
let eval; // Noncompliant
try { /* ... */ } catch (arguments) { } // Noncompliant
function x(eval) { /* ... */ } // Noncompliant
function arguments() { /* ... */ } // Noncompliant
const y = function eval() { /* ... */ }; // Noncompliant
function fun() {
if (arguments.length == 0) { // Compliant
// do something
}
}
Compliant solution
result = 17;
args++;
++result;
const obj = { set p(arg) { } };
let result;
try { /* ... */ } catch (args) { }
function x(arg) { /* ... */ }
function args() { /* ... */ }
const y = function fun() { /* ... */ };
function fun() {
if (arguments.length == 0) {
// do something
}
}
Resources
Documentation
- MDN web docs - Reserved words
- MDN web docs - Identifiers with special
meanings
- MDN web docs - Global Objects