Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [ban-types] allow banning types with specific parameters #2662

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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