Skip to content

Commit

Permalink
Add ignoreComments[] to comment-empty-line-before (#4841)
Browse files Browse the repository at this point in the history
  • Loading branch information
LKummer committed Jun 24, 2020
1 parent e931e55 commit a8042e7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project are documented in this file.
## Head

- Added: `ignoreContextFunctionalPseudoClasses` to `selector-max-id` ([#4835](https://github.com/stylelint/stylelint/pull/4835)).
- Added: `ignoreComments[]` to `comment-empty-line-before` ([#4841](https://github.com/stylelint/stylelint/pull/4841)).

## 13.6.1

Expand Down
28 changes: 28 additions & 0 deletions lib/rules/comment-empty-line-before/README.md
Expand Up @@ -165,3 +165,31 @@ a {
color: pink;
}
```

### `ignoreComments: ["/regex/", /regex/, "string"]`

Ignore comments matching the given regular expressions or strings.

For example, with `"always"` and given:

```
[/^ignore/, "string-ignore"]
```

The following comments are _not_ considered violations:

```css
:root {
background: pink;
/* ignore this comment because of the regex */
color: pink;
}
```

```css
:root {
background: pink;
/* string-ignore */
color: pink;
}
```
33 changes: 33 additions & 0 deletions lib/rules/comment-empty-line-before/__tests__/index.js
Expand Up @@ -148,6 +148,39 @@ testRule(
}),
);

testRule(
mergeTestDescriptions(alwaysTests, {
ruleName,
config: ['always', { ignoreComments: [/^ignore/u, '/ignore$/', 'string-ignored-comment'] }],
accept: [
{
code: 'a {\ncolor: pink;\n/*ignore-at-start*/\ntop: 0;\n}',
description: 'regex literal ignore value can be used',
},
{
code: 'a {\ncolor: pink;\n/*at-end-ignore*/\ntop: 0;\n}',
description: 'string regex ignore value can be used',
},
{
code: 'a {\ncolor: pink;\n/*string-ignored-comment*/\ntop: 0;\n}',
description: 'string ignore value can be used',
},
{
code: 'a {\ncolor: pink;\n/* ignore-at-start */\ntop: 0;\n}',
description: 'regex literal ignore works with spaces',
},
{
code: 'a {\ncolor: pink;\n/* at-end-ignore */\ntop: 0;\n}',
description: 'string regex ignore works with spaces',
},
{
code: 'a {\ncolor: pink;\n/* string-ignored-comment */\ntop: 0;\n}',
description: 'string ignore works with spaces',
},
],
}),
);

testRule({
ruleName,
config: ['always', { ignore: ['after-comment'] }],
Expand Down
7 changes: 7 additions & 0 deletions lib/rules/comment-empty-line-before/index.js
Expand Up @@ -2,6 +2,7 @@

'use strict';

const _ = require('lodash');
const addEmptyLineBefore = require('../../utils/addEmptyLineBefore');
const hasEmptyLine = require('../../utils/hasEmptyLine');
const isAfterComment = require('../../utils/isAfterComment');
Expand Down Expand Up @@ -37,6 +38,7 @@ function rule(expectation, options, context) {
possible: {
except: ['first-nested'],
ignore: ['stylelint-commands', 'after-comment'],
ignoreComments: [_.isString, _.isRegExp],
},
optional: true,
},
Expand Down Expand Up @@ -65,6 +67,11 @@ function rule(expectation, options, context) {
return;
}

// Ignore comments matching the ignoreComments option.
if (optionsMatches(options, 'ignoreComments', comment.text)) {
return;
}

// Ignore shared-line comments
if (isSharedLineComment(comment)) {
return;
Expand Down

0 comments on commit a8042e7

Please sign in to comment.