Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
feat(new-rule-option): improve strict-boolean-expressions by adding…
Browse files Browse the repository at this point in the history
… `allow-enum` rule option to disable warnings in case of using enum in boolean expression (#3604)

closes #3571
  • Loading branch information
atsu85 authored and giladgray committed Jul 28, 2018
1 parent 629126f commit 0a676b7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/rules/strictBooleanExpressionsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as Lint from "../index";
const OPTION_ALLOW_NULL_UNION = "allow-null-union";
const OPTION_ALLOW_UNDEFINED_UNION = "allow-undefined-union";
const OPTION_ALLOW_STRING = "allow-string";
const OPTION_ALLOW_ENUM = "allow-enum";
const OPTION_ALLOW_NUMBER = "allow-number";
const OPTION_ALLOW_MIX = "allow-mix";
const OPTION_ALLOW_BOOLEAN_OR_UNDEFINED = "allow-boolean-or-undefined";
Expand Down Expand Up @@ -51,6 +52,8 @@ export class Rule extends Lint.Rules.TypedRule {
* \`${OPTION_ALLOW_STRING}\` allows strings.
- It does *not* allow unions containing \`string\`.
- It does *not* allow string literal types.
* \`${OPTION_ALLOW_ENUM}\` allows enums.
- It does *not* allow unions containing \`enum\`.
* \`${OPTION_ALLOW_NUMBER}\` allows numbers.
- It does *not* allow unions containing \`number\`.
- It does *not* allow enums or number literal types.
Expand All @@ -70,6 +73,7 @@ export class Rule extends Lint.Rules.TypedRule {
OPTION_ALLOW_NULL_UNION,
OPTION_ALLOW_UNDEFINED_UNION,
OPTION_ALLOW_STRING,
OPTION_ALLOW_ENUM,
OPTION_ALLOW_NUMBER,
OPTION_ALLOW_BOOLEAN_OR_UNDEFINED,
],
Expand All @@ -79,7 +83,7 @@ export class Rule extends Lint.Rules.TypedRule {
},
optionExamples: [
true,
[true, OPTION_ALLOW_NULL_UNION, OPTION_ALLOW_UNDEFINED_UNION, OPTION_ALLOW_STRING, OPTION_ALLOW_NUMBER],
[true, OPTION_ALLOW_NULL_UNION, OPTION_ALLOW_UNDEFINED_UNION, OPTION_ALLOW_STRING, OPTION_ALLOW_ENUM, OPTION_ALLOW_NUMBER],
[true, OPTION_ALLOW_BOOLEAN_OR_UNDEFINED],
],
type: "functionality",
Expand All @@ -98,6 +102,7 @@ interface Options {
allowNullUnion: boolean;
allowUndefinedUnion: boolean;
allowString: boolean;
allowEnum: boolean;
allowNumber: boolean;
allowMix: boolean;
allowBooleanOrUndefined: boolean;
Expand All @@ -109,6 +114,7 @@ function parseOptions(ruleArguments: string[], strictNullChecks: boolean): Optio
allowNullUnion: has(OPTION_ALLOW_NULL_UNION),
allowUndefinedUnion: has(OPTION_ALLOW_UNDEFINED_UNION),
allowString: has(OPTION_ALLOW_STRING),
allowEnum: has(OPTION_ALLOW_ENUM),
allowNumber: has(OPTION_ALLOW_NUMBER),
allowMix: has(OPTION_ALLOW_MIX),
allowBooleanOrUndefined: has(OPTION_ALLOW_BOOLEAN_OR_UNDEFINED),
Expand Down Expand Up @@ -278,7 +284,7 @@ function failureForKind(kind: TypeKind, isInUnion: boolean, options: Options): T
case TypeKind.FalseNumberLiteral:
return options.allowNumber ? undefined : TypeFailure.Number;
case TypeKind.Enum:
return TypeFailure.Enum;
return options.allowEnum ? undefined : TypeFailure.Enum;
case TypeKind.Null:
return isInUnion && !options.allowNullUnion ? TypeFailure.Null : undefined;
case TypeKind.Undefined:
Expand Down Expand Up @@ -439,6 +445,7 @@ function showExpectedTypes(options: Options): string[] {
if (options.allowNullUnion) { parts.push("null-union"); }
if (options.allowUndefinedUnion) { parts.push("undefined-union"); }
if (options.allowString) { parts.push("string"); }
if (options.allowEnum) { parts.push("enum"); }
if (options.allowNumber) { parts.push("number"); }
if (options.allowBooleanOrUndefined) { parts.push("boolean-or-undefined"); }
return parts;
Expand Down
6 changes: 6 additions & 0 deletions test/rules/strict-boolean-expressions/allow-enum/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare function get<T>(): T;

if (get<boolean>()) {}

enum E {}
if (get<E>()) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"strictNullChecks": true
}
}
5 changes: 5 additions & 0 deletions test/rules/strict-boolean-expressions/allow-enum/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"strict-boolean-expressions": [true, "allow-enum"]
}
}

0 comments on commit 0a676b7

Please sign in to comment.