Skip to content

Commit

Permalink
feat(eslint-plugin): [ban-types] support banning [] (#2704)
Browse files Browse the repository at this point in the history
fix #2582
  • Loading branch information
jsone-studios committed Oct 25, 2020
1 parent 0763913 commit ef8b5a7
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/eslint-plugin/docs/rules/ban-types.md
Expand Up @@ -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<Bar>`), or the empty object literal (`{}`).
- The type can either be a type name literal (`Foo`), a type name with generic parameter instantiation(s) (`Foo<Bar>`), 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.
Expand Down
5 changes: 5 additions & 0 deletions packages/eslint-plugin/src/rules/ban-types.ts
Expand Up @@ -223,6 +223,11 @@ export default util.createRule<Options, MessageIds>({

checkBannedTypes(node);
},
TSTupleType(node): void {
if (node.elementTypes.length === 0) {
checkBannedTypes(node);
}
},
TSTypeReference(node): void {
checkBannedTypes(node.typeName);

Expand Down
89 changes: 89 additions & 0 deletions packages/eslint-plugin/tests/rules/ban-types.test.ts
Expand Up @@ -108,6 +108,7 @@ ruleTester.run('ban-types', rule, {
},
],
},
'let a: [];',
],
invalid: [
{
Expand Down Expand Up @@ -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<MessageIds, Options>[], key) => {
Expand Down

0 comments on commit ef8b5a7

Please sign in to comment.