fe0b82bd1980e21f72ae972df3cb44a4bc5d8339
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Rule to check the spacing around the * in generator functions.
3  * @author Jamund Ferguson
4  * @copyright 2015 Brandon Mills. All rights reserved.
5  * @copyright 2014 Jamund Ferguson. All rights reserved.
6  */
7
8 "use strict";
9
10 //------------------------------------------------------------------------------
11 // Rule Definition
12 //------------------------------------------------------------------------------
13
14 module.exports = function(context) {
15
16     var mode = {
17         before: { before: true, after: false },
18         after: { before: false, after: true },
19         both: { before: true, after: true },
20         neither: { before: false, after: false }
21     }[context.options[0] || "before"];
22
23     /**
24      * Checks the spacing between two tokens before or after the star token.
25      * @param {string} side Either "before" or "after".
26      * @param {Token} leftToken `function` keyword token if side is "before", or
27      *     star token if side is "after".
28      * @param {Token} rightToken Star token if side is "before", or identifier
29      *     token if side is "after".
30      * @returns {void}
31      */
32     function checkSpacing(side, leftToken, rightToken) {
33         if (!!(rightToken.range[0] - leftToken.range[1]) !== mode[side]) {
34             context.report(
35                 leftToken.value === "*" ? leftToken : rightToken,
36                 "{{type}} space {{side}} *.",
37                 {
38                     type: mode[side] ? "Missing" : "Unexpected",
39                     side: side
40                 }
41             );
42         }
43     }
44
45     /**
46      * Enforces the spacing around the star if node is a generator function.
47      * @param {ASTNode} node A function expression or declaration node.
48      * @returns {void}
49      */
50     function checkFunction(node) {
51         var isMethod, starToken, tokenBefore, tokenAfter;
52
53         if (!node.generator) {
54             return;
55         }
56
57         isMethod = !!context.getAncestors().pop().method;
58
59         if (isMethod) {
60             starToken = context.getTokenBefore(node, 1);
61         } else {
62             starToken = context.getFirstToken(node, 1);
63         }
64
65         // Only check before when preceded by `function` keyword
66         tokenBefore = context.getTokenBefore(starToken);
67         if (tokenBefore.value === "function") {
68             checkSpacing("before", tokenBefore, starToken);
69         }
70
71         // Only check after when followed by an identifier
72         tokenAfter = context.getTokenAfter(starToken);
73         if (tokenAfter.type === "Identifier") {
74             checkSpacing("after", starToken, tokenAfter);
75         }
76     }
77
78     return {
79         "FunctionDeclaration": checkFunction,
80         "FunctionExpression": checkFunction
81     };
82
83 };