首页 > 其他分享 >eslint版本9.0之后配置方法

eslint版本9.0之后配置方法

时间:2024-10-17 10:10:47浏览次数:1  
标签:vue js globals eslint 版本 import 9.0 prettier

 

 

  npm init @eslint/config@latest

 

√ How would you like to use ESLint? · problems
√ What type of modules does your project use? · esm
√ Which framework does your project use? · vue
√ Does your project use TypeScript? · javascript
√ Where does your code run? · browser, node
The config that you've selected requires the following dependencies:

[email protected], globals, @eslint/js, eslint-plugin-vue
√ Would you like to install them now? · No / Yes
√ Which package manager do you want to use? · npm
☕️Installing...

prettier安装

此时根目录下会生成eslint.config.js文件,这是eslint最新的配置文件。打开文件,内容如下:

import globals from 'globals'
import pluginJs from '@eslint/js'
import pluginVue from 'eslint-plugin-vue'

export default [
  { files: ['**/*.{js,mjs,cjs,vue}'] },
  { languageOptions: { globals: { ...globals.browser, ...globals.node } } },
  pluginJs.configs.recommended,
  ...pluginVue.configs['flat/essential']
]

到这里,就要开始配置prettier了。

npm i prettier eslint-config-prettier -D

执行安装命令,我们只安装prettier本体和eslint-config-prettier配置。看过上一篇介绍eslint+prettier的同学会发现少了一个eslint-plugin-prettier插件。这里我是通过实验,发现不需要插件,直接可以用本体+配置就可以实现之前的效果。后续如果有什么问题,我会更新。

安装完插件之后,需要手动创建文件.prettierrc。个人比较喜欢的选项如下:

{
  "printWidth": 160,
  "tabWidth": 2,
  "useTabs": false,
  "singleQuote": true,
  "semi": false,
  "trailingComma": "none",
  "arrowParens": "avoid",
  "bracketSpacing": true,
  "singleAttributePerLine": false,
  "endOfLine": "auto"
}

eslint + prettier配置

到最后,这里的配置比上个版本要简单很多。只需要把eslint-config-prettier引入进来即可。

import globals from 'globals'
import pluginJs from '@eslint/js'
import pluginVue from 'eslint-plugin-vue'
import eslintConfigPrettier from 'eslint-config-prettier' // 新增

export default [
  { files: ['**/*.{js,mjs,cjs,vue}'] },
  { languageOptions: { globals: { ...globals.browser, ...globals.node } } },
  pluginJs.configs.recommended,
  ...pluginVue.configs['flat/essential'],
  eslintConfigPrettier // 新增
]

 

vite配置eslint24年4月期,eslint.config.js

import globals from 'globals'
// 预定义配置
import pluginJs from '@eslint/js'
import tseslint from 'typescript-eslint'
import pluginVue from 'eslint-plugin-vue'

// import babelParser from "@typescript-eslint/parser";
import commpnParser from 'vue-eslint-parser'
import prettier from 'eslint-plugin-prettier'

// "@babel/eslint-parser";

// import customConfig from "./esconfig/custom_config.js";

export default [
  // languageOptions:配置如何检查 js 代码
  {
    // 1.1 处理 与 JavaScript 相关的配置项
    // - ecmaVersion
    // - sourceType
    // - globals
    // - parser
    // - parserOptions
    // files: ["**/*.ts", "**/*.vue"],
    // ignores: ["**/*.config.js"],
    ignores: [
      '**/*.config.js',
      'dist/**',
      'node_modules/**',
      '!**/eslint.config.js',
    ],
    languageOptions: {
      // 1.11 定义可用的全局变量
      globals: globals.browser,
      // 1.12 扩展
      // ecmaVersion: "latest",
      // sourceType: "module",
      parser: commpnParser,
      parserOptions: {
        ecmaVersion: 'latest',
        sourceType: 'module',
        parser: '@typescript-eslint/parser',
        jsxPragma: 'React',
        ecmaFeatures: {
          jsx: true,
        },
      },
    },
  },
  // 原来的extends替换为如下模式
  pluginJs.configs.recommended,
  ...tseslint.configs.recommended,
  ...pluginVue.configs['flat/essential'],
  {
    plugins: {
      prettier,
    },
    rules: {
      // 开启这条规则后,会将prettier的校验规则传递给eslint,这样eslint就可以按照prettier的方式来进行代码格式的校验
      'prettier/prettier': 'error',
      // eslint(https://eslint.bootcss.com/docs/rules/)
      'no-var': 'error', // 要求使用 let 或 const 而不是 var
      'no-multiple-empty-lines': ['warn', { max: 1 }], // 不允许多个空行
      'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
      'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
      'no-unexpected-multiline': 'error', // 禁止空余的多行
      'no-useless-escape': 'off', // 禁止不必要的转义字符

      // typeScript (https://typescript-eslint.io/rules)
      '@typescript-eslint/no-unused-vars': 'error', // 禁止定义未使用的变量
      '@typescript-eslint/prefer-ts-expect-error': 'error', // 禁止使用 @ts-ignore
      '@typescript-eslint/no-explicit-any': 'off', // 禁止使用 any 类型
      '@typescript-eslint/no-non-null-assertion': 'off',
      '@typescript-eslint/no-namespace': 'off', // 禁止使用自定义 TypeScript 模块和命名空间。
      '@typescript-eslint/semi': 'off',

      // eslint-plugin-vue (https://eslint.vuejs.org/rules/)
      'vue/multi-word-component-names': 'off', // 要求组件名称始终为 “-” 链接的单词
      'vue/script-setup-uses-vars': 'error', // 防止<script setup>使用的变量<template>被标记为未使用
      'vue/no-mutating-props': 'off', // 不允许组件 prop的改变
      'vue/attribute-hyphenation': 'off', // 对模板中的自定义组件强制执行属性命名样式
    },
  },
]

 

标签:vue,js,globals,eslint,版本,import,9.0,prettier
From: https://www.cnblogs.com/yeminglong/p/18471484

相关文章

  • win11显卡驱动退回上个版本
    前言  windows系统自动更新,会把显卡驱动升级,导致和现有的pytorch环境不兼容,下面是解决方法:解决方法  打开设备管理器,找到显示适配器  右键显卡设备->属性->驱动程序->回退驱动程序->确认,然后重启机器即可  下图是回退后的驱动版本和CUDA版本: ......
  • FL Studio21.2.3.4004中文破解完整版本下载覆盖升级
    ......
  • FL Studio21.2.3.4004最新中文破解版本下载链接
    ......
  • MongoDB安装(新版本保姆级教程)
    下载安装包首先进入官网(社区版),在对应页面选择需要安装的版本(这里下载当前适合版本号)DownloadMongoDBCommunityServer|MongoDB 选择在任一磁盘创建空文件夹(不要使用中文路径),解压之后把文件夹内容剪切进去手动创建data和log两个文件夹找到path,打开路径,将自......
  • 【VMware VCF】更新 VCF 5.1 至 VCF 5.2 版本。
    VMwareCloudFoundation(VCF)是一个由众多产品(vSphere、vSAN以及NSX等)所构成的SDDC解决方案,这些环境中的不同组件的生命周期统一由SDDCManager来进行管理,比如下载修补包、环境预检查、调度组件更新、监控运行报告等。比起传统解决方案来说,VCF环境的生命周期管理要远远复杂......
  • 极狐GitLab 发布安全补丁版本 17.4.2, 17.3.5, 17.2.9
    本分分享极狐GitLab补丁版本17.4.2,17.3.5,17.2.9的详细内容。今天,极狐GitLab专业技术团队正式发布了17.4.2,17.3.5,17.2.9版本。这几个版本包含重要的缺陷和安全修复代码,我们强烈建议所有私有化部署用户应该立即升级到上述的某一个版本。对于极狐GitLabSaaS,技术团队......
  • Excelize 开源基础库 2.9.0 版本正式发布
    Excelize是Go语言编写的用于操作OfficeExcel文档基础库,基于ECMA-376,ISO/IEC29500国际标准。可以使用它来读取、写入由Excel、WPS、OpenOffice等办公软件创建的电子表格文档。支持XLAM/XLSM/XLSX/XLTM/XLTX等多种文档格式,高度兼容带有样式、图片(表)、透视表......
  • 【Azure Developer】com.azure:azure-identity jar包版本从1.2.0 升级到1.12.2 版本之
    问题描述com.azure:azure-identityjar包版本从1.2.0升级到1.12.2版本之后报错,错误信息如下:Anattemptwasmadetocallamethodthatdoesnotexist.Theattemptwasmadefromthefollowinglocation:  com.azure.identity.implementation.IdentityClientBase.getConf......
  • 【Azure Developer】com.azure:azure-identity jar包版本从1.2.0 升级到1.12.2 版本之
    问题描述com.azure:azure-identityjar包版本从1.2.0升级到1.12.2版本之后报错,错误信息如下:Anattemptwasmadetocallamethodthatdoesnotexist.Theattemptwasmadefromthefollowinglocation:  com.azure.identity.implementation.IdentityClientBase.get......
  • 关于最新版本mysql9,使用Kettle连接Mysql 9 报错,驱动问题
    使用kettle连接mysql时,报“Driverclassorg.gjt.mm.mysql.Drivercouldnotbefound”错,没有需要的connector包,在Mysql官网下载了最新的connector的jar包,并将其放在如下图的Kettle所示的目录中:重试发现仍然连接失败。这时候对比了网上大家的解决方法,发现我所下载的最新conne......