Skip to content

Commit 3512256

Browse files
Adam Simpsonrobtarr
authored andcommitted
refactor: pull walk functions into their own funcs
1 parent e385f37 commit 3512256

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

parseSCSS.js

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,50 @@ const parser = (css) => {
88
const splits = [];
99

1010
/**
11-
* @returns {Function} - PostCSS function
11+
* @param {object} at - an at rule, e.g. @media
1212
*/
13-
const brandParse = postcss.plugin('brand-parse', (options) => {
14-
return (global, result) => {
15-
global.walkAtRules(at => {
16-
if (/brand/.test(at.params)) {
17-
if (at.parent.type === 'atrule') {
18-
splits.push(`@${at.parent.name} ${at.parent.params} {`);
19-
splits.push(at.toString());
20-
splits.push('}');
21-
} else {
22-
splits.push(`${at.parent.selector} {`);
23-
splits.push(at.toString());
24-
splits.push('}');
25-
}
13+
const parseAtRule = (at) => {
14+
if (/brand/.test(at.params)) {
15+
if (at.parent.type === 'atrule') {
16+
splits.push(`@${at.parent.name} ${at.parent.params} {`);
17+
splits.push(at.toString());
18+
splits.push('}');
19+
} else {
20+
splits.push(`${at.parent.selector} {`);
21+
splits.push(at.toString());
22+
splits.push('}');
23+
}
24+
25+
at.remove();
26+
}
27+
};
2628

27-
at.remove();
29+
/**
30+
* @param {object} rule - a CSS rule: a selector followed by a declaration block
31+
*/
32+
const parseDecl = (rule) => {
33+
rule.walkDecls(decl => {
34+
if (/brand-/.test(decl.value)) {
35+
if (rule.parent.type === 'atrule') {
36+
splits.push(`@${rule.parent.name} ${rule.parent.params} {`);
37+
splits.push(`${rule.selector} { ${decl.toString()}; }`);
38+
splits.push('}');
39+
} else {
40+
splits.push(`${rule.selector} { ${decl.toString()}; }`);
2841
}
29-
});
3042

31-
global.walkRules(rule => {
32-
rule.walkDecls(decl => {
33-
if (/brand-/.test(decl.value)) {
34-
if (rule.parent.type === 'atrule') {
35-
splits.push(`@${rule.parent.name} ${rule.parent.params} {`);
36-
splits.push(`${rule.selector} { ${decl.toString()}; }`);
37-
splits.push('}');
38-
} else {
39-
splits.push(`${rule.selector} { ${decl.toString()}; }`);
40-
}
41-
decl.remove();
42-
}
43-
});
44-
});
43+
decl.remove();
44+
}
45+
});
46+
};
47+
48+
/**
49+
* @returns {Function} - PostCSS function
50+
*/
51+
const brandParse = postcss.plugin('brand-parse', (options) => {
52+
return (global, result) => {
53+
global.walkAtRules(x => parseAtRule(x));
54+
global.walkRules(x => parseDecl(x));
4555
};
4656
});
4757

@@ -54,10 +64,8 @@ const parser = (css) => {
5464
//use the bubble option to specify mixins to unwrap
5565
const processor = postcss([nest({bubble: ['brand']}), brandParse()]);
5666

57-
return processor.process(css, {syntax})
58-
.then(x => {
59-
return {css: x.css, splits: splits};
60-
});
67+
return processor.process(css, { syntax })
68+
.then(x => ({ css: x.css, splits: splits }));
6169
};
6270

6371
return parseSCSS(css);

0 commit comments

Comments
 (0)