jshint是一個(gè)javaScript語法和風(fēng)格的檢查工具凹联,但檢查不出邏輯問題滤淳。
安裝
一爪飘、在sublime text 中使用jshint插件步驟:
(注:在為Sublime Text編輯器安裝Sublime-JSHint插件之前义起,要首先確保安裝了node.js)
方法1:
1 Ctrl+Shift+P 呼出Sublime命令面板
2 鍵入install,并選擇Package Control:Install Package
3 鍵入js gutter,并選擇JSHint Gutter
方法2:
1 獲取Sublime Text,可通過git命令悦施。
git clone https://github.com/victorporof/Sublime-JSHint.git
2 打開 Sublime Text Package 文件夾。Preferences -> Browse Packages去团。
3 將步驟1中獲取到的 Sublime-JSHint 文件夾移到 Packages 文件夾中抡诞。
4 重啟 Sublime Text。
Sublime-JSHint使用
方法1:由菜單 Tools -> Command Palette(或快捷鍵 Ctrl+Shift+P)打開命令面板土陪。鍵入 jshint 并選擇 JSHint昼汗。
方法2:打開一 js 文件,并打開控制臺(tái)(View -> Show Console)鬼雀,在控制臺(tái)中鍵入 view.run_command("jshint")顷窒。
方法3:Ctrl+Shift+J(或者M(jìn)ac使用Cmd+Shift+J)
方法4:右鍵選擇JSHint
設(shè)置
"lint_on_edit": false,
// Configurable duration before re-linting.
"lint_on_edit_timeout": 5,
// Automatically lint when a file is loaded.
"lint_on_load": false,
// Automatically lint when a file is saved.
"lint_on_save": false,
// Highlight problematic regions when selected.
"highlight_selected_regions": false,
// Log the settings passed to JSHint from `.jshintrc`.
"print_diagnostics": true
配置選項(xiàng)
"strict": true, //嚴(yán)格模式 參考文章(http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html)
"asi": true, //允許省略分號(hào)(寫上這條,規(guī)避檢查出很多警告 可以去掉)
"bitwise": true, //禁止使用位運(yùn)算符源哩,比如經(jīng)常把&&寫錯(cuò)& 規(guī)避此錯(cuò)誤
"noarg": true, //禁止使用.caller 和 .callee (ECMS5已經(jīng)禁用了此 可以去掉)
"eqeqeq": true, //禁止使用== 和 鞋吉!= 強(qiáng)制使用=== 和 !==
"undef": true, //禁止使用不在全局變量列表中的未定義變量
"curly": true, //循環(huán)或者條件語句必須使用花括號(hào)包住
"devel": true, //定義用于調(diào)試的全局變量:console,alert
"jquery": true, //定義全局暴露的jQuery庫 (可以去掉)
"browser": true, //暴露瀏覽器屬性的全局變量 如window document
"evil": true, //禁止使用eval (可以去掉)
"quotemark":true (商榷)
"globals": {"$":true,"require":true,"FastClick":true,"Swiper"},
編輯器 VS Gulp_jshint
1 個(gè)人偏向于在編輯器中使用jshint,這樣不用在每個(gè)項(xiàng)目都配置励烦,同時(shí)也能約束項(xiàng)目之外的編輯谓着。
2 在編輯器中使用jshint 比在Gulp_jshint 的更實(shí)時(shí),更清晰坛掠。jshint有錯(cuò)誤,會(huì)在每一行有提示赊锚。而后者會(huì)在命令窗口提示治筒,不方便。
3 Gulp_jshint 還需要進(jìn)一步探查舷蒲。
項(xiàng)目中一些問題
無法識(shí)別的一些問題:
1 //= require chartjs/chart.core (解決掉了)
2 封閉空間上面的(未解決)耸袜;
3 引入插件的實(shí)例化:comRadar = new Chart(ctx).Radar()(解決掉了);
//配置參數(shù)
"globals": {"$":true,"require":true,"FastClick":true,"Swiper"},
4 var CardPopup = function(el, opts) {
this.el = el;
this.defaults = {};
this.options = $.extend({}, this.defaults, opts);
} (此處加封號(hào))
CardPopup.prototype = {
init: function() {
var me = this,
el = me.el;
return me;
},
} (此處加封號(hào))
$.fn.CardPopup = function(opts) {
var com;
} (此處加封號(hào))
5 A && B
//要么寫成 if(A) B
//或者 /* jshint expr: true */
6 jshint 中希望 A.B 而不希望A[B]
7 /* jshint ignore:start */
var optionTemplate = Hogan.compile('<ul class="items options clearfix count-{{count}}">\
{{#options}}\
<li class="item option {{#selected}}selected{{/selected}}">\
<div class="pie">\
<canvas class="canvas-outer" data-percent="{{value}}"></canvas>\
<div class="num">{{value}}%</div>\
</div>\
<p class="text">{{text}}</p>\
</li>\
{{/options}}\
</ul>');
/* jshint ignore:end */
8 /*jshint scripturl:true*/
data.top[i].redirect_url = data.top[i].redirect_url || 'javascript:void(0);';
9 $(subscribeData).each(function(index, element) {
if (element == specialColumnId) {
elSubscribe.removeClass('icon-jiadingyue').addClass('icon-yidingyue');
}
}) (是否加)
10
/*jshint -W018 */
if (!!elForm.find('[name="comment[parent_id]"]').val() == true) {
isReplyComment = true;
}