Skip to content

Commit

Permalink
Merge pull request #301 from lxsymington/enforce-nesting
Browse files Browse the repository at this point in the history
[#300] - Messaging fix
  • Loading branch information
kristerkari committed Jan 16, 2019
2 parents 0cc576f + 8a63fff commit 356efa1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
32 changes: 16 additions & 16 deletions src/rules/selector-nest-combinators/__tests__/index.js
Expand Up @@ -25,7 +25,7 @@ testRule(rule, {
{
code: `
.foo {
& > bar {}
& > .bar {}
}
`,
description: "when direct descendant combinators are nested"
Expand Down Expand Up @@ -95,16 +95,16 @@ testRule(rule, {
.foo .bar {}
`,
description: "when a child combinator is used instead of nesting",
messages: messages.rejected("child combinator"),
messages: messages.expected(" ", "combinator"),
line: 2,
column: 12
column: 11
},
{
code: `
.foo.bar {}
`,
description: "when a selector is chained with another",
messages: messages.rejected("chaining"),
messages: messages.expected(".bar", "class"),
line: 2,
column: 11
},
Expand All @@ -114,16 +114,16 @@ testRule(rule, {
`,
description:
"when a direct descendant combinator is used without nesting",
messages: messages.rejected("direct descendant"),
messages: messages.expected(">", "combinator"),
line: 2,
column: 14
column: 12
},
{
code: `
.foo:hover {}
`,
description: "when pseudo classes are used without nesting",
messages: messages.rejected("pseudo class"),
messages: messages.expected(":hover", "pseudo"),
line: 2,
column: 11
},
Expand All @@ -132,16 +132,16 @@ testRule(rule, {
* + li {}
`,
description: "when universal selectors are used with a combinator",
messages: messages.rejected("direct sibling"),
messages: messages.expected("+", "combinator"),
line: 2,
column: 11
column: 9
},
{
code: `
:nth-child(2n - 1):last-child {}
`,
description: "when pseudo selectors only are chained",
messages: messages.rejected("pseudo class"),
messages: messages.expected(":last-child", "pseudo"),
line: 2,
column: 25
}
Expand Down Expand Up @@ -226,7 +226,7 @@ testRule(rule, {
}
`,
description: "when child combinators are nested",
messages: messages.rejected("nesting"),
messages: messages.rejected,
line: 3,
column: 9
},
Expand All @@ -237,7 +237,7 @@ testRule(rule, {
}
`,
description: "when chained combinators are nested",
messages: messages.rejected("nesting"),
messages: messages.rejected,
line: 3,
column: 9
},
Expand All @@ -248,7 +248,7 @@ testRule(rule, {
}
`,
description: "when direct descendant combinators are nested",
messages: messages.rejected("nesting"),
messages: messages.rejected,
line: 3,
column: 9
},
Expand All @@ -261,7 +261,7 @@ testRule(rule, {
}
`,
description: "when parent selectors are used for concatenation",
messages: messages.rejected("nesting"),
messages: messages.rejected,
line: 3,
column: 9
},
Expand All @@ -274,7 +274,7 @@ testRule(rule, {
}
`,
description: "when parent selectors are used for BEM style concatenation",
messages: messages.rejected("nesting"),
messages: messages.rejected,
line: 3,
column: 9
},
Expand All @@ -285,7 +285,7 @@ testRule(rule, {
}
`,
description: "when pseudo classes are nested",
messages: messages.rejected("nesting"),
messages: messages.rejected,
line: 3,
column: 9
}
Expand Down
31 changes: 20 additions & 11 deletions src/rules/selector-nest-combinators/index.js
Expand Up @@ -4,8 +4,9 @@ import { namespace, parseSelector } from "../../utils";
export const ruleName = namespace("selector-nest-combinators");

export const messages = utils.ruleMessages(ruleName, {
expected: prop => `Expected selector "${prop}" to be in a nested form`,
rejected: prop => `Unexpected "${prop}" found in selector`
expected: (combinator, type) =>
`Expected combinator "${combinator}" of type "${type}" to be in a nested form`,
rejected: () => `Unexpected nesting found in selector`
});

export default function(expectation) {
Expand All @@ -31,6 +32,8 @@ export default function(expectation) {
"universal"
];

let message;

fullSelector.walk(node => {
if (expectation === "always") {
if (node.type === "selector") {
Expand All @@ -54,40 +57,46 @@ export default function(expectation) {
return;
}

if (!chainingTypes.includes(node.type)) {
return;
}

if (node.prev().type === "combinator") {
if (!node.prev().prev()) {
if (node.type === "combinator") {
if (!chainingTypes.includes(node.next().type)) {
return;
}

if (!chainingTypes.includes(node.prev().prev().type)) {
if (!chainingTypes.includes(node.prev().type)) {
return;
}
}

if (
node.prev().type !== "combinator" &&
chainingTypes.includes(node.type) &&
!chainingTypes.includes(node.prev().type)
) {
return;
}

if (
node.type !== "combinator" &&
!chainingTypes.includes(node.type)
) {
return;
}

message = messages.expected(node.value, node.type);
}

if (expectation === "never") {
if (rule.parent.type === "root" || rule.parent.type === "atrule") {
return;
}

message = messages.rejected;
}

utils.report({
ruleName,
result,
node: rule,
message: messages.rejected,
message,
index: node.sourceIndex
});

Expand Down

0 comments on commit 356efa1

Please sign in to comment.