summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/rules/func-style.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/eslint/lib/rules/func-style.js')
-rw-r--r--tools/eslint/lib/rules/func-style.js43
1 files changed, 43 insertions, 0 deletions
diff --git a/tools/eslint/lib/rules/func-style.js b/tools/eslint/lib/rules/func-style.js
new file mode 100644
index 0000000000..f54350e765
--- /dev/null
+++ b/tools/eslint/lib/rules/func-style.js
@@ -0,0 +1,43 @@
+/**
+ * @fileoverview Rule to enforce a particular function style
+ * @author Nicholas C. Zakas
+ * @copyright 2013 Nicholas C. Zakas. All rights reserved.
+ */
+"use strict";
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+module.exports = function(context) {
+
+ var style = context.options[0],
+ enforceDeclarations = (style === "declaration");
+
+ return {
+
+ "FunctionDeclaration": function(node) {
+ if (!enforceDeclarations) {
+ context.report(node, "Expected a function expression.");
+ }
+ },
+
+ "FunctionExpression": function() {
+ var parent = context.getAncestors().pop();
+
+ if (enforceDeclarations && parent.type === "VariableDeclarator") {
+ context.report(parent, "Expected a function declaration.");
+ }
+ },
+
+ "ArrowFunctionExpression": function() {
+ var parent = context.getAncestors().pop();
+
+ if (enforceDeclarations && parent.type === "VariableDeclarator") {
+ context.report(parent, "Expected a function declaration.");
+ }
+ }
+
+ };
+
+};