Skip to content

Commit

Permalink
Add except: ["after-same-name"] option to at-rule-empty-line-before #…
Browse files Browse the repository at this point in the history
  • Loading branch information
hudochenkov authored and jeddy3 committed Jan 8, 2017
1 parent a35fc67 commit 8888dd7
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 3 deletions.
33 changes: 32 additions & 1 deletion lib/rules/at-rule-empty-line-before/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,38 @@ a {}

## Optional secondary options

### `except: ["all-nested", "blockless-after-same-name-blockless", "blockless-group", "first-nested"]`
### `except: ["after-same-name", "all-nested", "blockless-after-same-name-blockless", "blockless-group", "first-nested"]`

#### `"after-same-name"`

Reverse the primary option for at-rules that follow another at-rule with the same name.

This means that you can group your at-rules by name.

For example, with `"always"`:

The following patterns are *not* considered warnings:

```css
@charset "UTF-8";

@import url(x.css);
@import url(y.css);

@media (min-width: 100px) {}
@media (min-width: 200px) {}
```

```css
a {

@extends .foo;
@extends .bar;

@include x;
@include y {}
}
```

### `"all-nested"`

Expand Down
94 changes: 93 additions & 1 deletion lib/rules/at-rule-empty-line-before/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ testRule(rule, mergeTestDescriptions(sharedNeverTests, {
code: stripIndent`
@charset "UTF-8";
@import url(x.css);
@import url(y.css);`,
}, {
code: stripIndent`
Expand Down Expand Up @@ -694,3 +694,95 @@ testRule(rule, mergeTestDescriptions(sharedNeverTests, {
column: 3,
} ],
}))

testRule(rule, {
ruleName,
config: [ "always", {
except: ["after-same-name"],
} ],

accept: [ {
code: stripIndent`
@charset "UTF-8";
@include x;
@include y {}`,
}, {
code: stripIndent`
a {
@extends .foo;
@extends .bar;
@include y {}
@include x;
}`,
} ],

reject: [ {
code: stripIndent`
@charset "UTF-8";
@include x;
@include y {}`,
message: messages.expected,
line: 2,
column: 1,
}, {
code: stripIndent`
a {
@extends .foo;
@extends .bar;
@include y {}
@include x;
}`,
message: messages.expected,
line: 5,
column: 3,
} ],
})

testRule(rule, mergeTestDescriptions(sharedNeverTests, {
ruleName,
config: [ "never", {
except: ["after-same-name"],
} ],

accept: [ {
code: stripIndent`
@charset "UTF-8";
@include x;
@include y {}`,
}, {
code: stripIndent`
a {
@extends .foo;
@extends .bar;
@include y {}
@include x;
}`,
} ],

reject: [ {
code: stripIndent`
@charset "UTF-8";
@include x;
@include y {}`,
message: messages.expected,
line: 3,
column: 1,
}, {
code: stripIndent`
a {
@extends .bar;
@include x;
@include y {}
}`,
message: messages.expected,
line: 4,
column: 3,
} ],
}))
11 changes: 10 additions & 1 deletion lib/rules/at-rule-empty-line-before/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const rule = function (expectation, options) {
actual: options,
possible: {
except: [
"after-same-name",
"all-nested",
"blockless-after-same-name-blockless",
"blockless-group",
Expand Down Expand Up @@ -100,7 +101,9 @@ const rule = function (expectation, options) {

// Optionally reverse the expectation if any exceptions apply
if (
optionsMatches(options, "except", "all-nested")
optionsMatches(options, "except", "after-same-name")
&& isAfterSameName()
|| optionsMatches(options, "except", "all-nested")
&& isNested
|| optionsMatches(options, "except", "first-nested")
&& isFirstNested()
Expand Down Expand Up @@ -143,6 +146,12 @@ const rule = function (expectation, options) {
&& previousNode.name == atRule.name
}

function isAfterSameName() {
return previousNode
&& previousNode.type === "atrule"
&& previousNode.name === atRule.name
}

function isFirstNested() {
return isNested
&& atRule === atRule.parent.first
Expand Down

0 comments on commit 8888dd7

Please sign in to comment.