当前位置: 首页 > news >正文

eslint扁平化配置

扁平化配置模式Vue ESlint示例配置

以下是一个适用于 Vue 项目的常用 ESLint 配置文件(eslint.config.mjs),支持 Vue 3 和 TypeScript。
编辑器集成建议:
在 VS Code 中安装以下插件:ESLint、Prettier - Code formatter、Volar (Vue 语言支持)。
以下是一个适用于 Vue 项目的常用 ESLint 配置文件(eslint.config.mjs),支持 Vue 3 和 TypeScript。
.vscode\settings.json中添加
{
“editor.defaultFormatter”: “esbenp.prettier-vscode”,
“editor.formatOnSave”: true,
“prettier.requireConfig”: true
}

安装依赖包

npm install -D eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-plugin-vue @vue/eslint-config-typescript 

根目录新建eslint.config.mjs

import vueParser from 'vue-eslint-parser'
import tsParser from '@typescript-eslint/parser'
import vuePlugin from 'eslint-plugin-vue'
import tsPlugin from '@typescript-eslint/eslint-plugin'
import globals from 'globals'export default [{files: ['**/*.vue', '**/*.ts', '**/*.js'],//包含的文件类型/* 针对语言进行配置 */languageOptions: {globals: {...globals.node,//node环境运行...globals.browser,//浏览器环境运行...globals.es2021//es2021环境运行},parser: vueParser, //顶层vue解析器parserOptions: {parser: tsParser,//嵌套typescript解析器ecmaVersion: 'latest',//指定es版本sourceType: 'module',//指定源码类型,模块化// extraFileExtensions: ['.vue']//指定额外的文件扩展名}},plugins: {vue: vuePlugin,//指定vue插件,rules中使用'@typescript-eslint': tsPlugin//指定typescript插件,rules中使用},rules: {'vue/multi-word-component-names': 'off',//关闭vue组件名称中多个单词的限制'vue/require-default-prop': 'off',//关闭vue组件的props必须有默认值'vue/singleline-html-element-content-newline': 'off',//关闭vue单行html元素内容换行限制//限制每行 HTML 元素上允许的最大属性数量//'vue/max-attributes-per-line': [//  'error',//  {//    singleline: 3,// 单行模式最大属性数//    multiline: 1// 多行模式每行1个属性//  }//],'@typescript-eslint/no-explicit-any': 'off',//关闭typescript禁止any类型//需要声明但暂不使用某些参数或变量(如回调函数的未使用参数)// 通过添加前缀 _ 并配置此规则,可避免 ESLint 误报'@typescript-eslint/no-unused-vars': ['error',{ argsIgnorePattern: '^_', //忽略以 _ 下划线开头的函数参数命名检查varsIgnorePattern: '^_' //忽略以 _ 下划线开头的变量命名检查}],'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',//生产环境下关闭console警告'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'//生产环境下关闭debugger警告}},{ignores: ['**/dist', '**/node_modules']}
]

Prettier

若需与eslint配合使用,则在 .vscode/settings.json 中配置自动格式化
安装解决冲突的依赖

npm i eslint-config-prettier eslint-plugin-prettier -D

配套的 Prettier 配置:
在项目根目录创建 .prettierrc.json 与 ESLint 配合使用(json文件中不允许注释):

{semi: true,//在语句末尾自动添加分号singleQuote: true,//强制使用单引号而非双引号包裹字符串,统一代码风格printWidth: 100,//每行代码的最大宽度限制为100字符,超出时将自动换行格式化tabWidth: 2,//缩进宽度设置为2个空格trailingComma: 'none',//禁止在对象或数组最后一项后添加尾随逗号,如 {a:1, b:2} 而非 {a:1, b:2,}arrowParens: 'avoid',//当箭头函数仅有一个参数时省略括号,例如 x => x+1 而非 (x) => x+1endOfLine: 'auto'//自动识别并适配操作系统的换行符(CRLF/LF),确保跨平台一致性
}

运行以下命令检查项目文件是否符合规则

npx prettier --check .

若输出列出未格式化的文件,则说明配置未完全生效‌.
强制格式化测试

npx prettier --write .

文件内容被修改即表示 Prettier 正常工作‌

extends 继承

在 ESLint 扁平化配置中,extends 不能嵌套在其他配置对象内部,必须直接作为顶级属性使用

import js from "@eslint/js";
import pluginVue from "eslint-plugin-vue";
import tsPlugin from '@typescript-eslint/eslint-plugin';export default [js.configs.recommended,...pluginVue.configs['flat/recommended'],...tsPlugin.configs.recommended,{rules: {"vue/multi-word-component-names": "off"}}
];

ignores 继承

扁平化配置要求 ignores 必须作为独立配置对象存在,且需位于数组顶层。否则可能不生效。

// 正确示例(必须作为数组元素独立存在)
import js from "@eslint/js";export default [{"**/node_modules/**","**/dist/**","**/*.config.js","**/*.config.mjs"},js.configs.recommended  // 其他配置
]

globals 全局变量

需确保已安装 globals 包以使用预定义环境变量(如 browser/node)

npm install globals

环境变量

环境变量需通过 languageOptions.globals 显式导入,而非传统配置的 env 字段

import globals from 'globals';export default [{languageOptions: {globals: {...globals.browser,  // 浏览器环境变量(如 window、document)...globals.node      // Node.js 环境变量(如 process、require)}}
}]

全局变量

‌权限标记规范

  • “readonly” 替代旧版 false(禁止覆盖)
  • “writable” 替代旧版 true(允许修改)

类型检查‌:配合 @typescript-eslint 时需在 parserOptions 中同步声明类型
若多个配置存在同名变量,后加载的配置会覆盖前者
修改配置后建议运行 npx eslint --cache --cache-location .eslintcache --fix 确保生效

import globals from 'globals';export default [{languageOptions: {globals: {...globals.browser,  // 继承浏览器环境变量MY_VAR: "writable"   // 自定义变量需明确权限}}
}]

parserOptions 定义代码解析器

parserOptions 的配置方式已从传统的顶层键迁移至 languageOptions 对象内,用于定义代码解析器的相关参数‌的配置方式已从传统的顶层键迁移至 languageOptions 对象内,用于定义代码解析器的相关参数‌。

import tsEslintParser from "@typescript-eslint/parser";
import vueEslintParser from "vue-eslint-parser";export default [{files: ['**/*.vue', '**/*.ts', '**/*.js'],/* 针对语言进行配置 */languageOptions: {/* 指定解析器 */parser: vueEslintParser,//顶层使用 Vue 解析器/* 指定解析器 */parserOptions: {/* 解析器的语法parser配置 */parser: tsEslintParser,// 嵌套指定 TS 解析器project: "./tsconfig.json",  // 建议补充,指定 TS 配置文件extraFileExtensions: ['.vue']  // 可不配置,包含 Vue 文件类型sourceType: "module",// 模块类型ecmaVersion: "latest", // ECMAScript 版本    ecmaFeatures: { jsx: true }  // 启用 JSX 支持},}
}]

代码中需要安装的包 npm install -D eslint-plugin-vue @typescript-eslint/parser

http://www.lryc.cn/news/582975.html

相关文章:

  • IoTDB:专为物联网场景设计的高性能时序数据库
  • 深圳凭物联网软件开发构建智慧‘城市大脑‘
  • c语言学习_函数递归
  • 「Java案例」求n1-n2内的素数
  • 使用Node.js搭建Web应用有哪些注意事项?
  • 在 Vue2 与 Vue3 中,面对 **大数据量交互体验优化** 和 **ECharts 大数据渲染性能优化**
  • 萌新赛第(一)场
  • EfficientVMamba: Atrous Selective Scan for Light Weight Visual Mamba论文精读(逐段解析)
  • 华为泰山服务器重启后出现 XFS 文件系统磁盘“不识别”(无法挂载或访问),但挂载点目录仍在且无数据
  • Nginx完全指南 - 从入门到精通(加强版)
  • 【深度学习入门 鱼书学习笔记(1)感知机】
  • Java常用加密算法详解与实战代码 - 附可直接运行的测试示例
  • Spring Boot 多数据源切换:AbstractRoutingDataSource
  • 语言模型 RLHF 实践指南(一):策略网络、价值网络与 PPO 损失函数
  • MySQL索引面试问题梳理
  • 【Android】组件及布局介绍
  • Flutter基础(前端教程②-卡片列表)
  • 【牛客刷题】小红的v三元组
  • 从单体到微服务:Spring Cloud 开篇与微服务设计
  • 音频主动降噪技术
  • 暑假算法日记第四天
  • Spring AI:检索增强生成(RAG)
  • 工作中的思考
  • Java教程:【程序调试技巧】入门
  • 项目Win系统下可正常获取Header字段,但是到了linux、docker部署后无法获取
  • 数据湖技术之Iceberg-03 Iceberg整合Flink 实时写入与增量读取
  • 【HarmonyOS】鸿蒙端云一体化开发入门详解 (一)
  • 深度剖析 Linux ip neigh:邻居表项的查看与添加实践
  • RabbitMQ第二章(RocketMQ的五大工作模式)
  • 二进制安全-汇编语言-04-第一个程序