Edu*_*scu 7 javascript parsing json jslint javascript-debugger
是否可以JSLint通过在chrome或firefox中的调试/开发者控制台的头文件中加载JSLint 来运行一个或多个.js文件?
我想这样做的原因是,我想在打印console.log()的解析JSLint中JSON,它说,在文档:
// 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));
Run Code Online (Sandbox Code Playgroud)
您可以使用以下语法在JavaScript代码上运行JSLint:
var code = "var a = 1 + 2;";
JSLINT(code);
Run Code Online (Sandbox Code Playgroud)
您可以按照问题中提到的那样打印语法树.
现在,在您的情况下,您需要从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));
});
});
Run Code Online (Sandbox Code Playgroud)
根据您要实现的目标,独立的JavaScript控制台(例如NodeJS)将比浏览器控制台更好.我想有JSLint的Node包.但是如果你想手动包含它,你可以简单地按如下方式进行操作.
首先在jslint.js的末尾添加以下行
exports.JSLINT = JSLINT;
Run Code Online (Sandbox Code Playgroud)
然后在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");
Run Code Online (Sandbox Code Playgroud)
然后从控制台运行您的代码:
node mycode.js
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1472 次 |
| 最近记录: |