05ced44593438f1ed4bf6701bcbcf7f78cafd4ea
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Validates spacing before and after semicolon
3  * @author Mathias Schreck
4  * @copyright 2015 Mathias Schreck
5  */
6
7 "use strict";
8
9 //------------------------------------------------------------------------------
10 // Rule Definition
11 //------------------------------------------------------------------------------
12
13 module.exports = function (context) {
14
15     var config = context.options[0],
16         requireSpaceBefore = false,
17         requireSpaceAfter = true;
18
19     if (typeof config === "object") {
20         if (config.hasOwnProperty("before")) {
21             requireSpaceBefore = config.before;
22         }
23         if (config.hasOwnProperty("after")) {
24             requireSpaceAfter = config.after;
25         }
26     }
27
28     /**
29      * Determines whether two adjacent tokens are have whitespace between them.
30      * @param {Object} left - The left token object.
31      * @param {Object} right - The right token object.
32      * @returns {boolean} Whether or not there is space between the tokens.
33      */
34     function isSpaced(left, right) {
35         return left.range[1] < right.range[0];
36     }
37
38     /**
39      * Checks whether two tokens are on the same line.
40      * @param {Object} left The leftmost token.
41      * @param {Object} right The rightmost token.
42      * @returns {boolean} True if the tokens are on the same line, false if not.
43      * @private
44      */
45     function isSameLine(left, right) {
46         return left.loc.end.line === right.loc.start.line;
47     }
48
49     /**
50      * Checks if a given token has leading whitespace.
51      * @param {Object} token The token to check.
52      * @returns {boolean} True if the given token has leading space, false if not.
53      */
54     function hasLeadingSpace(token) {
55         var tokenBefore = context.getTokenBefore(token);
56         return tokenBefore && isSameLine(tokenBefore, token) && isSpaced(tokenBefore, token);
57     }
58
59     /**
60      * Checks if a given token has trailing whitespace.
61      * @param {Object} token The token to check.
62      * @returns {boolean} True if the given token has trailing space, false if not.
63      */
64     function hasTrailingSpace(token) {
65         var tokenAfter = context.getTokenAfter(token);
66         return tokenAfter && isSameLine(token, tokenAfter) && isSpaced(token, tokenAfter);
67     }
68
69     /**
70      * Checks if the given token is the last token in its line.
71      * @param {Token} token The token to check.
72      * @returns {boolean} Whether or not the token is the last in its line.
73      */
74     function isLastTokenInCurrentLine(token) {
75         var tokenAfter = context.getTokenAfter(token);
76         return !(tokenAfter && isSameLine(token, tokenAfter));
77     }
78
79     /**
80      * Checks if the given token is a semicolon.
81      * @param {Token} token The token to check.
82      * @returns {boolean} Whether or not the given token is a semicolon.
83      */
84     function isSemicolon(token) {
85         return token.type === "Punctuator" && token.value === ";";
86     }
87
88     /**
89      * Reports if the given token has invalid spacing.
90      * @param {Token} token The semicolon token to check.
91      * @param {ASTNode} node The corresponding node of the token.
92      * @returns {void}
93      */
94     function checkSemicolonSpacing(token, node) {
95         var location;
96
97         if (isSemicolon(token)) {
98             location = token.loc.start;
99
100             if (hasLeadingSpace(token)) {
101                 if (!requireSpaceBefore) {
102                     context.report(node, location, "Unexpected whitespace before semicolon.");
103                 }
104             } else {
105                 if (requireSpaceBefore) {
106                     context.report(node, location, "Missing whitespace before semicolon.");
107                 }
108             }
109
110             if (!isLastTokenInCurrentLine(token)) {
111                 if (hasTrailingSpace(token)) {
112                     if (!requireSpaceAfter) {
113                         context.report(node, location, "Unexpected whitespace after semicolon.");
114                     }
115                 } else {
116                     if (requireSpaceAfter) {
117                         context.report(node, location, "Missing whitespace after semicolon.");
118                     }
119                 }
120             }
121         }
122     }
123
124     /**
125      * Checks the spacing of the semicolon with the assumption that the last token is the semicolon.
126      * @param {ASTNode} node The node to check.
127      * @returns {void}
128      */
129     function checkNode(node) {
130         var token = context.getLastToken(node);
131         checkSemicolonSpacing(token, node);
132     }
133
134     return {
135         "VariableDeclaration": checkNode,
136         "ExpressionStatement": checkNode,
137         "BreakStatement": checkNode,
138         "ContinueStatement": checkNode,
139         "DebuggerStatement": checkNode,
140         "ReturnStatement": checkNode,
141         "ThrowStatement": checkNode,
142         "ForStatement": function (node) {
143             if (node.init) {
144                 checkSemicolonSpacing(context.getTokenAfter(node.init), node);
145             }
146
147             if (node.test) {
148                 checkSemicolonSpacing(context.getTokenAfter(node.test), node);
149             }
150         }
151     };
152 };