Don't mutate nested rules when generating variants

This commit is contained in:
Adam Wathan 2019-07-28 10:46:59 -04:00
parent 27a9cd9456
commit 660ea44db2
2 changed files with 42 additions and 1 deletions

View File

@ -393,6 +393,43 @@ test('the default variant can be generated in a specified position', () => {
})
})
test('nested rules are not modified', () => {
const input = `
@variants focus, active, hover {
.banana {
color: yellow;
.chocolate { color: brown; }
}
}
`
const output = `
.banana {
color: yellow;
.chocolate { color: brown; }
}
.focus\\:banana:focus {
color: yellow;
.chocolate { color: brown; }
}
.active\\:banana:active {
color: yellow;
.chocolate { color: brown; }
}
.hover\\:banana:hover {
color: yellow;
.chocolate { color: brown; }
}
`
return run(input, {
...config,
}).then(result => {
expect(result.css).toMatchCss(output)
expect(result.warnings().length).toBe(0)
})
})
test('plugin variants can modify rules using the raw PostCSS API', () => {
const input = `
@variants important {

View File

@ -12,7 +12,11 @@ export default function generateVariantFunction(generator) {
container: cloned,
separator: config.separator,
modifySelectors: modifierFunction => {
cloned.walkRules(rule => {
cloned.each(rule => {
if (rule.type !== 'rule') {
return
}
rule.selectors = rule.selectors.map(selector => {
const className = selectorParser(selectors => {
return selectors.first.filter(({ type }) => type === 'class').pop().value