您當前的位置:首頁 > 舞蹈

在 Vue3 + Vite + TS 專案中配置 ESLint,讓 VSCode 編輯器自動修復錯誤

作者:由 ejtoia 發表于 舞蹈時間:2021-12-28

我透過

create-vite

腳手架建立的 Vue3 + TS 模板專案中沒有預設整合 ESLint 程式碼檢查工具。

透過查閱 ESLint 官方文件和其他相關的部落格後,我對 Vue3 + TS 專案從零配置 ESLint 寫了一篇總結。

建立 Vue3 + TS 專案

Vite 需要 Node。js 版本 >= 12。0。0

透過下面命令快速建立一個支援 Vue3 + TS 的專案模板:

yarn

create

vite

my

-

vue

-

app

——

template

vue

-

ts

安裝 npm 包

透過 npm 或 yarn(推薦)包管理工具安裝以下軟體包,部分包指定了版本號,主要避免版本過高和其他包不相容,出現一些奇怪的問題。

yarn

add

eslint

@

7。2。0

eslint

-

plugin

-

vue

@

7。20。0

vue

-

eslint

-

parser

@

typescript

-

eslint

/

parser

@

typescript

-

eslint

/

eslint

-

plugin

eslint

-

config

-

airbnb

-

base

@

14。2。1

eslint

-

plugin

-

import

-

D

eslint@7。2。0

eslint-plugin-vue@7。20。0

vue-eslint-parser

@typescript-eslint/parser

@typescript-eslint/eslint-plugin

eslint-config-airbnb-base@14。2。1

eslint-plugin-import

新增 .eslintrc.js 配置檔案

ESLint 支援好幾種格式的配置檔案,本篇文章中使用

。eslintrc。js

檔案格式 。

在專案的根目錄下建立

。eslintrc。js

配置檔案,並新增下面配置項:

module

exports

=

{

root

true

globals

{

defineEmits

‘readonly’

defineProps

‘readonly’

},

extends

‘plugin:@typescript-eslint/recommended’

‘plugin:vue/vue3-recommended’

‘airbnb-base’

],

parserOptions

{

parser

‘@typescript-eslint/parser’

ecmaVersion

2020

},

rules

rules

{

‘no-debugger’

process

env

NODE_ENV

===

‘production’

‘warn’

‘off’

// 禁用 debugger

‘no-console’

process

env

NODE_ENV

===

‘production’

‘warn’

‘off’

// 禁用 console

‘no-bitwise’

‘off’

// 禁用按位運算子

‘no-tabs’

‘off’

// 禁用 tab

‘array-element-newline’

‘error’

‘consistent’

],

// 強制陣列元素間出現換行

indent

‘error’

2

{

MemberExpression

0

SwitchCase

1

ignoredNodes

‘TemplateLiteral’

},

],

// 強制使用一致的縮排

quotes

‘error’

‘single’

],

// 強制使用一致的反勾號、雙引號或單引號

‘comma-dangle’

‘error’

‘always-multiline’

],

// 要求或禁止末尾逗號

‘object-curly-spacing’

‘error’

‘always’

],

// 強制在大括號中使用一致的空格

‘max-len’

‘error’

120

],

// 強制一行的最大長度

‘no-new’

‘off’

// 禁止使用 new 以避免產生副作用

‘linebreak-style’

‘off’

// 強制使用一致的換行風格

‘import/extensions’

‘off’

// 確保在匯入路徑中統一使用副檔名

‘eol-last’

‘off’

// 要求或禁止檔案末尾存在空行

‘no-shadow’

‘off’

// 禁止變數宣告與外層作用域的變數同名

‘no-unused-vars’

‘warn’

// 禁止出現未使用過的變數

‘import/no-cycle’

‘off’

// 禁止一個模組匯入一個有依賴路徑的模組回到自己身上

‘arrow-parens’

‘off’

// 要求箭頭函式的引數使用圓括號

semi

‘error’

‘never’

],

// 要求或禁止使用分號代替 ASI

eqeqeq

‘off’

// 要求使用 === 和 !==

‘no-param-reassign’

‘off’

// 禁止對 function 的引數進行重新賦值

‘import/prefer-default-export’

‘off’

// 如果模組只輸入一個名字,則傾向於預設輸出

‘no-use-before-define’

‘off’

// 禁止在變數定義之前使用它們,則傾向於預設輸出

‘no-continue’

‘off’

// 禁用 continue 語句

‘prefer-destructuring’

‘off’

// 優先使用陣列和物件解構

‘no-plusplus’

‘off’

// 禁用一元運算子 ++ 和 ——

‘prefer-const’

‘warn’

// 要求使用 const 宣告那些聲明後不再被修改的變數

‘global-require’

‘off’

// 要求 require() 出現在頂層模組作用域中

‘no-prototype-builtins’

‘off’

// 禁止直接呼叫 Object。prototypes 的內建屬性

‘consistent-return’

‘off’

// 要求 return 語句要麼總是指定返回的值,要麼不指定

‘one-var-declaration-per-line’

‘off’

// 要求或禁止在變數宣告周圍換行

‘one-var’

‘off’

// 強制函式中的變數要麼一起宣告要麼分開宣告

‘import/named’

‘off’

// 確保命名匯入與遠端檔案中的命名匯出相對應

‘object-curly-newline’

‘off’

// 強制大括號內換行符的一致性

‘default-case’

‘off’

// 要求 switch 語句中有 default 分支

‘no-trailing-spaces’

‘off’

// 禁用行尾空格

‘func-names’

‘off’

// 要求或禁止使用命名的 function 表示式

radix

‘off’

// 強制在 parseInt() 使用基數引數

‘no-unused-expressions’

‘off’

// 禁止出現未使用過的表示式

‘no-underscore-dangle’

‘off’

// 禁止識別符號中有懸空下劃線

‘no-nested-ternary’

‘off’

// 禁用巢狀的三元表示式

‘no-restricted-syntax’

‘off’

// 禁用特定的語法

‘no-await-in-loop’

‘off’

// 禁止在迴圈中出現 await

‘import/no-extraneous-dependencies’

‘off’

// 禁止使用外部包

‘import/no-unresolved’

‘off’

// 確保匯入指向一個可以解析的檔案/模組

‘template-curly-spacing’

‘error’

‘always’

],

// 要求或禁止模板字串中的嵌入表示式周圍空格的使用

‘@typescript-eslint/no-var-requires’

‘off’

// 除import語句外,禁止使用require語句

‘@typescript-eslint/no-empty-function’

‘off’

// 不允許空函式

‘@typescript-eslint/no-explicit-any’

‘off’

// 禁止使用 any 型別

‘guard-for-in’

‘off’

// 要求 for-in 迴圈中有一個 if 語句

‘class-methods-use-this’

‘off’

// 強制類方法使用 this

‘vue/html-indent’

‘error’

2

],

// 在