ESLint使用說明

本文以使用eslint:recommended 和 eslint-config-airbnb-base 為例,介紹ESlint的使用。

入門HelloWorld

  • 安裝 ESLint
npm install -g eslint 
  • ESLint配置
    新建ESLint文件 .eslintrc.js :
module.exports = {
  extends: 'eslint:recommended',
};

執(zhí)行命令:

# ./app是應(yīng)用規(guī)則的程序路徑,--ext執(zhí)行程序文件后綴名
eslint ./app --ext .js

配置文件說明

parserOptions

  • ecmaVersion:指定ECMAScript版本,默認為5
  • sourceType:設(shè)置為 "script" (默認) 或 "module"
  • ecmaFeatures:
    • jsx:true 啟用jsx

.eslintrc.json示例:

{
    "parserOptions": {
        "ecmaVersion": 6,
        "sourceType": "module",
        "ecmaFeatures": {
            "jsx": true
        }
    },
    "rules": {
        "semi": 2
    }
}

parser

ESLint 默認使用Espree作為其解析器,平常項目中我一般使用babel-eslint作為parser。

{
    "parser": "babel-eslint",
}

env

環(huán)境定義了預(yù)定義的全局變量。

{
    "env": {
        "browser": true,
        "node": true
    }
}

globals

項目中特殊的全局變量

{
    "globals": {
        "var1": true,
        "var2": false
    }
}

plugins

plugin是一個npm包,通常輸出規(guī)則。一些插件也可以輸出一個或多個命名的配置。
ESLint 支持使用第三方插件。在使用插件之前,你必須使用 npm 安裝它。插件名稱可以省略 eslint-plugin- 前綴。

{
    "plugins": [
        "plugin1",
        "eslint-plugin-plugin2"
    ]
}

rules

ESLint 附帶有大量的規(guī)則。你可以通過配置文件修改你項目中使用的規(guī)則。改變一個規(guī)則設(shè)置,你必須設(shè)置規(guī)則 ID 等于這些值之一:
"off" 或 0 - 關(guān)閉規(guī)則
"warn" 或 1 - 開啟規(guī)則,使用警告級別的錯誤:warn (不會導致程序退出)
"error" 或 2 - 開啟規(guī)則,使用錯誤級別的錯誤:error (當被觸發(fā)的時候,程序會退出)

{
    "rules": {
        "eqeqeq": "off",
        "curly": "error",
        "quotes": ["error", "double"]
    }
}

eslint-config-airbnb-base

airbnb通過可共享配置的方式共享他們的eslint 規(guī)則??晒蚕砼渲?是一個npm包,它輸出一個配置對象。
下面我們介紹如何在項目中使用。

  • 安裝
# 首先 通過npm info命令查詢依賴
$ npm info "eslint-config-airbnb-base@latest" peerDependencies
{ eslint: '^4.9.0', 'eslint-plugin-import': '^2.7.0' }

# 根據(jù)查詢結(jié)果手動安裝指定版本的依賴包
npm install eslint@4.9.0 --save-dev
...

# Linux用戶可以直接執(zhí)行,自動完成依賴包安裝
export PKG=eslint-config-airbnb-base;
npm info "$PKG@latest" peerDependencies --json | command sed 's/[\{\},]//g ; s/: /@/g' | xargs npm install --save-dev "$PKG@latest"

# 安裝airbnb配置
npm install eslint-config-airbnb-base@latest
  • 配置 .eslintrc.js
/**
 *
 * eslint 配置
 */
module.exports = {
    "extends": "airbnb-base",
    "parserOptions": {
        "ecmaVersion": 6,
        "sourceType": "module",


    },
    parser: "babel-eslint",
    "env": {
        "browser": true,
        "node": true,
        "es6": true,
        "commonjs": true
    },
    "globals": {},

    "rules": {
        "indent": ["error", 4]
    }
};

  • 命令行檢測
$ ./node_modules/eslint/bin/eslint.js ./client --ext .js

/Users/candice/Development/Web/www-mis/client/index.js
  6:1  error  Too many blank lines at the end of file. Max of 1 allowed  no-multiple-empty-lines

? 1 problem (1 error, 0 warnings)
  1 error, 0 warnings potentially fixable with the `--fix` option.
  • webpack集成
    如果有babel-loader,應(yīng)當先執(zhí)行eslint-loader再進行babel-loader。
    module: {
        rules: [
            {
                test: /\.js$/,
                include: [
                    path.resolve(__dirname, '../public'),
                    path.resolve(__dirname, '../common'),
                    path.resolve(__dirname, '../app'),
                ],
                loader: ['babel-loader','eslint-loader'], //執(zhí)行順序從右往左
            },
  • 例外
    凡事總有例外。編碼時盡管懷著嚴格遵守規(guī)則的愿景,但是實際情況往往會有例外。ESLint提供多種臨時禁用規(guī)則的方式。
/* eslint-disable */

alert('foo');

/* eslint-enable */
/* eslint-disable no-alert, no-console */

alert('foo');
console.log('bar');

/* eslint-enable no-alert, no-console */
/* eslint-disable */

alert('foo');
/* eslint-disable no-alert */

// Disables no-alert for the rest of the file
alert('foo');
alert('foo'); // eslint-disable-line

// eslint-disable-next-line
alert('foo');
alert('foo'); // eslint-disable-line no-alert

// eslint-disable-next-line no-alert
alert('foo');
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ESLint 配置 ESlint 被設(shè)計為完全可配置的,這意味著你可以關(guān)閉每一個規(guī)則而只運行基本語法驗證,或混合和...
    靜默虛空閱讀 41,855評論 3 14
  • 前言 webpack2和vue2已經(jīng)不是新鮮東西了,滿大街的文章在講解webpack和vue,但是很多內(nèi)容寫的不是...
    技術(shù)宅小青年閱讀 6,693評論 4 43
  • 寫在開頭 先說說為什么要寫這篇文章, 最初的原因是組里的小朋友們看了webpack文檔后, 表情都是這樣的: (摘...
    Lefter閱讀 5,452評論 4 31
  • GitChat技術(shù)雜談 前言 本文較長,為了節(jié)省你的閱讀時間,在文前列寫作思路如下: 什么是 webpack,它要...
    蕭玄辭閱讀 12,916評論 7 110
  • EsLint入門學習整理 這兩天因為公司要求,就對ESLint進行了初步的了解,網(wǎng)上的內(nèi)容基本上都差不多,但是內(nèi)容...
    點柈閱讀 26,244評論 3 42

友情鏈接更多精彩內(nèi)容