@@ -4,8 +4,21 @@ import { RuleTester } from 'eslint';
4
4
import eslintPkg from 'eslint/package.json' ;
5
5
import semver from 'semver' ;
6
6
import flatMap from 'array.prototype.flatmap' ;
7
+ import { resolve } from 'path' ;
8
+ import { default as babelPresetFlow } from 'babel-preset-flow' ;
9
+
7
10
8
11
const ruleTester = new RuleTester ( ) ;
12
+ const flowRuleTester = new RuleTester ( {
13
+ parser : resolve ( __dirname , '../../../node_modules/babel-eslint' ) ,
14
+ parserOptions : {
15
+ babelOptions : {
16
+ configFile : false ,
17
+ babelrc : false ,
18
+ presets : [ babelPresetFlow ] ,
19
+ } ,
20
+ } ,
21
+ } ) ;
9
22
const rule = require ( 'rules/order' ) ;
10
23
11
24
function withoutAutofixOutput ( test ) {
@@ -1080,6 +1093,19 @@ ruleTester.run('order', rule, {
1080
1093
} ,
1081
1094
] ,
1082
1095
} ) ,
1096
+ // orderImportKind option that is not used
1097
+ test ( {
1098
+ code : `
1099
+ import B from './B';
1100
+ import b from './b';
1101
+ ` ,
1102
+ options : [
1103
+ {
1104
+ 'alphabetize' : { order : 'asc' , orderImportKind : 'asc' , 'caseInsensitive' : true } ,
1105
+ } ,
1106
+ ] ,
1107
+ } ) ,
1108
+
1083
1109
] ,
1084
1110
invalid : [
1085
1111
// builtin before external module (require)
@@ -2931,8 +2957,8 @@ context('TypeScript', function () {
2931
2957
errors : [
2932
2958
{
2933
2959
message : semver . satisfies ( eslintPkg . version , '< 3' )
2934
- ? '`bar` import should occur after import of `Bar`'
2935
- : / ( ` b a r ` i m p o r t s h o u l d o c c u r a f t e r i m p o r t o f ` B a r ` ) | ( ` B a r ` i m p o r t s h o u l d o c c u r b e f o r e i m p o r t o f ` b a r ` ) / ,
2960
+ ? '`bar` import should occur after type import of `Bar`'
2961
+ : / ( ` b a r ` i m p o r t s h o u l d o c c u r a f t e r t y p e i m p o r t o f ` B a r ` ) | ( ` B a r ` t y p e i m p o r t s h o u l d o c c u r b e f o r e i m p o r t o f ` b a r ` ) / ,
2936
2962
} ,
2937
2963
] ,
2938
2964
} ) ,
@@ -3002,10 +3028,10 @@ context('TypeScript', function () {
3002
3028
] ,
3003
3029
errors : semver . satisfies ( eslintPkg . version , '< 3' ) ? [
3004
3030
{ message : '`Bar` import should occur before import of `bar`' } ,
3005
- { message : '`Bar` import should occur before import of `foo`' } ,
3031
+ { message : '`Bar` type import should occur before type import of `foo`' } ,
3006
3032
] : [
3007
3033
{ message : / ( ` B a r ` i m p o r t s h o u l d o c c u r b e f o r e i m p o r t o f ` b a r ` ) | ( ` b a r ` i m p o r t s h o u l d o c c u r a f t e r i m p o r t o f ` B a r ` ) / } ,
3008
- { message : / ( ` B a r ` i m p o r t s h o u l d o c c u r b e f o r e i m p o r t o f ` f o o ` ) | ( ` f o o ` i m p o r t s h o u l d o c c u r a f t e r i m p o r t o f ` B a r ` ) / } ,
3034
+ { message : / ( ` B a r ` t y p e i m p o r t s h o u l d o c c u r b e f o r e t y p e i m p o r t o f ` f o o ` ) | ( ` f o o ` t y p e i m p o r t s h o u l d o c c u r a f t e r t y p e i m p o r t o f ` B a r ` ) / } ,
3009
3035
] ,
3010
3036
} ) ,
3011
3037
// Option alphabetize: {order: 'desc'} with type group
@@ -3039,10 +3065,10 @@ context('TypeScript', function () {
3039
3065
] ,
3040
3066
errors : semver . satisfies ( eslintPkg . version , '< 3' ) ? [
3041
3067
{ message : '`bar` import should occur before import of `Bar`' } ,
3042
- { message : '`foo` import should occur before import of `Bar`' } ,
3068
+ { message : '`foo` type import should occur before type import of `Bar`' } ,
3043
3069
] : [
3044
3070
{ message : / ( ` b a r ` i m p o r t s h o u l d o c c u r b e f o r e i m p o r t o f ` B a r ` ) | ( ` B a r ` i m p o r t s h o u l d o c c u r a f t e r i m p o r t o f ` b a r ` ) / } ,
3045
- { message : / ( ` f o o ` i m p o r t s h o u l d o c c u r b e f o r e i m p o r t o f ` B a r ` ) | ( ` B a r ` i m p o r t s h o u l d o c c u r a f t e r i m p o r t o f ` f o o ` ) / } ,
3071
+ { message : / ( ` f o o ` t y p e i m p o r t s h o u l d o c c u r b e f o r e t y p e i m p o r t o f ` B a r ` ) | ( ` B a r ` t y p e i m p o r t s h o u l d o c c u r a f t e r i m p o r t o f t y p e ` f o o ` ) / } ,
3046
3072
] ,
3047
3073
} ) ,
3048
3074
// warns for out of order unassigned imports (warnOnUnassignedImports enabled)
@@ -3113,9 +3139,9 @@ context('TypeScript', function () {
3113
3139
}
3114
3140
` ,
3115
3141
errors : [ {
3116
- message : '`fs` import should occur before import of `path`' ,
3142
+ message : '`fs` type import should occur before type import of `path`' ,
3117
3143
} , {
3118
- message : '`fs` import should occur before import of `path`' ,
3144
+ message : '`fs` type import should occur before type import of `path`' ,
3119
3145
} ] ,
3120
3146
...parserConfig ,
3121
3147
options : [
@@ -3128,3 +3154,83 @@ context('TypeScript', function () {
3128
3154
} ) ;
3129
3155
} ) ;
3130
3156
} ) ;
3157
+
3158
+ flowRuleTester . run ( 'order' , rule , {
3159
+ valid : [
3160
+ test ( {
3161
+ options : [
3162
+ {
3163
+ alphabetize : { order : 'asc' , orderImportKind : 'asc' } ,
3164
+ } ,
3165
+ ] ,
3166
+ code : `
3167
+ import type {Bar} from 'common';
3168
+ import typeof {foo} from 'common';
3169
+ import {bar} from 'common';
3170
+ ` ,
3171
+ } ) ] ,
3172
+ invalid : [
3173
+ test ( {
3174
+ options : [
3175
+ {
3176
+ alphabetize : { order : 'asc' , orderImportKind : 'asc' } ,
3177
+ } ,
3178
+ ] ,
3179
+ code : `
3180
+ import type {Bar} from 'common';
3181
+ import {bar} from 'common';
3182
+ import typeof {foo} from 'common';
3183
+ ` ,
3184
+ output : `
3185
+ import type {Bar} from 'common';
3186
+ import typeof {foo} from 'common';
3187
+ import {bar} from 'common';
3188
+ ` ,
3189
+ errors : [ {
3190
+ message : '`common` typeof import should occur before import of `common`' ,
3191
+ } ] ,
3192
+ } ) ,
3193
+ test ( {
3194
+ options : [
3195
+ {
3196
+ alphabetize : { order : 'asc' , orderImportKind : 'desc' } ,
3197
+ } ,
3198
+ ] ,
3199
+ code : `
3200
+ import type {Bar} from 'common';
3201
+ import {bar} from 'common';
3202
+ import typeof {foo} from 'common';
3203
+ ` ,
3204
+ output : `
3205
+ import {bar} from 'common';
3206
+ import typeof {foo} from 'common';
3207
+ import type {Bar} from 'common';
3208
+ ` ,
3209
+ errors : [ {
3210
+ message : '`common` type import should occur after typeof import of `common`' ,
3211
+ } ] ,
3212
+ } ) ,
3213
+ test ( {
3214
+ options : [
3215
+ {
3216
+ alphabetize : { order : 'asc' , orderImportKind : 'asc' } ,
3217
+ } ,
3218
+ ] ,
3219
+ code : `
3220
+ import type {Bar} from './local/sub';
3221
+ import {bar} from './local/sub';
3222
+ import {baz} from './local-sub';
3223
+ import typeof {foo} from './local/sub';
3224
+ ` ,
3225
+ output : `
3226
+ import type {Bar} from './local/sub';
3227
+ import typeof {foo} from './local/sub';
3228
+ import {bar} from './local/sub';
3229
+ import {baz} from './local-sub';
3230
+ ` ,
3231
+ errors : [ {
3232
+ message : '`./local/sub` typeof import should occur before import of `./local/sub`' ,
3233
+ } ] ,
3234
+ } ) ,
3235
+ ] ,
3236
+ } ) ;
0 commit comments