Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignoreComments[] to comment-empty-line-before #4841

Merged
merged 4 commits into from Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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