-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsort-keys-shorthand.js
More file actions
301 lines (272 loc) · 7.97 KB
/
sort-keys-shorthand.js
File metadata and controls
301 lines (272 loc) · 7.97 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
'use strict';
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const naturalCompare = require('natural-compare');
function getStaticPropertyName(node) {
let prop;
switch (node && node.type) {
case 'Property':
case 'MethodDefinition':
prop = node.key;
break;
case 'MemberExpression':
prop = node.property;
break;
// no default
}
switch (prop && prop.type) {
case 'Literal':
return { isShorthand: false, name: String(prop.value) };
case 'TemplateLiteral':
if (prop.expressions.length === 0 && prop.quasis.length === 1) {
return { isShorthand: false, name: prop.quasis[0].value.cooked };
}
break;
case 'Identifier':
if (!node.computed) {
return { isShorthand: node.shorthand, name: prop.name };
}
break;
// no default
}
return null;
}
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
/**
* Gets the property name of the given `Property` node.
*
* - If the property's key is an `Identifier` node, this returns the key's name
* whether it's a computed property or not.
* - If the property has a static name, this returns the static name.
* - Otherwise, this returns null.
* @param {ASTNode} node The `Property` node to get.
* @returns {string|null} The property name or null.
* @private
*/
function getPropertyData(node) {
const staticName = getStaticPropertyName(node);
if (staticName !== null && staticName.name !== null) {
return staticName;
}
return { isShorthand: false, name: node.key.name || null };
}
function checkShorthand(cmpFn, shorthand, aIsShorthand, bIsShorthand) {
if (shorthand === 'ignore' || aIsShorthand === bIsShorthand) {
return cmpFn();
}
if (shorthand === 'first') {
return aIsShorthand;
}
return !aIsShorthand;
}
/**
* Functions which check that the given 2 names are in specific order.
*
* Postfix `I` is meant insensitive.
* Postfix `N` is meant natual.
* @private
*/
const isValidOrders = {
asc(a, b, shorthand, aIsShorthand, bIsShorthand) {
return checkShorthand(
function() {
return a <= b;
},
shorthand,
aIsShorthand,
bIsShorthand
);
},
ascI(a, b, shorthand, aIsShorthand, bIsShorthand) {
return checkShorthand(
function() {
return a.toLowerCase() <= b.toLowerCase();
},
shorthand,
aIsShorthand,
bIsShorthand
);
},
ascN(a, b, shorthand, aIsShorthand, bIsShorthand) {
return checkShorthand(
function() {
return naturalCompare(a, b) <= 0;
},
shorthand,
aIsShorthand,
bIsShorthand
);
},
ascIN(a, b, shorthand, aIsShorthand, bIsShorthand) {
return checkShorthand(
function() {
return naturalCompare(a.toLowerCase(), b.toLowerCase()) <= 0;
},
shorthand,
aIsShorthand,
bIsShorthand
);
},
desc(a, b, shorthand, aIsShorthand, bIsShorthand) {
return isValidOrders.asc(b, a, shorthand, bIsShorthand, aIsShorthand);
},
descI(a, b, shorthand, aIsShorthand, bIsShorthand) {
return isValidOrders.ascI(b, a, shorthand, bIsShorthand, aIsShorthand);
},
descN(a, b, shorthand, aIsShorthand, bIsShorthand) {
return isValidOrders.ascN(b, a, shorthand, bIsShorthand, aIsShorthand);
},
descIN(a, b, shorthand, aIsShorthand, bIsShorthand) {
return isValidOrders.ascIN(b, a, shorthand, bIsShorthand, aIsShorthand);
}
};
function isSameLine(node) {
return node.loc.start.line === node.loc.end.line;
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'require object keys to be sorted',
category: 'Stylistic Issues',
recommended: false,
url:
'https://github.com/fxOne/eslint-plugin-sort-keys-shorthand/blob/master/docs/rules/sort-keys-shorthand.md'
},
hasSuggestions: true,
schema: [
{
enum: ['asc', 'desc']
},
{
type: 'object',
properties: {
caseSensitive: {
type: 'boolean',
default: true
},
natural: {
type: 'boolean',
default: false
},
minKeys: {
type: 'integer',
minimum: 2,
default: 2
},
shorthand: {
enum: ['first', 'last', 'ignore'],
default: 'ignore'
},
ignoreSingleline: {
type: 'boolean',
default: false
}
},
additionalProperties: false
}
]
},
create(context) {
// Parse options.
const order = context.options[0] || 'asc';
const options = context.options[1];
const insensitive = options && options.caseSensitive === false;
const natual = options && options.natural;
const minKeys = options && options.minKeys;
const shorthand = (options && options.shorthand) || 'ignore';
const ignoreSingleline = (options && options.ignoreSingleline) || false;
const isValidOrder =
isValidOrders[order + (insensitive ? 'I' : '') + (natual ? 'N' : '')];
// The stack to save the previous property's name for each object literals.
let stack = null;
return {
ObjectExpression(node) {
stack = {
isShorthand: false,
upper: stack,
prevName: null,
numKeys: node.properties.length,
isSameLine: isSameLine(node),
lastNode: node,
};
},
'ObjectExpression:exit'() {
stack = stack.upper;
},
SpreadElement(node) {
if (node.parent.type === 'ObjectExpression') {
stack.prevName = null;
stack.isShorthand = false;
}
},
Property(node) {
if (node.parent.type === 'ObjectPattern') {
return;
}
const prevName = stack.prevName;
const prevIsShorthand = stack.isShorthand;
const lastNode = stack.lastNode;
const numKeys = stack.numKeys;
const data = getPropertyData(node);
const thisName = data && data.name;
if (thisName !== null) {
stack.prevName = thisName;
stack.isShorthand = data.isShorthand;
stack.lastNode = node;
}
if (
prevName === null ||
thisName === null ||
numKeys < minKeys ||
(ignoreSingleline && stack.isSameLine)
) {
return;
}
if (
!isValidOrder(
prevName,
thisName,
shorthand,
prevIsShorthand,
data.isShorthand
)
) {
context.report({
node,
loc: node.key.loc,
message:
"Expected object keys to be in {{natual}}{{insensitive}}{{order}}ending order{{shorthand}}. '{{thisName}}' should be before '{{prevName}}'.",
data: {
prevName,
order,
thisName,
insensitive: insensitive ? 'insensitive ' : '',
natual: natual ? 'natural ' : '',
shorthand:
shorthand === 'ignore'
? ''
: ' with shorthand properties ' + shorthand,
},
suggest: [{
desc: 'Fix order',
fix(fixer) {
const sourceCode = context.sourceCode;
return fixer.replaceTextRange(
[lastNode.range[0], node.range[1]],
`${sourceCode.text.slice(node.range[0], node.range[1])},${sourceCode.text.slice(lastNode.range[0], lastNode.range[1])}`
);
}
}],
});
}
}
};
}
};