b40351d0b9f8e7c1d5b17d1070c6802dc0a904a6
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Rule to validate spacing before function parentheses.
3  * @author Mathias Schreck <https://github.com/lo1tuma>
4  * @copyright 2015 Mathias Schreck
5  */
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Rule Definition
10 //------------------------------------------------------------------------------
11
12 module.exports = function(context) {
13
14     var configuration = context.options[0],
15         requireAnonymousFunctionSpacing = true,
16         requireNamedFunctionSpacing = true;
17
18     if (typeof configuration === "object") {
19         requireAnonymousFunctionSpacing = configuration.anonymous !== "never";
20         requireNamedFunctionSpacing = configuration.named !== "never";
21     } else if (configuration === "never") {
22         requireAnonymousFunctionSpacing = false;
23         requireNamedFunctionSpacing = false;
24     }
25
26     /**
27      * Determines whether two adjacent tokens are have whitespace between them.
28      * @param {Object} left - The left token object.
29      * @param {Object} right - The right token object.
30      * @returns {boolean} Whether or not there is space between the tokens.
31      */
32     function isSpaced(left, right) {
33         return left.range[1] < right.range[0];
34     }
35
36     /**
37      * Determines whether a function has a name.
38      * @param {ASTNode} node The function node.
39      * @returns {boolean} Whether the function has a name.
40      */
41     function isNamedFunction(node) {
42         var parent;
43
44         if (node.id) {
45             return true;
46         }
47
48         parent = context.getAncestors().pop();
49         return parent.type === "MethodDefinition" ||
50             (parent.type === "Property" &&
51                 (
52                     parent.kind === "get" ||
53                     parent.kind === "set" ||
54                     parent.method
55                 )
56             );
57     }
58
59     /**
60      * Validates the spacing before function parentheses.
61      * @param {ASTNode} node The node to be validated.
62      * @returns {void}
63      */
64     function validateSpacingBeforeParentheses(node) {
65         var isNamed = isNamedFunction(node),
66             tokens,
67             leftToken,
68             rightToken,
69             location;
70
71         if (node.generator && !isNamed) {
72             return;
73         }
74
75         tokens = context.getTokens(node);
76
77         if (node.generator) {
78             if (node.id) {
79                 leftToken = tokens[2];
80                 rightToken = tokens[3];
81             } else {
82                 // Object methods are named but don't have an id
83                 leftToken = context.getTokenBefore(node);
84                 rightToken = tokens[0];
85             }
86         } else if (isNamed) {
87             if (node.id) {
88                 leftToken = tokens[1];
89                 rightToken = tokens[2];
90             } else {
91                 // Object methods are named but don't have an id
92                 leftToken = context.getTokenBefore(node);
93                 rightToken = tokens[0];
94             }
95         } else {
96             leftToken = tokens[0];
97             rightToken = tokens[1];
98         }
99
100         location = leftToken.loc.end;
101
102         if (isSpaced(leftToken, rightToken)) {
103             if ((isNamed && !requireNamedFunctionSpacing) || (!isNamed && !requireAnonymousFunctionSpacing)) {
104                 context.report(node, location, "Unexpected space before function parentheses.");
105             }
106         } else {
107             if ((isNamed && requireNamedFunctionSpacing) || (!isNamed && requireAnonymousFunctionSpacing)) {
108                 context.report(node, location, "Missing space before function parentheses.");
109             }
110         }
111     }
112
113     return {
114         "FunctionDeclaration": validateSpacingBeforeParentheses,
115         "FunctionExpression": validateSpacingBeforeParentheses
116     };
117 };