9fd0b8d930046db635749d63a1026c3a05ff3cc2
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Rule to flag trailing underscores in variable declarations.
3  * @author Matt DuVall <http://www.mattduvall.com>
4  */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Rule Definition
10 //------------------------------------------------------------------------------
11
12 module.exports = function(context) {
13
14     //-------------------------------------------------------------------------
15     // Helpers
16     //-------------------------------------------------------------------------
17
18     function hasTrailingUnderscore(identifier) {
19         var len = identifier.length;
20
21         return identifier !== "_" && (identifier[0] === "_" || identifier[len - 1] === "_");
22     }
23
24     function isSpecialCaseIdentifierForMemberExpression(identifier) {
25         return identifier === "__proto__";
26     }
27
28     function isSpecialCaseIdentifierInVariableExpression(identifier) {
29         // Checks for the underscore library usage here
30         return identifier === "_";
31     }
32
33     function checkForTrailingUnderscoreInFunctionDeclaration(node) {
34         if (node.id) {
35             var identifier = node.id.name;
36
37             if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier)) {
38                 context.report(node, "Unexpected dangling '_' in '" + identifier + "'.");
39             }
40         }
41     }
42
43     function checkForTrailingUnderscoreInVariableExpression(node) {
44         var identifier = node.id.name;
45
46         if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier) &&
47             !isSpecialCaseIdentifierInVariableExpression(identifier)) {
48             context.report(node, "Unexpected dangling '_' in '" + identifier + "'.");
49         }
50     }
51
52     function checkForTrailingUnderscoreInMemberExpression(node) {
53         var identifier = node.property.name;
54
55         if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier) &&
56             !isSpecialCaseIdentifierForMemberExpression(identifier)) {
57             context.report(node, "Unexpected dangling '_' in '" + identifier + "'.");
58         }
59     }
60
61     //--------------------------------------------------------------------------
62     // Public API
63     //--------------------------------------------------------------------------
64
65     return {
66         "FunctionDeclaration": checkForTrailingUnderscoreInFunctionDeclaration,
67         "VariableDeclarator": checkForTrailingUnderscoreInVariableExpression,
68         "MemberExpression": checkForTrailingUnderscoreInMemberExpression
69     };
70
71 };