css stylelint 规则捕获 'selector is expected' 错误

标签 css stylelint

在我的过程中,我使用 stylelint 在为我们的站点构建和部署之前查找 css 错误。然而,有人检查了以下内容,它在 css 解析器/缩小器上失败但通过了 stylelint。

.example-class ,
/*.example-class-two span*/ {
..... removed for brevity ..... 
}

我似乎无法找出任何现有规则来捕捉这个。有没有我想念的?否则,是时候编写自定义规则来捕获它了吗?

最佳答案

您需要编写此规则 as a plugin . stylelint 曾经有一个规则 selector-no-empty 来处理您的用例,但它在 7.8.0 版中被删除了。您可以使用该规则作为插件的起点。 Here's the source .

请注意,该规则最初是为 the following reason 删除的:

It is beyond the scope of stylelint's core package to effectively validate against the CSS spec. Please investigate csstree and css-values for this functionality, and contribute to those projects and to stylelint plugins wrapping them.

目前有一个csstree stylelint plugin用于检查 CSS 的有效性,但是 selector validation is not ready yet .因此,您可以只使用 csstree stylelint 插件并等待选择器验证完成。

stylelint 本身不是 CSS 规范验证器,它的设计更多是为了检测代码,尽管它是有效的,但可能会产生意想不到的后果 - 使用像 declaration-block-no-shorthand-property-overrides 这样的规则, no-descending-specificity, no-duplicate-selectors

或者限制团队在代码库中可以和不能使用哪些 CSS 功能 - 使用 unit-blacklistselector-max-id 等规则, selector-class-pattern

关于css stylelint 规则捕获 'selector is expected' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46433845/

相关文章:

css - Stylelint 验证 SCSS mixin 中的声明

css - 驼峰式 BEM 的正则表达式

javascript - 使用JQuery弹出框

javascript - 使用 .toggleClass() 切换不透明度时遇到问题

php - 下拉隐藏值

css - 如何在引导表 <th> 中创建下拉选项

javascript - 如何在 stylelint 配置 js 中添加特定规则的忽略文件

css - 如何使 stylelint 防止嵌套 block 中最后一个大括号后出现空行?

css - WebStorm 中的 Stylelint 修复

html - 使用 CSS 来适应 HTML 网站的所有元素的最佳方法是什么?