vue3+ts+EsLint+Prettier規(guī)范代碼的方法實(shí)現(xiàn)
本文主要介紹在Vue3中使用TypeScript做開(kāi)發(fā)時(shí),如何安裝與配置EsLint和Prettier,以提高編碼規(guī)范。
(1)EsLint 提供編碼規(guī)范;
(2)Prettier 是一個(gè) Opinionated 的代碼格式化工具。
使用
EsLint的使用
安裝依賴(lài)
npm i -D eslint eslint-plugin-vue @typescript-eslint/parser @typescript-eslint/eslint-plugin
這四個(gè)依賴(lài)分別是:
- - `eslint`: EsLint的核心代碼
- - `eslint-plugin-vue`:[為Vue使用Eslint的插件](https://eslint.vuejs.org/)
- - `@typescript-eslint/parser`:ESLint的解析器,用于解析typescript,從而檢查和規(guī)范Typescript代碼
- - `@typescript-eslint/eslint-plugin`:這是一個(gè)ESLint插件,包含了各類(lèi)定義好的檢測(cè)Typescript代碼的規(guī)范
添加配置文件
npx eslint --init
根目錄下增加.eslintrc.js文件。(建議選擇js文件,json不可以寫(xiě)注釋?zhuān)?修改配置文件
主要是修改rules中的相關(guān)配置,具體可查看官方配置
/*!
* https://eslint.bootcss.com/docs/rules/
* https://eslint.vuejs.org/rules/
*
* - 0: off
* - 1: warn
* - 2: error
*/
module.exports = {
root: true,
env: {
browser: true,
node: true,
es6: true
},
parser: 'vue-eslint-parser',
parserOptions: {
parser: '@typescript-eslint/parser',
ecmaVersion: 2020,
sourceType: 'module',
jsxPragma: 'React',
ecmaFeatures: {
jsx: true
}
},
globals: {
AMap: false,
AMapUI: false
},
extends: [
'plugin:vue/vue3-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'plugin:prettier/recommended'
],
rules: {
'@typescript-eslint/ban-ts-ignore': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-empty-function': 'off',
'vue/custom-event-name-casing': 'off',
'no-use-before-define': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/ban-types': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-unused-vars': [
'error',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_'
}
],
'no-unused-vars': [
'error',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_'
}
],
'space-before-function-paren': 'off',
'vue/name-property-casing': ['error', 'PascalCase'], // vue/component-definition-name-casing 對(duì)組件定義名稱(chēng)強(qiáng)制使用特定的大小
'vue/attributes-order': 'off',
'vue/one-component-per-file': 'off',
'vue/html-closing-bracket-newline': 'off',
'vue/max-attributes-per-line': 'off',
'vue/multiline-html-element-content-newline': 'off',
'vue/singleline-html-element-content-newline': 'off',
'vue/attribute-hyphenation': 'off',
'vue/require-default-prop': 'off',
'vue/script-setup-uses-vars': 'off',
'vue/html-self-closing': [
'error',
{
html: {
void: 'always',
normal: 'never',
component: 'always'
},
svg: 'always',
math: 'always'
}
]
}
}
Prettier的使用
安裝依賴(lài)
npm i --save-dev prettier eslint-config-prettier eslint-plugin-prettier
這三個(gè)依賴(lài)分別是:
- - `prettier`:prettier插件的核心代碼
- - `eslint-config-prettier`:解決ESLint中的樣式規(guī)范和prettier中樣式規(guī)范的沖突,以prettier的樣式規(guī)范為準(zhǔn),使ESLint中的樣式規(guī)范自動(dòng)失效
- - `eslint-plugin-prettier`:將prettier作為ESLint規(guī)范來(lái)使用
添加配置文件
在項(xiàng)目的根目錄下創(chuàng)建`.prettierrc.js`文件,并添加如下配置
module.exports = {
printWidth: 120, // 換行字符串閾值
tabWidth: 2, // 設(shè)置工具每一個(gè)水平縮進(jìn)的空格數(shù)
useTabs: false,
semi: false, // 句末是否加分號(hào)
vueIndentScriptAndStyle: true,
singleQuote: true, // 用單引號(hào)
trailingComma: 'none', // 最后一個(gè)對(duì)象元素加逗號(hào)
bracketSpacing: true, // 對(duì)象,數(shù)組加空格
jsxBracketSameLine: true, // jsx > 是否另起一行
arrowParens: 'always', // (x) => {} 是否要有小括號(hào)
requirePragma: false, // 不需要寫(xiě)文件開(kāi)頭的 @prettier
insertPragma: false // 不需要自動(dòng)在文件開(kāi)頭插入 @prettier
}
將Prettier添加到EsLint中
修改`.eslintrc.js`文件,在extends中增加
'prettier',
'plugin:prettier/recommended'
其中:
- - `prettier/@typescript-eslint`:使得@typescript-eslint中的樣式規(guī)范失效,遵循prettier中的樣式規(guī)范
- - `plugin:prettier/recommended`:使用prettier中的樣式規(guī)范,且如果使得ESLint會(huì)檢測(cè)prettier的格式問(wèn)題,同樣將格式問(wèn)題以error的形式拋出
使用husky和lint-staged構(gòu)建代碼
安裝依賴(lài)
npm i --save-dev husky lint-staged
修改package.json
添加以下代碼
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"src*/**/*.ts": [
"prettier --config .prettierrc.js --write",
"eslint",
"git add"
],
"src*/**/*.json": [
"prettier --config .prettierrc.js --write",
"eslint",
"git add"
]
}
這樣,在執(zhí)行g(shù)it commit時(shí),EsLint會(huì)檢查提交的代碼。
增加setting.json配置
在.vscode文件夾中增加`setting.json`配置文件,用于自動(dòng)保存時(shí),自動(dòng)修復(fù)及檢驗(yàn)代碼。
{
"typescript.tsdk": "./node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true,
"volar.tsPlugin": true,
"volar.tsPluginStatus": false,
//===========================================
//============= Editor ======================
//===========================================
"explorer.openEditors.visible": 0,
"editor.tabSize": 2,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"diffEditor.ignoreTrimWhitespace": false,
//===========================================
//============= Other =======================
//===========================================
"breadcrumbs.enabled": true,
"open-in-browser.default": "chrome",
//===========================================
//============= files =======================
//===========================================
"files.eol": "\n",
"search.exclude": {
"**/node_modules": true,
"**/*.log": true,
"**/*.log*": true,
"**/bower_components": true,
"**/dist": true,
"**/elehukouben": true,
"**/.git": true,
"**/.gitignore": true,
"**/.svn": true,
"**/.DS_Store": true,
"**/.idea": true,
"**/.vscode": false,
"**/yarn.lock": true,
"**/tmp": true,
"out": true,
"dist": true,
"node_modules": true,
"CHANGELOG.md": true,
"examples": true,
"res": true,
"screenshots": true,
"yarn-error.log": true,
"**/.yarn": true
},
"files.exclude": {
"**/.cache": true,
"**/.editorconfig": true,
"**/.eslintcache": true,
"**/bower_components": true,
"**/.idea": true,
"**/tmp": true,
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true
},
"files.watcherExclude": {
"**/.git/objects/**": true,
"**/.git/subtree-cache/**": true,
"**/.vscode/**": true,
"**/node_modules/**": true,
"**/tmp/**": true,
"**/bower_components/**": true,
"**/dist/**": true,
"**/yarn.lock": true
},
"stylelint.enable": true,
"stylelint.packageManager": "yarn",
"liveServer.settings.donotShowInfoMsg": true,
"telemetry.enableCrashReporter": false,
"workbench.settings.enableNaturalLanguageSearch": false,
"path-intellisense.mappings": {
"/@/": "${workspaceRoot}/src"
},
"prettier.requireConfig": true,
"typescript.updateImportsOnFileMove.enabled": "always",
"workbench.sideBar.location": "left",
"[javascriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[html]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[css]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[less]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[scss]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[markdown]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"[vue]": {
"editor.codeActionsOnSave": {
"source.fixAll.eslint": false
}
},
"cSpell.words": [
"vben",
"windi",
"browserslist",
"tailwindcss",
"esnext",
"antv",
"tinymce",
"qrcode",
"sider",
"pinia",
"sider",
"nprogress"
]
}
參考資料
Prettier官網(wǎng)
EsLint官網(wǎng)
EsLint Rules
Prettier看這一篇就行了
使用EsLint+Prettier規(guī)范TypeScript代碼
到此這篇關(guān)于vue3+ts+EsLint+Prettier規(guī)范代碼的方法實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)vue3 ts 規(guī)范代碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue模板配置與webstorm代碼格式規(guī)范設(shè)置
- 代碼規(guī)范需要防微杜漸code?review6個(gè)小錯(cuò)誤糾正
- Vue3如何通過(guò)ESLint校驗(yàn)代碼是否符合規(guī)范詳解
- vue2項(xiàng)目增加eslint配置代碼規(guī)范示例
- VS?Code?常用自定義配置代碼規(guī)范保存自動(dòng)格式化
- ESLint規(guī)范TypeScript代碼使用方法
- TypeScript開(kāi)發(fā)中規(guī)范編碼來(lái)提高代碼的可讀性
- 如何編寫(xiě)高質(zhì)量的前端代碼(快手電商前端前端代碼規(guī)范)
相關(guān)文章
vue webpack打包后圖片路徑錯(cuò)誤的完美解決方法
這篇文章主要介紹了vue webpack打包后圖片路徑錯(cuò)誤的解決方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-12-12
vue,angular,avalon這三種MVVM框架優(yōu)缺點(diǎn)
本文給大家具體分析了下vue,angular,avalon這三種MVVM框架優(yōu)缺點(diǎn),十分的細(xì)致全面,有需要的小伙伴可以參考下2016-04-04
vue keep-alive的簡(jiǎn)單總結(jié)
這篇文章主要介紹了vue中的keep-alive的相關(guān)資料,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2021-01-01
Vue0.1的過(guò)濾代碼如何添加到Vue2.0直接使用
Vue0.1的過(guò)濾代碼如何添加到Vue2.0直接使用,這篇文章主要介紹了過(guò)濾代碼添加到Vue2.0用的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
基于Vue實(shí)現(xiàn)平滑過(guò)渡的拖拽排序功能
這篇文章主要介紹了vue實(shí)現(xiàn)平滑過(guò)渡的拖拽排序功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
Vue.js每天必學(xué)之計(jì)算屬性computed與$watch
Vue.js每天必學(xué)之計(jì)算屬性computed與$watch,為大家詳細(xì)講解計(jì)算屬性computed與$watch,感興趣的小伙伴們可以參考一下2016-09-09

