From d3efd6a3f1c6cf3ae573c0078d1fb98d2fbb966e Mon Sep 17 00:00:00 2001 From: Mathias Bynens Date: Sun, 7 Jul 2013 11:29:12 +0200 Subject: [PATCH] Add `quote-props` rule Closes #17. --- config/eslint.json | 3 +- lib/rules/quote-props.js | 27 ++++++++++++ tests/lib/rules/quote-props.js | 78 ++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 lib/rules/quote-props.js create mode 100644 tests/lib/rules/quote-props.js diff --git a/config/eslint.json b/config/eslint.json index e20408d1fd9..ad2f826497e 100644 --- a/config/eslint.json +++ b/config/eslint.json @@ -13,7 +13,8 @@ "camelcase": 1, "curly": 1, "eqeqeq": 1, - "new-cap": 1 + "new-cap": 1, + "quote-props": 0 } } diff --git a/lib/rules/quote-props.js b/lib/rules/quote-props.js new file mode 100644 index 00000000000..2f66f3e2c1f --- /dev/null +++ b/lib/rules/quote-props.js @@ -0,0 +1,27 @@ +/** + * @fileoverview Rule to flag non-quoted property names in object literals. + * @author Mathias Bynens + */ + +/*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."); + } + + } + }; + +}; diff --git a/tests/lib/rules/quote-props.js b/tests/lib/rules/quote-props.js new file mode 100644 index 00000000000..67bb4a6d8cf --- /dev/null +++ b/tests/lib/rules/quote-props.js @@ -0,0 +1,78 @@ +/** + * @fileoverview Tests for quote-props rule. + * @author Mathias Bynens + */ + +/*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);