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

Commit 9d712b3

Browse files
santialboadidahiya
authored andcommitted
Add "check-type-operator" option for whitespace rule (#3083)
1 parent 3b2d456 commit 9d712b3

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

src/rules/whitespaceRule.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const OPTION_SEPARATOR = "check-separator";
3030
const OPTION_REST_SPREAD = "check-rest-spread";
3131
const OPTION_TYPE = "check-type";
3232
const OPTION_TYPECAST = "check-typecast";
33+
const OPTION_TYPE_OPERATOR = "check-type-operator";
3334
const OPTION_PREBLOCK = "check-preblock";
3435

3536
export class Rule extends Lint.Rules.AbstractRule {
@@ -48,16 +49,19 @@ export class Rule extends Lint.Rules.AbstractRule {
4849
* \`"check-rest-spread"\` checks that there is no whitespace after rest/spread operator (\`...\`).
4950
* \`"check-type"\` checks for whitespace before a variable type specification.
5051
* \`"check-typecast"\` checks for whitespace between a typecast and its target.
52+
* \`"check-type-operator"\` checks for whitespace between type operators \`|\` and \`&\`.
5153
* \`"check-preblock"\` checks for whitespace before the opening brace of a block`,
5254
options: {
5355
type: "array",
5456
items: {
5557
type: "string",
56-
enum: ["check-branch", "check-decl", "check-operator", "check-module",
57-
"check-separator", "check-rest-spread", "check-type", "check-typecast", "check-preblock"],
58+
enum: [
59+
"check-branch", "check-decl", "check-operator", "check-module", "check-separator",
60+
"check-rest-spread", "check-type", "check-typecast", "check-type-operator", "check-preblock",
61+
],
5862
},
5963
minLength: 0,
60-
maxLength: 9,
64+
maxLength: 10,
6165
},
6266
optionExamples: [[true, "check-branch", "check-operator", "check-typecast"]],
6367
type: "style",
@@ -72,7 +76,10 @@ export class Rule extends Lint.Rules.AbstractRule {
7276
}
7377
}
7478

75-
type Options = Record<"branch" | "decl" | "operator" | "module" | "separator" | "restSpread" | "type" | "typecast" | "preblock", boolean>;
79+
type Options = Record<
80+
"branch" | "decl" | "operator" | "module" | "separator" | "restSpread" | "type" | "typecast" | "typeOperator" | "preblock",
81+
boolean>;
82+
7683
function parseOptions(ruleArguments: string[]): Options {
7784
return {
7885
branch: has(OPTION_BRANCH),
@@ -83,6 +90,7 @@ function parseOptions(ruleArguments: string[]): Options {
8390
restSpread: has(OPTION_REST_SPREAD),
8491
type: has(OPTION_TYPE),
8592
typecast: has(OPTION_TYPECAST),
93+
typeOperator: has(OPTION_TYPE_OPERATOR),
8694
preblock: has(OPTION_PREBLOCK),
8795
};
8896

@@ -194,7 +202,7 @@ function walk(ctx: Lint.WalkContext<Options>) {
194202
case ts.SyntaxKind.VariableDeclaration:
195203
const { name, type, initializer } = node as ts.VariableDeclaration;
196204
if (options.decl && initializer !== undefined) {
197-
checkForTrailingWhitespace((type !== undefined ? type : name).getEnd());
205+
checkForTrailingWhitespace((type !== undefined ? type : name).getEnd());
198206
}
199207
break;
200208

@@ -212,6 +220,21 @@ function walk(ctx: Lint.WalkContext<Options>) {
212220
const position = (node as ts.SpreadAssignment).expression.getFullStart();
213221
checkForExcessiveWhitespace(position);
214222
}
223+
break;
224+
225+
case ts.SyntaxKind.UnionType:
226+
case ts.SyntaxKind.IntersectionType:
227+
if (options.typeOperator) {
228+
const { types } = node as ts.UnionOrIntersectionTypeNode;
229+
types.forEach((typeNode, index) => {
230+
if (index > 0) {
231+
checkForTrailingWhitespace(typeNode.getFullStart());
232+
}
233+
if (index < types.length - 1) {
234+
checkForTrailingWhitespace(typeNode.getEnd());
235+
}
236+
});
237+
}
215238
}
216239

217240
ts.forEachChild(node, cb);
@@ -262,7 +285,7 @@ function walk(ctx: Lint.WalkContext<Options>) {
262285
(parent as ts.CallExpression).expression.kind === ts.SyntaxKind.ImportKeyword) {
263286
return; // Don't check ImportCall
264287
}
265-
// falls through
288+
// falls through
266289
case ts.SyntaxKind.ExportKeyword:
267290
case ts.SyntaxKind.FromKeyword:
268291
if (options.typecast) {

test/rules/whitespace/all/test.ts.fix

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,9 @@ const foo = [ ...bar ];
112112
function foo (bar, ...baz) {}
113113

114114
const { foo, ...bar } = baz;
115+
116+
type A = number | string;
117+
118+
type B = number | string | {result: string};
119+
120+
type C = {result: string} & {type: "A" | "B"};

test/rules/whitespace/all/test.ts.lint

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,19 @@ function foo (bar, ... baz) {}
173173

174174
const { foo, ... bar } = baz;
175175
~ [invalid whitespace]
176+
177+
type A = number|string;
178+
~ [missing whitespace]
179+
~ [missing whitespace]
180+
181+
type B = number|string|{result: string};
182+
~ [missing whitespace]
183+
~ [missing whitespace]
184+
~ [missing whitespace]
185+
~ [missing whitespace]
186+
187+
type C = {result: string}&{type: "A"|"B"};
188+
~ [missing whitespace]
189+
~ [missing whitespace]
190+
~ [missing whitespace]
191+
~ [missing whitespace]

test/rules/whitespace/all/tslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"check-separator",
1010
"check-rest-spread",
1111
"check-type",
12-
"check-typecast"
12+
"check-typecast",
13+
"check-type-operator"
1314
]
1415
}
1516
}

0 commit comments

Comments
 (0)