diff --git a/src/rules/unnecessaryElseRule.ts b/src/rules/unnecessaryElseRule.ts index 26b9180f9dd..afd2379a0d1 100644 --- a/src/rules/unnecessaryElseRule.ts +++ b/src/rules/unnecessaryElseRule.ts @@ -21,25 +21,21 @@ import * as Lint from "../index"; import { codeExamples } from "./code-examples/unnecessaryElse.examples"; -const OPTION_ALLOW_ELSE_IF = "allow-else-if"; - export class Rule extends Lint.Rules.AbstractRule { /* tslint:disable:object-literal-sort-keys */ public static metadata: Lint.IRuleMetadata = { description: Lint.Utils.dedent` Disallows \`else\` blocks following \`if\` blocks ending with a \`break\`, \`continue\`, \`return\`, or \`throw\` statement.`, descriptionDetails: "", - optionExamples: [true, [true, OPTION_ALLOW_ELSE_IF]], + optionExamples: [true, [true, { allowElseIf: true }]], options: { - type: "array", - items: { - type: "string", - enum: [OPTION_ALLOW_ELSE_IF], + type: "object", + properties: { + allowElseIf: { type: "boolean" }, }, - uniqueItems: true, }, optionsDescription: Lint.Utils.dedent` - You can optionally specify the option \`"${OPTION_ALLOW_ELSE_IF}"\` to allow "else if" statements. + You can optionally specify the option \`"allowElseIf"\` to allow "else if" statements. `, rationale: Lint.Utils.dedent` When an \`if\` block is guaranteed to exit control flow when entered, @@ -57,9 +53,7 @@ export class Rule extends Lint.Rules.AbstractRule { } public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { - return this.applyWithFunction(sourceFile, walk, { - allowElseIf: this.ruleArguments.indexOf(OPTION_ALLOW_ELSE_IF) !== -1, - }); + return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments[0])); } } @@ -67,6 +61,13 @@ interface Options { allowElseIf: boolean; } +function parseOptions(option: Partial | undefined): Options { + return { + allowElseIf: false, + ...option, + }; +} + function walk(ctx: Lint.WalkContext): void { ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void { if (utils.isIfStatement(node)) { diff --git a/test/rules/unnecessary-else/allow-else-if/tslint.json b/test/rules/unnecessary-else/allow-else-if/tslint.json index 47a8b4f9485..e8a78e5df1b 100644 --- a/test/rules/unnecessary-else/allow-else-if/tslint.json +++ b/test/rules/unnecessary-else/allow-else-if/tslint.json @@ -1,5 +1,5 @@ { "rules": { - "unnecessary-else": [true, "allow-else-if"] + "unnecessary-else": [true, { "allowElseIf": true }] } }