Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Tighter jshint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Dec 9, 2012
1 parent 301a7f6 commit ee6be27
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 37 deletions.
6 changes: 4 additions & 2 deletions .jshintrc
@@ -1,7 +1,9 @@
{
"camelcase": true,
"indent": 2,
"strict": true,
"trailing": true,
"node": true,
"boss": true,
"browser": true,
"boss": true
"node": true
}
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -2,7 +2,7 @@ node_modules:
npm install

test: node_modules
PATH=./node_modules/.bin:$$PATH jshint css-explain.js
PATH=./node_modules/.bin:$$PATH jshint *.js
PATH=./node_modules/.bin:$$PATH nodeunit test.js

.PHONY: test
14 changes: 7 additions & 7 deletions css-explain.js
@@ -1,4 +1,4 @@
(function() {
(function () {
"use strict";

// From Sizzle
Expand Down Expand Up @@ -70,7 +70,7 @@
// Returns a pair with first 'id', 'class', 'tag', or 'universal'.
// Then a String key value
function detectCategoryAndKey(parts) {
var m, last = parts[parts.length-1];
var m, last = parts[parts.length - 1];
if (!last) {
return ['universal', '*'];
} else if (m = last.match(match.ID)) {
Expand All @@ -93,7 +93,7 @@
// Returns an Object with a score, Number 1 through 10 and
// an Array of reason strings.
function analyze(parts) {
var last = parts[parts.length-1];
var last = parts[parts.length - 1];
var score = 1;
var messages = [];

Expand All @@ -116,7 +116,7 @@

// Check for redundant descendant selectors
if (parts.length > 1) {
if (parts[parts.length-2] === '>') {
if (parts[parts.length - 2] === '>') {
messages.push("ID is overly qualified by a child selector");
score++;
} else {
Expand All @@ -131,7 +131,7 @@
score += 1;

if (parts.length > 1) {
if (parts[parts.length-2] === '>') {
if (parts[parts.length - 2] === '>') {
messages.push("Uses a child selector with a rightmost class selector");
score += 2;
} else {
Expand All @@ -146,7 +146,7 @@
score += 2;

if (parts.length > 1) {
if (parts[parts.length-2] === '>') {
if (parts[parts.length - 2] === '>') {
messages.push("Uses a child selector with a rightmost tag selector");
score += 4;
} else {
Expand All @@ -161,7 +161,7 @@
score += 3;

if (parts.length > 1) {
if (parts[parts.length-2] === '>') {
if (parts[parts.length - 2] === '>') {
messages.push("Uses a child selector with a rightmost universal selector");
score += 5;
} else {
Expand Down
56 changes: 29 additions & 27 deletions test.js
@@ -1,4 +1,6 @@
cssExplain = require('./css-explain').cssExplain;
"use strict";

var cssExplain = require('./css-explain').cssExplain;

// References
//
Expand All @@ -7,7 +9,7 @@ cssExplain = require('./css-explain').cssExplain;
// * http://www.stuffandnonsense.co.uk/archives/images/specificitywars-05v2.jpg

exports.selector = {
"simple": function(test) {
"simple": function (test) {
test.equal(cssExplain(null), null);

test.equal(cssExplain(".foo, .bar").selector, ".foo");
Expand All @@ -18,7 +20,7 @@ exports.selector = {
test.done();
},

"multiple": function(test) {
"multiple": function (test) {
var results;

test.equal(cssExplain(null, true)[0], null);
Expand All @@ -40,43 +42,43 @@ exports.selector = {
};

exports.score = {
"descendant selectors with universal selector key": function(test) {
"descendant selectors with universal selector key": function (test) {
test.equal(cssExplain("body *").score, 10);
test.equal(cssExplain(".hide-scrollbars *").score, 10);

test.done();
},

"descendant selectors with tag selector key": function(test) {
"descendant selectors with tag selector key": function (test) {
test.equal(cssExplain("ul li a").score, 8);
test.equal(cssExplain("#footer h3").score, 8);
test.equal(cssExplain("* html #atticPromo ul li a").score, 8);

test.done();
},

"descendant selectors with class selector key": function(test) {
"descendant selectors with class selector key": function (test) {
test.equal(cssExplain("li .item").score, 6);
test.equal(cssExplain("#footer .copyright").score, 6);

test.done();
},

"child selectors with universal selector key": function(test) {
"child selectors with universal selector key": function (test) {
test.equal(cssExplain("body > *").score, 9);
test.equal(cssExplain(".hide-scrollbars > *").score, 9);

test.done();
},

"child selectors with tag selector key": function(test) {
"child selectors with tag selector key": function (test) {
test.equal(cssExplain("ul > li > a").score, 7);
test.equal(cssExplain("#footer > h3").score, 7);

test.done();
},

"overly qualified selectors": function(test) {
"overly qualified selectors": function (test) {
test.equal(cssExplain("ul#top_blue_nav").score, 2);
test.equal(cssExplain("form#UserLogin").score, 2);
test.equal(cssExplain("button#backButton").score, 2);
Expand All @@ -85,7 +87,7 @@ exports.score = {
test.done();
},

"simple": function(test) {
"simple": function (test) {
test.equal(cssExplain("#footer").score, 1);
test.equal(cssExplain(".item").score, 2);
test.equal(cssExplain("li").score, 3);
Expand All @@ -95,37 +97,37 @@ exports.score = {
};

exports.messages = {
"descendant selectors with universal selector key": function(test) {
"descendant selectors with universal selector key": function (test) {
test.deepEqual(cssExplain("body *").messages, ["Uses a descendant selector with a rightmost universal selector"]);
test.done();
},

"descendant selectors with tag selector key": function(test) {
"descendant selectors with tag selector key": function (test) {
test.deepEqual(cssExplain("#footer h3").messages, ["Uses a descendant selector with a rightmost tag selector"]);
test.done();
},

"descendant selectors with class selector key": function(test) {
"descendant selectors with class selector key": function (test) {
test.deepEqual(cssExplain("li .item").messages, ["Uses a descendant selector with a rightmost class selector"]);
test.done();
},

"child selectors with universal selector key": function(test) {
"child selectors with universal selector key": function (test) {
test.deepEqual(cssExplain("body > *").messages, ["Uses a child selector with a rightmost universal selector"]);
test.done();
},

"child selectors with tag selector key": function(test) {
"child selectors with tag selector key": function (test) {
test.deepEqual(cssExplain("#footer > h3").messages, ["Uses a child selector with a rightmost tag selector"]);
test.done();
},

"child selectors with class selector key": function(test) {
"child selectors with class selector key": function (test) {
test.deepEqual(cssExplain("#footer > .copyright").messages, ["Uses a child selector with a rightmost class selector"]);
test.done();
},

"overly qualified selectors": function(test) {
"overly qualified selectors": function (test) {
test.deepEqual(cssExplain("button#backButton").messages, ["ID is overly qualified by a tag name"]);
test.deepEqual(cssExplain(".menu-left#newMenuIcon").messages, ["ID is overly qualified by a class name"]);

Expand All @@ -135,7 +137,7 @@ exports.messages = {
test.done();
},

"simple": function(test) {
"simple": function (test) {
test.deepEqual(cssExplain("#footer").messages, []);
test.deepEqual(cssExplain(".item").messages, []);
test.deepEqual(cssExplain("li").messages, []);
Expand All @@ -145,31 +147,31 @@ exports.messages = {
};

exports.key = {
"id category selectors": function(test) {
"id category selectors": function (test) {
test.equal(cssExplain("button#backButton").key, 'backButton');
test.equal(cssExplain("#urlBar[type=\"autocomplete\"]").key, 'urlBar');
test.equal(cssExplain("treeitem > treerow > treecell#myCell:active").key, 'myCell');

test.done();
},

"class category selectors": function(test) {
"class category selectors": function (test) {
test.equal(cssExplain("button.toolbarButton").key, 'toolbarButton');
test.equal(cssExplain(".fancyText").key, 'fancyText');
test.equal(cssExplain("menuitem > .menu-left[checked=\"true\"]").key, 'menu-left');

test.done();
},

"tag category selectors": function(test) {
"tag category selectors": function (test) {
test.equal(cssExplain("td").key, 'td');
test.equal(cssExplain("treeitem > treerow").key, 'treerow');
test.equal(cssExplain("input[type=\"checkbox\"]").key, 'input');

test.done();
},

"universal category selectors": function(test) {
"universal category selectors": function (test) {
test.equal(cssExplain("").key, '*');
test.equal(cssExplain("[hidden=\"true\"]").key, '*');
test.equal(cssExplain("*").key, '*');
Expand All @@ -180,31 +182,31 @@ exports.key = {
};

exports.category = {
"id category selectors": function(test) {
"id category selectors": function (test) {
test.equal(cssExplain("button#backButton").category, 'id');
test.equal(cssExplain("#urlBar[type=\"autocomplete\"]").category, 'id');
test.equal(cssExplain("treeitem > treerow > treecell#myCell:active").category, 'id');

test.done();
},

"class category selectors": function(test) {
"class category selectors": function (test) {
test.equal(cssExplain("button.toolbarButton").category, 'class');
test.equal(cssExplain(".fancyText").category, 'class');
test.equal(cssExplain("menuitem > .menu-left[checked=\"true\"]").category, 'class');

test.done();
},

"tag category selectors": function(test) {
"tag category selectors": function (test) {
test.equal(cssExplain("td").category, 'tag');
test.equal(cssExplain("treeitem > treerow").category, 'tag');
test.equal(cssExplain("input[type=\"checkbox\"]").category, 'tag');

test.done();
},

"universal category selectors": function(test) {
"universal category selectors": function (test) {
test.equal(cssExplain("[hidden=\"true\"]").category, 'universal');
test.equal(cssExplain("*").category, 'universal');
test.equal(cssExplain("tree > [collapsed=\"true\"]").category, 'universal');
Expand All @@ -213,7 +215,7 @@ exports.category = {
}
};

exports.specificity = function(test) {
exports.specificity = function (test) {
test.deepEqual(cssExplain("").specificity, [0, 0, 0]);

test.deepEqual(cssExplain("*").specificity, [0, 0, 0]);
Expand Down

0 comments on commit ee6be27

Please sign in to comment.