简体   繁体   中英

How to show all the errors and get a JSHint report in JavaScript?

I have the following code that validates JavaScript code:

var jsCode = ".....";  // JavaScript code here
var success = JSHint(jsCode); // 
alert(success); // Shows whether 'true' or 'false'

How can I show the list of all errors in the code instead of just 'true' or 'false'? In the documentation, it says, " In that case, you can use JSHINT.errors to retrieve the errors or request a complete report by calling the JSHINT.data() method. "

Try this:

var jsCode = ".....";  // JavaScript code here
var success = JSHint(jsCode); // 

var data = JSHINT.data();

//Print out list of object props:
for(var i=0; i<data.functions.length; i++){
    for(var j in data.functions[i]){
        document.write(j + ': '+ data.functions[i][j]  +'<br>');
    }
}

Validate Javascript code is a task that need to be automate. To automate task you need to use Grunt .

Grunt is an automate task for javascript based projects, then you achieve dependency management, js validate, reports.... etc.

The grunt-contrib-jshint plugin could help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM