-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
254 lines (236 loc) · 6.7 KB
/
test.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
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
const {rules: {_: rule}} = require('.');
const {RuleTester} = require('eslint');
const dedent = require('dedent');
const invalidOrderErrorMessage =
'Lines between \"start-enforce-alphabetization\" and \"end-enforce-alphabetization\" should be ordered. ' +
'To see the proper ordering, rerun this command with env var `ESLINT_ALPHABETIZE_DEBUG=true`.';
// Bug in the quotes rule: https://github.com/eslint/eslint/issues/9433.
/* eslint-disable quotes */
const invalidStartMessage =
`There is already a "start-enforce-alphabetization" block open, so it's invalid to have a start here.`;
const invalidEndMessage = `There is no "start-enforce-alphabetization" open, so it's invalid to have an end here.`;
/* eslint-enable quotes */
const runTest = (ruleTester, extraTests = {valid: [], invalid: []}) => {
const prepareTestCaseString = codeString => dedent(codeString.trim());
const prepareTestCases = testCases => testCases.map(testCase => {
if (typeof testCase === 'string') {
return prepareTestCaseString(testCase);
}
const {code, ...rest} = testCase;
return {
code: prepareTestCaseString(code),
...rest
};
});
const ruleName = extraTests.testPrefix ? `_ ${extraTests.testPrefix}` : '_';
ruleTester.run(ruleName, rule, {
valid: prepareTestCases([
'const a = 1; const b = 2;',
'const b = 1; const a = 2;',
`
// Sorting only applies in designated blocks.
let a;
let outOfOrder;
let b;
// start-enforce-alphabetization
const c = 1;
const d = 2;
// end-enforce-alphabetization
`,
`
// Sorting works for array members
const arr = [
// start-enforce-alphabetization
a(),
b(),
c()
// end-enforce-alphabetization
]
`,
`
// Sorting works for method arguments
f(
// start-enforce-alphabetization
a,
b,
c
// end-enforce-alphabetization
)
`,
`
// Sorting only applies to top-level nodes.
// start-enforce-alphabetization
function a() {
let z;
let a;
}
function c() {}
// end-enforce-alphabetization
`,
{
code: `
// Function call
// start-enforce-alphabetization:numeric
require('11-z');
require('100-a');
require('111-a')
// end-enforce-alphabetization
`
},
{
code: `
// Numeric mode, but some entries don't have numbers.
// start-enforce-alphabetization:numeric
require('100-a');
require('111-a')
require('a');
// end-enforce-alphabetization
`
},
{
code: `
// Numeric mode, but some entries don't start with numbers.
// start-enforce-alphabetization:numeric
require('100-a');
require('111-a')
require('a');
require('a-1');
require('a-2');
// end-enforce-alphabetization
`
},
...extraTests.valid
]),
invalid: prepareTestCases([
{
code: `
// Function call
// start-enforce-alphabetization:numeric
require('100-a');
require('11-z');
require('111-z')
// end-enforce-alphabetization
`,
errors: [{message: invalidOrderErrorMessage, line: 3, column: 1, endLine: 3, endColumn: 18}]
},
{
code: `
// VariableDeclaration
// start-enforce-alphabetization
const b = 1;
const a = 2;
// end-enforce-alphabetization
`,
errors: [{message: invalidOrderErrorMessage, line: 3, column: 1, endLine: 3, endColumn: 13}]
},
{
code: `
// FunctionDeclaration
// start-enforce-alphabetization
function a() {}
function c() {}
function b() {}
// end-enforce-alphabetization
`,
errors: [{message: invalidOrderErrorMessage, line: 4, column: 1, endLine: 4, endColumn: 16}]
},
{
code: `
// Two starts
// start-enforce-alphabetization
// start-enforce-alphabetization
`,
errors: [{message: invalidStartMessage, line: 3, column: 1, endLine: 3, endColumn: 33}]
},
{
code: `
// End with no start
// end-enforce-alphabetization
`,
errors: [{message: invalidEndMessage, line: 2, column: 1, endLine: 2, endColumn: 31}]
},
{
code: `
// Start with no end
// start-enforce-alphabetization
`,
errors: [{
message: 'This "start-enforce-alphabetization" does not have a "end-enforce-alphabetization".',
line: 2, column: 1, endLine: 2, endColumn: 33
}]
},
{
code: `
// Alphabetization only applies to enforced section.
let a;
let outOfOrder;
let b;
// start-enforce-alphabetization
const d = 2;
const c = 1;
// end-enforce-alphabetization
`,
errors: [{message: invalidOrderErrorMessage, line: 7, column: 1, endLine: 7, endColumn: 13}]
},
{
code: `
// Sorting works for array members
const arr = [
// start-enforce-alphabetization
b(),
a(),
c()
// end-enforce-alphabetization
]
`,
errors: [{message: invalidOrderErrorMessage, line: 4, column: 3, endLine: 4, endColumn: 6}]
},
{
code: `
// Sorting works for method arguments
f(
// start-enforce-alphabetization
b,
a,
c
// end-enforce-alphabetization
)
`,
errors: [{message: invalidOrderErrorMessage, line: 4, column: 3, endLine: 4, endColumn: 4}]
},
...extraTests.invalid
])
});
};
runTest(new RuleTester({
parserOptions: {ecmaVersion: 2019},
parser: require.resolve('@typescript-eslint/parser')
}), {
valid: [`
// Enum members
const enum E {
// start-enforce-alphabetization
A,
B,
C
// end-enforce-alphabetization
}
`],
invalid: [{
code: `
// Enum members
const enum E {
// start-enforce-alphabetization
B,
A,
C
// end-enforce-alphabetization
}
`,
errors: [{message: invalidOrderErrorMessage, line: 4, column: 3, endLine: 4, endColumn: 4}]
}],
testPrefix: 'typescript parser'
});
runTest(new RuleTester({
parserOptions: {ecmaVersion: 2019}
}));