-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (38 loc) · 995 Bytes
/
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
36
37
38
39
40
41
42
43
44
45
46
var fs = require('fs');
var path = require('path');
var postcss = require('postcss');
var defaultPattern = 'Regexp matched with %s on line %l';
var postcssResult;
module.exports = postcss.plugin('postcss-regexp', function(options) {
return function(css, result) {
postcssResult = result;
if (!options.regexp) {
throwOptionsError();
return;
}
css.eachDecl(function(decl) {
if (decl.value) {
processDecl(decl, options);
}
})
}
});
function throwOptionsError(){
postcssResult.messages.push({
type: 'error',
plugin: 'postcss-regexp',
text: 'No regexp provided'
});
}
function processDecl(decl, rule) {
var value = decl.value;
if (value.search(rule.regexp) !== -1) {
var pattern = rule.messagePattern || defaultPattern;
var from = postcssResult.opts.from || '[source file not specified]';
var message = pattern
.replace('%s', value)
.replace('%l', decl.source.start.line)
.replace('%f', from);
postcssResult.warn(message);
}
}