From 0edff45239c8cc0bf950220388eb59f381301a74 Mon Sep 17 00:00:00 2001 From: Santi Albo Date: Fri, 11 Aug 2017 17:29:10 +0200 Subject: [PATCH] Add "check-type-operator" option for whitespace rule (#3083) --- src/rules/whitespaceRule.ts | 35 +++++++++++++++++++++----- test/rules/whitespace/all/test.ts.fix | 6 +++++ test/rules/whitespace/all/test.ts.lint | 16 ++++++++++++ test/rules/whitespace/all/tslint.json | 3 ++- 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/rules/whitespaceRule.ts b/src/rules/whitespaceRule.ts index a78f8ea316c..3eb6f30caaa 100644 --- a/src/rules/whitespaceRule.ts +++ b/src/rules/whitespaceRule.ts @@ -30,6 +30,7 @@ const OPTION_SEPARATOR = "check-separator"; const OPTION_REST_SPREAD = "check-rest-spread"; const OPTION_TYPE = "check-type"; const OPTION_TYPECAST = "check-typecast"; +const OPTION_TYPE_OPERATOR = "check-type-operator"; const OPTION_PREBLOCK = "check-preblock"; export class Rule extends Lint.Rules.AbstractRule { @@ -48,16 +49,19 @@ export class Rule extends Lint.Rules.AbstractRule { * \`"check-rest-spread"\` checks that there is no whitespace after rest/spread operator (\`...\`). * \`"check-type"\` checks for whitespace before a variable type specification. * \`"check-typecast"\` checks for whitespace between a typecast and its target. + * \`"check-type-operator"\` checks for whitespace between type operators \`|\` and \`&\`. * \`"check-preblock"\` checks for whitespace before the opening brace of a block`, options: { type: "array", items: { type: "string", - enum: ["check-branch", "check-decl", "check-operator", "check-module", - "check-separator", "check-rest-spread", "check-type", "check-typecast", "check-preblock"], + enum: [ + "check-branch", "check-decl", "check-operator", "check-module", "check-separator", + "check-rest-spread", "check-type", "check-typecast", "check-type-operator", "check-preblock", + ], }, minLength: 0, - maxLength: 9, + maxLength: 10, }, optionExamples: [[true, "check-branch", "check-operator", "check-typecast"]], type: "style", @@ -72,7 +76,10 @@ export class Rule extends Lint.Rules.AbstractRule { } } -type Options = Record<"branch" | "decl" | "operator" | "module" | "separator" | "restSpread" | "type" | "typecast" | "preblock", boolean>; +type Options = Record< + "branch" | "decl" | "operator" | "module" | "separator" | "restSpread" | "type" | "typecast" | "typeOperator" | "preblock", + boolean>; + function parseOptions(ruleArguments: string[]): Options { return { branch: has(OPTION_BRANCH), @@ -83,6 +90,7 @@ function parseOptions(ruleArguments: string[]): Options { restSpread: has(OPTION_REST_SPREAD), type: has(OPTION_TYPE), typecast: has(OPTION_TYPECAST), + typeOperator: has(OPTION_TYPE_OPERATOR), preblock: has(OPTION_PREBLOCK), }; @@ -194,7 +202,7 @@ function walk(ctx: Lint.WalkContext) { case ts.SyntaxKind.VariableDeclaration: const { name, type, initializer } = node as ts.VariableDeclaration; if (options.decl && initializer !== undefined) { - checkForTrailingWhitespace((type !== undefined ? type : name).getEnd()); + checkForTrailingWhitespace((type !== undefined ? type : name).getEnd()); } break; @@ -212,6 +220,21 @@ function walk(ctx: Lint.WalkContext) { const position = (node as ts.SpreadAssignment).expression.getFullStart(); checkForExcessiveWhitespace(position); } + break; + + case ts.SyntaxKind.UnionType: + case ts.SyntaxKind.IntersectionType: + if (options.typeOperator) { + const { types } = node as ts.UnionOrIntersectionTypeNode; + types.forEach((typeNode, index) => { + if (index > 0) { + checkForTrailingWhitespace(typeNode.getFullStart()); + } + if (index < types.length - 1) { + checkForTrailingWhitespace(typeNode.getEnd()); + } + }); + } } ts.forEachChild(node, cb); @@ -262,7 +285,7 @@ function walk(ctx: Lint.WalkContext) { (parent as ts.CallExpression).expression.kind === ts.SyntaxKind.ImportKeyword) { return; // Don't check ImportCall } - // falls through + // falls through case ts.SyntaxKind.ExportKeyword: case ts.SyntaxKind.FromKeyword: if (options.typecast) { diff --git a/test/rules/whitespace/all/test.ts.fix b/test/rules/whitespace/all/test.ts.fix index e09c6814763..2c3ae4daa76 100644 --- a/test/rules/whitespace/all/test.ts.fix +++ b/test/rules/whitespace/all/test.ts.fix @@ -112,3 +112,9 @@ const foo = [ ...bar ]; function foo (bar, ...baz) {} const { foo, ...bar } = baz; + +type A = number | string; + +type B = number | string | {result: string}; + +type C = {result: string} & {type: "A" | "B"}; diff --git a/test/rules/whitespace/all/test.ts.lint b/test/rules/whitespace/all/test.ts.lint index 048cad1b366..583406ae5ad 100644 --- a/test/rules/whitespace/all/test.ts.lint +++ b/test/rules/whitespace/all/test.ts.lint @@ -173,3 +173,19 @@ function foo (bar, ... baz) {} const { foo, ... bar } = baz; ~ [invalid whitespace] + +type A = number|string; + ~ [missing whitespace] + ~ [missing whitespace] + +type B = number|string|{result: string}; + ~ [missing whitespace] + ~ [missing whitespace] + ~ [missing whitespace] + ~ [missing whitespace] + +type C = {result: string}&{type: "A"|"B"}; + ~ [missing whitespace] + ~ [missing whitespace] + ~ [missing whitespace] + ~ [missing whitespace] diff --git a/test/rules/whitespace/all/tslint.json b/test/rules/whitespace/all/tslint.json index 6f44761bd02..ec76c7e71f1 100644 --- a/test/rules/whitespace/all/tslint.json +++ b/test/rules/whitespace/all/tslint.json @@ -9,7 +9,8 @@ "check-separator", "check-rest-spread", "check-type", - "check-typecast" + "check-typecast", + "check-type-operator" ] } }