@@ -62,7 +62,10 @@ enum Selectors {
62
62
interface = 1 << 13 ,
63
63
typeAlias = 1 << 14 ,
64
64
enum = 1 << 15 ,
65
- typeParameter = 1 << 17 ,
65
+ typeParameter = 1 << 16 ,
66
+
67
+ // un-grouped
68
+ import = 1 << 17 ,
66
69
}
67
70
type SelectorsString = keyof typeof Selectors ;
68
71
@@ -120,6 +123,12 @@ enum Modifiers {
120
123
exported = 1 << 9 ,
121
124
// things that are unused
122
125
unused = 1 << 10 ,
126
+
127
+ // imports
128
+ default = 1 << 11 ,
129
+ namespace = 1 << 12 ,
130
+ named = 1 << 13 ,
131
+ renamed = 1 << 14 ,
123
132
}
124
133
type ModifiersString = keyof typeof Modifiers ;
125
134
@@ -352,6 +361,14 @@ const SCHEMA: JSONSchema.JSONSchema4 = {
352
361
...selectorSchema ( 'function' , false , [ 'global' , 'exported' , 'unused' ] ) ,
353
362
...selectorSchema ( 'parameter' , true , [ 'unused' ] ) ,
354
363
364
+ ...selectorSchema ( 'import' , false , [
365
+ 'namespace' ,
366
+ 'named' ,
367
+ 'default' ,
368
+ 'renamed' ,
369
+ 'unused' ,
370
+ ] ) ,
371
+
355
372
...selectorSchema ( 'memberLike' , false , [
356
373
'private' ,
357
374
'protected' ,
@@ -964,6 +981,60 @@ export default util.createRule<Options, MessageIds>({
964
981
} ,
965
982
966
983
// #endregion typeParameter
984
+
985
+ // #region import
986
+
987
+ ImportDeclaration ( node ) : void {
988
+ const validator = validators . import ;
989
+ if ( ! validator ) {
990
+ return ;
991
+ }
992
+
993
+ node . specifiers . forEach ( specifier => {
994
+ const identifier = specifier . local ;
995
+ const modifiers = new Set < Modifiers > ( ) ;
996
+
997
+ switch ( specifier . type ) {
998
+ case AST_NODE_TYPES . ImportDefaultSpecifier :
999
+ modifiers . add ( Modifiers . default ) ;
1000
+ break ;
1001
+
1002
+ case AST_NODE_TYPES . ImportNamespaceSpecifier :
1003
+ modifiers . add ( Modifiers . namespace ) ;
1004
+ break ;
1005
+
1006
+ case AST_NODE_TYPES . ImportSpecifier :
1007
+ modifiers . add ( Modifiers . named ) ;
1008
+ if ( specifier . local . name !== specifier . imported . name ) {
1009
+ modifiers . add ( Modifiers . renamed ) ;
1010
+ }
1011
+
1012
+ break ;
1013
+ }
1014
+
1015
+ if ( isUnused ( identifier . name , context . getScope ( ) ) ) {
1016
+ modifiers . add ( Modifiers . unused ) ;
1017
+ }
1018
+
1019
+ validator ( identifier , modifiers ) ;
1020
+ } ) ;
1021
+ } ,
1022
+ TSImportEqualsDeclaration ( node ) : void {
1023
+ const validator = validators . import ;
1024
+ if ( ! validator ) {
1025
+ return ;
1026
+ }
1027
+
1028
+ const modifiers = new Set < Modifiers > ( [ Modifiers . default ] ) ;
1029
+
1030
+ if ( isUnused ( node . id . name , context . getScope ( ) ) ) {
1031
+ modifiers . add ( Modifiers . unused ) ;
1032
+ }
1033
+
1034
+ validator ( node . id , modifiers ) ;
1035
+ } ,
1036
+
1037
+ // #endregion import
967
1038
} ;
968
1039
} ,
969
1040
} ) ;
@@ -1021,7 +1092,7 @@ function isGlobal(scope: TSESLint.Scope.Scope | null): boolean {
1021
1092
}
1022
1093
1023
1094
type ValidatorFunction = (
1024
- node : TSESTree . Identifier | TSESTree . Literal ,
1095
+ node : TSESTree . Identifier | TSESTree . StringLiteral | TSESTree . NumberLiteral ,
1025
1096
modifiers ?: Set < Modifiers > ,
1026
1097
) => void ;
1027
1098
type ParsedOptions = Record < SelectorsString , null | ValidatorFunction > ;
@@ -1041,7 +1112,7 @@ function createValidator(
1041
1112
type : SelectorsString ,
1042
1113
context : Context ,
1043
1114
allConfigs : NormalizedSelector [ ] ,
1044
- ) : ( node : TSESTree . Identifier | TSESTree . Literal ) => void {
1115
+ ) : ValidatorFunction {
1045
1116
// make sure the "highest priority" configs are checked first
1046
1117
const selectorType = Selectors [ type ] ;
1047
1118
const configs = allConfigs
@@ -1074,10 +1145,7 @@ function createValidator(
1074
1145
return b . selector - a . selector ;
1075
1146
} ) ;
1076
1147
1077
- return (
1078
- node : TSESTree . Identifier | TSESTree . Literal ,
1079
- modifiers : Set < Modifiers > = new Set < Modifiers > ( ) ,
1080
- ) : void => {
1148
+ return ( node , modifiers = new Set < Modifiers > ( ) ) : void => {
1081
1149
const originalName =
1082
1150
node . type === AST_NODE_TYPES . Identifier ? node . name : `${ node . value } ` ;
1083
1151
0 commit comments