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

Commit

Permalink
Add "check-type-operator" option for whitespace rule (#3083)
Browse files Browse the repository at this point in the history
  • Loading branch information
santialbo authored and adidahiya committed Aug 11, 2017
1 parent 3b2d456 commit 9d712b3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
35 changes: 29 additions & 6 deletions src/rules/whitespaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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",
Expand All @@ -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),
Expand All @@ -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),
};

Expand Down Expand Up @@ -194,7 +202,7 @@ function walk(ctx: Lint.WalkContext<Options>) {
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;

Expand All @@ -212,6 +220,21 @@ function walk(ctx: Lint.WalkContext<Options>) {
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);
Expand Down Expand Up @@ -262,7 +285,7 @@ function walk(ctx: Lint.WalkContext<Options>) {
(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) {
Expand Down
6 changes: 6 additions & 0 deletions test/rules/whitespace/all/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -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"};
16 changes: 16 additions & 0 deletions test/rules/whitespace/all/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -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]
3 changes: 2 additions & 1 deletion test/rules/whitespace/all/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"check-separator",
"check-rest-spread",
"check-type",
"check-typecast"
"check-typecast",
"check-type-operator"
]
}
}

0 comments on commit 9d712b3

Please sign in to comment.