Skip to content

Commit 77732a2

Browse files
authored
fix(eslint-plugin): [ban-types] allow banning types with specific parameters (typescript-eslint#2662)
1 parent dfbf115 commit 77732a2

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

packages/eslint-plugin/src/rules/ban-types.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function removeSpaces(str: string): string {
2828
return str.replace(/ /g, '');
2929
}
3030

31-
function stringifyTypeName(
31+
function stringifyNode(
3232
node: TSESTree.Node,
3333
sourceCode: TSESLint.SourceCode,
3434
): string {
@@ -175,7 +175,7 @@ export default util.createRule<Options, MessageIds>({
175175

176176
function checkBannedTypes(
177177
typeNode: TSESTree.Node,
178-
name = stringifyTypeName(typeNode, context.getSourceCode()),
178+
name = stringifyNode(typeNode, context.getSourceCode()),
179179
): void {
180180
const bannedType = bannedTypes.get(name);
181181

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

224224
checkBannedTypes(node);
225225
},
226-
TSTypeReference({ typeName }): void {
227-
checkBannedTypes(typeName);
226+
TSTypeReference(node): void {
227+
checkBannedTypes(node.typeName);
228+
229+
if (node.typeParameters) {
230+
checkBannedTypes(node);
231+
}
228232
},
229233
};
230234
},

packages/eslint-plugin/tests/rules/ban-types.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,48 @@ let bar: object = {};
487487
},
488488
],
489489
},
490+
{
491+
code: 'type Foo = Bar<any>;',
492+
errors: [
493+
{
494+
messageId: 'bannedTypeMessage',
495+
data: {
496+
name: 'Bar<any>',
497+
customMessage: " Don't use `any` as a type parameter to `Bar`",
498+
},
499+
line: 1,
500+
column: 12,
501+
},
502+
],
503+
options: [
504+
{
505+
types: {
506+
'Bar<any>': "Don't use `any` as a type parameter to `Bar`",
507+
},
508+
},
509+
],
510+
},
511+
{
512+
code: noFormat`type Foo = Bar<A,B>;`,
513+
errors: [
514+
{
515+
messageId: 'bannedTypeMessage',
516+
data: {
517+
name: 'Bar<A,B>',
518+
customMessage: " Don't pass `A, B` as parameters to `Bar`",
519+
},
520+
line: 1,
521+
column: 12,
522+
},
523+
],
524+
options: [
525+
{
526+
types: {
527+
'Bar<A, B>': "Don't pass `A, B` as parameters to `Bar`",
528+
},
529+
},
530+
],
531+
},
490532
...objectReduceKey(
491533
TYPE_KEYWORDS,
492534
(acc: TSESLint.InvalidTestCase<MessageIds, Options>[], key) => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy