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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignore: ["comments"] to block-no-empty #4008

Merged
merged 1 commit into from Apr 7, 2019
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
6 changes: 3 additions & 3 deletions lib/__tests__/disableRanges-integration.test.js
Expand Up @@ -8,7 +8,7 @@ const stringQuotes = require("../rules/string-quotes");
// disabling all rules
testRule(blockNoEmpty, {
ruleName: blockNoEmpty.ruleName,
config: [undefined],
config: [true],
skipBasicChecks: true,

accept: [
Expand Down Expand Up @@ -76,7 +76,7 @@ testRule(selectorCombinatorSpaceBefore, {

testRule(blockNoEmpty, {
ruleName: blockNoEmpty.ruleName,
config: [undefined],
config: [true],
skipBasicChecks: true,

accept: [
Expand Down Expand Up @@ -146,7 +146,7 @@ testRule(selectorCombinatorSpaceBefore, {

testRule(blockNoEmpty, {
ruleName: blockNoEmpty.ruleName,
config: [undefined],
config: [true],
skipBasicChecks: true,

accept: [
Expand Down
36 changes: 33 additions & 3 deletions lib/rules/block-no-empty/README.md
Expand Up @@ -23,15 +23,45 @@ a { }
```

```css
@media print { a {} }
@media print {
a {}
}
```

The following patterns are *not* considered violations:

```css
a { color: pink; }
a {
/* foo */
}
```

```css
@media print { a { color: pink; } }
@media print {
a {
color: pink;
}
}
```

## Optional secondary options

### `ignore: ["comments"]`

Exclude comments from being treated as content inside of a block.

The following patterns are considered violations:

```css
hudochenkov marked this conversation as resolved.
Show resolved Hide resolved
a {
/* foo */
}
```

```css
@media print {
a {
/* foo */
}
}
```
126 changes: 126 additions & 0 deletions lib/rules/block-no-empty/__tests__/index.js
Expand Up @@ -18,9 +18,24 @@ testRule(rule, {
{
code: "a { color: pink; }"
},
{
code: "a { /* foo */ }"
},
{
code: "a {\n/* foo */\n}"
},
{
code: "a {\n/* foo */\n/* bar */\n}"
},
{
code: "a {\n/*\nfoo\nbar\n*/\n}"
},
{
code: "@media print { a { color: pink; } }"
},
{
code: "@media print { a { /* foo */ } }"
},
{
code: "@import url(x.css)"
}
Expand Down Expand Up @@ -102,6 +117,59 @@ testRule(rule, {
]
});

testRule(rule, {
ruleName,
config: [
true,
{
ignore: ["comments"]
}
],
skipBasicChecks: true,

accept: [
{
code: "a { /* foo */ color: pink; }"
},
{
code: "a {\n/* foo */\ncolor: pink;\n}"
}
],

reject: [
{
code: "a { /* foo */ }",
message: messages.rejected,
line: 1,
column: 3
},
{
code: "a {\n/* foo */\n}",
message: messages.rejected,
line: 1,
column: 3
},
{
code: "a {\n/* foo */\n/* bar */\n}",
message: messages.rejected,
line: 1,
column: 3
},
{
code: "a {\n/*\nfoo\nbar\n*/\n}",
message: messages.rejected,
line: 1,
column: 3
},
{
code: "@media print { a { /* foo */ } }",
message: messages.rejected,
line: 1,
column: 18
}
]
});

testRule(rule, {
ruleName,
config: [true],
Expand All @@ -123,3 +191,61 @@ testRule(rule, {
}
]
});

testRule(rule, {
ruleName,
config: [true],
skipBasicChecks: true,
syntax: "scss",

accept: [
{
code: "a {\n// foo\n}"
},
{
code: "a {\n// foo\n// bar\n}"
},
{
code: "@media print { a {\n// foo\n} }"
}
]
});

testRule(rule, {
ruleName,
config: [
true,
{
ignore: ["comments"]
}
],
skipBasicChecks: true,
syntax: "scss",

accept: [
{
code: "a {\n// foo\ncolor: pink;\n}"
}
],

reject: [
{
code: "a {\n// foo\n}",
message: messages.rejected,
line: 1,
column: 3
},
{
code: "a {\n// foo\n// bar\n}",
message: messages.rejected,
line: 1,
column: 3
},
{
code: "@media print { a {\n// foo\n} }",
message: messages.rejected,
line: 1,
column: 18
}
]
});
32 changes: 29 additions & 3 deletions lib/rules/block-no-empty/index.js
@@ -1,7 +1,9 @@
"use strict";

const _ = require("lodash");
const beforeBlockString = require("../../utils/beforeBlockString");
const hasEmptyBlock = require("../../utils/hasEmptyBlock");
const optionsMatches = require("../../utils/optionsMatches");
const report = require("../../utils/report");
const ruleMessages = require("../../utils/ruleMessages");
const validateOptions = require("../../utils/validateOptions");
Expand All @@ -12,20 +14,44 @@ const messages = ruleMessages(ruleName, {
rejected: "Unexpected empty block"
});

const rule = function(actual) {
const rule = function(primary, options = {}) {
return (root, result) => {
const validOptions = validateOptions(result, ruleName, { actual });
const validOptions = validateOptions(
result,
ruleName,
{
actual: primary,
possible: _.isBoolean
},
{
actual: options,
possible: {
ignore: ["comments"]
},
optional: true
}
);

if (!validOptions) {
return;
}

const ignoreComments = optionsMatches(options, "ignore", "comments");

// Check both kinds of statements: rules and at-rules
root.walkRules(check);
root.walkAtRules(check);

function check(statement) {
if (!hasEmptyBlock(statement)) {
if (!hasEmptyBlock(statement) && !ignoreComments) {
return;
}

const hasCommentsOnly = statement.nodes.every(
node => node.type === "comment"
);

if (!hasCommentsOnly) {
return;
}

Expand Down