欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue3+ts+EsLint+Prettier規(guī)范代碼的方法實(shí)現(xiàn)

 更新時間:2021年10月25日 15:23:52   作者:開心就是最好  
本文主要介紹在Vue3中使用TypeScript做開發(fā)時,如何安裝與配置EsLint和Prettier,以提高編碼規(guī)范。感興趣的可以了解一下

本文主要介紹在Vue3中使用TypeScript做開發(fā)時,如何安裝與配置EsLint和Prettier,以提高編碼規(guī)范。
(1)EsLint 提供編碼規(guī)范;
(2)Prettier 是一個 Opinionated 的代碼格式化工具。

使用

EsLint的使用

安裝依賴

npm i -D eslint eslint-plugin-vue @typescript-eslint/parser @typescript-eslint/eslint-plugin

這四個依賴分別是:

  • - `eslint`: EsLint的核心代碼
  • - `eslint-plugin-vue`:[為Vue使用Eslint的插件](https://eslint.vuejs.org/)
  • - `@typescript-eslint/parser`:ESLint的解析器,用于解析typescript,從而檢查和規(guī)范Typescript代碼
  • - `@typescript-eslint/eslint-plugin`:這是一個ESLint插件,包含了各類定義好的檢測Typescript代碼的規(guī)范

添加配置文件

npx eslint --init

根目錄下增加.eslintrc.js文件。(建議選擇js文件,json不可以寫注釋) 修改配置文件
主要是修改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 對組件定義名稱強(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的使用

安裝依賴

npm i --save-dev prettier eslint-config-prettier eslint-plugin-prettier

這三個依賴分別是:

  • - `prettier`:prettier插件的核心代碼
  • - `eslint-config-prettier`:解決ESLint中的樣式規(guī)范和prettier中樣式規(guī)范的沖突,以prettier的樣式規(guī)范為準(zhǔn),使ESLint中的樣式規(guī)范自動失效
  • - `eslint-plugin-prettier`:將prettier作為ESLint規(guī)范來使用

添加配置文件

在項目的根目錄下創(chuàng)建`.prettierrc.js`文件,并添加如下配置

module.exports = {
  printWidth: 120, // 換行字符串閾值
  tabWidth: 2, // 設(shè)置工具每一個水平縮進(jìn)的空格數(shù)
  useTabs: false,
  semi: false, // 句末是否加分號
  vueIndentScriptAndStyle: true,
  singleQuote: true, // 用單引號
  trailingComma: 'none', // 最后一個對象元素加逗號
  bracketSpacing: true, // 對象,數(shù)組加空格
  jsxBracketSameLine: true, // jsx > 是否另起一行
  arrowParens: 'always', // (x) => {} 是否要有小括號
  requirePragma: false, // 不需要寫文件開頭的 @prettier
  insertPragma: false // 不需要自動在文件開頭插入 @prettier
}

將Prettier添加到EsLint中

修改`.eslintrc.js`文件,在extends中增加

    'prettier',
    'plugin:prettier/recommended'

其中:

  • - `prettier/@typescript-eslint`:使得@typescript-eslint中的樣式規(guī)范失效,遵循prettier中的樣式規(guī)范
  • - `plugin:prettier/recommended`:使用prettier中的樣式規(guī)范,且如果使得ESLint會檢測prettier的格式問題,同樣將格式問題以error的形式拋出

使用husky和lint-staged構(gòu)建代碼

安裝依賴

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時,EsLint會檢查提交的代碼。

 增加setting.json配置

在.vscode文件夾中增加`setting.json`配置文件,用于自動保存時,自動修復(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue webpack打包后圖片路徑錯誤的完美解決方法

    vue webpack打包后圖片路徑錯誤的完美解決方法

    這篇文章主要介紹了vue webpack打包后圖片路徑錯誤的解決方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-12-12
  • vue,angular,avalon這三種MVVM框架優(yōu)缺點(diǎn)

    vue,angular,avalon這三種MVVM框架優(yōu)缺點(diǎn)

    本文給大家具體分析了下vue,angular,avalon這三種MVVM框架優(yōu)缺點(diǎn),十分的細(xì)致全面,有需要的小伙伴可以參考下
    2016-04-04
  • vue keep-alive的簡單總結(jié)

    vue keep-alive的簡單總結(jié)

    這篇文章主要介紹了vue中的keep-alive的相關(guān)資料,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2021-01-01
  • Vue可左右滑動按鈕組組件使用詳解

    Vue可左右滑動按鈕組組件使用詳解

    這篇文章主要為大家詳細(xì)介紹了基于Vue可左右滑動按鈕組組件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 詳解vue-amap引入高德JS API的原理

    詳解vue-amap引入高德JS API的原理

    vue-amap是對高德地圖JS API進(jìn)行封裝的、適用于vue項目的地圖組件庫,本文主要介紹了vue-amap引入高德JS API的原理,具有一定的參考價值,感興趣的可以了解一下
    2022-06-06
  • 簡單的Vue SSR的示例代碼

    簡單的Vue SSR的示例代碼

    本篇文章主要介紹了簡單的 Vue SSR的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • 在vscode里使用.vue代碼模板的方法

    在vscode里使用.vue代碼模板的方法

    本篇文章主要介紹了在vscode里使用.vue代碼模板的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • Vue0.1的過濾代碼如何添加到Vue2.0直接使用

    Vue0.1的過濾代碼如何添加到Vue2.0直接使用

    Vue0.1的過濾代碼如何添加到Vue2.0直接使用,這篇文章主要介紹了過濾代碼添加到Vue2.0用的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • 基于Vue實(shí)現(xiàn)平滑過渡的拖拽排序功能

    基于Vue實(shí)現(xiàn)平滑過渡的拖拽排序功能

    這篇文章主要介紹了vue實(shí)現(xiàn)平滑過渡的拖拽排序功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-06-06
  • Vue.js每天必學(xué)之計算屬性computed與$watch

    Vue.js每天必學(xué)之計算屬性computed與$watch

    Vue.js每天必學(xué)之計算屬性computed與$watch,為大家詳細(xì)講解計算屬性computed與$watch,感興趣的小伙伴們可以參考一下
    2016-09-09

最新評論