Skip to content

Commit 0485042

Browse files
Add duplicate property-value pairs rule
1 parent 627ea5a commit 0485042

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Rule: Be aware of duplicate property-value pairs.
3+
*/
4+
/*global CSSLint*/
5+
CSSLint.addRule({
6+
7+
//rule information
8+
id: "duplicate-property-value-pairs",
9+
name: "Duplicate property-value pairs",
10+
desc: "Be aware of duplicate property-value pairs. Many duplicates may indicate the need for abstratcion.",
11+
browsers: "All",
12+
13+
//initialization
14+
init: function(parser, reporter){
15+
var rule = this;
16+
var count = {};
17+
18+
//count how many times "float" is used
19+
parser.addListener("property", function(event){
20+
var prop = event.property.text.toLowerCase(),
21+
val = event.value.text.toLowerCase(),
22+
key = prop + ':' + val;
23+
24+
if (!count[key]) {
25+
count[key] = 0;
26+
}
27+
28+
count[key] += 1;
29+
});
30+
31+
//report the results
32+
parser.addListener("endstylesheet", function(){
33+
var data = [],
34+
msg = [];
35+
36+
for (var prop in count) {
37+
if (count.hasOwnProperty(prop)) {
38+
data.push([prop, count[prop]]);
39+
}
40+
}
41+
42+
data.sort(function (a, b) {
43+
return b[1] - a[1];
44+
});
45+
46+
data = data.map(function (item) {
47+
return item[1] + ' => ' + item[0];
48+
}).join('\n');
49+
50+
reporter.rollupWarn('Duplicate property-value-pairs:\n\n' + data);
51+
});
52+
}
53+
54+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
(function(){
2+
3+
/*global YUITest, CSSLint*/
4+
var Assert = YUITest.Assert;
5+
6+
YUITest.TestRunner.add(new YUITest.TestCase({
7+
8+
name: "Duplicate Property-Value Pair Rule Errors",
9+
10+
"Duplicate property-value pairs should result in a warning": function () {
11+
var result = CSSLint.verify(".foo { color: #f00; } .bar { color: #f00; }", {"duplicate-property-value-pairs": 1});
12+
Assert.areEqual(1, result.messages.length);
13+
Assert.areEqual("warning", result.messages[0].type);
14+
Assert.areEqual("Duplicate property-value-pairs:\n\n2 => color:#f00", result.messages[0].message);
15+
}
16+
}));
17+
18+
})();

0 commit comments

Comments
 (0)