Skip to content
Permalink
Browse files

fix(eslint-plugin): [ban-types] allow banning types with specific par…

…ameters (#2662)
  • Loading branch information
binoche9 committed Oct 11, 2020
1 parent dfbf115 commit 77732a2f3979f638e471b6de327b2ea0e976d568
@@ -28,7 +28,7 @@ function removeSpaces(str: string): string {
return str.replace(/ /g, '');
}

function stringifyTypeName(
function stringifyNode(
node: TSESTree.Node,
sourceCode: TSESLint.SourceCode,
): string {
@@ -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);

@@ -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);
}
},
};
},
@@ -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) => {

0 comments on commit 77732a2

Please sign in to comment.
You can’t perform that action at this time.