1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
/**
* @fileoverview Rule to enforce concise object methods and properties.
* @author Jamund Ferguson
*/
"use strict";
var OPTIONS = {
always: "always",
never: "never",
methods: "methods",
properties: "properties"
};
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: "require or disallow method and property shorthand syntax for object literals",
category: "ECMAScript 6",
recommended: false
},
fixable: "code",
schema: {
anyOf: [
{
type: "array",
items: [
{
enum: ["always", "methods", "properties", "never"]
}
],
minItems: 0,
maxItems: 1
},
{
type: "array",
items: [
{
enum: ["always", "methods", "properties"]
},
{
type: "object",
properties: {
avoidQuotes: {
type: "boolean"
}
},
additionalProperties: false
}
],
minItems: 0,
maxItems: 2
},
{
type: "array",
items: [
{
enum: ["always", "methods"]
},
{
type: "object",
properties: {
ignoreConstructors: {
type: "boolean"
},
avoidQuotes: {
type: "boolean"
}
},
additionalProperties: false
}
],
minItems: 0,
maxItems: 2
}
]
}
},
create: function(context) {
var APPLY = context.options[0] || OPTIONS.always;
var APPLY_TO_METHODS = APPLY === OPTIONS.methods || APPLY === OPTIONS.always;
var APPLY_TO_PROPS = APPLY === OPTIONS.properties || APPLY === OPTIONS.always;
var APPLY_NEVER = APPLY === OPTIONS.never;
var PARAMS = context.options[1] || {};
var IGNORE_CONSTRUCTORS = PARAMS.ignoreConstructors;
var AVOID_QUOTES = PARAMS.avoidQuotes;
//--------------------------------------------------------------------------
// Helpers
//--------------------------------------------------------------------------
/**
* Determines if the first character of the name is a capital letter.
* @param {string} name The name of the node to evaluate.
* @returns {boolean} True if the first character of the property name is a capital letter, false if not.
* @private
*/
function isConstructor(name) {
var firstChar = name.charAt(0);
return firstChar === firstChar.toUpperCase();
}
/**
* Checks whether a node is a string literal.
* @param {ASTNode} node - Any AST node.
* @returns {boolean} `true` if it is a string literal.
*/
function isStringLiteral(node) {
return node.type === "Literal" && typeof node.value === "string";
}
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
return {
Property: function(node) {
var isConciseProperty = node.method || node.shorthand,
type;
// Ignore destructuring assignment
if (node.parent.type === "ObjectPattern") {
return;
}
// getters and setters are ignored
if (node.kind === "get" || node.kind === "set") {
return;
}
// only computed methods can fail the following checks
if (node.computed && node.value.type !== "FunctionExpression") {
return;
}
//--------------------------------------------------------------
// Checks for property/method shorthand.
if (isConciseProperty) {
// if we're "never" and concise we should warn now
if (APPLY_NEVER) {
type = node.method ? "method" : "property";
context.report({
node: node,
message: "Expected longform " + type + " syntax.",
fix: function(fixer) {
if (node.method) {
if (node.value.generator) {
return fixer.replaceTextRange([node.range[0], node.key.range[1]], node.key.name + ": function*");
}
return fixer.insertTextAfter(node.key, ": function");
}
return fixer.insertTextAfter(node.key, ": " + node.key.name);
}
});
}
// {'xyz'() {}} should be written as {'xyz': function() {}}
if (AVOID_QUOTES && isStringLiteral(node.key)) {
context.report({
node: node,
message: "Expected longform method syntax for string literal keys.",
fix: function(fixer) {
if (node.computed) {
return fixer.insertTextAfterRange([node.key.range[0], node.key.range[1] + 1], ": function");
}
return fixer.insertTextAfter(node.key, ": function");
}
});
}
return;
}
//--------------------------------------------------------------
// Checks for longform properties.
if (node.value.type === "FunctionExpression" && !node.value.id && APPLY_TO_METHODS) {
if (IGNORE_CONSTRUCTORS && isConstructor(node.key.name)) {
return;
}
if (AVOID_QUOTES && isStringLiteral(node.key)) {
return;
}
// {[x]: function(){}} should be written as {[x]() {}}
if (node.computed) {
context.report({
node: node,
message: "Expected method shorthand.",
fix: function(fixer) {
if (node.value.generator) {
return fixer.replaceTextRange(
[node.key.range[0], node.value.range[0] + "function*".length],
"*[" + node.key.name + "]"
);
}
return fixer.removeRange([node.key.range[1] + 1, node.value.range[0] + "function".length]);
}
});
return;
}
// {x: function(){}} should be written as {x() {}}
context.report({
node: node,
message: "Expected method shorthand.",
fix: function(fixer) {
if (node.value.generator) {
return fixer.replaceTextRange(
[node.key.range[0], node.value.range[0] + "function*".length],
"*" + node.key.name
);
}
return fixer.removeRange([node.key.range[1], node.value.range[0] + "function".length]);
}
});
} else if (node.value.type === "Identifier" && node.key.name === node.value.name && APPLY_TO_PROPS) {
// {x: x} should be written as {x}
context.report({
node: node,
message: "Expected property shorthand.",
fix: function(fixer) {
return fixer.replaceText(node, node.value.name);
}
});
} else if (node.value.type === "Identifier" && node.key.type === "Literal" && node.key.value === node.value.name && APPLY_TO_PROPS) {
if (AVOID_QUOTES) {
return;
}
// {"x": x} should be written as {x}
context.report({
node: node,
message: "Expected property shorthand.",
fix: function(fixer) {
return fixer.replaceText(node, node.value.name);
}
});
}
}
};
}
};
|