Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The same overqualification can occur more than once #614

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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