7

Is it possible to run JSLint on one or more .js files by having JSLint loaded afterwards in the header from debugging/developer console in chrome or firefox?

The reason that I want to do that is that I want to print in console.log() the parsing of JSLint in JSON,it says in documentation:

// You can obtain the parse tree that JSLint constructed while parsing. The
// latest tree is kept in JSLINT.tree. A nice stringication can be produced
// with
//     JSON.stringify(JSLINT.tree, [
//         'string',  'arity', 'name',  'first',
//         'second', 'third', 'block', 'else'
//     ], 4));
Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179
  • Do you mean you want to run JSLint on some JavaScript file on in your file system but which is not included on the page using script tag? – Cracker Aug 09 '12 at 15:25
  • @Cracker Yes, eventually to run the line by line in the console, and the to put a `console.log(JSON.stringify(JSLINT.tree,...))` – Eduard Florinescu Aug 09 '12 at 17:54
  • I don't think you would be able to access files on your system without including them on the page with script tags. Can I know the purpose of doing this so that I can suggest if there are any alternatives? – Cracker Aug 09 '12 at 18:21
  • Also are you sure you need the parse tree and not the errors detected by JSLint? – Cracker Aug 09 '12 at 18:22
  • @Cracker Yes I want the parsing tree not the errors. And from console I am able to load .js in the header see this:http://stackoverflow.com/questions/5282228/include-javascript-file-in-chrome-console My question remains once I loaded JSLint how do I run it on the javascript files already running. – Eduard Florinescu Aug 09 '12 at 18:46
  • I am not able to understand what problem you are facing then. If you are able to include JSLint and your JS files dynamically from the console then what are you not able to do? Using the method you have referred you can include jslint.js and the JS files which you need to parse. Then simply execute the JSON.stringify code. – Cracker Aug 09 '12 at 18:50
  • @Cracker I am unfamiliar with JSLint and I don't know how it works, I don't know how to check those files with it, I don't know how to use it. Probably will read those manuals – Eduard Florinescu Aug 09 '12 at 18:53

1 Answers1

8

You can run JSLint on JavaScript code using the below syntax:

var code = "var a = 1 + 2;";
JSLINT(code);

And you can print the syntax tree as you have mentioned in the question.

Now in your case, you need to read the JavaScript source from JavaScript files. You can make an AJAX call to read the source code of the JavaScript file into a variable. Then make a call to JSLINT as above passing that variable. A sample using jQuery would be like below.

$(function() {
    // Include jslint.js
    $('<script src="http://localhost/yourapp/jslint.js">').appendTo("head");

    // Read JavaScript file contents into 'code'
    $.get('http://localhost/yourapp/somescript.js', function(code) {

        // Run JSLINT over code
            JSLINT(code);

        // Print the parse tree
        console.log(JSON.stringify(JSLINT.tree, [
                    'string',  'arity', 'name',  'first',
                    'second', 'third', 'block', 'else'
                    ], 4));
        });
});

Depending on what you are trying to achieve, a standalone JavaScript console (eg. NodeJS) would be a better alternative than a browser console. I guess there exist Node packages for JSLint. But if you want to include it manually you can simply do it as below.

First add the below line at the end of jslint.js

exports.JSLINT = JSLINT;

Then write your code in say mycode.js.

var fs = require("fs");
var jslint = require("./jslint.js");

fs.readFile("./test.js", function(err, code) {
    var source = code.toString('ascii');    

    jslint.JSLINT(source);

    console.log(JSON.stringify(jslint.JSLINT.tree, [
         'string',  'arity', 'name',  'first',
         'second', 'third', 'block', 'else'
        ], 4));
},"text");

Then run your code from console as:

node mycode.js
Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179
Cracker
  • 1,780
  • 4
  • 24
  • 34
  • That was what I needed, especially the JQuery sample. I have also the Node installation. – Eduard Florinescu Aug 09 '12 at 19:27
  • @EduardFlorinescu I have included a sample for Node as well. – Cracker Aug 09 '12 at 19:44
  • Some specifications about running in chrome console you need to chrome with to be run with this command line switch `--disable-web-security` see http://superuser.com/questions/384871/how-to-override-access-control-allow-origin-restriction-in-google-chrome also there is the nee to make sure that jquery is loaded `var script= document.createElement('script');`     `script.type='text/javascript';` `script.src='http://localhost/jquery.js';` `document.head.appendChild(script);` With all this the code inside the get still doesn't run. The node one works. – Eduard Florinescu Aug 10 '12 at 12:03
  • I edited the answer the jQuery get, to add the datatype, without specifying the "text" data type in the get the code doesn't work, with that it works just fine. – Eduard Florinescu Aug 10 '12 at 12:45