gulp-jshint使用說明

來源:互聯網
上載者:User

標籤:代碼   oba   image   repo   for   asc   com   author   maps   

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

1.使用npm安裝

cnpm i --save-dev gulp-jshint jshint

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

2. 設定檔

有兩種方法:

a> 建立.sjhint檔案,參考配置如下 :

 

{  "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進行提示});

  

 

 

 

  

 

gulp-jshint使用說明

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.