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 rule-empty-line-before rule #2309

Merged
merged 6 commits into from
Feb 2, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion lib/rules/rule-empty-line-before/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,28 @@ b

## Optional secondary options

### `except: ["after-single-line-comment", "inside-block-and-after-rule", "first-nested"]`
### `except: ["after-rule", "after-single-line-comment", "inside-block-and-after-rule", "first-nested"]`

#### `"after-rule"`

Reverse the primary option if the rule comes after another rule.

For example, with `"always"`:

The following patterns are considered warnings:

```css
a {}

b {}
```

The following patterns are *not* considered warnings:

```css
a {}
b {}
```

#### `"after-single-line-comment"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it's explicitly for a single-line comment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rule mimics the options from the previous two rules. Only the rule-nested... had the after single line comment exception, as there was a specific request for that functionality.


Expand Down
26 changes: 26 additions & 0 deletions lib/rules/rule-empty-line-before/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,32 @@ testRule(rule, {
}],
})

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

accept: [ {
code: "a {} \n b {}",
}, {
code: "$var: pink;\n\nb {}",
description: "scss variable",
}, {
code: "@media {}\n\na{}",
description: "media rule",
} ],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add tests for nested rules either, please? New option affects both non-nested and nested rules.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


reject: [ {
code: "a {}\n\nb {}",
message: messages.rejected,
}, {
code: "$var: pink;\nb {}",
message: messages.expected,
}, {
code: "@media {}\na{}",
message: messages.expected,
} ],
})

testRule(rule, {
ruleName,
config: [ "always", { except: ["after-single-line-comment"] } ],
Expand Down
10 changes: 10 additions & 0 deletions lib/rules/rule-empty-line-before/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const rule = function (expectation, options) {
"inside-block",
],
except: [
"after-rule",
"after-single-line-comment",
"first-nested",
"inside-block-and-after-rule",
Expand Down Expand Up @@ -91,6 +92,15 @@ const rule = function (expectation, options) {
}

// Optionally reverse the expectation if a rule precedes this node
if (
optionsMatches(options, "except", "after-rule")
&& rule.prev()
&& rule.prev().type === "rule"
) {
expectEmptyLineBefore = !expectEmptyLineBefore
}

// Optionally reverse the expectation if a rule precedes this node and is inside a block
if (
optionsMatches(options, "except", "inside-block-and-after-rule")
&& rule.prev()
Expand Down