New rule which looks for new-line characters in selectors #282

Closed
wants to merge 1 commit into from
View
37 src/rules/no-new-lines-in-selectors.js
@@ -0,0 +1,37 @@
+/*
+ * Rule: Avoid new-line characters in selectors.
+ */
+
+/*jslint plusplus:false*/
+/*global CSSLint*/
+
+CSSLint.addRule({
+
+ //rule information
+ id: "no-new-lines-in-selectors",
+ name: "Disallow new-line characters in selectors",
+ desc: "New-line characters in selectors are usually a forgotten comma and not a descendant combinator.",
+ browsers: "All",
+
+ //initialization
+ init: function (parser, reporter) {
+ var rule = this;
+
+ parser.addListener("startrule", function (event) {
+ var i, len, selectors, selector, p, pLen, part, previousLine, currentLine;
+ selectors = event.selectors;
+
+ for (i = 0, len = selectors.length; i < len; i++) {
+ selector = selectors[i];
+ for (p = 0, pLen = selector.parts.length; p < pLen; p++) {
+ part = selector.parts[p];
+ currentLine = part.line;
+ if (currentLine === previousLine) {
+ reporter.report("new-line character found in selector (forgot a comma?)", currentLine, selectors[i].parts[0].col, rule);
+ }
+ previousLine = currentLine;
+ }
+ }
+ });
+ }
+});
View
34 tests/rules/no-new-lines-in-selectors.js
@@ -0,0 +1,34 @@
+/*global YUITest, CSSLint*/
+(function () {
+
+ var ruleId = "no-new-lines-in-selectors", expectWarning, expectPass;
+
+ expectWarning = function (ruleset, expectedMessage) {
+ var result, enabledRules = {};
+ enabledRules[ruleId] = 1;
+ result = CSSLint.verify(ruleset, enabledRules);
+ YUITest.Assert.areEqual(1, result.messages.length);
+ YUITest.Assert.areEqual("warning", result.messages[0].type);
+ YUITest.Assert.areEqual(expectedMessage, result.messages[0].message);
+ };
+
+ expectPass = function (ruleset) {
+ var result, enabledRules = {};
+ enabledRules[ruleId] = 1;
+ result = CSSLint.verify(ruleset, enabledRules);
+ YUITest.Assert.areEqual(0, result.messages.length);
+ };
+
+ YUITest.TestRunner.add(new YUITest.TestCase({
+
+ name: ruleId + " Rule Errors",
+
+ "a new-line in a selector should result in a warning": function () {
+ expectWarning(".foo\n.bar{}", "new-line character found in selector (forgot a comma?)");
+ },
+ "a new-line between selectors should not result in a warning": function () {
+ expectPass(".foo,\n.bar{}");
+ }
+ }));
+
+}());