css/prefer-nested-selectors
建议为后代选择器使用 CSS 嵌套。
| 默认严重程度 | 预设 | 自动修复 | 选项 |
|---|---|---|---|
warning |
opinionated, nuxt |
不支持 | 无 |
适用范围:纯 CSS 的 style 块(scss、sass、less 和 stylus 块会被跳过)。
错误示例
.card .title 后代选择器在平铺规则中重复了父选择器。
<style scoped>
.card .title { color: red; }
</style>
正确示例
.title 规则嵌套在 .card 内,将父子样式关系放在一起。
<style scoped>
.card { .title { color: red; } }
</style>
配置(Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({}).withVue({
linter: {
"preset": "incremental",
"rules": {
"css/prefer-nested-selectors": "warn"
}
},
});
然后运行 vp run lint。