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

rules.jshint.W054.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

    

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
 
1
var add = new Function("a", "b", "return a + b");
2
JSLint found 1 errorVersion 2015-09-23
Line 0:Unexpected 'new Function'.

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
 
1
var add = function (a, b) {
2
    "use strict";
3
    return a + b;
4
};
5
JSLint found no errorsVersion 2015-09-23

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 */
2
var add = new Function("a", "b", "return a + b");
3
JSLint found 2 errorsVersion 2015-09-23
Line 0:Bad option 'evil'.
Line 1:Unexpected 'new Function'.

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.





© 2015 - 2025 Weber Informatics LLC | Privacy Policy