Skip to content

Commit d5b1bfd

Browse files
committed
Add alphabetical order rule
1 parent a0bce62 commit d5b1bfd

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/rules/order-alphabetical.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Rule: All properties should be in alphabetical order..
3+
*/
4+
/*global CSSLint*/
5+
CSSLint.addRule({
6+
7+
//rule information
8+
id: "order-alphabetical",
9+
name: "Alphabetical order",
10+
desc: "Assure properties are in alphabetical order",
11+
browsers: "All",
12+
13+
//initialization
14+
init: function(parser, reporter){
15+
var rule = this,
16+
properties;
17+
18+
var startRule = function () {
19+
properties = [];
20+
};
21+
22+
parser.addListener("startrule", startRule);
23+
parser.addListener("startfontface", startRule);
24+
parser.addListener("startpage", startRule);
25+
parser.addListener("startpagemargin", startRule);
26+
parser.addListener("startkeyframerule", startRule);
27+
28+
parser.addListener("property", function(event){
29+
var name = event.property.text,
30+
lowerCasePrefixLessName = name.toLowerCase().replace(/^-.*?-/, "");
31+
32+
properties.push(lowerCasePrefixLessName);
33+
});
34+
35+
parser.addListener("endrule", function(event){
36+
var currentProperties = properties.join(','),
37+
expectedProperties = properties.sort().join(',');
38+
39+
if (currentProperties !== expectedProperties){
40+
reporter.report("Rule doesn't have all its properties in alphabetical ordered.", event.line, event.col, rule);
41+
}
42+
});
43+
}
44+
45+
});

tests/rules/order-alphabetical.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
(function(){
2+
3+
/*global YUITest, CSSLint*/
4+
var Assert = YUITest.Assert;
5+
6+
YUITest.TestRunner.add(new YUITest.TestCase({
7+
8+
name: "Alphabetical order Errors",
9+
10+
"Rules with properties not in alphabetical order should result in a warning": function(){
11+
var result = CSSLint.verify("li { z-index: 2; color: red; }", { "order-alphabetical": 1 });
12+
Assert.areEqual(1, result.messages.length);
13+
Assert.areEqual("warning", result.messages[0].type);
14+
Assert.areEqual("Rule doesn't have all its properties in alphabetical ordered.", result.messages[0].message);
15+
},
16+
17+
"Rules with prefixed properties not in alphabetical order (without the prefix) should result in a warning": function(){
18+
var result = CSSLint.verify("li { -moz-transition: none; -webkit-box-shadow: none; }", { "order-alphabetical": 1 });
19+
Assert.areEqual(1, result.messages.length);
20+
Assert.areEqual("warning", result.messages[0].type);
21+
Assert.areEqual("Rule doesn't have all its properties in alphabetical ordered.", result.messages[0].message);
22+
},
23+
24+
"Rules with properties in alphabetical order should not result in a warning": function(){
25+
var result = CSSLint.verify("li { box-shadow: none; color: red; transition: none; }", { "order-alphabetical": 1 });
26+
Assert.areEqual(0, result.messages.length);
27+
},
28+
29+
"Rules with prefixed properties in alphabetical order should not result in a warning": function(){
30+
var result = CSSLint.verify("li { -webkit-box-shadow: none; color: red; -moz-transition: none; }", { "order-alphabetical": 1 });
31+
Assert.areEqual(0, result.messages.length);
32+
}
33+
34+
}));
35+
36+
})();

0 commit comments

Comments
 (0)