是否可以通过在Chrome或Firefox中的调试/开发人员控制台的标头中加载JSLint来在一个或多个.js文件上运行JSLint
?
我想这样做的原因是我想在JSON
中打印console.log()
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));
以下语法在 JavaScript 代码上运行 JSLint:
var code = "var a = 1 + 2;";
JSLINT(code);
您可以打印语法树,如问题中所述。
现在在你的例子中,你需要从JavaScript文件中读取JavaScript源代码。您可以进行 AJAX 调用以将 JavaScript 文件的源代码读入变量。然后像上面一样调用 JSLINT 传递该变量。使用 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));
});
});
根据您要实现的目标,一个独立的JavaScript控制台(例如。NodeJS)将是比浏览器控制台更好的选择。我想JSLint存在Node包。但是,如果您想手动包含它,您可以简单地执行以下操作。
首先在 jslint 末尾添加以下行.js
exports.JSLINT = JSLINT;
然后用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");
然后按以下方式从控制台运行代码:
node mycode.js