Skip to content
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

feat(eslint-plugin): [ban-types] allow selective disable of default options with false value #2137

Merged
merged 1 commit into from May 31, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/eslint-plugin/docs/rules/ban-types.md
Expand Up @@ -14,6 +14,7 @@ Note that it does not ban the corresponding runtime objects from being used.
type Options = {
types?: {
[typeName: string]:
| false
| string
| {
message: string;
Expand All @@ -28,7 +29,7 @@ The rule accepts a single object as options, with the following keys:

- `types` - An object whose keys are the types you want to ban, and the values are error messages.
- 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 (`{}`).
- The values can be a string, which is the error message to be reported,
- The values can be a string, which is the error message to be reported, `false` to specifically disable this type
or it can be an object with the following properties:
- `message: string` - the message to display when the type is matched.
- `fixWith?: string` - a string to replace the banned type with when the fixer is run. If this is omitted, no fix will be done.
Expand Down
38 changes: 21 additions & 17 deletions packages/eslint-plugin/src/rules/ban-types.ts
Expand Up @@ -7,8 +7,9 @@ import * as util from '../util';

type Types = Record<
string,
| string
| null
| false
| string
| {
message: string;
fixWith?: string;
Expand Down Expand Up @@ -138,6 +139,7 @@ export default util.createRule<Options, MessageIds>({
additionalProperties: {
oneOf: [
{ type: 'null' },
{ type: 'boolean' },
{ type: 'string' },
{
type: 'object',
Expand Down Expand Up @@ -177,23 +179,25 @@ export default util.createRule<Options, MessageIds>({
): void {
const bannedType = bannedTypes.get(name);

if (bannedType !== undefined) {
const customMessage = getCustomMessage(bannedType);
const fixWith =
bannedType && typeof bannedType === 'object' && bannedType.fixWith;

context.report({
node: typeNode,
messageId: 'bannedTypeMessage',
data: {
name,
customMessage,
},
fix: fixWith
? (fixer): TSESLint.RuleFix => fixer.replaceText(typeNode, fixWith)
: null,
});
if (bannedType === undefined || bannedType === false) {
return;
}

const customMessage = getCustomMessage(bannedType);
const fixWith =
bannedType && typeof bannedType === 'object' && bannedType.fixWith;

context.report({
node: typeNode,
messageId: 'bannedTypeMessage',
data: {
name,
customMessage,
},
fix: fixWith
? (fixer): TSESLint.RuleFix => fixer.replaceText(typeNode, fixWith)
: null,
});
}

const keywordSelectors = util.objectReduceKey(
Expand Down
11 changes: 11 additions & 0 deletions packages/eslint-plugin/tests/rules/ban-types.test.ts
Expand Up @@ -97,6 +97,17 @@ ruleTester.run('ban-types', rule, {
},
],
},
{
code: 'type Props = {};',
options: [
{
types: {
'{}': false,
},
extendDefaults: true,
},
],
},
],
invalid: [
{
Expand Down