简体   繁体   English

从chrome或firefox中的调试控制台在.js文件上运行JSLint

[英]Run JSLint on a .js file from debugging console in chrome or firefox

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? 是否可以通过在chrome或firefox中的调试/开发者控制台的头文件中加载JSLint在一个或多个.js文件上运行JSLint

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: 我想这样做的原因是我想在console.log()JSLintJSON解析JSLint ,它在文档中说:

// 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));

You can run JSLint on JavaScript code using the below syntax: 您可以使用以下语法在JavaScript代码上运行JSLint:

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. 现在,在您的情况下,您需要从JavaScript文件中读取JavaScript源代码。 You can make an AJAX call to read the source code of the JavaScript file into a variable. 您可以进行AJAX调用以将JavaScript文件的源代码读入变量。 Then make a call to JSLINT as above passing that variable. 然后按上面的方式调用JSLINT传递该变量。 A sample using jQuery would be like below. 使用jQuery的示例如下所示。

$(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. 根据您要实现的目标,独立的JavaScript控制台(例如NodeJS)将比浏览器控制台更好。 I guess there exist Node packages for JSLint. 我想有JSLint的Node包。 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 首先在jslint.js的末尾添加以下行

exports.JSLINT = JSLINT;

Then write your code in say mycode.js. 然后在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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