Skip to content

Commit

Permalink
Merge pull request #307 from kristerkari/bugfix/selector-nest-combina…
Browse files Browse the repository at this point in the history
…tors-interpolation

Handle interpolation in selector-nest-combinators
  • Loading branch information
kristerkari committed Feb 2, 2019
2 parents 5032a3b + 9ff7144 commit cf5ac71
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 25 deletions.
40 changes: 39 additions & 1 deletion src/rules/selector-nest-combinators/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import rule, { ruleName, messages } from "..";
import rule, { messages, ruleName } from "..";

testRule(rule, {
ruleName,
Expand Down Expand Up @@ -86,6 +86,16 @@ testRule(rule, {
:not([class]:last-child) {}
`,
description: "when selectors are chained within a not selector"
},
{
code: `
.class-name {
#{if(&, "&", "")} {
}
}
`,
description: "should support interpolation"
}
],

Expand Down Expand Up @@ -144,6 +154,15 @@ testRule(rule, {
messages: messages.expected(":last-child", "pseudo"),
line: 2,
column: 25
},
{
code: `
.class-name #{if(&, "&", "")} {}
`,
description: "when interpolation is used",
messages: messages.expectedInterpolation,
line: 2,
column: 18
}
]
});
Expand Down Expand Up @@ -215,6 +234,12 @@ testRule(rule, {
#foo:not([class]:last-child) {}
`,
description: "when using a not selector"
},
{
code: `
.class-name #{if(&, "&", "")} {}
`,
description: "should support interpolation"
}
],

Expand Down Expand Up @@ -288,6 +313,19 @@ testRule(rule, {
messages: messages.rejected,
line: 3,
column: 9
},
{
code: `
.class-name {
#{if(&, "&", "")} {
}
}
`,
description: "when interpolation is used",
messages: messages.rejected,
line: 3,
column: 8
}
]
});
65 changes: 41 additions & 24 deletions src/rules/selector-nest-combinators/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { namespace, parseSelector } from "../../utils";
export const ruleName = namespace("selector-nest-combinators");

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

export default function(expectation) {
Expand All @@ -20,21 +21,39 @@ export default function(expectation) {
return;
}

function precedesParentSelector(current) {
do {
current = current.next();

if (current.type === "nesting") {
return true;
}
} while (current.next());

return false;
}

// attribute, class, combinator, comment, id, nesting, pseudo, root, selector, string, tag, or universal
const chainingTypes = [
"attribute",
"class",
"id",
"pseudo",
"tag",
"universal"
];

const interpolationRe = /#{.+?}$/;

root.walkRules(rule => {
parseSelector(rule.selector, result, rule, fullSelector => {
// attribute, class, combinator, comment, id, nesting, pseudo, root, selector, string, tag, or universal
const chainingTypes = [
"attribute",
"class",
"id",
"pseudo",
"tag",
"universal"
];

let message;

fullSelector.walk(node => {
if (node.value === "}") {
return;
}

if (expectation === "always") {
if (node.type === "selector") {
return;
Expand Down Expand Up @@ -81,7 +100,17 @@ export default function(expectation) {
return;
}

message = messages.expected(node.value, node.type);
const hasInterpolation = interpolationRe.test(rule.selector);

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

if (hasInterpolation) {
message = messages.expectedInterpolation;
} else {
message = messages.expected(node.value, node.type);
}
}

if (expectation === "never") {
Expand All @@ -99,18 +128,6 @@ export default function(expectation) {
message,
index: node.sourceIndex
});

function precedesParentSelector(current) {
do {
current = current.next();

if (current.type === "nesting") {
return true;
}
} while (current.next());

return false;
}
});
});
});
Expand Down

0 comments on commit cf5ac71

Please sign in to comment.