8000 feat(eslint-plugin): [ban-types] allow selective disable of default o… · ascendancyy/typescript-eslint@1cb8ca4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1cb8ca4

Browse files
authored
feat(eslint-plugin): [ban-types] allow selective disable of default options with false value (typescript-eslint#2137)
1 parent df95338 commit 1cb8ca4

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Note that it does not ban the corresponding runtime objects from being used.
1414
type Options = {
1515
types?: {
1616
[typeName: string]:
17+
| false
1718
| string
1819
| {
1920
message: string;
@@ -28,7 +29,7 @@ The rule accepts a single object as options, with the following keys:
2829

2930
- `types` - An object whose keys are the types you want to ban, and the values are error messages.
3031
- 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 values can be a string, which is the error message to be reported,
32+
- The values can be a string, which is the error message to be reported, `false` to specifically disable this type
3233
or it can be an object with the following properties:
3334
- `message: string` - the message to display when the type is matched.
3435
- `fixWith?: string` - a string to replace the banned type with when the fixer is run. If this is omitted, no fix will be done.

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import * as util from '../util';
77

88
type Types = Record<
99
string,
10-
| string
1110
| null
11+
| false
12+
| string
1213
| {
1314
message: string;
1415
fixWith?: string;
@@ -138,6 +139,7 @@ export default util.createRule<Options, MessageIds>({
138139
additionalProperties: {
139140
oneOf: [
140141
{ type: 'null' },
142+
{ type: 'boolean' },
141143
{ type: 'string' },
142144
{
143145
type: 'object',
@@ -177,23 +179,25 @@ export default util.createRule<Options, MessageIds>({
177179
): void {
178180
const bannedType = bannedTypes.get(name);
179181

180-
if (bannedType !== undefined) {
181-
const customMessage = getCustomMessage(bannedType);
182-
const fixWith =
183-
bannedType && typeof bannedType === 'object' && bannedType.fixWith;
184-
185-
context.report({
186-
node: typeNode,
187-
messageId: 'bannedTypeMessage',
188-
data: {
189-
name,
190-
customMessage,
191-
},
192-
fix: fixWith
193-
? (fixer): TSESLint.RuleFix => fixer.replaceText(typeNode, fixWith)
194-
: null,
195-
});
182+
if (bannedType === undefined || bannedType === false) {
183+
return;
196184
}
185+
186+
const customMessage = getCustomMessage(bannedType);
187+
const fixWith =
188+
bannedType && typeof bannedType === 'object' && bannedType.fixWith;
189+
190+
context.report({
191+
node: typeNode,
192+
messageId: 'bannedTypeMessage',
193+
data: {
194+
name,
195+
customMessage,
196+
},
197+
fix: fixWith
198+
? (fixer): TSESLint.RuleFix => fixer.replaceText(typeNode, fixWith)
199+
: null,
200+
});
197201
}
198202

199203
const keywordSelectors = util.objectReduceKey(

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ ruleTester.run('ban-types', rule, {
9797
},
9898
],
9999
},
100+
{
101+
code: 'type Props = {};',
102+
options: [
103+
{
104+
types: {
105+
'{}': false,
106+
},
107+
extendDefaults: true,
108+
},
109+
],
110+
},
100111
],
101112
invalid: [
102113
{

0 commit comments

Comments
 (0)
0