JSHint
JSHint 是一個開源工具,可以檢測 JavaScript 程式碼中的錯誤和潛在問題。
要提示你的 JavaScript,你有兩種選擇。
- 轉到 JSHint.com 並將程式碼貼上到行文字編輯器中。
- 在 IDE 中安裝 JSHint 。
- Atom: linter-jshint (必須安裝 Linter 外掛)
- Sublime Text: JSHint Gutter 和/或 Sublime Linter
- Vim: jshint.vim 或 jshint2.vim
- Visual Studio: VSCode JSHint
將它新增到 IDE 的好處是,你可以建立一個名為 .jshintrc
的 JSON 配置檔案,該檔案將在 linting 你的程式時使用。如果要在專案之間共享配置,這是修道院。
示例 .jshintrc
檔案
{
"-W097": false, // Allow "use strict" at document level
"browser": true, // defines globals exposed by modern browsers http://jshint.com/docs/options/#browser
"curly": true, // requires you to always put curly braces around blocks in loops and conditionals http://jshint.com/docs/options/#curly
"devel": true, // defines globals that are usually used for logging poor-man's debugging: console, alert, etc. http://jshint.com/docs/options/#devel
// List global variables (false means read only)
"globals": {
"globalVar": true
},
"jquery": true, // This option defines globals exposed by the jQuery JavaScript library.
"newcap": false,
// List any global functions or const vars
"predef": [
"GlobalFunction",
"GlobalFunction2"
],
"undef": true, // warn about undefined vars
"unused": true // warn about unused vars
}
JSHint 還允許配置特定的行/程式碼塊
switch(operation)
{
case '+'
{
result = a + b;
break;
}
// JSHint W086 Expected a 'break' statement
// JSHint flag to allow cases to not need a break
/* falls through */
case '*':
case 'x':
{
result = a * b;
break;
}
}
// JSHint disable error for variable not defined, because it is defined in another file
/* jshint -W117 */
globalVariable = 'in-another-file.js';
/* jshint +W117 */
http://jshint.com/docs/options/ 中記錄了更多配置選項。