From 9c335360fe66483a9be6182046af8104b1bbae9c Mon Sep 17 00:00:00 2001 From: Charles Suh Date: Sat, 29 Feb 2020 21:13:37 -0800 Subject: [PATCH] Add ignoreSelectors option to block-opening-brace-space-before --- .../README.md | 21 +++ .../__tests__/index.js | 170 ++++++++++-------- .../block-opening-brace-space-before/index.js | 6 + 3 files changed, 119 insertions(+), 78 deletions(-) diff --git a/lib/rules/block-opening-brace-space-before/README.md b/lib/rules/block-opening-brace-space-before/README.md index b4febe5201..b637275141 100644 --- a/lib/rules/block-opening-brace-space-before/README.md +++ b/lib/rules/block-opening-brace-space-before/README.md @@ -195,3 +195,24 @@ The following patterns are _not_ considered violations: ```css @for ...{} ``` + +### `ignoreSelectors: ["/regex/", /regex/, "non-regex"]` + +Given: + +``` +[":root"] +``` + +The following patterns are _not_ considered violations: + + +```css +:root +{} +``` + + +```css +:root{} +``` diff --git a/lib/rules/block-opening-brace-space-before/__tests__/index.js b/lib/rules/block-opening-brace-space-before/__tests__/index.js index 16aac542e9..baa824f66d 100644 --- a/lib/rules/block-opening-brace-space-before/__tests__/index.js +++ b/lib/rules/block-opening-brace-space-before/__tests__/index.js @@ -80,89 +80,103 @@ testRule(rule, { ], }); -testRule(rule, { - ruleName, - config: ['always', { ignoreAtRules: ['for'] }], - fix: true, +for (const selector of ['for', '/fo/', /fo/]) { + testRule(rule, { + ruleName, + config: ['always', { ignoreAtRules: [selector] }], + fix: true, - accept: [ - { - code: 'a { color: pink; }', - }, - { - code: '@for ...\n{ color: pink; }', - }, - { - code: '@for ...\r\n{ color: pink; }', - }, - ], + accept: [ + { + code: 'a { color: pink; }', + }, + { + code: '@for ...\n{ color: pink; }', + }, + { + code: '@for ...\r\n{ color: pink; }', + }, + ], - reject: [ - { - code: 'a{ color: pink; }', - fixed: 'a { color: pink; }', - message: messages.expectedBefore(), - line: 1, - column: 1, - }, - ], -}); + reject: [ + { + code: 'a{ color: pink; }', + fixed: 'a { color: pink; }', + message: messages.expectedBefore(), + line: 1, + column: 1, + }, + ], + }); +} -testRule(rule, { - ruleName, - config: ['always', { ignoreAtRules: '/fo/' }], - fix: true, +for (const selector of ['a', '/a/', /a/]) { + testRule(rule, { + ruleName, + config: ['always', { ignoreSelectors: [selector] }], + fix: true, - accept: [ - { - code: 'a { color: pink; }', - }, - { - code: '@for ...\n{ color: pink; }', - }, - { - code: '@for ...\r\n{ color: pink; }', - }, - ], + accept: [ + { + code: 'a { color: pink; }', + }, + { + code: 'a{ color: pink; }', + }, + { + code: 'a\n{ color: pink; }', + }, + { + code: 'a\r\n{ color: pink; }', + }, + ], - reject: [ - { - code: 'a{ color: pink; }', - fixed: 'a { color: pink; }', - message: messages.expectedBefore(), - line: 1, - column: 1, - }, - ], -}); - -testRule(rule, { - ruleName, - config: ['always', { ignoreAtRules: /fo/ }], - fix: true, - - accept: [ - { - code: 'a { color: pink; }', - }, - { - code: '@for ...\n{ color: pink; }', - }, - { - code: '@for ...\r\n{ color: pink; }', - }, - ], - - reject: [ - { - code: 'a{ color: pink; }', - fixed: 'a { color: pink; }', - message: messages.expectedBefore(), - line: 1, - column: 1, - }, - ], -}); + reject: [ + { + code: 'b{ color: pink; }', + fixed: 'b { color: pink; }', + message: messages.expectedBefore(), + line: 1, + column: 1, + }, + { + code: 'b\n{ color: pink; }', + fixed: 'b { color: pink; }', + message: messages.expectedBefore(), + line: 1, + column: 2, + }, + { + code: 'b\r\n{ color: pink; }', + fixed: 'b { color: pink; }', + message: messages.expectedBefore(), + line: 1, + column: 2, + }, + { + code: '@a{ color: pink; }', + fixed: '@a { color: pink; }', + message: messages.expectedBefore(), + line: 1, + column: 2, + }, + { + code: '@a\n{ color: pink; }', + fixed: '@a { color: pink; }', + message: messages.expectedBefore(), + line: 1, + column: 3, + }, + { + code: '@a\r\n{ color: pink; }', + fixed: '@a { color: pink; }', + message: messages.expectedBefore(), + line: 1, + column: 3, + }, + ], + }); +} testRule(rule, { ruleName, diff --git a/lib/rules/block-opening-brace-space-before/index.js b/lib/rules/block-opening-brace-space-before/index.js index 0097fc9f19..9977dc023f 100644 --- a/lib/rules/block-opening-brace-space-before/index.js +++ b/lib/rules/block-opening-brace-space-before/index.js @@ -44,6 +44,7 @@ function rule(expectation, options, context) { actual: options, possible: { ignoreAtRules: [_.isString, _.isRegExp], + ignoreSelectors: [_.isString, _.isRegExp], }, optional: true, }, @@ -68,6 +69,11 @@ function rule(expectation, options, context) { return; } + // Return early if selector is to be ignored + if (optionsMatches(options, 'ignoreSelectors', statement.selector)) { + return; + } + const source = beforeBlockString(statement); const beforeBraceNoRaw = beforeBlockString(statement, { noRawBefore: true,