Skip to content
Permalink
Browse files

feat(eslint-plugin): [ban-types] support banning `[]` (#2704)

fix #2582
  • Loading branch information
Jonas
Jonas committed Oct 25, 2020
1 parent 0763913 commit ef8b5a7e09cca4bdacf205da28f99f2b1a419d00
@@ -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.
@@ -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);

@@ -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<MessageIds, Options>[], key) => {

0 comments on commit ef8b5a7

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