From 1f6764908acec67a4ca2e50a263de63d54246c09 Mon Sep 17 00:00:00 2001 From: jsone-studios Date: Sat, 24 Oct 2020 10:06:26 +0200 Subject: [PATCH] feat(eslint-plugin): [ban-types] support banning `[]` fix #2582 --- .../eslint-plugin/docs/rules/ban-types.md | 2 +- packages/eslint-plugin/src/rules/ban-types.ts | 5 ++ .../tests/rules/ban-types.test.ts | 89 +++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/docs/rules/ban-types.md b/packages/eslint-plugin/docs/rules/ban-types.md index ab8320d10dc7..761cac128f37 100644 --- a/packages/eslint-plugin/docs/rules/ban-types.md +++ b/packages/eslint-plugin/docs/rules/ban-types.md @@ -28,7 +28,7 @@ type Options = { The rule accepts a single object as options, with the following keys: - `types` - An object whose keys are the types you want to ban, and the values are error messages. - - The type can either be a type name literal (`Foo`), a type name with generic parameter instantiation(s) (`Foo`), or the empty object literal (`{}`). + - The type can either be a type name literal (`Foo`), a type name with generic parameter instantiation(s) (`Foo`), the empty object literal (`{}`), or the empty tuple type (`[]`). - The values can be a string, which is the error message to be reported, `false` to specifically disable this type or it can be an object with the following properties: - `message: string` - the message to display when the type is matched. diff --git a/packages/eslint-plugin/src/rules/ban-types.ts b/packages/eslint-plugin/src/rules/ban-types.ts index beaadb14ec29..0187134e9917 100644 --- a/packages/eslint-plugin/src/rules/ban-types.ts +++ b/packages/eslint-plugin/src/rules/ban-types.ts @@ -223,6 +223,11 @@ export default util.createRule({ checkBannedTypes(node); }, + TSTupleType(node): void { + if (node.elementTypes.length === 0) { + checkBannedTypes(node); + } + }, TSTypeReference(node): void { checkBannedTypes(node.typeName); diff --git a/packages/eslint-plugin/tests/rules/ban-types.test.ts b/packages/eslint-plugin/tests/rules/ban-types.test.ts index ca7078585b13..27743ecc5a3a 100644 --- a/packages/eslint-plugin/tests/rules/ban-types.test.ts +++ b/packages/eslint-plugin/tests/rules/ban-types.test.ts @@ -108,6 +108,7 @@ ruleTester.run('ban-types', rule, { }, ], }, + 'let a: [];', ], invalid: [ { @@ -529,6 +530,94 @@ let bar: object = {}; }, ], }, + { + code: 'let a: [];', + errors: [ + { + messageId: 'bannedTypeMessage', + data: { + name: '[]', + customMessage: ' `[]` does only allow empty arrays.', + }, + line: 1, + column: 8, + }, + ], + options: [ + { + types: { + '[]': '`[]` does only allow empty arrays.', + }, + }, + ], + }, + { + code: noFormat`let a: [ ] ;`, + errors: [ + { + messageId: 'bannedTypeMessage', + data: { + name: '[]', + customMessage: ' `[]` does only allow empty arrays.', + }, + line: 1, + column: 9, + }, + ], + options: [ + { + types: { + '[]': '`[]` does only allow empty arrays.', + }, + }, + ], + }, + { + code: 'let a: [];', + output: 'let a: any[];', + errors: [ + { + messageId: 'bannedTypeMessage', + data: { + name: '[]', + customMessage: ' `[]` does only allow empty arrays.', + }, + line: 1, + column: 8, + }, + ], + options: [ + { + types: { + '[]': { + message: '`[]` does only allow empty arrays.', + fixWith: 'any[]', + }, + }, + }, + ], + }, + { + code: 'let a: [[]];', + errors: [ + { + messageId: 'bannedTypeMessage', + data: { + name: '[]', + customMessage: ' `[]` does only allow empty arrays.', + }, + line: 1, + column: 9, + }, + ], + options: [ + { + types: { + '[]': '`[]` does only allow empty arrays.', + }, + }, + ], + }, ...objectReduceKey( TYPE_KEYWORDS, (acc: TSESLint.InvalidTestCase[], key) => {