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

Handle interpolation in selector-nest-combinators #307

Merged
merged 3 commits into from Feb 2, 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
40 changes: 39 additions & 1 deletion src/rules/selector-nest-combinators/__tests__/index.js
@@ -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
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 = [
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I move these things on top, so that they're not inside a loop

"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 === "}") {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this because the selector parser can not properly parse interpolation, so the } was being reported on its own.

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