summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/rules/object-shorthand.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/eslint/lib/rules/object-shorthand.js')
-rw-r--r--tools/eslint/lib/rules/object-shorthand.js63
1 files changed, 63 insertions, 0 deletions
diff --git a/tools/eslint/lib/rules/object-shorthand.js b/tools/eslint/lib/rules/object-shorthand.js
new file mode 100644
index 0000000000..548c9245d6
--- /dev/null
+++ b/tools/eslint/lib/rules/object-shorthand.js
@@ -0,0 +1,63 @@
+/**
+ * @fileoverview Rule to enforce concise object methods and properties.
+ * @author Jamund Ferguson
+ * @copyright 2015 Jamund Ferguson. All rights reserved.
+ */
+
+"use strict";
+
+var OPTIONS = {
+ always: "always",
+ never: "never",
+ methods: "methods",
+ properties: "properties"
+};
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+module.exports = function(context) {
+
+ var APPLY = context.options[0] || OPTIONS.always;
+ var APPLY_TO_METHODS = APPLY === OPTIONS.methods || APPLY === OPTIONS.always;
+ var APPLY_TO_PROPS = APPLY === OPTIONS.properties || APPLY === OPTIONS.always;
+ var APPLY_NEVER = APPLY === OPTIONS.never;
+
+ //--------------------------------------------------------------------------
+ // Public
+ //--------------------------------------------------------------------------
+
+ return {
+ "Property": function(node) {
+ var isConciseProperty = node.method || node.shorthand,
+ type;
+
+ // if we're "never" and concise we should warn now
+ if (APPLY_NEVER && isConciseProperty) {
+ type = node.method ? "method" : "property";
+ context.report(node, "Expected longform " + type + " syntax.");
+ }
+
+ // at this point if we're concise or if we're "never" we can leave
+ if (APPLY_NEVER || isConciseProperty) {
+ return;
+ }
+
+ if (node.value.type === "ArrowFunctionExpression" && APPLY_TO_METHODS) {
+
+ // {x: ()=>{}} should be written as {x() {}}
+ context.report(node, "Expected method shorthand.");
+ } else if (node.value.type === "FunctionExpression" && APPLY_TO_METHODS) {
+
+ // {x: function(){}} should be written as {x() {}}
+ context.report(node, "Expected method shorthand.");
+ } else if (node.key.name === node.value.name && APPLY_TO_PROPS) {
+
+ // {x: x} should be written as {x}
+ context.report(node, "Expected property shorthand.");
+ }
+ }
+ };
+
+};