【eslint prettier 】webstorm使用eslint 和prettier格式化代碼

強(qiáng)迫癥。想統(tǒng)一一下代碼格式

步驟 安裝 eslint安裝 prettier修改他們的配置文件設(shè)置webstorm

一、安裝環(huán)境

1.1 項目下安裝 eslint

npm install eslint

1.2 安裝 eslint-config-ali

npm install --save-dev eslint-config-ali eslint@^6.8.0 eslint-plugin-import@^2.19.1 eslint-plugin-vue@^6.0.1 vue-eslint-parser@^7.0.0

1.3 項目下安裝 prettiereslint-plugin-prettier

npm install --save-dev eslint-plugin-prettier 
npm install --save-dev --save-exact prettier

npm install --save-dev eslint-config-prettier

2. 修改.eslintrc.js

extends: [
  'eslint-config-ali/vue',
  'plugin:prettier/recommended'
],

2.1. 根目錄添加文件 .prettierrc.js

module.exports = {
  printWidth: 80,
  semi: true,
  singleQuote: true,
  trailingComma: 'none',
  bracketSpacing: true,
  jsxBracketSameLine: false,
  arrowParens: 'avoid',
  requirePragma: false,
  proseWrap: 'preserve'
};

3.依賴參考

image.png

二、開始設(shè)置webstorm

1. 設(shè)置eslint(注意要下eslint的插件)

image.png

2. 設(shè)置prettier

image.png

三、FAQ

現(xiàn)在在文件下Ctrl+S就會自己格式化代碼,不需要fix file

image.png

若出現(xiàn)以上錯誤可以設(shè)置 .eslintrc.js

rules: {
  'vue/comment-directive': 0,
  'vue/no-unused-components': 0,
  'vue/no-use-v-if-with-v-for': 0,
  'vue/require-prop-type-constructor': 0,
  'vue/use-v-on-exact': 0,
}

.eslintrc.js 參考

module.exports = {
  root: true,
  env: {
    node: true
  },

  extends: ['eslint-config-ali/vue', 'plugin:prettier/recommended'],
  rules: {
    'vue/comment-directive': 0,
    'vue/no-unused-components': 0,
    'vue/no-use-v-if-with-v-for': 0,
    'vue/require-prop-type-constructor': 0,
    'vue/use-v-on-exact': 0,
    // 只使用單引號
    quotes: [1, 'single']
  }
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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