Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/rules/overqualified-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,20 @@ CSSLint.addRule({

parser.addListener("endstylesheet", function(){

var prop;
var equalParts,
lines,
prop;
for (prop in classes){
if (classes.hasOwnProperty(prop)){

//one use means that this is overqualified
if (classes[prop].length === 1 && classes[prop][0].part.elementName){
reporter.report("Element (" + classes[prop][0].part + ") is overqualified, just use " + classes[prop][0].modifier + " without element name.", classes[prop][0].part.line, classes[prop][0].part.col, rule);
equalParts = true;
lines = [];
for (var i = 0; i < classes[prop].length; i++) {
lines.push(classes[prop][i].part.line);
equalParts = equalParts && (classes[prop][i].part.text === classes[prop][0].part.text);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the check for equalParts needed here?

}
//one use or multiple equal uses means that this is overqualified
if (classes[prop][0].part.elementName && equalParts) {
reporter.report("Element (" + classes[prop][0].part + ") is overqualified, just use " + classes[prop][0].modifier + " without element name" + ((lines.length > 1) ? " (found " + lines.length + "x)" : "") + ".", classes[prop][0].part.line, classes[prop][0].part.col, rule);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/rules/overqualified-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
"Using a class with an element and without should not result in a warning": function(){
var result = CSSLint.verify("li.foo { float: left;} .foo { float: right; }", { "overqualified-elements": 1 });
Assert.areEqual(0, result.messages.length);
},

"Using a class with the same element more than once should result in a warning": function(){
var result = CSSLint.verify("li.foo { float: left;} li.foo { float: right; }", { "overqualified-elements": 1 });
Assert.areEqual(1, result.messages.length);
Assert.areEqual("warning", result.messages[0].type);
Assert.areEqual("Element (li.foo) is overqualified, just use .foo without element name (found 2x).", result.messages[0].message);
}

}));
Expand Down