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

Bugfix/selector no union class name nesting #347

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
14 changes: 14 additions & 0 deletions src/rules/selector-no-union-class-name/__tests__/index.js
Expand Up @@ -95,6 +95,20 @@ testRule(rule, {
}
`,
description: "ignores an ampersand chained with an attribute"
},
{
code: `
@mixin demo() {
@content;
}
.a-selector {
@include demo() {
button:hover & {
background:purple
}
}
}`,
description: "should not throw an error when using nesting (issue #345)"
}
],

Expand Down
30 changes: 27 additions & 3 deletions src/rules/selector-no-union-class-name/index.js
Expand Up @@ -35,9 +35,15 @@ export default function(actual) {
root.walkRules(/&/, rule => {
const parentNodes = [];

parseSelector(rule.parent.selector, result, rule, fullSelector => {
fullSelector.walk(node => parentNodes.push(node));
});
const selector = getSelectorFromRule(rule.parent);

if (selector) {
parseSelector(selector, result, rule, fullSelector => {
fullSelector.walk(node => parentNodes.push(node));
});
}

if (parentNodes.length === 0) return;

const lastParentNode = parentNodes[parentNodes.length - 1];

Expand All @@ -63,3 +69,21 @@ export default function(actual) {
});
};
}

/**
* Searches for the closest rule which
* has a selector and returns the selector
* @returns {string|undefined}
*/
function getSelectorFromRule(rule) {
// All non at-rules have their own selector
if (rule.selector !== undefined) {
return rule.selector;
}

// At-rules like @mixin don't have a selector themself
// but their parents might have one
if (rule.parent) {
return getSelectorFromRule(rule.parent);
}
}