eslint.config.js 6.68 KB
import antfu from "@antfu/eslint-config";

export default antfu({
	markdown: true,
	vue: true,
	css: true,
	jsonc: true,
	json: true,
	ignores: [
		// 忽略uni_modules目录
		"**/uni_modules/",
		"dist",
		// unplugin-auto-import 生成的类型文件,每次提交都改变,所以加入这里吧,与 .gitignore 配合使用
		"auto-import.d.ts",
		"build/",
		"node_modules/",
		// # node 覆盖率文件
		"coverage/",
		"**/*-min.js",
		"**/*.min.js",

	],
	stylistic: {
		"indent": "tab", // 4个空格
		"quotes": "double", // or 'double'
		"semi": true,
		"array-bracket-spacing": ["error", "always"],
	},
	rules: {
		"unicorn/prefer-node-protocol": "off",
		"curly": ["error", "all"],
		"vue/no-deprecated-slot-attribute": "off",
		"vue/no-deprecated-v-on-native-modifier": "off",
		"vue/no-deprecated-destroyed-lifecycle": "off",
		"vue/no-deprecated-model-definition": "off",
		"jsdoc/check-types": "off",
		"no-useless-catch": "off",
		"vue/no-unused-refs": "warn",
		"unused-imports/no-unused-imports": "warn",
		"no-unused-vars": "warn",
		"style/eol-last": "off",
		"no-prototype-builtins": "warn",
		"prefer-template": "off",
		"no-useless-return": "off",
		"vue/no-deprecated-delete-set": "off",
		"vue/no-deprecated-dollar-listeners-api": "off",
		"vue/require-explicit-emits": "off",
		"eqeqeq": "warn",
		"vue/eqeqeq": "warn",
		"vue/v-on-event-hyphenation": "off",
		"style/no-tabs": "off",
		"vue/no-deprecated-v-bind-sync": "off",
		"vue/no-lone-template": "off",
		"vue/no-mutating-props": "off",
		"vue/no-deprecated-filter": "off",
		"vue/no-v-for-template-key-on-child": "off",
		// 关闭:强制组件 name 使用 PascalCase(由于 keep-alive 需要组件 name 与路由 name 一致,路由多为小写)
		"vue/component-definition-name-casing": "off",
		// 强制:Vue 多行 HTML 元素的内容前后必须换行
		"vue/multiline-html-element-content-newline": [
			"error",
			{
				ignoreWhenEmpty: false, // 即使元素内容为空,也要求换行(如 <div>\n</div>)
				ignores: [], // 无忽略的元素(所有多行元素都生效)
				allowEmptyLines: true, // 允许内容前后存在空白行
			},
		],
		// 强制:Vue HTML 标签闭合括号不换行
		"vue/html-closing-bracket-newline": [
			"error",
			{
				singleline: "never", // 单行标签:闭合括号不换行(如 <image src="xxx" />)
				multiline: "never", // 多行标签:闭合括号不换行(如 <image\n  src="xxx"\n/>)
			},
		],
		// 强制:Vue HTML 元素自闭合格式(如 <image /> 而非 <image></image>)
		"vue/html-self-closing": [
			"error",
			{
				html: {
					void: "any", // 原生空标签(如 <img>)允许任意自闭合格式
					normal: "any", // 原生普通标签(如 <div>)允许任意自闭合格式
					component: "any", // 自定义组件(如 <MyComponent>)允许任意自闭合格式
				},
				svg: "any", // SVG 标签允许任意自闭合格式
				math: "any", // MathML 标签允许任意自闭合格式
			},
		],
		// 强制:Vue HTML 模板中标签缩进 4 个空格
		"vue/html-indent": [
			"error",
			2,
			{
				attribute: 1, // 属性相对于标签缩进 1 级(4 个空格)
				baseIndent: 1, // 基础缩进 1 级(适配组件嵌套场景)
				closeBracket: 0, // 闭合标签与属性对齐(不额外缩进)
				alignAttributesVertically: true, // 多行属性时垂直对齐(如所有属性左侧对齐)
				ignores: [], // 无忽略的标签
			},
		],
		"vue/attributes-order": ["error", {
			order: [
				"DEFINITION", // is
				"LIST_RENDERING", // v-for
				"CONDITIONALS", // v-if v-else-if v-else v-show v-cloak
				"RENDER_MODIFIERS", // v-once v-pre
				"GLOBAL", // id
				"UNIQUE", // ref key
				"SLOT", // v-slot slot
				"TWO_WAY_BINDING", // v-model
				"OTHER_DIRECTIVES", // v-custom-directive
				"ATTR_STATIC", // 静态属性排最前
				"ATTR_SHORTHAND_BOOL", // 布尔简写排中间
				"ATTR_DYNAMIC", // 动态绑定排最后
				"EVENTS", // @click @focus
				"CONTENT", // v-text v-html
			],
			alphabetical: true, // 是否在同组内再按字母排序(建议 false,官方默认顺序更好)
		}],
		// 强制:Vue SFC 中块的顺序(template -> script -> style)
		"vue/block-order": [
			"error",
			{
				order: ["template", "script", "style"], // 固定三个核心块的顺序
			},
		],

		// 强制:Vue 元素首属性的换行格式
		"vue/first-attribute-linebreak": [
			"error",
			{
				singleline: "beside", // 单行属性:首属性紧跟标签名(不换行)
				multiline: "beside", // 多行属性:首属性也紧跟标签名(注:原注释描述与配置冲突,按配置「不换行」,如需换行可改为 "below")
			},
		],
		// 强制:Vue 元素属性每行最多数量限制
		"vue/max-attributes-per-line": [
			"error",
			{
				singleline: 2, // 单行元素最多允许 2 个属性(如 <image src="xxx" alt="xxx">)
				multiline: 1, // 多行元素每行最多 1 个属性(每个属性单独一行)
			},
		],

		// 关闭:unused-imports 插件的「禁止未使用变量」检测(与 no-unused-vars 功能重叠,同步关闭)
		"unused-imports/no-unused-vars": "off",
		// 关闭:禁止无限制使用 eslint-disable 注释(允许临时禁用多个规则,按需关闭)
		"eslint-comments/no-unlimited-disable": "off",
		// 关闭:检查 JSDoc 注释中参数名与实际参数是否一致(无需强制 JSDoc 规范时关闭)
		"jsdoc/check-param-names": "off",
		// 强制:代码块前必须加空格(如 function fn() { 前加空格,报错级别 error)
		"space-before-blocks": "error",
		// 关闭:禁止使用 console(开发环境保留 console.log 便于调试,按需关闭)
		"no-console": "off",
		"style/brace-style": ["error", "1tbs"],
	},
	formatters: {
		/**
		 * Format CSS, LESS, SCSS files, also the `<style>` blocks in Vue
		 * By default uses Prettier
		 */
		css: true,
		scss: true,
		less: true,
		/**
		 * Format HTML files
		 * By default uses Prettier
		 */
		html: true,
	},
}, {
	files: ["**/*.spec.js", "**/*.integration.spec.js"],
	languageOptions: {
		globals: {
			jest: "readonly",
			describe: "readonly",
			it: "readonly",
			test: "readonly",
			expect: "readonly",
			beforeEach: "readonly",
			afterEach: "readonly",
			beforeAll: "readonly",
			afterAll: "readonly",
			require: "readonly",
			module: "readonly",
			process: "readonly",
			window: "readonly",
			document: "readonly",
			navigator: "readonly",
			location: "readonly",
			URL: "readonly",
			Blob: "readonly",
			File: "readonly",
			FormData: "readonly",
			MessageEvent: "readonly",
			localStorage: "readonly",
			sessionStorage: "readonly",
			setTimeout: "readonly",
			clearTimeout: "readonly",
			setInterval: "readonly",
			clearInterval: "readonly",
		},
	},
});