1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | const Container = require('postcss/lib/container');
|
12 |
|
13 | const registerWalker = (constructor) => {
|
14 | let walkerName = `walk${constructor.name}`;
|
15 |
|
16 |
|
17 | if (walkerName.lastIndexOf('s') !== walkerName.length - 1) {
|
18 | walkerName += 's';
|
19 | }
|
20 |
|
21 |
|
22 | if (Container.prototype[walkerName]) {
|
23 | return;
|
24 | }
|
25 |
|
26 |
|
27 | Container.prototype[walkerName] = function walker(callback) {
|
28 | return this.walkType(constructor, callback);
|
29 | };
|
30 | };
|
31 |
|
32 | Container.prototype.walkType = function walkType(type, callback) {
|
33 |
|
34 | if (!type || !callback) {
|
35 | throw new Error('Parameters {type} and {callback} are required.');
|
36 | }
|
37 |
|
38 |
|
39 | const isTypeCallable = typeof type === 'function';
|
40 |
|
41 |
|
42 | return this.walk((node, index) => {
|
43 | if ((isTypeCallable && node instanceof type) || (!isTypeCallable && node.type === type)) {
|
44 | return callback.call(this, node, index);
|
45 | }
|
46 | });
|
47 | };
|
48 |
|
49 | module.exports = { registerWalker };
|