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

rules.jshint.E009.html Maven / Gradle / Ivy

Go to download

Consume reports generated by jshint for code quality. Also consume reports for code duplication (either simian or cpd). Consumes the unit/integration tests reports (generated by Jasmin) coverage report (lcov generated by Istanbul). The information generated by reports are added in Sonar

There is a newer version: 2.1.0
Show newest version

    

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 */
2
function example() {
3
    "use strict";
4
    this.x = 10;
5
}
6
7
var obj = {};
8
example.call(obj);
9
JSHint found 2 errorsVersion 2.9.0
Line 1:Option 'validthis' can't be used in a global scope.
Line 4:Possible strict violation.

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
 
1
function example() {
2
    /*jshint validthis: true */
3
    "use strict";
4
    this.x = 10;
5
}
6
7
var obj = {};
8
example.call(obj);
9
JSHint found no errorsVersion 2.9.0

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.





© 2015 - 2025 Weber Informatics LLC | Privacy Policy