From 8a63fff353607a25eabe686a974db3d008a06e1f Mon Sep 17 00:00:00 2001 From: Luke Xavier Symington Date: Tue, 15 Jan 2019 15:36:29 +0000 Subject: [PATCH] [#300] - Messaging fix The messaging for selector-nest-combinators was not functioning as expected, this commit should fix those issues. --- .../__tests__/index.js | 32 +++++++++---------- src/rules/selector-nest-combinators/index.js | 31 +++++++++++------- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/rules/selector-nest-combinators/__tests__/index.js b/src/rules/selector-nest-combinators/__tests__/index.js index 2353a92d..46d25180 100644 --- a/src/rules/selector-nest-combinators/__tests__/index.js +++ b/src/rules/selector-nest-combinators/__tests__/index.js @@ -25,7 +25,7 @@ testRule(rule, { { code: ` .foo { - & > bar {} + & > .bar {} } `, description: "when direct descendant combinators are nested" @@ -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 }, @@ -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 }, @@ -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 } @@ -226,7 +226,7 @@ testRule(rule, { } `, description: "when child combinators are nested", - messages: messages.rejected("nesting"), + messages: messages.rejected, line: 3, column: 9 }, @@ -237,7 +237,7 @@ testRule(rule, { } `, description: "when chained combinators are nested", - messages: messages.rejected("nesting"), + messages: messages.rejected, line: 3, column: 9 }, @@ -248,7 +248,7 @@ testRule(rule, { } `, description: "when direct descendant combinators are nested", - messages: messages.rejected("nesting"), + messages: messages.rejected, line: 3, column: 9 }, @@ -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 }, @@ -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 }, @@ -285,7 +285,7 @@ testRule(rule, { } `, description: "when pseudo classes are nested", - messages: messages.rejected("nesting"), + messages: messages.rejected, line: 3, column: 9 } diff --git a/src/rules/selector-nest-combinators/index.js b/src/rules/selector-nest-combinators/index.js index 285d1b6e..65470817 100644 --- a/src/rules/selector-nest-combinators/index.js +++ b/src/rules/selector-nest-combinators/index.js @@ -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) { @@ -31,6 +32,8 @@ export default function(expectation) { "universal" ]; + let message; + fullSelector.walk(node => { if (expectation === "always") { if (node.type === "selector") { @@ -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 });