3599f0cecdf48ad5b076fc99a0c1db14e7c0a18c
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Rule to disalow whitespace that is not a tab or space, whitespace inside strings and comments are allowed
3  * @author Jonathan Kingston
4  * @copyright 2014 Jonathan Kingston. All rights reserved.
5  */
6
7 "use strict";
8
9 //------------------------------------------------------------------------------
10 // Rule Definition
11 //------------------------------------------------------------------------------
12
13 module.exports = function(context) {
14
15     var irregularWhitespace = /[\u0085\u00A0\ufeff\f\v\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u202f\u205f\u3000]+/mg;
16
17     // Module store of errors that we have found
18     var errors = [];
19
20     /**
21      * Removes errors that occur inside a string node
22      * @param {ASTNode} node to check for matching errors.
23      * @returns {void}
24      * @private
25      */
26     function removeStringError(node) {
27         var locStart = node.loc.start;
28         var locEnd = node.loc.end;
29
30         errors = errors.filter(function (error) {
31             var errorLoc = error[1];
32             if (errorLoc.line >= locStart.line && errorLoc.line <= locEnd.line) {
33                 if (errorLoc.column >= locStart.column && errorLoc.column <= locEnd.column) {
34                     return false;
35                 }
36             }
37             return true;
38         });
39     }
40
41     /**
42      * Checks nodes for errors that we are choosing to ignore and calls the relevent methods to remove the errors
43      * @param {ASTNode} node to check for matching errors.
44      * @returns {void}
45      * @private
46      */
47     function removeInvalidNodeErrors(node) {
48         if (typeof node.value === "string") {
49             // If we have irregular characters remove them from the errors list
50             if (node.value.match(irregularWhitespace)) {
51                 removeStringError(node);
52             }
53         }
54     }
55
56     return {
57         "Program": function (node) {
58             /**
59              * As we can easily fire warnings for all white space issues with all the source its simpler to fire them here
60              * This means we can check all the application code without having to worry about issues caused in the parser tokens
61              * When writing this code also evaluating per node was missing out connecting tokens in some cases
62              * We can later filter the errors when they are found to be not an issue in nodes we don't care about
63              */
64             var sourceLines = context.getSourceLines();
65
66             sourceLines.forEach(function (sourceLine, lineIndex) {
67                 var location,
68                     match = irregularWhitespace.exec(sourceLine);
69
70                 if (match !== null) {
71                     location = {
72                         line: lineIndex + 1,
73                         column: match.index
74                     };
75
76                     errors.push([node, location, "Irregular whitespace not allowed"]);
77                 }
78             });
79         },
80         "Identifier": removeInvalidNodeErrors,
81         "Literal": removeInvalidNodeErrors,
82         "Statement": removeInvalidNodeErrors,
83         "Expression": removeInvalidNodeErrors,
84         "Program:exit": function () {
85
86             // If we have any errors remaining report on them
87             errors.forEach(function (error) {
88                 context.report.apply(this, error);
89             });
90         }
91     };
92 };