-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (31 loc) · 1.04 KB
/
index.js
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
const postcss = require('postcss');
const difference = require('lodash.difference');
module.exports = postcss.plugin('postcss-class-checker', (opts) => {
if (typeof opts.expected !== 'object' && typeof opts.blacklist !== 'object') {
throw new Error('You need to pass expected or blacklisted selectors / rules');
}
return (root) => {
// Build array of all available selectors
const selectors = [];
root.walkRules((rule) => {
selectors.push(rule.selectors[0]);
});
// Expected selectors
const missingRules = difference(opts.expected, selectors);
if (missingRules.length > 0) {
throw new Error(
`You are missing expected classes: \n${
missingRules}`,
);
}
// Blacklisted selectors
const blacklistPrep = difference(opts.blacklist, selectors);
const blacklistMatches = difference(opts.blacklist, blacklistPrep);
if (blacklistMatches.length > 0) {
throw new Error(
`You are using blacklisted classes: \n${
blacklistMatches}`,
);
}
};
});