bf89cbfdf8e4d9fb4db9ce8d38a00b660bf276a6
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Rule to warn about using dot notation instead of square bracket notation when possible.
3  * @author Josh Perez
4  */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Rule Definition
9 //------------------------------------------------------------------------------
10
11 var validIdentifier = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
12 var keywords = [
13     "this",
14     "function",
15     "if",
16     "return",
17     "var",
18     "else",
19     "for",
20     "new",
21     "in",
22     "typeof",
23     "while",
24     "case",
25     "break",
26     "try",
27     "catch",
28     "delete",
29     "throw",
30     "switch",
31     "continue",
32     "default",
33     "instanceof",
34     "do",
35     "void",
36     "finally",
37     "with",
38     "debugger",
39     "implements",
40     "interface",
41     "package",
42     "private",
43     "protected",
44     "public",
45     "static",
46     "class",
47     "enum",
48     "export",
49     "extends",
50     "import",
51     "super",
52     "true",
53     "false",
54     "null",
55     "abstract",
56     "boolean",
57     "byte",
58     "char",
59     "const",
60     "double",
61     "final",
62     "float",
63     "goto",
64     "int",
65     "long",
66     "native",
67     "short",
68     "synchronized",
69     "throws",
70     "transient",
71     "volatile"
72 ];
73
74 module.exports = function(context) {
75     var options = context.options[0] || {};
76     var allowKeywords = options.allowKeywords === void 0 || !!options.allowKeywords;
77
78     var allowPattern;
79     if (options.allowPattern) {
80         allowPattern = new RegExp(options.allowPattern);
81     }
82
83     return {
84         "MemberExpression": function(node) {
85             if (
86                 node.computed &&
87                 node.property.type === "Literal" &&
88                 validIdentifier.test(node.property.value) &&
89                 (allowKeywords || keywords.indexOf("" + node.property.value) === -1)
90             ) {
91                 if (!(allowPattern && allowPattern.test(node.property.value))) {
92                     context.report(node, "[" + JSON.stringify(node.property.value) + "] is better written in dot notation.");
93                 }
94             }
95             if (
96                 !allowKeywords &&
97                 !node.computed &&
98                 keywords.indexOf("" + node.property.name) !== -1
99             ) {
100                 context.report(node, "." + node.property.name + " is a syntax error.");
101             }
102         }
103     };
104 };