Skip to content

Commit 71b9d22

Browse files
committed
Don't mutate nested rules when generating variants
1 parent 5a6894e commit 71b9d22

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

__tests__/variantsAtRule.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,43 @@ test('the default variant can be generated in a specified position', () => {
267267
})
268268
})
269269

270+
test('nested rules are not modified', () => {
271+
const input = `
272+
@variants focus, active, hover {
273+
.banana {
274+
color: yellow;
275+
.chocolate { color: brown; }
276+
}
277+
}
278+
`
279+
280+
const output = `
281+
.banana {
282+
color: yellow;
283+
.chocolate { color: brown; }
284+
}
285+
.focus\\:banana:focus {
286+
color: yellow;
287+
.chocolate { color: brown; }
288+
}
289+
.active\\:banana:active {
290+
color: yellow;
291+
.chocolate { color: brown; }
292+
}
293+
.hover\\:banana:hover {
294+
color: yellow;
295+
.chocolate { color: brown; }
296+
}
297+
`
298+
299+
return run(input, {
300+
...config,
301+
}).then(result => {
302+
expect(result.css).toMatchCss(output)
303+
expect(result.warnings().length).toBe(0)
304+
})
305+
})
306+
270307
test('plugin variants can modify rules using the raw PostCSS API', () => {
271308
const input = `
272309
@variants important {

src/util/generateVariantFunction.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ export default function generateVariantFunction(generator) {
1212
container: cloned,
1313
separator: config.separator,
1414
modifySelectors: modifierFunction => {
15-
cloned.walkRules(rule => {
15+
cloned.each(rule => {
16+
if (rule.type !== 'rule') {
17+
return
18+
}
19+
1620
rule.selectors = rule.selectors.map(selector => {
1721
const className = selectorParser(selectors => {
1822
return selectors.first.filter(({ type }) => type === 'class').pop().value

0 commit comments

Comments
 (0)