963ff42598d3a1d99ebd9972a5c79a6fdc2947b5
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview A rule to ensure whitespace before blocks.
3  * @author Mathias Schreck <https://github.com/lo1tuma>
4  * @copyright 2014 Mathias Schreck. All rights reserved.
5  */
6
7 "use strict";
8
9 //------------------------------------------------------------------------------
10 // Rule Definition
11 //------------------------------------------------------------------------------
12
13 module.exports = function (context) {
14     var requireSpace = context.options[0] !== "never";
15
16     /**
17      * Determines whether two adjacent tokens are have whitespace between them.
18      * @param {Object} left - The left token object.
19      * @param {Object} right - The right token object.
20      * @returns {boolean} Whether or not there is space between the tokens.
21      */
22     function isSpaced(left, right) {
23         return left.range[1] < right.range[0];
24     }
25
26     /**
27      * Determines whether two adjacent tokens are on the same line.
28      * @param {Object} left - The left token object.
29      * @param {Object} right - The right token object.
30      * @returns {boolean} Whether or not the tokens are on the same line.
31      */
32     function isSameLine(left, right) {
33         return left.loc.start.line === right.loc.start.line;
34     }
35
36     /**
37      * Checks the given BlockStatement node has a preceding space if it doesn’t start on a new line.
38      * @param {ASTNode|Token} node The AST node of a BlockStatement.
39      * @returns {void} undefined.
40      */
41     function checkPrecedingSpace(node) {
42         var precedingToken = context.getTokenBefore(node),
43             hasSpace;
44
45         if (precedingToken && isSameLine(precedingToken, node)) {
46             hasSpace = isSpaced(precedingToken, node);
47
48             if (requireSpace) {
49                 if (!hasSpace) {
50                     context.report(node, "Missing space before opening brace.");
51                 }
52             } else {
53                 if (hasSpace) {
54                     context.report(node, "Unexpected space before opening brace.");
55                 }
56             }
57         }
58     }
59
60     /**
61      * Checks if the CaseBlock of an given SwitchStatement node has a preceding space.
62      * @param {ASTNode} node The node of a SwitchStatement.
63      * @returns {void} undefined.
64      */
65     function checkSpaceBeforeCaseBlock(node) {
66         var cases = node.cases,
67             firstCase,
68             openingBrace;
69
70         if (cases.length > 0) {
71             firstCase = cases[0];
72             openingBrace = context.getTokenBefore(firstCase);
73         } else {
74             openingBrace = context.getLastToken(node, 1);
75         }
76
77         checkPrecedingSpace(openingBrace);
78     }
79
80     return {
81         "BlockStatement": checkPrecedingSpace,
82         "SwitchStatement": checkSpaceBeforeCaseBlock
83     };
84
85 };