Skip to content

Commit

Permalink
Add ignoreSelectors option to block-opening-brace-space-before (#4640)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlessuh committed Jun 2, 2020
1 parent 28ea42f commit 617f68e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
21 changes: 21 additions & 0 deletions lib/rules/block-opening-brace-space-before/README.md
Expand Up @@ -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:

<!-- prettier-ignore -->
```css
:root
{}
```

<!-- prettier-ignore -->
```css
:root{}
```
37 changes: 6 additions & 31 deletions lib/rules/block-opening-brace-space-before/__tests__/index.js
Expand Up @@ -81,7 +81,7 @@ testRule({

testRule({
ruleName,
config: ['always', { ignoreAtRules: ['for'] }],
config: ['always', { ignoreAtRules: ['for', '/fo/', /fo/] }],
fix: true,

accept: [
Expand Down Expand Up @@ -109,53 +109,28 @@ testRule({

testRule({
ruleName,
config: ['always', { ignoreAtRules: '/fo/' }],
config: ['always', { ignoreSelectors: ['a', '/a/', /a/] }],
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,
},
],
});

testRule({
ruleName,
config: ['always', { ignoreAtRules: /fo/ }],
fix: true,

accept: [
{
code: 'a { color: pink; }',
},
{
code: '@for ...\n{ color: pink; }',
code: 'a\n{ color: pink; }',
},
{
code: '@for ...\r\n{ color: pink; }',
code: 'a\r\n{ color: pink; }',
},
],

reject: [
{
code: 'a{ color: pink; }',
fixed: 'a { color: pink; }',
code: 'b{ color: pink; }',
fixed: 'b { color: pink; }',
message: messages.expectedBefore(),
line: 1,
column: 1,
Expand Down
6 changes: 6 additions & 0 deletions lib/rules/block-opening-brace-space-before/index.js
Expand Up @@ -46,6 +46,7 @@ function rule(expectation, options, context) {
actual: options,
possible: {
ignoreAtRules: [_.isString, _.isRegExp],
ignoreSelectors: [_.isString, _.isRegExp],
},
optional: true,
},
Expand All @@ -70,6 +71,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,
Expand Down

0 comments on commit 617f68e

Please sign in to comment.