如何在VUE專案中新增ESLint
1. 首先在專案的根目錄下 新建 .eslintrc.js檔案,其配置規則可以如下:(自己小整理了一份),所有的程式碼如下:
// https://eslint.org/docs/user-guide/configuring module.exports = { //此項是用來告訴eslint找當前配置檔案不能往父級查詢 root: true, //此項是用來指定eslint解析器的,解析器必須符合規則,babel-eslint解析器是對babel解析器的包裝使其與ESLint解析 parser: 'babel-eslint', //此項是用來指定javaScript語言型別和風格,sourceType用來指定js匯入的方式,預設是script,此處設定為module,指某塊匯入方式 parserOptions: { // 設定 script(預設) 或 module,如果程式碼是在ECMASCRIPT中的模組 sourceType: 'module', "ecmaVersion": 6, "ecmaFeatures": { "jsx": true } }, // 此項指定環境的全域性變數,下面的配置指定為瀏覽器環境 env: { "browser": true, "node": true, "commonjs": true, "es6": true, "amd": true }, // https://github.com/standard/standard/blob/master/docs/RULES-en.md // 此項是用來配置標準的js風格,就是說寫程式碼的時候要規範的寫,如果你使用vs-code我覺得應該可以避免出錯 extends: 'vue', // 此項是用來提供外掛的,外掛名稱省略了eslint-plugin-,下面這個配置是用來規範html的 plugins: [ 'html', "flow-vars", "react" ], /* 下面這些rules是用來設定從外掛來的規範程式碼的規則,使用必須去掉字首eslint-plugin- 主要有如下的設定規則,可以設定字串也可以設定數字,兩者效果一致 "off" -> 0 關閉規則 "warn" -> 1 開啟警告規則 "error" -> 2 開啟錯誤規則 */ rules: { // 不需要 "space-before-function-paren": 0, // 函式定義時括號前面要不要有空格 "eol-last": 0, // 檔案以單一的換行符結束 "no-extra-semi": 0, // 可以多餘的冒號 "semi": 0, // 語句可以不需要分號結尾 "eqeqeq": 0, // 必須使用全等 "one-var": 0, // 連續宣告 "no-undef": 0, // 可以 有未定義的變數 // 警告 "no-extra-boolean-cast": 1, // 不必要的bool轉換 "no-extra-parens": 1, // 非必要的括號 "no-empty": 1, // 塊語句中的內容不能為空 "no-use-before-define": [1, "nofunc"], // 未定義前不能使用 "complexity": [1, 10], // 迴圈複雜度 "no-unused-vars": 1, // 不能有宣告後未被使用的變數或引數 // vue "flow-vars/define-flow-type": 1, "flow-vars/use-flow-type": 1, // react "react/jsx-uses-react": 2, "react/jsx-uses-vars": 2, // 錯誤 "comma-dangle": [2, "never"], // 物件字面量項尾不能有逗號 "no-debugger": 2, // 禁止使用debugger "no-constant-condition": 2, // 禁止在條件中使用常量表示式 if(true) if(1) "no-dupe-args": 2, // 函式引數不能重複 "no-dupe-keys": 2, // 在建立物件字面量時不允許鍵重複 {a:1,a:1} "no-duplicate-case": 2, // switch中的case標籤不能重複 "no-empty-character-class": 2, // 正規表示式中的[]內容不能為空 "no-invalid-regexp": 2, // 禁止無效的正規表示式 "no-func-assign": 2, // 禁止重複的函式宣告 "valid-typeof": 2, // 必須使用合法的typeof的值 "no-unreachable": 2, // 不能有無法執行的程式碼 "no-unexpected-multiline": 2, // 避免多行表示式 "no-sparse-arrays": 2, // 禁止稀疏陣列, [1,,2] "no-shadow-restricted-names": 2, // 嚴格模式中規定的限制識別符號不能作為宣告時的變數名使用 "no-cond-assign": 2, // 禁止在條件表示式中使用賦值語句 "no-native-reassign": 2, // 不能重寫native物件 // 程式碼風格 "no-else-return": 1, // 如果if語句裡面有return,後面不能跟else語句 "no-multi-spaces": 1, // 不能用多餘的空格 "key-spacing": [1, { // 物件字面量中冒號的前後空格 "beforeColon": false, "afterColon": true }], "block-scoped-var": 2, // 塊語句中使用var "consistent-return": 2, // return 後面是否允許省略 "accessor-pairs": 2, // 在物件中使用getter/setter "dot-location": [2, "property"], // 物件訪問符的位置,換行的時候在行首還是行尾 "no-lone-blocks": 2, // 禁止不必要的巢狀塊 "no-labels": 2, // 禁止標籤宣告 "no-extend-native": 2, // 禁止擴充套件native物件 "no-floating-decimal": 2, // 禁止省略浮點數中的0 .5 3. "no-loop-func": 2, // 禁止在迴圈中使用函式(如果沒有引用外部變數不形成閉包就可以) "no-new-func": 2, // 禁止使用new Function "no-self-compare": 2, // 不能比較自身 "no-sequences": 2, // 禁止使用逗號運算子 "no-throw-literal": 2, // 禁止丟擲字面量錯誤 throw "error"; "no-return-assign": [2, "always"], // return 語句中不能有賦值表示式 "no-redeclare": [2, { // 禁止重複宣告變數 "builtinGlobals": true }], "no-unused-expressions": [2, { // 禁止無用的表示式 "allowShortCircuit": true, "allowTernary": true }], "no-useless-call": 2, // 禁止不必要的call和apply "no-useless-concat": 2, "no-void": 2, // 禁用void操作符 "no-with": 2, // 禁用with "space-infix-ops": 2, // 中綴操作符周圍要不要有空格 "valid-jsdoc": [2, { // jsdoc規則 "requireParamDescription": true, "requireReturnDescription": true }], "no-warning-comments": [2, { // 不能有警告備註 "terms": ["todo", "fixme", "any other term"], "location": "anywhere" }], "curly": 1, // 必須使用 if(){} 中的{} // common js "no-duplicate-imports": 1 } }
2. 進入專案的根目錄後,在命令列中 安裝如下包:
npm install --save-dev babel-eslint eslint eslint-friendly-formatter eslint-loader eslint-plugin-html eslint-config-standard eslint-plugin-promise eslint-plugin-standard eslint-plugin-import eslint-plugin-node eslint-plugin-flow-vars eslint-plugin-react eslint-config-vue eslint-plugin-vue
也可以直接在 package.json 中devDependencies新增如下依賴於包
"babel-eslint": "^7.1.1", "eslint": "^3.19.0", "eslint-config-standard": "^10.2.1", "eslint-friendly-formatter": "^3.0.0", "eslint-loader": "^1.7.1", "eslint-plugin-html": "^3.0.0", "eslint-plugin-import": "^2.7.0", "eslint-plugin-node": "^5.2.0", "eslint-plugin-promise": "^3.4.0", "eslint-plugin-standard": "^3.0.1",
"eslint-plugin-flow-vars": "^0.5.0",
"eslint-plugin-react": "^7.5.1",
"eslint-config-vue": "^2.0.2",
"eslint-plugin-vue": "^2.1.0",
注意 eslint-plugin-html不要選擇預設版本,一定要選擇3.0.0; 如: "eslint-plugin-html": "^3.0.0"
3. 然後在 webpack.base.conf.js 新增如下程式碼:(專案是基於vue-cli中配置的)
/* 對於以.js或.vue字尾結尾的檔案(在src目錄下或test目錄下的檔案),使用eslint進行檔案語法檢測。 */ const createLintingRule = () => ({ test: /\.(js|vue)$/, loader: 'eslint-loader', enforce: 'pre', include: [path.resolve(__dirname, '../src')], options: { formatter: require('eslint-friendly-formatter'), emitWarning: !config.dev.showEslintErrorsInOverlay } }); rules: [ // 在開發環境下 對於以.js或.vue字尾結尾的檔案(在src目錄下或test目錄下的檔案),使用eslint進行檔案語法檢測。 ...(config.dev.useEslint ? [createLintingRule()] : []), { test: /\.vue$/, // vue 檔案字尾的 loader: 'vue-loader', // 使用vue-loader處理 options: vueLoaderConfig // options是對vue-loader做的額外選項配置 檔案配置在 ./vue-loader.conf 內可以檢視程式碼 }, { ..... } ]
4. 最後 npm install 一下,應該就可以了,先把所有的檔案關掉,重新開啟一下 就可以生效了。