Skip to content

Commit ae7f7c5

Browse files
fix(eslint-plugin): [ban-types] add option extendDefaults (typescript-eslint#1379)
Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
1 parent 3534c6e commit ae7f7c5

File tree

3 files changed

+84
-28
lines changed

3 files changed

+84
-28
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@ The banned type can either be a type name literal (`Foo`), a type name with gene
5858
}
5959
```
6060

61+
By default, this rule includes types which are likely to be mistakes, such as `String` and `Number`. If you don't want these enabled, set the `extendDefaults` option to `false`:
62+
63+
```CJSON
64+
{
65+
"@typescript-eslint/ban-types": ["error", {
66+
"types": {
67+
// add a custom message, AND tell the plugin how to fix it
68+
"String": {
69+
"message": "Use string instead",
70+
"fixWith": "string"
71+
}
72+
},
73+
"extendDefaults": false
74+
}]
75+
}
76+
```
77+
6178
### Example
6279

6380
```json

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

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ type Types = Record<
1313

1414
type Options = [
1515
{
16-
types: Types;
16+
types?: Types;
17+
extendDefaults?: boolean;
1718
},
1819
];
1920
type MessageIds = 'bannedTypeMessage';
@@ -51,6 +52,35 @@ function getCustomMessage(
5152
return '';
5253
}
5354

55+
/*
56+
Defaults for this rule should be treated as an "all or nothing"
57+
merge, so we need special handling here.
58+
59+
See: https://github.com/typescript-eslint/typescript-eslint/issues/686
60+
*/
61+
const defaultTypes = {
62+
String: {
63+
message: 'Use string instead',
64+
fixWith: 'string',
65+
},
66+
Boolean: {
67+
message: 'Use boolean instead',
68+
fixWith: 'boolean',
69+
},
70+
Number: {
71+
message: 'Use number instead',
72+
fixWith: 'number',
73+
},
74+
Object: {
75+
message: 'Use Record<string, any> instead',
76+
fixWith: 'Record<string, any>',
77+
},
78+
Symbol: {
79+
message: 'Use symbol instead',
80+
fixWith: 'symbol',
81+
},
82+
};
83+
5484
export default util.createRule<Options, MessageIds>({
5585
name: 'ban-types',
5686
meta: {
@@ -85,38 +115,22 @@ export default util.createRule<Options, MessageIds>({
85115
],
86116
},
87117
},
118+
extendDefaults: {
119+
type: 'boolean',
120+
},
88121
},
89122
additionalProperties: false,
90123
},
91124
],
92125
},
93-
defaultOptions: [
94-
{
95-
types: {
96-
String: {
97-
message: 'Use string instead',
98-
fixWith: 'string',
99-
},
100-
Boolean: {
101-
message: 'Use boolean instead',
102-
fixWith: 'boolean',
103-
},
104-
Number: {
105-
message: 'Use number instead',
106-
fixWith: 'number',
107-
},
108-
Object: {
109-
message: 'Use Record<string, any> instead',
110-
fixWith: 'Record<string, any>',
111-
},
112-
Symbol: {
113-
message: 'Use symbol instead',
114-
fixWith: 'symbol',
115-
},
116-
},
117-
},
118-
],
119-
create(context, [{ types }]) {
126+
defaultOptions: [{}],
127+
create(context, [options]) {
128+
const extendDefaults = options.extendDefaults ?? true;
129+
const customTypes = options.types ?? {};
130+
const types: Types = {
131+
...(extendDefaults ? defaultTypes : {}),
132+
...customTypes,
133+
};
120134
const bannedTypes = new Map(
121135
Object.entries(types).map(([type, data]) => [removeSpaces(type), data]),
122136
);

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ ruleTester.run('ban-types', rule, {
7272
code: 'let a: NS.Bad._',
7373
options,
7474
},
75+
// Replace default options instead of merging with extendDefaults: false
76+
{
77+
code: 'let a: String;',
78+
options: [
79+
{
80+
types: {
81+
Number: {
82+
message: 'Use number instead.',
83+
fixWith: 'number',
84+
},
85+
},
86+
extendDefaults: false,
87+
},
88+
],
89+
},
7590
{
7691
code: 'let a: undefined',
7792
options: options2,
@@ -82,6 +97,16 @@ ruleTester.run('ban-types', rule, {
8297
},
8398
],
8499
invalid: [
100+
{
101+
code: 'let a: String;',
102+
errors: [
103+
{
104+
messageId: 'bannedTypeMessage',
105+
line: 1,
106+
column: 8,
107+
},
108+
],
109+
},
85110
{
86111
code: 'let a: Object;',
87112
errors: [

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy