From 75c8e27c43bfdd9977c1ad7b734f1f18a314f8d3 Mon Sep 17 00:00:00 2001 From: asponda Date: Fri, 4 Oct 2019 15:07:05 -0300 Subject: [PATCH 1/5] feat: added no-unused-expression converter and unit tests --- src/rules/converters.ts | 3 +- src/rules/converters/no-unused-expression.ts | 15 ++++++ .../tests/no-unused-expression.test.ts | 46 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/rules/converters/no-unused-expression.ts create mode 100644 src/rules/converters/tests/no-unused-expression.test.ts diff --git a/src/rules/converters.ts b/src/rules/converters.ts index 96a036500..678d9a729 100644 --- a/src/rules/converters.ts +++ b/src/rules/converters.ts @@ -79,6 +79,7 @@ import { convertNoUnnecessaryQualifier } from "./converters/no-unnecessary-quali import { convertNoUnnecessarySemicolons } from "./converters/no-unnecessary-semicolons"; import { convertNoUnnecessaryTypeAssertion } from "./converters/no-unnecessary-type-assertion"; import { convertNoUnsafeFinally } from "./converters/no-unsafe-finally"; +import { convertNoUnusedExpression } from "./converters/no-unused-expression"; import { convertNoUseBeforeDeclare } from "./converters/no-use-before-declare"; import { convertNoVarKeyword } from "./converters/no-var-keyword"; import { convertNoVarRequires } from "./converters/no-var-requires"; @@ -171,6 +172,7 @@ export const converters = new Map([ ["no-unnecessary-qualifier", convertNoUnnecessaryQualifier], ["no-unnecessary-type-assertion", convertNoUnnecessaryTypeAssertion], ["no-unsafe-finally", convertNoUnsafeFinally], + ["no-unused-expression", convertNoUnusedExpression], ["no-use-before-declare", convertNoUseBeforeDeclare], ["no-var-keyword", convertNoVarKeyword], ["no-var-requires", convertNoVarRequires], @@ -234,7 +236,6 @@ export const converters = new Map([ // ["no-duplicate-variable", convertNoDuplicateVariable], // no-redeclare // ["no-shadowed-variable", convertNoShadowedVariable], // no-shadow // ["no-trailing-whitespace", convertNoTrailingWhitespace], // no-trailing-spaces - // ["no-unused-expression", convertNoUnusedExpression], // no-unused-expressions // ["no-void-expression", convertNoVoidExpression], // (no exact equivalent) // ["space-within-parens", convertSpaceWithinParens], // space-in-parens // ["variable-name", convertVariableName], // a bunch of rules... diff --git a/src/rules/converters/no-unused-expression.ts b/src/rules/converters/no-unused-expression.ts new file mode 100644 index 000000000..964521f58 --- /dev/null +++ b/src/rules/converters/no-unused-expression.ts @@ -0,0 +1,15 @@ +import { RuleConverter } from "../converter"; + +export const convertNoUnusedExpression: RuleConverter = tslintRule => { + return { + rules: [ + { + ...(tslintRule.ruleArguments.length !== 0 && + tslintRule.ruleArguments.includes("allow-tagged-template") && { + ruleArguments: [{ allowTaggedTemplates: true }], + }), + ruleName: "no-unused-expressions", + }, + ], + }; +}; diff --git a/src/rules/converters/tests/no-unused-expression.test.ts b/src/rules/converters/tests/no-unused-expression.test.ts new file mode 100644 index 000000000..3ed7a5a53 --- /dev/null +++ b/src/rules/converters/tests/no-unused-expression.test.ts @@ -0,0 +1,46 @@ +import { convertNoUnusedExpression } from "../no-unused-expression"; + +describe(convertNoUnusedExpression, () => { + test("conversion without arguments", () => { + const result = convertNoUnusedExpression({ + ruleArguments: [], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "no-unused-expressions", + }, + ], + }); + }); + + test("conversion with allow-tagged-template argument", () => { + const result = convertNoUnusedExpression({ + ruleArguments: ["allow-tagged-template"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleArguments: [{ allowTaggedTemplates: true }], + ruleName: "no-unused-expressions", + }, + ], + }); + }); + + test("conversion with argument not equals allow-tagged-template", () => { + const result = convertNoUnusedExpression({ + ruleArguments: ["allow-fast-null-checks"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "no-unused-expressions", + }, + ], + }); + }); +}); From 696d09c734795c597370f8f1789614e520571522 Mon Sep 17 00:00:00 2001 From: asponda Date: Sun, 6 Oct 2019 20:07:03 -0300 Subject: [PATCH 2/5] feat: Add notice for optional configs that not equals to allow-tagged-template for no-unused-expression-converter rule --- src/rules/converters/no-unused-expression.ts | 25 ++++++++++++++++--- .../tests/no-unused-expression.test.ts | 1 + 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/rules/converters/no-unused-expression.ts b/src/rules/converters/no-unused-expression.ts index 964521f58..30d3675a1 100644 --- a/src/rules/converters/no-unused-expression.ts +++ b/src/rules/converters/no-unused-expression.ts @@ -4,12 +4,29 @@ export const convertNoUnusedExpression: RuleConverter = tslintRule => { return { rules: [ { - ...(tslintRule.ruleArguments.length !== 0 && - tslintRule.ruleArguments.includes("allow-tagged-template") && { - ruleArguments: [{ allowTaggedTemplates: true }], - }), ruleName: "no-unused-expressions", + ...collectNoticesAndArguments(tslintRule.ruleArguments), }, ], }; }; + +const collectNoticesAndArguments = (tsLintRuleArguments: any[]) => { + if (tsLintRuleArguments.length === 0) { + return undefined; + } + + const notices = []; + const ruleArguments: any[] = []; + + if (tsLintRuleArguments.includes("allow-tagged-template")) { + ruleArguments.push({ allowTaggedTemplates: true }); + } else { + notices.push(`ESLint does not support optional config ${tsLintRuleArguments[0]}.`); + } + + return { + ...(notices.length > 0 && { notices }), + ...(ruleArguments.length > 0 && { ruleArguments }), + }; +}; diff --git a/src/rules/converters/tests/no-unused-expression.test.ts b/src/rules/converters/tests/no-unused-expression.test.ts index 3ed7a5a53..844df5cec 100644 --- a/src/rules/converters/tests/no-unused-expression.test.ts +++ b/src/rules/converters/tests/no-unused-expression.test.ts @@ -39,6 +39,7 @@ describe(convertNoUnusedExpression, () => { rules: [ { ruleName: "no-unused-expressions", + notices: ["ESLint does not support optional config allow-fast-null-checks."], }, ], }); From dd271af2d6b7ccaa805c333e6bca8e827f062ce8 Mon Sep 17 00:00:00 2001 From: asponda Date: Mon, 7 Oct 2019 09:40:34 -0300 Subject: [PATCH 3/5] fix: no-unused-expression converter - add converter for allow-fast-null-checks and notice for no allow-new --- src/rules/converters/no-unused-expression.ts | 22 ++++++++-- .../tests/no-unused-expression.test.ts | 44 +++++++++++++++++-- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/rules/converters/no-unused-expression.ts b/src/rules/converters/no-unused-expression.ts index 30d3675a1..6ac7e9113 100644 --- a/src/rules/converters/no-unused-expression.ts +++ b/src/rules/converters/no-unused-expression.ts @@ -12,8 +12,12 @@ export const convertNoUnusedExpression: RuleConverter = tslintRule => { }; const collectNoticesAndArguments = (tsLintRuleArguments: any[]) => { + const noAllowNewNotice = `The TSLint optional config "allow-new" is the default ESLint behavior and will no longer be ignored.`; + if (tsLintRuleArguments.length === 0) { - return undefined; + return { + notices: [noAllowNewNotice], + }; } const notices = []; @@ -21,12 +25,22 @@ const collectNoticesAndArguments = (tsLintRuleArguments: any[]) => { if (tsLintRuleArguments.includes("allow-tagged-template")) { ruleArguments.push({ allowTaggedTemplates: true }); - } else { - notices.push(`ESLint does not support optional config ${tsLintRuleArguments[0]}.`); + } + + if (tsLintRuleArguments.includes("allow-fast-null-checks")) { + ruleArguments.push({ allowShortCircuit: true }); + } + + if (!tsLintRuleArguments.includes("allow-new")) { + notices.push(noAllowNewNotice); } return { ...(notices.length > 0 && { notices }), - ...(ruleArguments.length > 0 && { ruleArguments }), + ...(ruleArguments.length > 0 && { + ruleArguments: [ + ruleArguments.reduce((value, current) => Object.assign(value, current), {}), + ], + }), }; }; diff --git a/src/rules/converters/tests/no-unused-expression.test.ts b/src/rules/converters/tests/no-unused-expression.test.ts index 844df5cec..d9fa75966 100644 --- a/src/rules/converters/tests/no-unused-expression.test.ts +++ b/src/rules/converters/tests/no-unused-expression.test.ts @@ -10,6 +10,27 @@ describe(convertNoUnusedExpression, () => { rules: [ { ruleName: "no-unused-expressions", + notices: [ + `The TSLint optional config "allow-new" is the default ESLint behavior and will no longer be ignored.`, + ], + }, + ], + }); + }); + + test("conversion without allow-new argument", () => { + const result = convertNoUnusedExpression({ + ruleArguments: ["allow-fast-null-checks"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "no-unused-expressions", + ruleArguments: [{ allowShortCircuit: true }], + notices: [ + `The TSLint optional config "allow-new" is the default ESLint behavior and will no longer be ignored.`, + ], }, ], }); @@ -17,7 +38,7 @@ describe(convertNoUnusedExpression, () => { test("conversion with allow-tagged-template argument", () => { const result = convertNoUnusedExpression({ - ruleArguments: ["allow-tagged-template"], + ruleArguments: ["allow-new", "allow-tagged-template"], }); expect(result).toEqual({ @@ -30,16 +51,31 @@ describe(convertNoUnusedExpression, () => { }); }); - test("conversion with argument not equals allow-tagged-template", () => { + test("conversion with allow-fast-null-checks argument", () => { const result = convertNoUnusedExpression({ - ruleArguments: ["allow-fast-null-checks"], + ruleArguments: ["allow-new", "allow-fast-null-checks"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "no-unused-expressions", + ruleArguments: [{ allowShortCircuit: true }], + }, + ], + }); + }); + + test("conversion with multiple arguments", () => { + const result = convertNoUnusedExpression({ + ruleArguments: ["allow-new", "allow-tagged-template", "allow-fast-null-checks"], }); expect(result).toEqual({ rules: [ { ruleName: "no-unused-expressions", - notices: ["ESLint does not support optional config allow-fast-null-checks."], + ruleArguments: [{ allowTaggedTemplates: true, allowShortCircuit: true }], }, ], }); From a5b71df5143be287d42a6e2c9a59171d0cc8d42e Mon Sep 17 00:00:00 2001 From: asponda Date: Mon, 7 Oct 2019 10:07:59 -0300 Subject: [PATCH 4/5] fix: undo unrelated removed comments in converters.ts --- src/rules/converters.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/rules/converters.ts b/src/rules/converters.ts index d694a3ca8..d7846f633 100644 --- a/src/rules/converters.ts +++ b/src/rules/converters.ts @@ -259,6 +259,9 @@ export const converters = new Map([ // ["ban", convertBan], // no-restricted-properties // ["import-blacklist", convertImportBlacklist], // no-restricted-imports // ["no-duplicate-variable", convertNoDuplicateVariable], // no-redeclare + // ["no-void-expression", convertNoVoidExpression], // (no exact equivalent) + // ["quotemark", convertQuotemark], // quotes + // ["triple-equals", convertTripleEquals], // eqeqeq // ["variable-name", convertVariableName], // a bunch of rules... // tslint-microsoft-contrib rules: From d7be84614ee238e1011e683992e5eb74a96819d7 Mon Sep 17 00:00:00 2001 From: asponda Date: Mon, 7 Oct 2019 10:10:21 -0300 Subject: [PATCH 5/5] fix: no-unused-expression - fix espace in the notice message --- src/rules/converters/no-unused-expression.ts | 2 +- src/rules/converters/tests/no-unused-expression.test.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rules/converters/no-unused-expression.ts b/src/rules/converters/no-unused-expression.ts index 6ac7e9113..a1142b5e8 100644 --- a/src/rules/converters/no-unused-expression.ts +++ b/src/rules/converters/no-unused-expression.ts @@ -12,7 +12,7 @@ export const convertNoUnusedExpression: RuleConverter = tslintRule => { }; const collectNoticesAndArguments = (tsLintRuleArguments: any[]) => { - const noAllowNewNotice = `The TSLint optional config "allow-new" is the default ESLint behavior and will no longer be ignored.`; + const noAllowNewNotice = `The TSLint optional config "allow-new" is the default ESLint behavior and will no longer be ignored.`; if (tsLintRuleArguments.length === 0) { return { diff --git a/src/rules/converters/tests/no-unused-expression.test.ts b/src/rules/converters/tests/no-unused-expression.test.ts index d9fa75966..cd31fbeb1 100644 --- a/src/rules/converters/tests/no-unused-expression.test.ts +++ b/src/rules/converters/tests/no-unused-expression.test.ts @@ -11,7 +11,7 @@ describe(convertNoUnusedExpression, () => { { ruleName: "no-unused-expressions", notices: [ - `The TSLint optional config "allow-new" is the default ESLint behavior and will no longer be ignored.`, + `The TSLint optional config "allow-new" is the default ESLint behavior and will no longer be ignored.`, ], }, ], @@ -29,7 +29,7 @@ describe(convertNoUnusedExpression, () => { ruleName: "no-unused-expressions", ruleArguments: [{ allowShortCircuit: true }], notices: [ - `The TSLint optional config "allow-new" is the default ESLint behavior and will no longer be ignored.`, + `The TSLint optional config "allow-new" is the default ESLint behavior and will no longer be ignored.`, ], }, ],