Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef8b5a7

Browse files
feat(eslint-plugin): [ban-types] support banning [] (typescript-eslint#2704)
fix typescript-eslint#2582
1 parent 0763913 commit ef8b5a7

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

packages/eslint-plugin/docs/rules/ban-types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Options = {
2828
The rule accepts a single object as options, with the following keys:
2929

3030
- `types` - An object whose keys are the types you want to ban, and the values are error messages.
31-
- 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 (`{}`).
31+
- 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 (`[]`).
3232
- The values can be a string, which is the error message to be reported, `false` to specifically disable this type
3333
or it can be an object with the following properties:
3434
- `message: string` - the message to display when the type is matched.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ export default util.createRule<Options, MessageIds>({
223223

224224
checkBannedTypes(node);
225225
},
226+
TSTupleType(node): void {
227+
if (node.elementTypes.length === 0) {
228+
checkBannedTypes(node);
229+
}
230+
},
226231
TSTypeReference(node): void {
227232
checkBannedTypes(node.typeName);
228233

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

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ ruleTester.run('ban-types', rule, {
108108
},
109109
],
110110
},
111+
'let a: [];',
111112
],
112113
invalid: [
113114
{
@@ -529,6 +530,94 @@ let bar: object = {};
529530
},
530531
],
531532
},
533+
{
534+
code: 'let a: [];',
535+
errors: [
536+
{
537+
messageId: 'bannedTypeMessage',
538+
data: {
539+
name: '[]',
540+
customMessage: ' `[]` does only allow empty arrays.',
541+
},
542+
line: 1,
543+
column: 8,
544+
},
545+
],
546+
options: [
547+
{
548+
types: {
549+
'[]': '`[]` does only allow empty arrays.',
550+
},
551+
},
552+
],
553+
},
554+
{
555+
code: noFormat`let a: [ ] ;`,
556+
errors: [
557+
{
558+
messageId: 'bannedTypeMessage',
559+
data: {
560+
name: '[]',
561+
customMessage: ' `[]` does only allow empty arrays.',
562+
},
563+
line: 1,
564+
column: 9,
565+
},
566+
],
567+
options: [
568+
{
569+
types: {
570+
'[]': '`[]` does only allow empty arrays.',
571+
},
572+
},
573+
],
574+
},
575+
{
576+
code: 'let a: [];',
577+
output: 'let a: any[];',
578+
errors: [
579+
{
580+
messageId: 'bannedTypeMessage',
581+
data: {
582+
name: '[]',
583+
customMessage: ' `[]` does only allow empty arrays.',
584+
},
585+
line: 1,
586+
column: 8,
587+
},
588+
],
589+
options: [
590+
{
591+
types: {
592+
'[]': {
593+
message: '`[]` does only allow empty arrays.',
594+
fixWith: 'any[]',
595+
},
596+
},
597+
},
598+
],
599+
},
600+
{
601+
code: 'let a: [[]];',
602+
errors: [
603+
{
604+
messageId: 'bannedTypeMessage',
605+
data: {
606+
name: '[]',
607+
customMessage: ' `[]` does only allow empty arrays.',
608+
},
609+
line: 1,
610+
column: 9,
611+
},
612+
],
613+
options: [
614+
{
615+
types: {
616+
'[]': '`[]` does only allow empty arrays.',
617+
},
618+
},
619+
],
620+
},
532621
...objectReduceKey(
533622
TYPE_KEYWORDS,
534623
(acc: TSESLint.InvalidTestCase<MessageIds, Options>[], key) => {

0 commit comments

Comments
 (0)