gulp-jshint使用說明


hint是暗示的意思,jshint是什么意思?

1.使用npm安裝

cnpm i --save-dev gulp-jshint jshint

  ps:gulp-jshint和jshnt要一起下載,安裝。

2. 配置文件

有兩種方法:

a> 新建.jshint文件,參考配置如下 :

 

{
  	"undef": true,  // 所有的非全局變量,在使用前必須都被聲明
  	"unused": true, // 所有的變量必須都被使用
  	"predef": [ "MY_GLOBAL" ]  // 這里的變量可以不用檢測是否已經提前聲明
}

 

b> 在package.json 添加jshintConfig選項

{
  "jshintConfig":{
    "undef": true,
    "unused": true,
    "predef": [ "MY_GLOBAL", "ads" ] // 聲明幾個全局變量
  },
  "author": "wenzi",
  "license": "ISC"
}

  參數配置說明(傳送門

 

3.gulpfile.js 配置文件:

var gulp = require('gulp');
var jshint = require('gulp-jshint');

// 建立js任務,進行代碼檢查
gulp.task('js', function(){
	gulp.src('./js/**/*.js')  // 檢查文件:js目錄下所有的js文件
     //.pipe(jshint(jshintConfig)) //根據jshintConfig的規則而檢測 .pipe(jshint()) // 進行檢查 .pipe(jshint.reporter('default')) // 對代碼進行報錯提示 }); gulp.task('default', ['js'])

  

自定義錯誤(引入map-stream模塊)提示:

var mapstream = require( 'map-stream' );

/* file.jshint.success = true; // or false 代碼檢查是否成功 
file.jshint.errorCount = 0; // 錯誤的數量
file.jshint.results = []; // 錯誤的結果集
file.jshint.data = []; // JSHint returns details about implied globals, cyclomatic complexity, etc 
file.jshint.opt = {}; // The options you passed to JSHint 
*/
var myReporter = map(function (file, cb) {
    if (!file.jshint.success) {
        console.log('[ '+file.jshint.errorCount+' errors in ]'+file.path);
        file.jshint.results.forEach(function (err) {
            /*
                err.line 錯誤所在的行號
                err.col  錯誤所在的列號
                err.message/err.reason 錯誤信息
            */
            if (err) {
                console.log(' '+file.path + ': line ' + err.line + ', col ' + err.character + ', code ' + err.code + ', ' + err.reason);
            }
        });
    }
    cb(null, file);
});

gulp.task('jshint', function() {
  return gulp.src('./lib/*.js')  // lib目錄下所有的js文件
    .pipe(jshint())     // js代碼檢查
    .pipe(myReporter);  // 若有錯誤,則調用myReporter進行提示
});

  

 

 

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM