c477768c467f096c66938aa3a998058d39ea9bfd
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Rule to enforce consistent spacing after function names
3  * @author Roberto Vidal
4  * @copyright 2014 Roberto Vidal. All rights reserved.
5  */
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Rule Definition
10 //------------------------------------------------------------------------------
11
12 module.exports = function(context) {
13
14     var requiresSpace = context.options[0] === "always";
15
16     /**
17      * Reports if the give named function node has the correct spacing after its name
18      *
19      * @param {ASTNode} node  The node to which the potential problem belongs.
20      * @returns {void}
21      */
22     function check(node) {
23         var tokens = context.getFirstTokens(node, 3),
24             hasSpace = tokens[1].range[1] < tokens[2].range[0];
25
26         if (hasSpace !== requiresSpace) {
27             context.report(node, "Function name \"{{name}}\" must {{not}}be followed by whitespace.", {
28                 name: node.id.name,
29                 not: requiresSpace ? "" : "not "
30             });
31         }
32     }
33
34     return {
35         "FunctionDeclaration": check,
36         "FunctionExpression": function (node) {
37             if (node.id) {
38                 check(node);
39             }
40         }
41     };
42
43 };