summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/rules/space-after-function-name.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/eslint/lib/rules/space-after-function-name.js')
-rw-r--r--tools/eslint/lib/rules/space-after-function-name.js43
1 files changed, 43 insertions, 0 deletions
diff --git a/tools/eslint/lib/rules/space-after-function-name.js b/tools/eslint/lib/rules/space-after-function-name.js
new file mode 100644
index 0000000000..c477768c46
--- /dev/null
+++ b/tools/eslint/lib/rules/space-after-function-name.js
@@ -0,0 +1,43 @@
+/**
+ * @fileoverview Rule to enforce consistent spacing after function names
+ * @author Roberto Vidal
+ * @copyright 2014 Roberto Vidal. All rights reserved.
+ */
+"use strict";
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+module.exports = function(context) {
+
+ var requiresSpace = context.options[0] === "always";
+
+ /**
+ * Reports if the give named function node has the correct spacing after its name
+ *
+ * @param {ASTNode} node The node to which the potential problem belongs.
+ * @returns {void}
+ */
+ function check(node) {
+ var tokens = context.getFirstTokens(node, 3),
+ hasSpace = tokens[1].range[1] < tokens[2].range[0];
+
+ if (hasSpace !== requiresSpace) {
+ context.report(node, "Function name \"{{name}}\" must {{not}}be followed by whitespace.", {
+ name: node.id.name,
+ not: requiresSpace ? "" : "not "
+ });
+ }
+ }
+
+ return {
+ "FunctionDeclaration": check,
+ "FunctionExpression": function (node) {
+ if (node.id) {
+ check(node);
+ }
+ }
+ };
+
+};