From a8042e7192cad993081cf982901948dbd5004048 Mon Sep 17 00:00:00 2001 From: Lior Kummer Date: Wed, 24 Jun 2020 17:00:31 +0300 Subject: [PATCH] Add ignoreComments[] to comment-empty-line-before (#4841) --- CHANGELOG.md | 1 + lib/rules/comment-empty-line-before/README.md | 28 ++++++++++++++++ .../__tests__/index.js | 33 +++++++++++++++++++ lib/rules/comment-empty-line-before/index.js | 7 ++++ 4 files changed, 69 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45869e889e..1550515ef0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rules/comment-empty-line-before/README.md b/lib/rules/comment-empty-line-before/README.md index 37e9100d63..00e036b4cf 100644 --- a/lib/rules/comment-empty-line-before/README.md +++ b/lib/rules/comment-empty-line-before/README.md @@ -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; +} +``` diff --git a/lib/rules/comment-empty-line-before/__tests__/index.js b/lib/rules/comment-empty-line-before/__tests__/index.js index 5af404aadc..df50c6b3f8 100644 --- a/lib/rules/comment-empty-line-before/__tests__/index.js +++ b/lib/rules/comment-empty-line-before/__tests__/index.js @@ -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'] }], diff --git a/lib/rules/comment-empty-line-before/index.js b/lib/rules/comment-empty-line-before/index.js index ea7c52b7c6..c587155b59 100644 --- a/lib/rules/comment-empty-line-before/index.js +++ b/lib/rules/comment-empty-line-before/index.js @@ -2,6 +2,7 @@ 'use strict'; +const _ = require('lodash'); const addEmptyLineBefore = require('../../utils/addEmptyLineBefore'); const hasEmptyLine = require('../../utils/hasEmptyLine'); const isAfterComment = require('../../utils/isAfterComment'); @@ -37,6 +38,7 @@ function rule(expectation, options, context) { possible: { except: ['first-nested'], ignore: ['stylelint-commands', 'after-comment'], + ignoreComments: [_.isString, _.isRegExp], }, optional: true, }, @@ -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;