
rules.jshint.E009.html Maven / Gradle / Ivy
Show all versions of sonar-web-frontend-js Show documentation
When do I get this error?
The "Option 'validthis' can't be used in a global scope" error is thrown when
JSHint encounters the validthis
option in a global scope. Here's a silly
example in which we declare a function that is intended to be invoked in the
context of an object:
x 1/*jshint validthis: true */
2function example() {
3 "use strict";
4 this.x = 10;
5}
6
7var obj = {};
8example.call(obj);
9
Why do I get this error?
This error is raised to highlight a breach of JSHint rules. Your code will
most likely run without error if you do not fix this issue, but you are breaking
the rules of JSHint and will be unable to validate the rest of your code.
The validthis
option is used to indicate to JSHint that a function including
the use of this is not going to violate the rules of strict mode. Since this
applies only to functions, it makes no sense to define this option in the global
scope. You should only use it (in function scope) when you are certain the
function will be called with a context. Here's what the JSHint docs
have to say:
This option suppresses warnings about possible strict violations when the code
is running in strict mode and you use this
in a non-constructor function.
You should use this option—in a function scope only—when you are positive that
your use of this
is valid in the strict mode (for example, if you call your
function using Function.call
).
Note: This option can be used only inside of a function scope. JSHint will
fail with an error if you will try to set this option globally.
To resolve this issue, you simply need to move the directive including the
validthis
option into the function. You will need it in each function that
runs in strict mode and contains references to this
:
9 1function example() {
2 /*jshint validthis: true */
3 "use strict";
4 this.x = 10;
5}
6
7var obj = {};
8example.call(obj);
9
In JSHint 1.0.0 and above you have the ability to ignore any warning with a
special option syntax. Since this message relates to JSHint error
rather than a warning it is not possible to disable it.