Skip to content

Commit 6ae5bb7

Browse files
aspondaJosh Goldberg
authored and
Josh Goldberg
committed
Added no-unused-expression converter (#205)
* feat: added no-unused-expression converter and unit tests * feat: Add notice for optional configs that not equals to allow-tagged-template for no-unused-expression-converter rule * fix: no-unused-expression converter - add converter for allow-fast-null-checks and notice for no allow-new * fix: undo unrelated removed comments in converters.ts * fix: no-unused-expression - fix espace in the notice message
1 parent 4a88942 commit 6ae5bb7

File tree

3 files changed

+131
-1
lines changed

3 files changed

+131
-1
lines changed

src/rules/converters.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import { convertNoUnnecessaryQualifier } from "./converters/no-unnecessary-quali
8888
import { convertNoUnnecessarySemicolons } from "./converters/no-unnecessary-semicolons";
8989
import { convertNoUnnecessaryTypeAssertion } from "./converters/no-unnecessary-type-assertion";
9090
import { convertNoUnsafeFinally } from "./converters/no-unsafe-finally";
91+
import { convertNoUnusedExpression } from "./converters/no-unused-expression";
9192
import { convertNoUseBeforeDeclare } from "./converters/no-use-before-declare";
9293
import { convertNoVarKeyword } from "./converters/no-var-keyword";
9394
import { convertNoVarRequires } from "./converters/no-var-requires";
@@ -215,6 +216,7 @@ export const converters = new Map([
215216
["no-unnecessary-semicolons", convertNoUnnecessarySemicolons],
216217
["no-unnecessary-type-assertion", convertNoUnnecessaryTypeAssertion],
217218
["no-unsafe-finally", convertNoUnsafeFinally],
219+
["no-unused-expression", convertNoUnusedExpression],
218220
["no-use-before-declare", convertNoUseBeforeDeclare],
219221
["no-var-keyword", convertNoVarKeyword],
220222
["no-var-requires", convertNoVarRequires],
@@ -257,7 +259,6 @@ export const converters = new Map([
257259
// ["ban", convertBan], // no-restricted-properties
258260
// ["import-blacklist", convertImportBlacklist], // no-restricted-imports
259261
// ["no-duplicate-variable", convertNoDuplicateVariable], // no-redeclare
260-
// ["no-unused-expression", convertNoUnusedExpression], // no-unused-expressions
261262
// ["no-void-expression", convertNoVoidExpression], // (no exact equivalent)
262263
// ["quotemark", convertQuotemark], // quotes
263264
// ["triple-equals", convertTripleEquals], // eqeqeq
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { RuleConverter } from "../converter";
2+
3+
export const convertNoUnusedExpression: RuleConverter = tslintRule => {
4+
return {
5+
rules: [
6+
{
7+
ruleName: "no-unused-expressions",
8+
...collectNoticesAndArguments(tslintRule.ruleArguments),
9+
},
10+
],
11+
};
12+
};
13+
14+
const collectNoticesAndArguments = (tsLintRuleArguments: any[]) => {
15+
const noAllowNewNotice = `The TSLint optional config "allow-new" is the default ESLint behavior and will no longer be ignored.`;
16+
17+
if (tsLintRuleArguments.length === 0) {
18+
return {
19+
notices: [noAllowNewNotice],
20+
};
21+
}
22+
23+
const notices = [];
24+
const ruleArguments: any[] = [];
25+
26+
if (tsLintRuleArguments.includes("allow-tagged-template")) {
27+
ruleArguments.push({ allowTaggedTemplates: true });
28+
}
29+
30+
if (tsLintRuleArguments.includes("allow-fast-null-checks")) {
31+
ruleArguments.push({ allowShortCircuit: true });
32+
}
33+
34+
if (!tsLintRuleArguments.includes("allow-new")) {
35+
notices.push(noAllowNewNotice);
36+
}
37+
38+
return {
39+
...(notices.length > 0 && { notices }),
40+
...(ruleArguments.length > 0 && {
41+
ruleArguments: [
42+
ruleArguments.reduce((value, current) => Object.assign(value, current), {}),
43+
],
44+
}),
45+
};
46+
};
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { convertNoUnusedExpression } from "../no-unused-expression";
2+
3+
describe(convertNoUnusedExpression, () => {
4+
test("conversion without arguments", () => {
5+
const result = convertNoUnusedExpression({
6+
ruleArguments: [],
7+
});
8+
9+
expect(result).toEqual({
10+
rules: [
11+
{
12+
ruleName: "no-unused-expressions",
13+
notices: [
14+
`The TSLint optional config "allow-new" is the default ESLint behavior and will no longer be ignored.`,
15+
],
16+
},
17+
],
18+
});
19+
});
20+
21+
test("conversion without allow-new argument", () => {
22+
const result = convertNoUnusedExpression({
23+
ruleArguments: ["allow-fast-null-checks"],
24+
});
25+
26+
expect(result).toEqual({
27+
rules: [
28+
{
29+
ruleName: "no-unused-expressions",
30+
ruleArguments: [{ allowShortCircuit: true }],
31+
notices: [
32+
`The TSLint optional config "allow-new" is the default ESLint behavior and will no longer be ignored.`,
33+
],
34+
},
35+
],
36+
});
37+
});
38+
39+
test("conversion with allow-tagged-template argument", () => {
40+
const result = convertNoUnusedExpression({
41+
ruleArguments: ["allow-new", "allow-tagged-template"],
42+
});
43+
44+
expect(result).toEqual({
45+
rules: [
46+
{
47+
ruleArguments: [{ allowTaggedTemplates: true }],
48+
ruleName: "no-unused-expressions",
49+
},
50+
],
51+
});
52+
});
53+
54+
test("conversion with allow-fast-null-checks argument", () => {
55+
const result = convertNoUnusedExpression({
56+
ruleArguments: ["allow-new", "allow-fast-null-checks"],
57+
});
58+
59+
expect(result).toEqual({
60+
rules: [
61+
{
62+
ruleName: "no-unused-expressions",
63+
ruleArguments: [{ allowShortCircuit: true }],
64+
},
65+
],
66+
});
67+
});
68+
69+
test("conversion with multiple arguments", () => {
70+
const result = convertNoUnusedExpression({
71+
ruleArguments: ["allow-new", "allow-tagged-template", "allow-fast-null-checks"],
72+
});
73+
74+
expect(result).toEqual({
75+
rules: [
76+
{
77+
ruleName: "no-unused-expressions",
78+
ruleArguments: [{ allowTaggedTemplates: true, allowShortCircuit: true }],
79+
},
80+
],
81+
});
82+
});
83+
});

0 commit comments

Comments
 (0)