JSHint 提供了一个 JavaScript API,用于在 Web 浏览器和 Node.js 等环境中进行编程访问。
JSHINT( source, options, predef )
分析源代码中的错误和潜在问题。
参数
source
JSHINT(["'use strict';", "console.log('hello, world!');"]);
options
JSHINT(mySource, { undef: true });
predef
globals
代码风格选项 相同JSHINT(mySource, myOptions, { jQuery: false });
JSHINT.errors
由最近一次调用 JSHINT
生成的警告和错误数组。
JSHINT.data()
生成包含有关最近一次调用 JSHINT
的详细信息的报告。
例如,以下代码
var source = [
'function goo() {}',
'foo = 3;'
];
var options = {
undef: true
};
var predef = {
foo: false
};
JSHINT(source, options, predef);
console.log(JSHINT.data());
...将产生以下输出
{
functions: [
{
name: 'goo',
param: undefined,
line: 1,
character: 14,
last: 1,
lastcharacter: 18,
metrics: {
complexity: 1,
parameters: 0,
statements: 0
}
}
],
options: {
undef: true,
indent: 4,
maxerr: 50
},
errors: [
{
id: '(error)',
raw: 'Read only.',
code: 'W020',
evidence: 'foo = 3;',
line: 2,
character: 1,
scope: '(main)',
a: undefined,
b: undefined,
c: undefined,
d: undefined,
reason: 'Read only.'
}
],
globals: [
'goo',
'foo'
],
unused: [
{
name: 'goo',
line: 1, character: 10
}
]
}