a36dd73b1bb938f3672367f9554dd4b9637b2a64
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Rule to check for max length on a line.
3  * @author Matt DuVall <http://www.mattduvall.com>
4  * @copyright 2013 Matt DuVall. All rights reserved.
5  */
6
7 "use strict";
8
9 //------------------------------------------------------------------------------
10 // Rule Definition
11 //------------------------------------------------------------------------------
12
13 module.exports = function(context) {
14
15     /**
16      * Creates a string that is made up of repeating a given string a certain
17      * number of times. This uses exponentiation of squares to achieve significant
18      * performance gains over the more traditional implementation of such
19      * functionality.
20      * @param {string} str The string to repeat.
21      * @param {int} num The number of times to repeat the string.
22      * @returns {string} The created string.
23      * @private
24      */
25     function stringRepeat(str, num) {
26         var result = "";
27         for (num |= 0; num > 0; num >>>= 1, str += str) {
28             if (num & 1) {
29                 result += str;
30             }
31         }
32         return result;
33     }
34
35     var tabWidth = context.options[1];
36
37     var maxLength = context.options[0],
38         tabString = stringRepeat(" ", tabWidth);
39
40     //--------------------------------------------------------------------------
41     // Helpers
42     //--------------------------------------------------------------------------
43     function checkProgramForMaxLength(node) {
44         var lines = context.getSourceLines();
45
46         // Replace the tabs
47         // Split (honors line-ending)
48         // Iterate
49         lines.forEach(function(line, i) {
50             if (line.replace(/\t/g, tabString).length > maxLength) {
51                 context.report(node, { line: i + 1, col: 1 }, "Line " + (i + 1) + " exceeds the maximum line length of " + maxLength + ".");
52             }
53         });
54     }
55
56
57     //--------------------------------------------------------------------------
58     // Public API
59     //--------------------------------------------------------------------------
60
61     return {
62         "Program": checkProgramForMaxLength
63     };
64
65 };