From f679c9950b36ed0e947d3855986bfb899b72a44e Mon Sep 17 00:00:00 2001 From: kmin-jeong Date: Thu, 24 Nov 2022 22:43:31 +0900 Subject: [PATCH 01/14] fix:fix messages --- packages/eslint-plugin/src/rules/ban-types.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/ban-types.ts b/packages/eslint-plugin/src/rules/ban-types.ts index f94808a50a5..dce41140515 100644 --- a/packages/eslint-plugin/src/rules/ban-types.ts +++ b/packages/eslint-plugin/src/rules/ban-types.ts @@ -86,14 +86,14 @@ const defaultTypes: Types = { Object: { message: [ 'The `Object` type actually means "any non-nullish value", so it is marginally better than `unknown`.', - '- If you want a type meaning "any object", you probably want `Record` instead.', + '- If you want a type meaning "any object", you probably want `object` instead.', '- If you want a type meaning "any value", you probably want `unknown` instead.', ].join('\n'), }, '{}': { message: [ '`{}` actually means "any non-nullish value".', - '- If you want a type meaning "any object", you probably want `Record` instead.', + '- If you want a type meaning "any object", you probably want `object` instead.', '- If you want a type meaning "any value", you probably want `unknown` instead.', '- If you want a type meaning "empty object", you probably want `Record` instead.', ].join('\n'), From 63d00683dfb7e5d52438096345a532bb24702293 Mon Sep 17 00:00:00 2001 From: kmin-jeong Date: Tue, 29 Nov 2022 15:14:48 +0900 Subject: [PATCH 02/14] fix:fix type record to object --- packages/eslint-plugin/src/rules/ban-types.ts | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/packages/eslint-plugin/src/rules/ban-types.ts b/packages/eslint-plugin/src/rules/ban-types.ts index dce41140515..34816150724 100644 --- a/packages/eslint-plugin/src/rules/ban-types.ts +++ b/packages/eslint-plugin/src/rules/ban-types.ts @@ -3,16 +3,12 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils'; import * as util from '../util'; -type Types = Record< - string, - | null - | false - | string +type Types = + | object | { message: string; fixWith?: string; - } ->; + }; export type Options = [ { @@ -33,9 +29,10 @@ function stringifyNode( return removeSpaces(sourceCode.getText(node)); } -function getCustomMessage( - bannedType: null | string | { message?: string; fixWith?: string }, -): string { +function getCustomMessage(bannedType: { + message?: string; + fixWith?: string; +}): string { if (bannedType === null) { return ''; } From faf1ac2653fd9d7f875bfbd8654a19299d7677e4 Mon Sep 17 00:00:00 2001 From: kmin-jeong Date: Tue, 29 Nov 2022 16:10:44 +0900 Subject: [PATCH 03/14] fix:fix record to object --- packages/eslint-plugin/src/rules/ban-types.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/ban-types.ts b/packages/eslint-plugin/src/rules/ban-types.ts index 34816150724..ba8e199eca7 100644 --- a/packages/eslint-plugin/src/rules/ban-types.ts +++ b/packages/eslint-plugin/src/rules/ban-types.ts @@ -6,8 +6,10 @@ import * as util from '../util'; type Types = | object | { - message: string; - fixWith?: string; + [key: string]: { + message: string; + fixWith?: string; + }; }; export type Options = [ From ac2f4f1d5c270d6a87e7c71e29bb7c7737f163fe Mon Sep 17 00:00:00 2001 From: kmin-jeong Date: Tue, 29 Nov 2022 17:05:28 +0900 Subject: [PATCH 04/14] feat: add test code --- .../tests/rules/ban-types.test.ts | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/eslint-plugin/tests/rules/ban-types.test.ts b/packages/eslint-plugin/tests/rules/ban-types.test.ts index 7883f0f45df..8d31fe7ac8c 100644 --- a/packages/eslint-plugin/tests/rules/ban-types.test.ts +++ b/packages/eslint-plugin/tests/rules/ban-types.test.ts @@ -388,6 +388,39 @@ let b: Foo; }, ], }, + + { + code: noFormat` +declare let j: unknown; +let m3 = { ...(j as Record) }; + `, + output: ` +declare let j: unknown; +let m3 = { ...(j as object) }; + `, + options: [ + { + types: { + 'Record': { + message: 'Use object instead.', + fixWith: 'object', + }, + }, + }, + ], + errors: [ + { + messageId: 'bannedTypeMessage', + data: { + name: 'Record', + customMessage: ' Use object instead.', + }, + line: 3, + column: 21, + }, + ], + }, + { code: noFormat` let foo: {} = {}; From 209590cffca7040ffa3265410aa57cacf3445b62 Mon Sep 17 00:00:00 2001 From: kmin-jeong Date: Wed, 30 Nov 2022 17:29:29 +0900 Subject: [PATCH 05/14] fix:fix 5 lint error --- packages/eslint-plugin/src/rules/ban-types.ts | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/packages/eslint-plugin/src/rules/ban-types.ts b/packages/eslint-plugin/src/rules/ban-types.ts index ba8e199eca7..806ac4d066b 100644 --- a/packages/eslint-plugin/src/rules/ban-types.ts +++ b/packages/eslint-plugin/src/rules/ban-types.ts @@ -3,14 +3,12 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils'; import * as util from '../util'; -type Types = - | object - | { - [key: string]: { - message: string; - fixWith?: string; - }; - }; +type Index = string | number | boolean | undefined | null; +type Types = Index | object | { fixWith: string; message: string }; +// | { +// message: string; +// fixWith?: string; +// }; export type Options = [ { @@ -31,10 +29,9 @@ function stringifyNode( return removeSpaces(sourceCode.getText(node)); } -function getCustomMessage(bannedType: { - message?: string; - fixWith?: string; -}): string { +function getCustomMessage( + bannedType: null | string | { message?: string; fixWith?: string }, +): string { if (bannedType === null) { return ''; } @@ -85,14 +82,14 @@ const defaultTypes: Types = { Object: { message: [ 'The `Object` type actually means "any non-nullish value", so it is marginally better than `unknown`.', - '- If you want a type meaning "any object", you probably want `object` instead.', + '- If you want a type meaning "any object", you probably want `Record` instead.', '- If you want a type meaning "any value", you probably want `unknown` instead.', ].join('\n'), }, '{}': { message: [ '`{}` actually means "any non-nullish value".', - '- If you want a type meaning "any object", you probably want `object` instead.', + '- If you want a type meaning "any object", you probably want `Record` instead.', '- If you want a type meaning "any value", you probably want `unknown` instead.', '- If you want a type meaning "empty object", you probably want `Record` instead.', ].join('\n'), @@ -172,7 +169,8 @@ export default util.createRule({ typeNode: TSESTree.Node, name = stringifyNode(typeNode, context.getSourceCode()), ): void { - const bannedType = bannedTypes.get(name); + type m = { fixWith?: string; message: string } | false; + const bannedType: m = bannedTypes.get(name); if (bannedType === undefined || bannedType === false) { return; From ecbc1385055ce978fb52c8a65395d369f4673e27 Mon Sep 17 00:00:00 2001 From: kmin-jeong Date: Wed, 30 Nov 2022 17:40:03 +0900 Subject: [PATCH 06/14] fix:fix variable name in checkBannedTypes --- packages/eslint-plugin/src/rules/ban-types.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/eslint-plugin/src/rules/ban-types.ts b/packages/eslint-plugin/src/rules/ban-types.ts index 806ac4d066b..8e55fe3bea0 100644 --- a/packages/eslint-plugin/src/rules/ban-types.ts +++ b/packages/eslint-plugin/src/rules/ban-types.ts @@ -5,10 +5,6 @@ import * as util from '../util'; type Index = string | number | boolean | undefined | null; type Types = Index | object | { fixWith: string; message: string }; -// | { -// message: string; -// fixWith?: string; -// }; export type Options = [ { @@ -169,8 +165,8 @@ export default util.createRule({ typeNode: TSESTree.Node, name = stringifyNode(typeNode, context.getSourceCode()), ): void { - type m = { fixWith?: string; message: string } | false; - const bannedType: m = bannedTypes.get(name); + type total = { fixWith?: string; message: string } | false; + const bannedType: total = bannedTypes.get(name); if (bannedType === undefined || bannedType === false) { return; From 7fa5ebd7af74635985f217ae053235b09a7d4039 Mon Sep 17 00:00:00 2001 From: kmin-jeong Date: Wed, 30 Nov 2022 18:08:58 +0900 Subject: [PATCH 07/14] fix:fix lint error lastone --- packages/eslint-plugin/src/rules/ban-types.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/ban-types.ts b/packages/eslint-plugin/src/rules/ban-types.ts index 8e55fe3bea0..aec37e6e877 100644 --- a/packages/eslint-plugin/src/rules/ban-types.ts +++ b/packages/eslint-plugin/src/rules/ban-types.ts @@ -165,8 +165,10 @@ export default util.createRule({ typeNode: TSESTree.Node, name = stringifyNode(typeNode, context.getSourceCode()), ): void { - type total = { fixWith?: string; message: string } | false; - const bannedType: total = bannedTypes.get(name); + const bannedType = bannedTypes.get(name) as + | string + | false + | { fixWith?: string }; if (bannedType === undefined || bannedType === false) { return; From 8db5ef0c6816bf6982e363d8bcd13118a4908b44 Mon Sep 17 00:00:00 2001 From: kmin-jeong Date: Wed, 30 Nov 2022 18:18:27 +0900 Subject: [PATCH 08/14] fix: add total types and delete duplication things --- packages/eslint-plugin/src/rules/ban-types.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/eslint-plugin/src/rules/ban-types.ts b/packages/eslint-plugin/src/rules/ban-types.ts index aec37e6e877..db78fc1af98 100644 --- a/packages/eslint-plugin/src/rules/ban-types.ts +++ b/packages/eslint-plugin/src/rules/ban-types.ts @@ -3,8 +3,7 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils'; import * as util from '../util'; -type Index = string | number | boolean | undefined | null; -type Types = Index | object | { fixWith: string; message: string }; +type Types = string | number | boolean | undefined | null | object; export type Options = [ { @@ -165,10 +164,8 @@ export default util.createRule({ typeNode: TSESTree.Node, name = stringifyNode(typeNode, context.getSourceCode()), ): void { - const bannedType = bannedTypes.get(name) as - | string - | false - | { fixWith?: string }; + type total = string | false | { fixWith?: string; message: string }; + const bannedType = bannedTypes.get(name) as total; if (bannedType === undefined || bannedType === false) { return; From da8a46eb2179a7fb2a641520f7129e3907730d71 Mon Sep 17 00:00:00 2001 From: kmin-jeong Date: Wed, 30 Nov 2022 18:28:47 +0900 Subject: [PATCH 09/14] fix: fix type total to checTypes --- packages/eslint-plugin/src/rules/ban-types.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/ban-types.ts b/packages/eslint-plugin/src/rules/ban-types.ts index db78fc1af98..efa13566fcd 100644 --- a/packages/eslint-plugin/src/rules/ban-types.ts +++ b/packages/eslint-plugin/src/rules/ban-types.ts @@ -164,8 +164,8 @@ export default util.createRule({ typeNode: TSESTree.Node, name = stringifyNode(typeNode, context.getSourceCode()), ): void { - type total = string | false | { fixWith?: string; message: string }; - const bannedType = bannedTypes.get(name) as total; + type checkTypes = string | false | { fixWith?: string; message: string }; + const bannedType = bannedTypes.get(name) as checkTypes; if (bannedType === undefined || bannedType === false) { return; From 8aad9def14b603629f47078c3bb24363a527d998 Mon Sep 17 00:00:00 2001 From: kmin-jeong Date: Wed, 30 Nov 2022 18:47:43 +0900 Subject: [PATCH 10/14] fix: fix variable names --- packages/eslint-plugin/src/rules/ban-types.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/ban-types.ts b/packages/eslint-plugin/src/rules/ban-types.ts index efa13566fcd..6730a8c9a5c 100644 --- a/packages/eslint-plugin/src/rules/ban-types.ts +++ b/packages/eslint-plugin/src/rules/ban-types.ts @@ -164,8 +164,11 @@ export default util.createRule({ typeNode: TSESTree.Node, name = stringifyNode(typeNode, context.getSourceCode()), ): void { - type checkTypes = string | false | { fixWith?: string; message: string }; - const bannedType = bannedTypes.get(name) as checkTypes; + type checkrequiredTypes = + | string + | false + | { fixWith?: string; message: string }; + const bannedType = bannedTypes.get(name) as checkrequiredTypes; if (bannedType === undefined || bannedType === false) { return; From 5890f893d03c661aca3230d807ef35e533576c94 Mon Sep 17 00:00:00 2001 From: kmin-jeong Date: Wed, 30 Nov 2022 19:08:33 +0900 Subject: [PATCH 11/14] fix: fix types elements --- packages/eslint-plugin/src/rules/ban-types.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/eslint-plugin/src/rules/ban-types.ts b/packages/eslint-plugin/src/rules/ban-types.ts index 6730a8c9a5c..f94808a50a5 100644 --- a/packages/eslint-plugin/src/rules/ban-types.ts +++ b/packages/eslint-plugin/src/rules/ban-types.ts @@ -3,7 +3,16 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils'; import * as util from '../util'; -type Types = string | number | boolean | undefined | null | object; +type Types = Record< + string, + | null + | false + | string + | { + message: string; + fixWith?: string; + } +>; export type Options = [ { @@ -164,11 +173,7 @@ export default util.createRule({ typeNode: TSESTree.Node, name = stringifyNode(typeNode, context.getSourceCode()), ): void { - type checkrequiredTypes = - | string - | false - | { fixWith?: string; message: string }; - const bannedType = bannedTypes.get(name) as checkrequiredTypes; + const bannedType = bannedTypes.get(name); if (bannedType === undefined || bannedType === false) { return; From 9e71fcf06823019ceca670d2e77df05eab562251 Mon Sep 17 00:00:00 2001 From: kmin-jeong Date: Wed, 30 Nov 2022 21:00:55 +0900 Subject: [PATCH 12/14] fix:fix type Types --- packages/eslint-plugin/src/rules/ban-types.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/eslint-plugin/src/rules/ban-types.ts b/packages/eslint-plugin/src/rules/ban-types.ts index f94808a50a5..6d6a62507b9 100644 --- a/packages/eslint-plugin/src/rules/ban-types.ts +++ b/packages/eslint-plugin/src/rules/ban-types.ts @@ -3,16 +3,14 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils'; import * as util from '../util'; -type Types = Record< - string, - | null - | false - | string +type Index = string | number | boolean | undefined | null; +type Types = + | object | { + [key: string]: Index; message: string; fixWith?: string; - } ->; + }; export type Options = [ { @@ -173,7 +171,11 @@ export default util.createRule({ typeNode: TSESTree.Node, name = stringifyNode(typeNode, context.getSourceCode()), ): void { - const bannedType = bannedTypes.get(name); + type checkrequiredTypes = + | string + | false + | { fixWith?: string; message: string }; + const bannedType = bannedTypes.get(name) as checkrequiredTypes; if (bannedType === undefined || bannedType === false) { return; From e2f6ff00ad6c112da62e94ddfe614a75bdb1396e Mon Sep 17 00:00:00 2001 From: kmin-jeong Date: Fri, 2 Dec 2022 13:29:55 +0900 Subject: [PATCH 13/14] fix: fix messages in object --- packages/eslint-plugin/src/rules/ban-types.ts | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/packages/eslint-plugin/src/rules/ban-types.ts b/packages/eslint-plugin/src/rules/ban-types.ts index 6d6a62507b9..dce41140515 100644 --- a/packages/eslint-plugin/src/rules/ban-types.ts +++ b/packages/eslint-plugin/src/rules/ban-types.ts @@ -3,14 +3,16 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils'; import * as util from '../util'; -type Index = string | number | boolean | undefined | null; -type Types = - | object +type Types = Record< + string, + | null + | false + | string | { - [key: string]: Index; message: string; fixWith?: string; - }; + } +>; export type Options = [ { @@ -84,14 +86,14 @@ const defaultTypes: Types = { Object: { message: [ 'The `Object` type actually means "any non-nullish value", so it is marginally better than `unknown`.', - '- If you want a type meaning "any object", you probably want `Record` instead.', + '- If you want a type meaning "any object", you probably want `object` instead.', '- If you want a type meaning "any value", you probably want `unknown` instead.', ].join('\n'), }, '{}': { message: [ '`{}` actually means "any non-nullish value".', - '- If you want a type meaning "any object", you probably want `Record` instead.', + '- If you want a type meaning "any object", you probably want `object` instead.', '- If you want a type meaning "any value", you probably want `unknown` instead.', '- If you want a type meaning "empty object", you probably want `Record` instead.', ].join('\n'), @@ -171,11 +173,7 @@ export default util.createRule({ typeNode: TSESTree.Node, name = stringifyNode(typeNode, context.getSourceCode()), ): void { - type checkrequiredTypes = - | string - | false - | { fixWith?: string; message: string }; - const bannedType = bannedTypes.get(name) as checkrequiredTypes; + const bannedType = bannedTypes.get(name); if (bannedType === undefined || bannedType === false) { return; From 6a22bef95d6a3b3a17e3143320a0586dd20321c0 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Fri, 2 Dec 2022 10:17:44 -0500 Subject: [PATCH 14/14] Update packages/eslint-plugin/tests/rules/ban-types.test.ts --- .../tests/rules/ban-types.test.ts | 33 ------------------- 1 file changed, 33 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/ban-types.test.ts b/packages/eslint-plugin/tests/rules/ban-types.test.ts index 8d31fe7ac8c..7883f0f45df 100644 --- a/packages/eslint-plugin/tests/rules/ban-types.test.ts +++ b/packages/eslint-plugin/tests/rules/ban-types.test.ts @@ -388,39 +388,6 @@ let b: Foo; }, ], }, - - { - code: noFormat` -declare let j: unknown; -let m3 = { ...(j as Record) }; - `, - output: ` -declare let j: unknown; -let m3 = { ...(j as object) }; - `, - options: [ - { - types: { - 'Record': { - message: 'Use object instead.', - fixWith: 'object', - }, - }, - }, - ], - errors: [ - { - messageId: 'bannedTypeMessage', - data: { - name: 'Record', - customMessage: ' Use object instead.', - }, - line: 3, - column: 21, - }, - ], - }, - { code: noFormat` let foo: {} = {};