从chrome或firefox中的调试控制台在.js文件上运行JSLint [英] Run JSLint on a .js file from debugging console in chrome or firefox

查看:113
本文介绍了从chrome或firefox中的调试控制台在.js文件上运行JSLint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过在chrome或firefox中从调试/开发人员控制台的标头中加载JSLint之后在一个或多个.js文件上运行JSLint?

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?

我要这样做的原因是我想在console.log()中打印JSLint的解析,并在console.log()中打印,它在文档中说:

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

推荐答案

您可以使用以下语法在JavaScript代码上运行JSLint:

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.

现在,您需要从JavaScript文件中读取JavaScript源.您可以进行AJAX调用,以将JavaScript文件的源代码读取到一个变量中.然后像上面一样调用JSLINT并传递该变量.使用jQuery的示例如下所示.

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

根据您要实现的目标,独立的JavaScript控制台(例如NodeJS)比浏览器控制台更好.我猜有JSLint的Node包.但是,如果您想手动添加它,则只需按照以下步骤操作即可.

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.

首先在jslint.js的末尾添加以下行

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

exports.JSLINT = JSLINT;

然后在mycode.js中编写您的代码.

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

这篇关于从chrome或firefox中的调试控制台在.js文件上运行JSLint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆