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

Rule: count-spaces for multiple spaces in regular expressions #141

Merged
merged 3 commits into from
Jul 27, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions conf/eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"quotes": [1, "double"],
"max-params": [0, 3],
"max-statements": [0, 10],
"count-spaces": 1,
"complexity": [0, 11]

}
Expand Down
31 changes: 31 additions & 0 deletions lib/rules/count-spaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @fileoverview Rule to count multiple spaces in regular expressions
* @author Matt DuVall <http://www.mattduvall.com/>
*/

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {

return {

"Literal": function(node) {
var token = context.getTokens(node)[0],
nodeType = token.type,
nodeValue = token.value,
multipleSpacesRegex = /( {2,})+?/,
regexResults;

if (nodeType === "RegularExpression") {
regexResults = multipleSpacesRegex.exec(nodeValue);

if (regexResults !== null) {
context.report(node, "Spaces are hard to count. Use {" + regexResults[0].length + "}.");
}
}
}
};

};
75 changes: 75 additions & 0 deletions tests/lib/rules/count-spaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* @fileoverview Tests for the count-spaces rule
* @author Matt DuVall <http://www.mattduvall.com/>
*/

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var vows = require("vows"),
assert = require("assert"),
eslint = require("../../../lib/eslint");

//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------

var RULE_ID = "count-spaces";

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

vows.describe(RULE_ID).addBatch({

"when evaluating a regular expression with multiple spaces": {

topic: "var foo = /bar baz/;",

"should report a violation": function(topic) {

var config = { rules: {} };
config.rules[RULE_ID] = 1;

var messages = eslint.verify(topic, config);

assert.equal(messages.length, 1);
assert.equal(messages[0].ruleId, RULE_ID);
assert.equal(messages[0].message, "Spaces are hard to count. Use {4}.");
assert.include(messages[0].node.type, "Literal");
}
},

"when evaluating a regular expression with the reptition operator": {

topic: "var foo = /bar {3}baz/;",

"should not report a violation": function(topic) {

var config = { rules: {} };
config.rules[RULE_ID] = 1;

var messages = eslint.verify(topic, config);

assert.equal(messages.length, 0);
}
},

"when evaluating a regular expression with multiple non-space whitespace": {

topic: "var foo = /bar\t\t\tbaz/;",

"should not report a violation": function(topic) {

var config = { rules: {} };
config.rules[RULE_ID] = 1;

var messages = eslint.verify(topic, config);

assert.equal(messages.length, 0);
}
}


}).export(module);