Search code examples
cssstylelint

Stylelint - new line before first nested block


How can I use the stylelint 'rule-empty-line-before' to generate a new line for only the first nested block:

Example:

a {
    margin: 0;
    padding: 0;

    b {
       margin: 0;
    }
    c {
      margin: 0;
    }
}

The following adds new lines to all nested blocks:

"rule-empty-line-before": [
            "never-multi-line",
            {
                "except": ["inside-block"]
            }
        ],

Solution

  • You can enforce this convention using:

    "rule-empty-line-before": [
      "always", {
        "except": ["inside-block-and-after-rule"]
      }
    ],
    

    From the "About rules" section of the documentation:

    We recommend that you set your primary option (e.g. "always" or "never") to whatever is your most common occurrence and define your exceptions with the except optional secondary options.

    The most common occurrence in your case is "always", with rules inside a block and after another rule being the exception. (The inside-block-and-after-rule secondary option is documented in the rule README.)