summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/rules/func-names.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/eslint/lib/rules/func-names.js')
-rw-r--r--tools/eslint/lib/rules/func-names.js43
1 files changed, 43 insertions, 0 deletions
diff --git a/tools/eslint/lib/rules/func-names.js b/tools/eslint/lib/rules/func-names.js
new file mode 100644
index 0000000000..eb4d9be10a
--- /dev/null
+++ b/tools/eslint/lib/rules/func-names.js
@@ -0,0 +1,43 @@
+/**
+ * @fileoverview Rule to warn when a function expression does not have a name.
+ * @author Kyle T. Nunery
+ * @copyright 2015 Brandon Mills. All rights reserved.
+ * @copyright 2014 Kyle T. Nunery. All rights reserved.
+ */
+
+"use strict";
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+module.exports = function(context) {
+
+ /**
+ * Determines whether the current FunctionExpression node is a get, set, or
+ * shorthand method in an object literal or a class.
+ * @returns {boolean} True if the node is a get, set, or shorthand method.
+ */
+ function isObjectOrClassMethod() {
+ var parent = context.getAncestors().pop();
+
+ return (parent.type === "MethodDefinition" || (
+ parent.type === "Property" && (
+ parent.method ||
+ parent.kind === "get" ||
+ parent.kind === "set"
+ )
+ ));
+ }
+
+ return {
+ "FunctionExpression": function(node) {
+
+ var name = node.id && node.id.name;
+
+ if (!name && !isObjectOrClassMethod()) {
+ context.report(node, "Missing function expression name.");
+ }
+ }
+ };
+};