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

Add quote-props rule #17

Merged
merged 1 commit into from Jul 7, 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
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);