Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
bradzacher merged 4 commits intotypescript-eslint:masterfromthreehams:master
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionspackages/eslint-plugin/docs/rules/ban-types.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/eslint-plugin/src/rules/ban-types.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,7 +13,8 @@ type Types = Record<

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

/*
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 DownExpand 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 deletionspackages/eslint-plugin/tests/rules/ban-types.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp