From d5b1bfd4ee4728326629cf828dd2f478a3765197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hubert=20SABLONNI=C3=88RE?= Date: Sun, 24 Nov 2013 20:38:45 +0100 Subject: [PATCH] Add alphabetical order rule --- src/rules/order-alphabetical.js | 45 +++++++++++++++++++++++++++++++ tests/rules/order-alphabetical.js | 36 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/rules/order-alphabetical.js create mode 100644 tests/rules/order-alphabetical.js diff --git a/src/rules/order-alphabetical.js b/src/rules/order-alphabetical.js new file mode 100644 index 00000000..ec16da7c --- /dev/null +++ b/src/rules/order-alphabetical.js @@ -0,0 +1,45 @@ +/* + * Rule: All properties should be in alphabetical order.. + */ +/*global CSSLint*/ +CSSLint.addRule({ + + //rule information + id: "order-alphabetical", + name: "Alphabetical order", + desc: "Assure properties are in alphabetical order", + browsers: "All", + + //initialization + init: function(parser, reporter){ + var rule = this, + properties; + + var startRule = function () { + properties = []; + }; + + parser.addListener("startrule", startRule); + parser.addListener("startfontface", startRule); + parser.addListener("startpage", startRule); + parser.addListener("startpagemargin", startRule); + parser.addListener("startkeyframerule", startRule); + + parser.addListener("property", function(event){ + var name = event.property.text, + lowerCasePrefixLessName = name.toLowerCase().replace(/^-.*?-/, ""); + + properties.push(lowerCasePrefixLessName); + }); + + parser.addListener("endrule", function(event){ + var currentProperties = properties.join(','), + expectedProperties = properties.sort().join(','); + + if (currentProperties !== expectedProperties){ + reporter.report("Rule doesn't have all its properties in alphabetical ordered.", event.line, event.col, rule); + } + }); + } + +}); diff --git a/tests/rules/order-alphabetical.js b/tests/rules/order-alphabetical.js new file mode 100644 index 00000000..277a7e79 --- /dev/null +++ b/tests/rules/order-alphabetical.js @@ -0,0 +1,36 @@ +(function(){ + + /*global YUITest, CSSLint*/ + var Assert = YUITest.Assert; + + YUITest.TestRunner.add(new YUITest.TestCase({ + + name: "Alphabetical order Errors", + + "Rules with properties not in alphabetical order should result in a warning": function(){ + var result = CSSLint.verify("li { z-index: 2; color: red; }", { "order-alphabetical": 1 }); + Assert.areEqual(1, result.messages.length); + Assert.areEqual("warning", result.messages[0].type); + Assert.areEqual("Rule doesn't have all its properties in alphabetical ordered.", result.messages[0].message); + }, + + "Rules with prefixed properties not in alphabetical order (without the prefix) should result in a warning": function(){ + var result = CSSLint.verify("li { -moz-transition: none; -webkit-box-shadow: none; }", { "order-alphabetical": 1 }); + Assert.areEqual(1, result.messages.length); + Assert.areEqual("warning", result.messages[0].type); + Assert.areEqual("Rule doesn't have all its properties in alphabetical ordered.", result.messages[0].message); + }, + + "Rules with properties in alphabetical order should not result in a warning": function(){ + var result = CSSLint.verify("li { box-shadow: none; color: red; transition: none; }", { "order-alphabetical": 1 }); + Assert.areEqual(0, result.messages.length); + }, + + "Rules with prefixed properties in alphabetical order should not result in a warning": function(){ + var result = CSSLint.verify("li { -webkit-box-shadow: none; color: red; -moz-transition: none; }", { "order-alphabetical": 1 }); + Assert.areEqual(0, result.messages.length); + } + + })); + +})();