Skip to content

Commit befe67c

Browse files
KingDarBojaJosh Goldberg
authored and
Josh Goldberg
committed
Add strict-boolean-expression converter (#237)
* Add strict-boolean-expression converter * Typo fix on strict-boolean.expression notice
1 parent e408e53 commit befe67c

File tree

3 files changed

+148
-4
lines changed

3 files changed

+148
-4
lines changed

src/rules/converters.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ import { convertRestrictPlusOperands } from "./converters/restrict-plus-operands
112112
import { convertSemicolon } from "./converters/semicolon";
113113
import { convertSpaceBeforeFunctionParen } from "./converters/space-before-function-paren";
114114
import { convertSpaceWithinParens } from "./converters/space-within-parens";
115+
import { convertStrictBooleanExpressions } from "./converters/strict-boolean-expressions";
115116
import { convertSwitchDefault } from "./converters/switch-default";
116117
import { convertTripleEquals } from "./converters/triple-equals";
117118
import { convertTypedefWhitespace } from "./converters/typedef-whitespace";
@@ -242,6 +243,7 @@ export const converters = new Map([
242243
["semicolon", convertSemicolon],
243244
["space-before-function-paren", convertSpaceBeforeFunctionParen],
244245
["space-within-parens", convertSpaceWithinParens],
246+
["strict-boolean-expressions", convertStrictBooleanExpressions],
245247
["switch-default", convertSwitchDefault],
246248
["triple-equals", convertTripleEquals],
247249
["type-literal-delimiter", convertTypeLiteralDelimiter],
@@ -262,10 +264,6 @@ export const converters = new Map([
262264
// TSLint core rules:
263265
// ["ban", convertBan], // no-restricted-properties
264266
// ["import-blacklist", convertImportBlacklist], // no-restricted-imports
265-
// ["no-duplicate-variable", convertNoDuplicateVariable], // no-redeclare
266-
// ["no-void-expression", convertNoVoidExpression], // (no exact equivalent)
267-
// ["quotemark", convertQuotemark], // quotes
268-
// ["triple-equals", convertTripleEquals], // eqeqeq
269267

270268
// tslint-microsoft-contrib rules:
271269
// ["max-func-body-length", convertMaxFuncBodyLength],
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { RuleConverter } from "../converter";
2+
3+
export const ForbiddenOtherNonBooleanTypes =
4+
"String, number, enum, and mixed union types are now forbidden.";
5+
6+
export const convertStrictBooleanExpressions: RuleConverter = tslintRule => {
7+
const notices: string[] = [];
8+
const ruleArguments: { [key: string]: boolean } = {};
9+
10+
if (tslintRule.ruleArguments.length >= 2 || tslintRule.ruleArguments[0] === true) {
11+
notices.push(ForbiddenOtherNonBooleanTypes);
12+
tslintRule.ruleArguments.forEach(ruleArgument => {
13+
switch (ruleArgument) {
14+
case "allow-undefined-union":
15+
case "allow-boolean-or-undefined":
16+
case "allow-null-union":
17+
ruleArguments["allowNullable"] = true;
18+
break;
19+
case "ignore-rhs":
20+
ruleArguments["ignoreRhs"] = true;
21+
break;
22+
default:
23+
break;
24+
}
25+
});
26+
}
27+
28+
return {
29+
rules: [
30+
{
31+
ruleName: "@typescript-eslint/strict-boolean-expressions",
32+
ruleArguments: Object.keys(ruleArguments).length !== 0 ? [ruleArguments] : [],
33+
notices,
34+
},
35+
],
36+
};
37+
};
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import {
2+
convertStrictBooleanExpressions,
3+
ForbiddenOtherNonBooleanTypes,
4+
} from "../strict-boolean-expressions";
5+
6+
describe(convertStrictBooleanExpressions, () => {
7+
test("conversion without arguments", () => {
8+
const result = convertStrictBooleanExpressions({
9+
ruleArguments: [],
10+
});
11+
12+
expect(result).toEqual({
13+
rules: [
14+
{
15+
ruleName: "@typescript-eslint/strict-boolean-expressions",
16+
ruleArguments: [],
17+
notices: [],
18+
},
19+
],
20+
});
21+
});
22+
23+
test("conversion with true argument", () => {
24+
const result = convertStrictBooleanExpressions({
25+
ruleArguments: [true],
26+
});
27+
28+
expect(result).toEqual({
29+
rules: [
30+
{
31+
ruleName: "@typescript-eslint/strict-boolean-expressions",
32+
ruleArguments: [],
33+
notices: [ForbiddenOtherNonBooleanTypes],
34+
},
35+
],
36+
});
37+
});
38+
39+
test("conversion with allow-undefined-union argument", () => {
40+
const result = convertStrictBooleanExpressions({
41+
ruleArguments: [true, "allow-undefined-union"],
42+
});
43+
44+
expect(result).toEqual({
45+
rules: [
46+
{
47+
ruleName: "@typescript-eslint/strict-boolean-expressions",
48+
ruleArguments: [
49+
{
50+
allowNullable: true,
51+
},
52+
],
53+
notices: [ForbiddenOtherNonBooleanTypes],
54+
},
55+
],
56+
});
57+
});
58+
59+
test("conversion with ignore-rhs argument", () => {
60+
const result = convertStrictBooleanExpressions({
61+
ruleArguments: [true, "ignore-rhs"],
62+
});
63+
64+
expect(result).toEqual({
65+
rules: [
66+
{
67+
ruleName: "@typescript-eslint/strict-boolean-expressions",
68+
ruleArguments: [
69+
{
70+
ignoreRhs: true,
71+
},
72+
],
73+
notices: [ForbiddenOtherNonBooleanTypes],
74+
},
75+
],
76+
});
77+
});
78+
79+
test("conversion with all arguments", () => {
80+
const result = convertStrictBooleanExpressions({
81+
ruleArguments: [
82+
true,
83+
"allow-undefined-union",
84+
"allow-boolean-or-undefined",
85+
"allow-null-union",
86+
"ignore-rhs",
87+
"allow-string",
88+
"allow-enum",
89+
"allow-number",
90+
"allow-mix",
91+
],
92+
});
93+
94+
expect(result).toEqual({
95+
rules: [
96+
{
97+
ruleName: "@typescript-eslint/strict-boolean-expressions",
98+
ruleArguments: [
99+
{
100+
allowNullable: true,
101+
ignoreRhs: true,
102+
},
103+
],
104+
notices: [ForbiddenOtherNonBooleanTypes],
105+
},
106+
],
107+
});
108+
});
109+
});

0 commit comments

Comments
 (0)