简体   繁体   English

stylelint 规则强制使用预定义的类而不是 css

[英]stylelint rule to enforce use of predefined classes instead of css

I have defined common css classnames to re-use in multiple places, like我已经定义了常见的 css 类名以在多个地方重复使用,例如

common.scss: common.scss:

 .mr-4 {margin-right: 4px}.ml-4 {margin-left: 4px}.pr-4 {padding-right: 4px}.pl-4 {padding-left: 4px}

Is there any stylelint plugin to give warning to use these classes instead of writing same css rules (ex: margin-right: 4px ) in other css files?是否有任何 stylelint 插件警告使用这些类,而不是在其他 css 文件中编写相同的 css 规则(例如: margin-right: 4px )? The project uses React, Webpack and StyleLint该项目使用 React、Webpack 和 StyleLint

If there is no plugin for this, any links/tutorial that can help to develop plugin like this would be helpful如果没有插件,任何可以帮助开发这样的插件的链接/教程都会有所帮助

Thanks谢谢

You can use the built-in property-disallowed-list rule to disallow properties, and you can use a custom message to communicate your intent.您可以使用内置的property-disallowed-list规则来禁止属性,并且可以使用自定义消息来传达您的意图。

{
  "rules": {
    "property-disallowed-list": [
      ["/^margin/", "/^padding/"],
      {
        "message": "Use the appropriate utility class instead"
      }
    ]
  }
}

You can then turn off the rule in your common.scss file:然后,您可以在common.scss文件中关闭规则

/* stylelint-disable property-disallowed-list */
.m-r-4 {margin-right: 4px}
.m-l-4 {margin-left: 4px}
.p-r-4 {padding-right: 4px}
.p-l-4 {padding-left: 4px}

Alternatively, you can write a plugin that checks against the built-in rule , but not for the common.scss file by checking the filename before processing.或者,您可以编写一个插件检查内置规则,但不能通过在处理之前检查文件名来检查common.scss文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM