diff --git a/src/configs/all.ts b/src/configs/all.ts index 822a2f9e4ae..33c5fd18c24 100644 --- a/src/configs/all.ts +++ b/src/configs/all.ts @@ -243,6 +243,7 @@ export const rules = { ], "return-undefined": true, "semicolon": [true, "always"], + "space-after-branch": true, "space-before-function-paren": [true, { "anonymous": "never", "asyncArrow": "always", diff --git a/src/rules/spaceAfterBranchRule.ts b/src/rules/spaceAfterBranchRule.ts new file mode 100644 index 00000000000..b108f1cb979 --- /dev/null +++ b/src/rules/spaceAfterBranchRule.ts @@ -0,0 +1,118 @@ +/** + * @license + * Copyright 2018 Palantir Technologies, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as utils from "tsutils"; +import * as ts from "typescript"; +import * as Lint from "../index"; + +type Option = "never"; + +/* tslint:disable:object-literal-sort-keys */ +export class Rule extends Lint.Rules.AbstractRule { + public static metadata: Lint.IRuleMetadata = { + ruleName: "space-after-branch", + description: + "Requires or restricts whitespace after branching keywords (`for`, `if`, `switch`, ...)", + optionsDescription: Lint.Utils.dedent` + By default one whitespace is allowed after branching keywords. + Optional argument \`'never'\` checks that branching keywords are not followed by whitespace. + `, + options: { + type: "array", + items: { + type: "string", + enum: ["never"] + }, + maxLength: 1 + }, + optionExamples: [[true, "never"]], + type: "style", + typescriptOnly: false, + hasFix: true + }; + /* tslint:enable:object-literal-sort-keys */ + + public static FAILURE_INVALID_SPACE = "invalid whitespace after keyword"; + public static FAILURE_MISSING_SPACE = "missing whitespace after keyword"; + + public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { + const options = this.ruleArguments[0]; + + return this.applyWithWalker(new SpaceAfterBranchWalker(sourceFile, this.ruleName, options)); + } +} + +class SpaceAfterBranchWalker extends Lint.AbstractWalker