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 ignore: ["next-sibling"] to selector-max-type #3832

Merged
merged 6 commits into from
Jan 3, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/rules/selector-max-type/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ The following patterns are *not* considered violations:

## Optional secondary options

### `ignore: ["child", "compounded", "descendant"]`
### `ignore: ["child", "compounded", "descendant", "next-sibling"]`

#### `"child"`

Expand Down Expand Up @@ -119,6 +119,24 @@ The following patterns are *not* considered violations:
#bar div span a {}
```

#### "next-sibling"
iliyaZelenko marked this conversation as resolved.
Show resolved Hide resolved

Discount next-sibling type selectors.

For example, with `2`:

The following patterns are *not* considered violations:

```css
div a + span {}
```

```css
#bar + div + span + a + span {}
```

iliyaZelenko marked this conversation as resolved.
Show resolved Hide resolved

iliyaZelenko marked this conversation as resolved.
Show resolved Hide resolved

### `ignoreTypes: ["/regex/", "string"]`

Given:
Expand Down
79 changes: 79 additions & 0 deletions lib/rules/selector-max-type/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,3 +549,82 @@ testRule(rule, {
}
]
});

testRule(rule, {
ruleName,
config: [0, { ignore: ["next-sibling"] }],
skipBasicChecks: true,
syntax: "scss",
iliyaZelenko marked this conversation as resolved.
Show resolved Hide resolved

accept: [
{
code: ".foo + a {}"
},
{
code: ".foo, .bar + a {}"
},
{
code: ".foo + a + b {}"
},
{
code: ".foo { .bar + a {} }"
iliyaZelenko marked this conversation as resolved.
Show resolved Hide resolved
},
{
code: ".foo, .bar + baz {}"
},
{
iliyaZelenko marked this conversation as resolved.
Show resolved Hide resolved
code:
".foo + bar + foo + baz + bar, .foo + baz, .baz + .foo { .baz + foo + bar + foo { .foo + foo {} } }, .lol + .lol {}"
}
],

reject: [
{
code: "foo {}",
message: messages.expected("foo", 0),
line: 1,
column: 1
},
{
code: "bar baz {}",
message: messages.expected("bar baz", 0),
line: 1,
column: 1
},
{
code: "bar + baz {}",
message: messages.expected("bar + baz", 0),
line: 1,
column: 1
},
{
code: "foo + bar + baz {}",
message: messages.expected("foo + bar + baz", 0),
line: 1,
column: 1
},
{
code: "foo, .bar + baz {}",
message: messages.expected("foo", 0),
line: 1,
column: 1
},
{
code: ".foo + bar, baz {}",
message: messages.expected("baz", 0),
line: 1,
column: 13
},
{
code: ".foo + .bar { baz {} }",
iliyaZelenko marked this conversation as resolved.
Show resolved Hide resolved
message: messages.expected(".foo + .bar baz", 0),
line: 1,
column: 15
},
{
code: ".foo + .bar { .baz + .foo { foo + bar {} } }",
iliyaZelenko marked this conversation as resolved.
Show resolved Hide resolved
message: messages.expected(".foo + .bar .baz + .foo foo + bar", 0),
line: 1
}
]
});
23 changes: 16 additions & 7 deletions lib/rules/selector-max-type/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function rule(max, options) {
{
actual: options,
possible: {
ignore: ["descendant", "child", "compounded"],
ignore: ["descendant", "child", "compounded", "next-sibling"],
ignoreTypes: [_.isString]
},
optional: true
Expand All @@ -49,6 +49,7 @@ function rule(max, options) {
const ignoreDescendant = optionsMatches(options, "ignore", "descendant");
const ignoreChild = optionsMatches(options, "ignore", "child");
const ignoreCompounded = optionsMatches(options, "ignore", "compounded");
const ignoreNextSibling = optionsMatches(options, "ignore", "next-sibling");

function checkSelector(selectorNode, ruleNode) {
const count = selectorNode.reduce((total, childNode) => {
Expand All @@ -73,7 +74,11 @@ function rule(max, options) {
return total;
}

return (total += childNode.type === "tag" ? 1 : 0);
if (ignoreNextSibling && hasNextSiblingCombinator(childNode)) {
return total;
}

return total + (childNode.type === "tag");
}, 0);

if (
Expand Down Expand Up @@ -148,11 +153,11 @@ function hasCompoundSelector(node) {
return true;
}

if (node.next() && !isCombinator(node.next())) {
return true;
}
return node.next() && !isCombinator(node.next());
}

return false;
function hasNextSiblingCombinator(node) {
return node.prev() && isNextSiblingCombinator(node.prev());
}

function isCombinator(node) {
Expand All @@ -170,7 +175,11 @@ function isDescendantCombinator(node) {
function isChildCombinator(node) {
if (!node) return false;

return isCombinator(node) && node.value.includes(">");
return isCombinator(node) && node.value === ">";
iliyaZelenko marked this conversation as resolved.
Show resolved Hide resolved
}

function isNextSiblingCombinator(node) {
return isCombinator(node) && node.value === "+";
}

rule.ruleName = ruleName;
Expand Down