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 ignoreSelectors option to block-opening-brace-space-before #4640

Merged
merged 2 commits into from Jun 2, 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
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