Search code examples
csscommentsstylelint

Why are some of the secondary options not working for the rules in my .stylelintrc?


I am trying to format my css so there's no more than one empty line between lines, but I don't want that to apply to comments. The documentation for stylelint has a secondary option that should make this easy, but for some reason it isn't working.

Here's the rule:

"max-empty-lines": [
      1,
      {
        "ignore": [
          "comments"
        ]
      }
    ],

And here's the rest of my .stylelintrc for reference:

{
  "rules": {
    "at-rule-empty-line-before": "always",
    "at-rule-no-unknown": true,
    "block-closing-brace-empty-line-before": "never",
    "block-closing-brace-newline-before": "always",
    "block-opening-brace-newline-after": "always",
    "block-opening-brace-space-before": "always",
    "block-no-empty": null,
    "color-no-invalid-hex": true,
    "color-hex-case": "upper",
    "color-hex-length": "short",
    "comment-empty-line-before": [
      "always",
      {
        "except": [
          "first-nested"
        ]
      }
    ],
    "comment-no-empty": true,
    "comment-whitespace-inside": "never",
    "custom-property-empty-line-before": "never",
    "custom-property-no-missing-var-function": true,
    "declaration-block-no-duplicate-custom-properties": true,
    "declaration-block-no-duplicate-properties": [
      true,
      {
        "ignore": [
          "consecutive-duplicates-with-different-values"
        ]
      }
    ],
    "declaration-block-no-shorthand-property-overrides": true,
    "declaration-block-semicolon-newline-after": "always",
    "declaration-block-semicolon-newline-before": "never-multi-line",
    "declaration-block-semicolon-space-before": "never",
    "declaration-block-semicolon-space-after": "never-single-line",
    "declaration-block-trailing-semicolon": "always",
    "declaration-colon-space-before": "never",
    "declaration-colon-space-after": "always",
    "declaration-empty-line-before": "never",
    "font-family-no-duplicate-names": true,
    "font-family-no-missing-generic-family-keyword": true,
    "function-calc-no-unspaced-operator": true,
    "function-linear-gradient-no-nonstandard-direction": true,
    "function-no-unknown": true,
    "keyframe-declaration-no-important": true,
    "max-empty-lines": [
      1,
      {
        "ignore": [
          "comments"
        ]
      }
    ],
    "media-feature-name-no-unknown": true,
    "named-grid-areas-no-invalid": true,
    "no-descending-specificity": true,
    "no-duplicate-at-import-rules": true,
    "no-duplicate-selectors": true,
    "no-empty-source": true,
    "no-eol-whitespace": true,
    "no-extra-semicolons": true,
    "no-invalid-double-slash-comments": true,
    "no-invalid-position-at-import-rule": true,
    "no-irregular-whitespace": true,
    "property-no-unknown": true,
    "rule-empty-line-before": "always",
    "selector-list-comma-newline-after": "always",
    "selector-list-comma-newline-before": "never-multi-line",
    "selector-list-comma-space-before": "never",
    "selector-max-empty-lines": 0,
    "selector-pseudo-class-no-unknown": true,
    "selector-pseudo-element-no-unknown": true,
    "selector-type-no-unknown": [
      true,
      {
        "ignore": [
          "custom-elements"
        ]
      }
    ],
    "string-no-newline": true,
    "unit-no-unknown": true
  }
}

Solution

  • You are using the secondary option correctly as this demo shows:

    {
      "rules": {
        "max-empty-lines": [1, {"ignore": ["comments"]}]
      }
    }
    
    /* comment 
    
    
    */
    
    a {}
    
    
    b {}
    

    Gives only one error on line 8:

    8:1 error Expected no more than 1 empty line (max-empty-lines)
    

    If you aren't seeing this behaviour locally, I suggest reporting a bug in the Stylelint issue tracker.