UNPKG

1.52 kBJavaScriptView Raw
1// tooling
2import transformDecl from './transform-decl';
3import transformAtrule from './transform-atrule';
4import transformEachAtrule from './transform-each-atrule';
5import transformForAtrule from './transform-for-atrule';
6import transformIfAtrule from './transform-if-atrule';
7import transformRule from './transform-rule';
8
9// transform all nodes
10export default function transformNode(node, result, opts) {
11 // walk the children of the current node
12 getNodesArray(node).forEach(
13 childNode => {
14 if ('atrule' === childNode.type) {
15 if ('each' === childNode.name) {
16 // transform @each at-rules
17 transformEachAtrule(childNode, result, opts);
18 } else if ('if' === childNode.name) {
19 // transform @if at-rules
20 transformIfAtrule(childNode, result, opts);
21 } else if ('for' === childNode.name) {
22 // transform @for at-rules
23 transformForAtrule(childNode, result, opts);
24 } else {
25 // transform all other at-rules
26 transformAtrule(childNode, result, opts);
27 }
28 } else if ('decl' === childNode.type) {
29 // transform declarations
30 transformDecl(childNode, result, opts);
31 } else if ('rule' === childNode.type) {
32 // transform rule
33 transformRule(childNode, result, opts);
34 }
35
36 // conditionally walk grandchildren if the current child still has a parent
37 if (childNode.parent) {
38 transformNode(childNode, result, opts);
39 }
40 }
41 );
42}
43
44// return the children of a node as an array
45const getNodesArray = node => Array.from(Object(node).nodes || []);