Skip to content

Commit

Permalink
fix(eslint-plugin): [ban-types] allow banning types with specific par…
Browse files Browse the repository at this point in the history
…ameters (#2662)
  • Loading branch information
binoche9 committed Oct 11, 2020
1 parent dfbf115 commit 77732a2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/eslint-plugin/src/rules/ban-types.ts
Expand Up @@ -28,7 +28,7 @@ function removeSpaces(str: string): string {
return str.replace(/ /g, '');
}

function stringifyTypeName(
function stringifyNode(
node: TSESTree.Node,
sourceCode: TSESLint.SourceCode,
): string {
Expand Down Expand Up @@ -175,7 +175,7 @@ export default util.createRule<Options, MessageIds>({

function checkBannedTypes(
typeNode: TSESTree.Node,
name = stringifyTypeName(typeNode, context.getSourceCode()),
name = stringifyNode(typeNode, context.getSourceCode()),
): void {
const bannedType = bannedTypes.get(name);

Expand Down Expand Up @@ -223,8 +223,12 @@ export default util.createRule<Options, MessageIds>({

checkBannedTypes(node);
},
TSTypeReference({ typeName }): void {
checkBannedTypes(typeName);
TSTypeReference(node): void {
checkBannedTypes(node.typeName);

if (node.typeParameters) {
checkBannedTypes(node);
}
},
};
},
Expand Down
42 changes: 42 additions & 0 deletions packages/eslint-plugin/tests/rules/ban-types.test.ts
Expand Up @@ -487,6 +487,48 @@ let bar: object = {};
},
],
},
{
code: 'type Foo = Bar<any>;',
errors: [
{
messageId: 'bannedTypeMessage',
data: {
name: 'Bar<any>',
customMessage: " Don't use `any` as a type parameter to `Bar`",
},
line: 1,
column: 12,
},
],
options: [
{
types: {
'Bar<any>': "Don't use `any` as a type parameter to `Bar`",
},
},
],
},
{
code: noFormat`type Foo = Bar<A,B>;`,
errors: [
{
messageId: 'bannedTypeMessage',
data: {
name: 'Bar<A,B>',
customMessage: " Don't pass `A, B` as parameters to `Bar`",
},
line: 1,
column: 12,
},
],
options: [
{
types: {
'Bar<A, B>': "Don't pass `A, B` as parameters to `Bar`",
},
},
],
},
...objectReduceKey(
TYPE_KEYWORDS,
(acc: TSESLint.InvalidTestCase<MessageIds, Options>[], key) => {
Expand Down

0 comments on commit 77732a2

Please sign in to comment.