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

Commit

Permalink
[unnecessary-else] Moved from old-style options to new one (object-ba…
Browse files Browse the repository at this point in the history
…sed)
  • Loading branch information
timocov committed Mar 24, 2019
1 parent 16a2e55 commit 7696d0d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
25 changes: 13 additions & 12 deletions src/rules/unnecessaryElseRule.ts
Expand Up @@ -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,
Expand All @@ -57,16 +53,21 @@ 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]));
}
}

interface Options {
allowElseIf: boolean;
}

function parseOptions(option: Partial<Options> | undefined): Options {
return {
allowElseIf: false,
...option,
};
}

function walk(ctx: Lint.WalkContext<Options>): void {
ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void {
if (utils.isIfStatement(node)) {
Expand Down
2 changes: 1 addition & 1 deletion 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 }]
}
}

0 comments on commit 7696d0d

Please sign in to comment.