
rules.jshint.W054.html Maven / Gradle / Ivy
Show all versions of sonar-web-frontend-js Show documentation
History
This warning has existed in two forms in JSLint, JSHint and ESLint. It was
introduced in the original version of JSLint and has remained in all three
linters ever since.
In JSLint and JSHint prior to version 1.0.0 the warning given is "The
Function constructor is eval"
In JSHint 1.0.0 and above the message used is "The Function constructor is a
form of eval"
In ESLint the messages has always been "The Function constructor is eval"
When do I get this error?
The "The Function constructor is eval" error (and the alternative "The Function
constructor is a form of eval" error) is thrown when JSLint, JSHint or ESLint
encounters a call to the Function
constructor preceded by the new
operator. Here's a simple example which defines a function to add two numbers:
x 1var add = new Function("a", "b", "return a + b");
2
Why do I get this error?
This error is raised to highlight a bad practice. By passing a string to the
Function
constructor you are requiring the engine to parse that string much in
the way it has to when you call the eval
function. For full details of why
this is a problem, see the article on the related "eval is evil"
message.
In simple cases like that of our example above, you can fix the issue by using a
function declaration or function expression:
5 1var add = function (a, b) {
2 "use strict";
3 return a + b;
4};
5
In more advanced cases where you really need to use the Function
constructor,
you can set the evil
option to true
to prevent both JSLint and JSHint from
complaining about it:
3 1/*jslint evil: true */
2var add = new Function("a", "b", "return a + b");
3
In JSHint 1.0.0 and above you have the ability to ignore any warning with a
special option syntax. The identifier of this warning is W054.
This means you can tell JSHint to not issue this warning with the /*jshint
-W054 */
directive.
In ESLint the rule that generates this warning is named no-new-func
. You can
disable it by setting it to 0
, or enable it by setting it to 1
.