-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathorder.js
712 lines (689 loc) · 18.7 KB
/
order.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
import { test } from '../utils'
import { RuleTester } from 'eslint'
const ruleTester = new RuleTester()
, rule = require('rules/order')
ruleTester.run('order', rule, {
valid: [
// Default order using require
test({
code: `
var fs = require('fs');
var async = require('async');
var relParent1 = require('../foo');
var relParent2 = require('../foo/bar');
var relParent3 = require('../');
var sibling = require('./foo');
var index = require('./');`,
}),
// Default order using import
test({
code: `
import fs from 'fs';
import async, {foo1} from 'async';
import relParent1 from '../foo';
import relParent2, {foo2} from '../foo/bar';
import relParent3 from '../';
import sibling, {foo3} from './foo';
import index from './';`,
}),
// Multiple module of the same rank next to each other
test({
code: `
var fs = require('fs');
var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var async = require('async');`,
}),
// Overriding order to be the reverse of the default order
test({
code: `
var index = require('./');
var sibling = require('./foo');
var relParent3 = require('../');
var relParent2 = require('../foo/bar');
var relParent1 = require('../foo');
var async = require('async');
var fs = require('fs');
`,
options: [{groups: ['index', 'sibling', 'parent', 'external', 'builtin']}],
}),
// Ignore dynamic requires
test({
code: `
var path = require('path');
var _ = require('lodash');
var async = require('async');
var fs = require('f' + 's');`,
}),
// Ignore non-require call expressions
test({
code: `
var path = require('path');
var result = add(1, 2);
var _ = require('lodash');`,
}),
// Ignore requires that are not at the top-level
test({
code: `
var index = require('./');
function foo() {
var fs = require('fs');
}
() => require('fs');
if (a) {
require('fs');
}`,
}),
// Ignore unknown/invalid cases
test({
code: `
var unknown1 = require('/unknown1');
var fs = require('fs');
var unknown2 = require('/unknown2');
var async = require('async');
var unknown3 = require('/unknown3');
var foo = require('../foo');
var unknown4 = require('/unknown4');
var bar = require('../foo/bar');
var unknown5 = require('/unknown5');
var parent = require('../');
var unknown6 = require('/unknown6');
var foo = require('./foo');
var unknown7 = require('/unknown7');
var index = require('./');
var unknown8 = require('/unknown8');
`}),
// Ignoring unassigned values by default (require)
test({
code: `
require('./foo');
require('fs');
var path = require('path');
`}),
// Ignoring unassigned values by default (import)
test({
code: `
import './foo';
import 'fs';
import path from 'path';
`}),
// No imports
test({
code: `
function add(a, b) {
return a + b;
}
var foo;
`}),
// Grouping import types
test({
code: `
var fs = require('fs');
var index = require('./');
var path = require('path');
var sibling = require('./foo');
var relParent3 = require('../');
var async = require('async');
var relParent1 = require('../foo');
`,
options: [{groups: [
['builtin', 'index'],
['sibling', 'parent', 'external'],
]}],
}),
// Omitted types should implicitly be considered as the last type
test({
code: `
var index = require('./');
var path = require('path');
`,
options: [{groups: [
'index',
['sibling', 'parent', 'external'],
// missing 'builtin'
]}],
}),
// Mixing require and import should have import up top
test({
code: `
import async, {foo1} from 'async';
import relParent2, {foo2} from '../foo/bar';
import sibling, {foo3} from './foo';
var fs = require('fs');
var relParent1 = require('../foo');
var relParent3 = require('../');
var index = require('./');
`,
}),
// Option: newlines-between: 'always'
test({
code: `
var fs = require('fs');
var index = require('./');
var path = require('path');
var sibling = require('./foo');
var relParent1 = require('../foo');
var relParent3 = require('../');
var async = require('async');
`,
options: [
{
groups: [
['builtin', 'index'],
['sibling'],
['parent', 'external'],
],
'newlines-between': 'always',
},
],
}),
// Option: newlines-between: 'never'
test({
code: `
var fs = require('fs');
var index = require('./');
var path = require('path');
var sibling = require('./foo');
var relParent1 = require('../foo');
var relParent3 = require('../');
var async = require('async');
`,
options: [
{
groups: [
['builtin', 'index'],
['sibling'],
['parent', 'external'],
],
'newlines-between': 'never',
},
],
}),
// Option newlines-between: 'always' with multiline imports #1
test({
code: `
import path from 'path';
import {
I,
Want,
Couple,
Imports,
Here
} from 'bar';
import external from 'external'
`,
options: [{ 'newlines-between': 'always' }]
}),
// Option newlines-between: 'always' with multiline imports #2
test({
code: `
import path from 'path';
import net
from 'net';
import external from 'external'
`,
options: [{ 'newlines-between': 'always' }]
}),
// Option newlines-between: 'always' with multiline imports #3
test({
code: `
import foo
from '../../../../this/will/be/very/long/path/and/therefore/this/import/has/to/be/in/two/lines';
import bar
from './sibling';
`,
options: [{ 'newlines-between': 'always' }]
}),
// Option newlines-between: 'always' with not assigned import #1
test({
code: `
import path from 'path';
import 'loud-rejection';
import 'something-else';
import _ from 'lodash';
`,
options: [{ 'newlines-between': 'always' }]
}),
// Option newlines-between: 'never' with not assigned import #2
test({
code: `
import path from 'path';
import 'loud-rejection';
import 'something-else';
import _ from 'lodash';
`,
options: [{ 'newlines-between': 'never' }]
}),
// Option newlines-between: 'always' with not assigned require #1
test({
code: `
var path = require('path');
require('loud-rejection');
require('something-else');
var _ = require('lodash');
`,
options: [{ 'newlines-between': 'always' }]
}),
// Option newlines-between: 'never' with not assigned require #2
test({
code: `
var path = require('path');
require('loud-rejection');
require('something-else');
var _ = require('lodash');
`,
options: [{ 'newlines-between': 'never' }]
}),
// Option newlines-between: 'never' should ignore nested require statement's #1
test({
code: `
var some = require('asdas');
var config = {
port: 4444,
runner: {
server_path: require('runner-binary').path,
cli_args: {
'webdriver.chrome.driver': require('browser-binary').path
}
}
}
`,
options: [{ 'newlines-between': 'never' }]
}),
// Option newlines-between: 'always' should ignore nested require statement's #2
test({
code: `
var some = require('asdas');
var config = {
port: 4444,
runner: {
server_path: require('runner-binary').path,
cli_args: {
'webdriver.chrome.driver': require('browser-binary').path
}
}
}
`,
options: [{ 'newlines-between': 'always' }]
}),
],
invalid: [
// builtin before external module (require)
test({
code: `
var async = require('async');
var fs = require('fs');
`,
errors: [{
ruleId: 'order',
message: '`fs` import should occur before import of `async`',
}],
}),
// builtin before external module (import)
test({
code: `
import async from 'async';
import fs from 'fs';
`,
errors: [{
ruleId: 'order',
message: '`fs` import should occur before import of `async`',
}],
}),
// builtin before external module (mixed import and require)
test({
code: `
var async = require('async');
import fs from 'fs';
`,
errors: [{
ruleId: 'order',
message: '`fs` import should occur before import of `async`',
}],
}),
// external before parent
test({
code: `
var parent = require('../parent');
var async = require('async');
`,
errors: [{
ruleId: 'order',
message: '`async` import should occur before import of `../parent`',
}],
}),
// parent before sibling
test({
code: `
var sibling = require('./sibling');
var parent = require('../parent');
`,
errors: [{
ruleId: 'order',
message: '`../parent` import should occur before import of `./sibling`',
}],
}),
// sibling before index
test({
code: `
var index = require('./');
var sibling = require('./sibling');
`,
errors: [{
ruleId: 'order',
message: '`./sibling` import should occur before import of `./`',
}],
}),
// Multiple errors
test({
code: `
var sibling = require('./sibling');
var async = require('async');
var fs = require('fs');
`,
errors: [{
ruleId: 'order',
message: '`async` import should occur before import of `./sibling`',
}, {
ruleId: 'order',
message: '`fs` import should occur before import of `./sibling`',
}],
}),
// Uses 'after' wording if it creates less errors
test({
code: `
var index = require('./');
var fs = require('fs');
var path = require('path');
var _ = require('lodash');
var foo = require('foo');
var bar = require('bar');
`,
errors: [{
ruleId: 'order',
message: '`./` import should occur after import of `bar`',
}],
}),
// Overriding order to be the reverse of the default order
test({
code: `
var fs = require('fs');
var index = require('./');
`,
options: [{groups: ['index', 'sibling', 'parent', 'external', 'builtin']}],
errors: [{
ruleId: 'order',
message: '`./` import should occur before import of `fs`',
}],
}),
// member expression of require
test({
code: `
var foo = require('./foo').bar;
var fs = require('fs');
`,
errors: [{
ruleId: 'order',
message: '`fs` import should occur before import of `./foo`',
}],
}),
// nested member expression of require
test({
code: `
var foo = require('./foo').bar.bar.bar;
var fs = require('fs');
`,
errors: [{
ruleId: 'order',
message: '`fs` import should occur before import of `./foo`',
}],
}),
// Grouping import types
test({
code: `
var fs = require('fs');
var index = require('./');
var sibling = require('./foo');
var path = require('path');
`,
options: [{groups: [
['builtin', 'index'],
['sibling', 'parent', 'external'],
]}],
errors: [{
ruleId: 'order',
message: '`path` import should occur before import of `./foo`',
}],
}),
// Omitted types should implicitly be considered as the last type
test({
code: `
var path = require('path');
var async = require('async');
`,
options: [{groups: [
'index',
['sibling', 'parent', 'external', 'internal'],
// missing 'builtin'
]}],
errors: [{
ruleId: 'order',
message: '`async` import should occur before import of `path`',
}],
}),
// Setting the order for an unknown type
// should make the rule trigger an error and do nothing else
test({
code: `
var async = require('async');
var index = require('./');
`,
options: [{groups: [
'index',
['sibling', 'parent', 'UNKNOWN', 'internal'],
]}],
errors: [{
ruleId: 'order',
message: 'Incorrect configuration of the rule: Unknown type `"UNKNOWN"`',
}],
}),
// Type in an array can't be another array, too much nesting
test({
code: `
var async = require('async');
var index = require('./');
`,
options: [{groups: [
'index',
['sibling', 'parent', ['builtin'], 'internal'],
]}],
errors: [{
ruleId: 'order',
message: 'Incorrect configuration of the rule: Unknown type `["builtin"]`',
}],
}),
// No numbers
test({
code: `
var async = require('async');
var index = require('./');
`,
options: [{groups: [
'index',
['sibling', 'parent', 2, 'internal'],
]}],
errors: [{
ruleId: 'order',
message: 'Incorrect configuration of the rule: Unknown type `2`',
}],
}),
// Duplicate
test({
code: `
var async = require('async');
var index = require('./');
`,
options: [{groups: [
'index',
['sibling', 'parent', 'parent', 'internal'],
]}],
errors: [{
ruleId: 'order',
message: 'Incorrect configuration of the rule: `parent` is duplicated',
}],
}),
// Mixing require and import should have import up top
test({
code: `
import async, {foo1} from 'async';
import relParent2, {foo2} from '../foo/bar';
var fs = require('fs');
var relParent1 = require('../foo');
var relParent3 = require('../');
import sibling, {foo3} from './foo';
var index = require('./');
`,
errors: [{
ruleId: 'order',
message: '`./foo` import should occur before import of `fs`',
}],
}),
test({
code: `
var fs = require('fs');
import async, {foo1} from 'async';
import relParent2, {foo2} from '../foo/bar';
`,
errors: [{
ruleId: 'order',
message: '`fs` import should occur after import of `../foo/bar`',
}],
}),
// Option newlines-between: 'never' - should report unnecessary line between groups
test({
code: `
var fs = require('fs');
var index = require('./');
var path = require('path');
var sibling = require('./foo');
var relParent1 = require('../foo');
var relParent3 = require('../');
var async = require('async');
`,
options: [
{
groups: [
['builtin', 'index'],
['sibling'],
['parent', 'external'],
],
'newlines-between': 'never',
},
],
errors: [
{
line: 4,
message: 'There should be no empty line between import groups',
},
{
line: 6,
message: 'There should be no empty line between import groups',
},
],
}),
// // Option newlines-between: 'always' - should report lack of newline between groups
test({
code: `
var fs = require('fs');
var index = require('./');
var path = require('path');
var sibling = require('./foo');
var relParent1 = require('../foo');
var relParent3 = require('../');
var async = require('async');
`,
options: [
{
groups: [
['builtin', 'index'],
['sibling'],
['parent', 'external'],
],
'newlines-between': 'always',
},
],
errors: [
{
line: 4,
message: 'There should be at least one empty line between import groups',
},
{
line: 5,
message: 'There should be at least one empty line between import groups',
},
],
}),
//Option newlines-between: 'always' should report unnecessary empty lines space between import groups
test({
code: `
var fs = require('fs');
var path = require('path');
var index = require('./');
var sibling = require('./foo');
var async = require('async');
`,
options: [
{
groups: [
['builtin', 'index'],
['sibling', 'parent', 'external']
],
'newlines-between': 'always',
},
],
errors: [
{
line: 2,
message: 'There should be no empty line within import group',
},
{
line: 7,
message: 'There should be no empty line within import group',
},
],
}),
// Option newlines-between: 'never' should report unnecessary empty lines when using not assigned imports
test({
code: `
import path from 'path';
import 'loud-rejection';
import 'something-else';
import _ from 'lodash';
`,
options: [{ 'newlines-between': 'never' }],
errors: [
{
line: 2,
message: 'There should be no empty line between import groups',
},
],
}),
// Option newlines-between: 'always' should report missing empty lines when using not assigned imports
test({
code: `
import path from 'path';
import 'loud-rejection';
import 'something-else';
import _ from 'lodash';
`,
options: [{ 'newlines-between': 'always' }],
errors: [
{
line: 2,
message: 'There should be at least one empty line between import groups',
},
],
}),
],
})