Skip to content

Commit ce7b22b

Browse files
author
epavlenko
committed
Use message pattern
1 parent 33f1686 commit ce7b22b

File tree

3 files changed

+40
-31
lines changed

3 files changed

+40
-31
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ var css = fs.readFileSync("input.css", "utf8")
2222

2323
// process css
2424
var output = postcss()
25-
.use(regexp)
25+
.use(regexp({
26+
regexp: new RegExp('.+px'),
27+
messagePattern: 'Pixel value %s found on line %l'
28+
}))
2629
.use(reporter)
2730
.process(css, {
2831
from: "src/stylesheet/index.css"
@@ -38,10 +41,13 @@ Type: `String` or `RegExp`
3841

3942
Regexp to match
4043

41-
#### `message` (optional)
44+
#### `messagePattern` (optional)
4245
Type: `String`
43-
Default: `Regexp matched with`
46+
Default: `Regexp matched with %s on line %l`
47+
48+
Pattern for message text.
49+
* %s is for matched strings
50+
* %l is for line number
4451

45-
Message text. For expample you'll get 'Regexp matched with _$variable_ on line 134'
4652

4753
## [License](LICENSE)

index.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,42 @@ var fs = require('fs');
22
var path = require('path');
33
var postcss = require('postcss');
44

5-
var messageText = 'Regexp matched with';
6-
var regexp;
5+
var defaultPattern = 'Regexp matched with %s on line %l';
6+
var logger;
77

8-
module.exports = postcss.plugin('postcss-regexp', function(options, t) {
8+
module.exports = postcss.plugin('postcss-regexp', function(options) {
99
return function(css, result) {
10+
logger = result;
1011
if (!options.regexp) {
11-
throwOptionsError(result);
12+
throwOptionsError();
1213
return;
1314
}
14-
messageText = options.message ? options.message : messageText;
15-
regexp = options.regexp;
1615

1716
css.eachDecl(function(decl) {
1817
if (decl.value) {
19-
processDecl(decl, result);
18+
processDecl(decl, options);
2019
}
2120
})
2221
}
2322
});
24-
function throwOptionsError(result){
25-
result.messages.push({
23+
24+
function throwOptionsError(){
25+
logger.messages.push({
2626
type: 'error',
2727
plugin: 'postcss-regexp',
2828
text: 'No regexp provided'
2929
});
3030
}
3131

32-
function processDecl(decl, result) {
32+
function processDecl(decl, rule) {
3333
var value = decl.value;
34+
if (value.search(rule.regexp) === 0) {
35+
var pattern = rule.messagePattern || defaultPattern;
36+
var message = pattern
37+
.replace('%s', value)
38+
.replace('%l', decl.source.start.line);
39+
logger.warn(message);
40+
}
41+
3442

35-
if (value.search(regexp) === 0) {
36-
var message = messageText + ' ' + value
37-
+ ' on line ' + decl.source.start.line;
38-
result.warn(message);
39-
}
4043
}

test/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ var chalk = require('chalk')
33
var regexpPlugin = require('..');
44
var fs = require('fs');
55
var postcss = require('postcss');
6-
6+
var reporter = require('postcss-reporter')
77
var source = fs.readFileSync('test/fixtures/test.css', 'utf8').trim();
88

9+
var defaultRegexp = new RegExp('^\\$.*');
10+
911
function composeMessage(text) {
1012
return {
1113
type: 'warning',
@@ -22,12 +24,13 @@ function assertMessages(messages, expected) {
2224
assert.equal(message.text, expected[index].text);
2325
})
2426
}
25-
function exec(expected, regexp, message) {
27+
function exec(expected, regexp, messagePattern) {
2628
return postcss()
2729
.use(regexpPlugin({
28-
message: message,
30+
messagePattern: messagePattern,
2931
regexp: regexp
3032
}))
33+
.use(reporter)
3134
.process(source)
3235
.then(function(result) {
3336
assertMessages(result.messages, expected)
@@ -37,8 +40,7 @@ function exec(expected, regexp, message) {
3740
describe('matching: ', function() {
3841
it('Should warn if regexp matched once', function() {
3942
var expected = [composeMessage('Regexp matched with $var on line 4')];
40-
var regexp = new RegExp('^\\$.*');
41-
return exec(expected, regexp);
43+
return exec(expected, defaultRegexp);
4244
});
4345
it('Should warn if regexp matched multiple times', function() {
4446
var expected = [
@@ -58,18 +60,16 @@ describe('options: ', function() {
5860
plugin: 'postcss-regexp',
5961
text: 'No regexp provided'
6062
}];
61-
return exec(expected);
63+
return exec(expected, null);
6264
});
6365

6466
it('Should use standard message \'Regexp matched with\' by default', function() {
6567
var expected = [composeMessage('Regexp matched with $var on line 4')];
66-
var regexp = new RegExp('^\\$.*');
67-
return exec(expected, regexp);
68+
return exec(expected, defaultRegexp);
6869
});
6970

70-
it('Should use custom message if provided', function() {
71-
var expected = [composeMessage('Hello $var on line 4')];
72-
var regexp = new RegExp('^\\$.*');
73-
return exec(expected, regexp, 'Hello');
71+
it('Should use custom message pattern if provided', function() {
72+
var expected = [composeMessage('Hello $var line 4')];
73+
return exec(expected, defaultRegexp, 'Hello %s line %l');
7474
});
7575
});

0 commit comments

Comments
 (0)