-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
228 lines (211 loc) · 7.9 KB
/
index.js
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
'use strict';
module.exports = {
rules: {
'require-sort': {
meta: {
type: 'suggestion',
docs: {
description: 'enforce sorted require declarations within modules',
category: 'ECMAScript 6',
recommended: false,
url: 'https://github.com/zcuric/eslint-plugin-require-sort'
},
schema: [
{
type: 'object',
properties: {
ignoreCase: {
type: 'boolean',
default: false
},
propertySyntaxSortOrder: {
type: 'array',
items: {
enum: ['none', 'multiple', 'single']
},
uniqueItems: true,
minItems: 3,
maxItems: 3
},
ignoreDeclarationSort: {
type: 'boolean',
default: false
},
ignorePropertySort: {
type: 'boolean',
default: false
}
},
additionalProperties: false
}
],
fixable: 'code'
},
create(context) {
const configuration = context.options[0] || {};
const {
ignoreCase = false,
ignoreDeclarationSort = false,
ignorePropertySort = false,
propertySyntaxSortOrder = ['none', 'multiple', 'single']
} = configuration;
const sourceCode = context.getSourceCode();
const nodes = [];
let previousNode = null;
const handleDeclarationSort = node => {
if (previousNode) {
const currentIndex = getPropertySyntaxIndex(node);
const previousIndex = getPropertySyntaxIndex(previousNode);
/*
* When the current declaration uses a different property syntax,
* then check if the ordering is correct.
* Otherwise, make a default string compare (like rule sort-vars to be consistent)
* of the first used property name.
*/
if (currentIndex === previousIndex) {
reportOnAlphabeticalSort(node, previousNode);
}
if (currentIndex < previousIndex) {
reportOnExpectedSyntax(node, currentIndex, previousIndex);
}
}
previousNode = node;
};
const handlePropertySort = node => {
if (isStaticRequire(node)) return;
if (!node.declarations[0].id.properties) return;
const properties = node.declarations[0].id.properties;
const mergeText = (sourceText, property, index) => {
let textAfterProperty = '';
if (index !== properties.length - 1) {
textAfterProperty = sourceCode
.getText()
.slice(
properties[index].range[1],
properties[index + 1].range[0]
);
}
return (
sourceText + sourceCode.getText(property) + textAfterProperty
);
};
const firstUnsortedIndex = properties
.map(getSortableName)
.findIndex((name, index, array) => array[index - 1] > name);
const fix = ({ replaceTextRange }) => {
// If there are comments in the property list, don't rearrange the properties.
if (hasComments(properties)) return null;
const range = [
properties[0].range[0],
properties[properties.length - 1].range[1]
];
const text = [...properties].sort(sortByName).reduce(mergeText, '');
return replaceTextRange(range, text);
};
if (firstUnsortedIndex === -1) return;
const { value } = properties[firstUnsortedIndex];
const propertyName = isAssignmentPattern(value)
? value.left.name
: value.name;
context.report({
node: properties[firstUnsortedIndex],
message:
"Property '{{propertyName}}' of the require declaration should be sorted alphabetically.",
data: { propertyName },
fix
});
};
const isTopLevel = ({ parent }) => parent.type === 'Program';
const isStaticRequire = node => {
if (node.type !== 'CallExpression') return false;
return (
node.callee?.type === 'Identifier' &&
node.callee?.name === 'require' &&
node.arguments?.length === 1
);
};
const isRequire = node =>
node.declarations[0]?.init?.callee?.name === 'require';
const isAssignmentPattern = node => node?.type === 'AssignmentPattern';
const hasObjectPattern = node =>
node.declarations[0]?.id?.type === 'ObjectPattern';
const hasMultipleProperties = node =>
node.declarations[0]?.id?.properties.length > 1;
const hasComments = properties =>
properties.some(property => {
const commentsBefore = sourceCode.getCommentsBefore(property);
const commentsAfter = sourceCode.getCommentsAfter(property);
return commentsBefore.length || commentsAfter.length;
});
const getSortableName = ({ value }) => {
const name = isAssignmentPattern(value)
? value.left.name
: value.name;
if (name) return ignoreCase ? name.toLowerCase() : name;
return null;
};
const sortByName = (propertyA, propertyB) => {
const aName = getSortableName(propertyA);
const bName = getSortableName(propertyB);
return aName > bName ? 1 : -1;
};
const getPropertySyntax = node => {
if (isStaticRequire(node)) return 'none';
if (!hasObjectPattern(node) || !hasMultipleProperties(node)) {
return 'single';
}
return 'multiple';
};
const getPropertySyntaxIndex = node =>
propertySyntaxSortOrder.indexOf(getPropertySyntax(node));
const getDeclarationName = node => {
if (isStaticRequire(node)) return node.arguments[0].value;
if (!hasObjectPattern(node)) return node.declarations[0].id.name;
const value = node.declarations[0].id.properties[0].value;
return isAssignmentPattern(value) ? value.left.name : value.name;
};
const reportOnAlphabeticalSort = (node, previousNode) => {
let firstName = getDeclarationName(node);
let previousName = getDeclarationName(previousNode);
if (ignoreCase) {
previousName = previousName && previousName.toLowerCase();
firstName = firstName && firstName.toLowerCase();
}
if (previousName && firstName && firstName < previousName) {
context.report({
node,
message: 'Requires should be sorted alphabetically.'
});
}
};
const reportOnExpectedSyntax = (node, currentIndex, previousIndex) => {
context.report({
node,
message:
"Expected '{{syntaxA}}' syntax before '{{syntaxB}}' syntax.",
data: {
syntaxA: propertySyntaxSortOrder[currentIndex],
syntaxB: propertySyntaxSortOrder[previousIndex]
}
});
};
return {
ExpressionStatement(node) {
if (!isTopLevel(node)) return;
if (!isStaticRequire(node.expression)) return;
nodes.push(node.expression);
},
VariableDeclaration(node) {
if (!isTopLevel(node)) return;
if (!isRequire(node)) return;
nodes.push(node);
},
'Program:exit'() {
if (!ignoreDeclarationSort) nodes.forEach(handleDeclarationSort);
if (!ignorePropertySort) nodes.forEach(handlePropertySort);
}
};
}
}
}
};