Skip to content

Commit 18608e5

Browse files
committed
feat(eslint-plugin): [naming-convention] add import selector
Fixes typescript-eslint#2106 This is a work in progress.
1 parent dd0576a commit 18608e5

File tree

1 file changed

+75
-7
lines changed

1 file changed

+75
-7
lines changed

packages/eslint-plugin/src/rules/naming-convention.ts

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ enum Selectors {
6262
interface = 1 << 13,
6363
typeAlias = 1 << 14,
6464
enum = 1 << 15,
65-
typeParameter = 1 << 17,
65+
typeParameter = 1 << 16,
66+
67+
// un-grouped
68+
import = 1 << 17,
6669
}
6770
type SelectorsString = keyof typeof Selectors;
6871

@@ -120,6 +123,12 @@ enum Modifiers {
120123
exported = 1 << 9,
121124
// things that are unused
122125
unused = 1 << 10,
126+
127+
// imports
128+
default = 1 << 11,
129+
namespace = 1 << 12,
130+
named = 1 << 13,
131+
renamed = 1 << 14,
123132
}
124133
type ModifiersString = keyof typeof Modifiers;
125134

@@ -352,6 +361,14 @@ const SCHEMA: JSONSchema.JSONSchema4 = {
352361
...selectorSchema('function', false, ['global', 'exported', 'unused']),
353362
...selectorSchema('parameter', true, ['unused']),
354363

364+
...selectorSchema('import', false, [
365+
'namespace',
366+
'named',
367+
'default',
368+
'renamed',
369+
'unused',
370+
]),
371+
355372
...selectorSchema('memberLike', false, [
356373
'private',
357374
'protected',
@@ -964,6 +981,60 @@ export default util.createRule<Options, MessageIds>({
964981
},
965982

966983
// #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
9671038
};
9681039
},
9691040
});
@@ -1021,7 +1092,7 @@ function isGlobal(scope: TSESLint.Scope.Scope | null): boolean {
10211092
}
10221093

10231094
type ValidatorFunction = (
1024-
node: TSESTree.Identifier | TSESTree.Literal,
1095+
node: TSESTree.Identifier | TSESTree.StringLiteral | TSESTree.NumberLiteral,
10251096
modifiers?: Set<Modifiers>,
10261097
) => void;
10271098
type ParsedOptions = Record<SelectorsString, null | ValidatorFunction>;
@@ -1041,7 +1112,7 @@ function createValidator(
10411112
type: SelectorsString,
10421113
context: Context,
10431114
allConfigs: NormalizedSelector[],
1044-
): (node: TSESTree.Identifier | TSESTree.Literal) => void {
1115+
): ValidatorFunction {
10451116
// make sure the "highest priority" configs are checked first
10461117
const selectorType = Selectors[type];
10471118
const configs = allConfigs
@@ -1074,10 +1145,7 @@ function createValidator(
10741145
return b.selector - a.selector;
10751146
});
10761147

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 => {
10811149
const originalName =
10821150
node.type === AST_NODE_TYPES.Identifier ? node.name : `${node.value}`;
10831151

0 commit comments

Comments
 (0)