Skip to content

Commit

Permalink
Use closest selector
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon committed Jul 18, 2019
1 parent 124e835 commit c765d13
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/rules/selector-no-union-class-name/index.js
Expand Up @@ -35,8 +35,10 @@ export default function(actual) {
root.walkRules(/&/, rule => {
const parentNodes = [];

if (rule.parent && rule.parent.selector) {
parseSelector(rule.parent.selector, result, rule, fullSelector => {
const selector = getSelectorFromRule(rule.parent);

if (selector) {
parseSelector(selector, result, rule, fullSelector => {
fullSelector.walk(node => parentNodes.push(node));
});
}
Expand Down Expand Up @@ -67,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);
}
}

0 comments on commit c765d13

Please sign in to comment.