8000 fix(eslint-plugin): [ban-types] add option extendDefaults by threehams · Pull Request #1379 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content

fix(eslint-plugin): [ban-types] add option extendDefaults #1379

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

Merged
merged 4 commits into from
Feb 28, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/eslint-plugin/docs/rules/ban-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ The banned type can either be a type name literal (`Foo`), a type name with gene
}
```

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`:

```CJSON
{
"@typescript-eslint/ban-types": ["error", {
"types": {
// add a custom message, AND tell the plugin how to fix it
"String": {
"message": "Use string instead",
"fixWith": "string"
}
},
"extendDefaults": false
}]
}
```

### Example

```json
Expand Down
70 changes: 42 additions & 28 deletions packages/eslint-plugin/src/rules/ban-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ type Types = Record<

type Options = [
{
types: Types;
types?: Types;
extendDefaults?: boolean;
},
];
type MessageIds = 'bannedTypeMessage';
Expand Down Expand Up @@ -51,6 +52,35 @@ function getCustomMessage(
return '';
10000 }

/*
Defaults for this rule should be treated as an "all or nothing"
merge, so we need special handling here.

See: https://github.com/typescript-eslint/typescript-eslint/issues/686
*/
const defaultTypes = {
String: {
message: 'Use string instead',
fixWith: 'string',
},
Boolean: {
message: 'Use boolean instead',
fixWith: 'boolean',
},
Number: {
message: 'Use number instead',
fixWith: 'number',
},
Object: {
message: 'Use Record<string, any> instead',
fixWith: 'Record<string, any>',
},
Symbol: {
message: 'Use symbol instead',
fixWith: 'symbol',
},
};

export default util.createRule<Options, MessageIds>({
name: 'ban-types',
meta: {
Expand Down Expand Up @@ -85,38 +115,22 @@ export default util.createRule<Options, MessageIds>({
],
},
},
extendDefaults: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
},
defaultOptions: [
{
types: {
String: {
message: 'Use string instead',
fixWith: 'string',
},
Boolean: {
message: 'Use boolean instead',
fixWith: 'boolean',
},
Number: {
message: 'Use number instead',
fixWith: 'number',
},
Object: {
message: 'Use Record<string, any> instead',
fixWith: 'Record<string, any>',
},
Symbol: {
message: 'Use symbol instead',
fixWith: 'symbol',
},
},
},
],
create(context, [{ types }]) {
defaultOptions: [{}],
create(context, [options]) {
const extendDefaults = options.extendDefaults ?? true;
const customTypes = options.types ?? {};
const types: Types = {
...(extendDefaults ? defaultTypes : {}),
...customTypes,
};
const bannedTypes = new Map(
Object.entries(types).map(([type, data]) => [removeSpaces(type), data]),
);
Expand Down
25 changes: 25 additions & 0 deletions packages/eslint-plugin/tests/rules/ban-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ ruleTester.run('ban-types', rule, {
code: 'let a: NS.Bad._',
options,
},
// Replace default options instead of merging with extendDefaults: false
{
code: 'let a: String;',
options: [
{
types: {
Number: {
message: 'Use number instead.',
fixWith: 'number',
},
},
extendDefaults: false,
},
],
},
{
code: 'let a: undefined',
options: options2,
Expand All @@ -82,6 +97,16 @@ ruleTester.run('ban-types', rule, {
},
],
invalid: [
{
code: 'let a: String;',
errors: [
{
messageId: 'bannedTypeMessage',
line: 1,
column: 8,
},
],
},
{
code: 'let a: Object;',
errors: [
Expand Down
0