Skip to content

Commit

Permalink
Merge pull request #347 from jantimon/bugfix/selector-no-union-class-…
Browse files Browse the repository at this point in the history
…name-nesting

Bugfix/selector no union class name nesting
  • Loading branch information
kristerkari committed Jul 18, 2019
2 parents 147b44c + c765d13 commit c10b8a2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
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);
}
}

0 comments on commit c10b8a2

Please sign in to comment.