為什么要使用eslint
你在接手一個項目的維護(hù)迭代任務(wù),閱讀代碼的時候是否會因為項目中充斥著各種風(fēng)格的代碼而感到頭疼?沒錯,eslint就是為了解決這類問題
eslint能做什么?
1.代碼風(fēng)格錯誤提示
配置好eslint后,如果代碼風(fēng)格與配置描述的不符,eslint會提示代碼中存在的風(fēng)格問題;一般提示的情形有:
1.編輯器內(nèi),大多數(shù)編輯器配置好后能讀取eslint配置文件并在文件中進(jìn)行相應(yīng)提示
2.eslint-loader配合webpack-dev-server能在頁面中彈出相應(yīng)錯誤內(nèi)容
3.eslint通過命令號對代碼進(jìn)行風(fēng)格檢查
2.修復(fù)相應(yīng)風(fēng)格問題
eslint --fix 命令能修復(fù)一部分代碼風(fēng)格問題;能修復(fù)的范圍見https://cn.eslint.org/docs/rules/中帶工具圖標(biāo)的部分
常見問題
如何在局部禁用eslint
/* eslint-disable no-alert, no-console */alert('foo');console.log('bar');/* eslint-enable no-alert, no-console */以下是詳細(xì)配置
{ root: true,// 直接在根目錄讀取配置文件,能提高eslint性能 "env": { "node": true,// 允許使用nodejs相關(guān)的變量,下同 "es6": true, "browser": true, "commonjs": true }, "extends": "standard", // 繼承eslint-config-standard中的配置,可以在rules中覆蓋 "parser": "babel-eslint", // 為eslint制定parser,默認(rèn)的Esprima只允許已納入es標(biāo)準(zhǔn)的內(nèi)容 "plugins": "vue",// 使用eslint-plugin-vue,使eslint能對vue語法進(jìn)行處理,相應(yīng)rules見https://eslint.vuejs.org/rules/ "rules": { "no-alert": 2, "indent": ["error", 4, { "SwitchCase": 1, "VariableDeclarator": 1, "outerIIFEBody": 1, "MemberExpression": 1, "FunctionDeclaration": { "parameters": 1, "body": 1 }, "FunctionExpression": { "parameters": 1, "body": 1 }, "CallExpression": { "arguments": 1 }, "ArrayExpression": 1, "ObjectExpression": 1, "ImportDeclaration": 1, "flatTernaryExpressions": false, "ignoreComments": false }] }}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。
新聞熱點
疑難解答