1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 |
|
8 |
|
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 | "arguments",
|
22 | "in",
|
23 | "typeof",
|
24 | "while",
|
25 | "case",
|
26 | "break",
|
27 | "try",
|
28 | "catch",
|
29 | "delete",
|
30 | "throw",
|
31 | "switch",
|
32 | "continue",
|
33 | "default",
|
34 | "instanceof",
|
35 | "do",
|
36 | "void",
|
37 | "finally",
|
38 | "with",
|
39 | "debugger",
|
40 | "eval",
|
41 | "implements",
|
42 | "interface",
|
43 | "package",
|
44 | "private",
|
45 | "protected",
|
46 | "public",
|
47 | "static",
|
48 | "yield",
|
49 | "let",
|
50 | "class",
|
51 | "enum",
|
52 | "export",
|
53 | "extends",
|
54 | "import",
|
55 | "super"
|
56 | ];
|
57 |
|
58 | function canBeWrittenInDotNotation(node) {
|
59 | return node.computed === true &&
|
60 | node.property.type === "Literal" &&
|
61 | validIdentifier.test(node.property.value) &&
|
62 | keywords.indexOf(node.property.value) === -1;
|
63 | }
|
64 |
|
65 | module.exports = function(context) {
|
66 | return {
|
67 | "MemberExpression": function(node) {
|
68 | if (canBeWrittenInDotNotation(node)) {
|
69 | context.report(node, "['" + node.property.value + "'] is better written in dot notation.");
|
70 | }
|
71 | }
|
72 | };
|
73 | };
|