Skip to content

Commit

Permalink
Merge pull request #17 from mathiasbynens/quote-props
Browse files Browse the repository at this point in the history
Add `quote-props` rule
  • Loading branch information
nzakas committed Jul 7, 2013
2 parents 16585dd + d3efd6a commit 3ee21c5
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
3 changes: 2 additions & 1 deletion config/eslint.json
Expand Up @@ -13,7 +13,8 @@
"camelcase": 1,
"curly": 1,
"eqeqeq": 1,
"new-cap": 1
"new-cap": 1,
"quote-props": 0

}
}
27 changes: 27 additions & 0 deletions lib/rules/quote-props.js
@@ -0,0 +1,27 @@
/**
* @fileoverview Rule to flag non-quoted property names in object literals.
* @author Mathias Bynens <http://mathiasbynens.be/>
*/

/*jshint node:true*/

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

module.exports = function(context) {

return {

"Property": function(node) {
var key = node.key;

// Check if the property name is quoted
if (key.type != "Literal") {
context.report(node, "Non-quoted property `" + key.name + "` found.");
}

}
};

};
78 changes: 78 additions & 0 deletions tests/lib/rules/quote-props.js
@@ -0,0 +1,78 @@
/**
* @fileoverview Tests for quote-props rule.
* @author Mathias Bynens <http://mathiasbynens.be/>
*/

/*jshint node:true*/

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

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

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

var RULE_ID = "quote-props";

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

vows.describe(RULE_ID).addBatch({

"when evaluating `var x = { foo: 42 }`": {

topic: "var x = { foo: 42 }",

"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, "Non-quoted property `foo` found.");
assert.include(messages[0].node.type, "Property");
assert.include(messages[0].node.key.name, "foo");
}
},

"when evaluating `var x = { 'foo': 42 }`": {

topic: "var x = { 'foo': 42 }",

"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 `var x = { \"foo\": 42 }`": {

topic: "var x = { \"foo\": 42 }",

"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);

0 comments on commit 3ee21c5

Please sign in to comment.