让你的 vue/* rules 直接在 <template lang="pug"> 上运行,用 eslint --fix 格式化 Pug,并保证模板完好无损:每个修复在应用之前都会经过检查。
npm i -D eslint-plugin-pug-vue<template lang="pug">div.card(·class="shadow"·,·:title·=·"title"·)····h2(·v-if='title'·)·{{·title·}}····input(v-model="name"·,·type='text'·disabled)/</template>vue/* rules,直接用在 Pug 上 eslint-plugin-vue 的 rules 以原有的名称和选项在 Pug 模板上运行:缺失的 key、 未使用的变量、属性顺序、已弃用的语法。少数无法在 Pug 上工作的 rules 会被 关闭,或只在 Pug 模板内部被屏蔽,因此它们仍会继续检查你的 HTML 模板。
<template lang="pug">
ul
li(v-for="todo in todos") {{ todo.title }}
</template>3:3 error Elements in iteration expect to have 'v-bind:key' directives vue/require-v-for-keyeslint --fix 格式化 十二条小巧的 vue-pug/* rules 负责格式化 Pug 本身:缩进、属性块的间距、逗号、引号 和换行,以及 .class / #id 简写。每条 rule 都有选项, 也都可以关闭。不需要 Prettier,也不会有第二个格式化工具在保存时和 ESLint 打架。
// eslint.config.js
import pluginVue from 'eslint-plugin-vue'
import vuePug from 'eslint-plugin-pug-vue'
export default [
...pluginVue.configs['flat/recommended'],
...vuePug.configs.recommended,
]在 Pug 中,缩进就是结构:移动一行,树就会改变。所以在 ESLint 把修复写入 Pug 模板之前, 插件会先检查它,无论这个修复来自哪条 rule:
//- the same template to the guard
p(class="intro lead") Hello
p.intro.lead Hello
//- a different one: a class is gone
p.intro Hello因此 vue-pug/prefer-shorthand 可以把第一行改写成第二行,但丢失 class,或把某一行移到另一个父级下的修复,永远不会被应用。
它需要使用 flat config 的 ESLint 9.15 或 10、eslint-plugin-vue 10 和 vue-eslint-parser 10。 快速开始会带你完成安装、配置,以及在 VS Code 中设置保存时修复。 从 eslint-plugin-vue-pug 迁移过来?请阅读迁移指南。