Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Oct 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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