-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathrule-within-rule.ts
More file actions
39 lines (30 loc) · 1.24 KB
/
rule-within-rule.ts
File metadata and controls
39 lines (30 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import shiftNodesBeforeParent from './shift-nodes-before-parent.js';
import cleanupParent from './cleanup-parent.js';
import mergeSelectors from './merge-selectors/merge-selectors.js';
import type { Result, Rule } from 'postcss';
import groupDeclarations from './group-declarations.js';
export default function transformRuleWithinRule(node: Rule, parent: Rule, result: Result): void {
let selectors: Array<string>;
try {
selectors = mergeSelectors(node, result, parent.selectors, node.selectors);
} catch (err) {
if (!(err instanceof Error)) {
throw err;
}
node.warn(result, `Failed to transform selectors : "${parent.selector}" / "${node.selector}" with message: "${err.message}"`);
return;
}
// Group all declarations after the first one.
groupDeclarations(parent);
// move previous siblings and the node to before the parent
shiftNodesBeforeParent(node, parent);
// update the selectors of the node to be merged with the parent
node.selectors = selectors;
// merge similar rules back together
const areSameRule = (node.type === 'rule' && parent.type === 'rule' && node.selector === parent.selector);
if (areSameRule) {
node.append(...parent.nodes);
}
// conditionally cleanup an empty parent rule
cleanupParent(parent);
}