From 8e5043e10e407f7983ede41e7da5f6196151a02e Mon Sep 17 00:00:00 2001 From: Nik Mouzourides Date: Wed, 2 Oct 2019 17:33:00 +0100 Subject: [PATCH] Implement converter for triple-equals rule #187 --- src/rules/converters.ts | 4 +- .../converters/tests/triple-equals.test.ts | 56 +++++++++++++++++++ src/rules/converters/triple-equals.ts | 48 ++++++++++++++++ 3 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 src/rules/converters/tests/triple-equals.test.ts create mode 100644 src/rules/converters/triple-equals.ts diff --git a/src/rules/converters.ts b/src/rules/converters.ts index f1a6289b0..30fdbf5c2 100644 --- a/src/rules/converters.ts +++ b/src/rules/converters.ts @@ -99,6 +99,7 @@ import { convertUnifiedSignatures } from "./converters/unified-signatures"; import { convertUnnecessaryBind } from "./converters/unnecessary-bind"; import { convertUnnecessaryConstructor } from "./converters/unnecessary-constructor"; import { convertUseIsnan } from "./converters/use-isnan"; +import { convertTripleEquals } from "./converters/triple-equals"; /** * Keys TSLint rule names to their ESLint rule converters. @@ -205,7 +206,7 @@ export const converters = new Map([ ["no-octal-literal", convertNoOctalLiteral], ["no-regex-spaces", convertNoRegexSpaces], ["no-unnecessary-semicolons", convertNoUnnecessarySemicolons], - + ["triple-equals", convertTripleEquals], // These converters are all for rules that need more complex option conversions. // Some of them will likely need to have notices about changed lint behaviors... // If you're willing to take on that work, that'd be great! Please send PRs! 💖 @@ -223,7 +224,6 @@ export const converters = new Map([ // ["no-void-expression", convertNoVoidExpression], // (no exact equivalent) // ["quotemark", convertQuotemark], // quotes // ["space-within-parens", convertSpaceWithinParens], // space-in-parens - // ["triple-equals", convertTripleEquals], // eqeqeq // ["variable-name", convertVariableName], // a bunch of rules... // tslint-microsoft-contrib rules: diff --git a/src/rules/converters/tests/triple-equals.test.ts b/src/rules/converters/tests/triple-equals.test.ts new file mode 100644 index 000000000..ad2940b17 --- /dev/null +++ b/src/rules/converters/tests/triple-equals.test.ts @@ -0,0 +1,56 @@ +import { convertTripleEquals } from "../triple-equals"; + +describe(convertTripleEquals, () => { + test("conversion without arguments", () => { + const result = convertTripleEquals({ + ruleArguments: [], + }); + + expect(result).toEqual({ + rules: [ + { + ruleArguments: ["always"], + ruleName: "eqeqeq", + notices: [], + }, + ], + }); + }); + + test("conversion with an allow-null-check argument", () => { + const result = convertTripleEquals({ + ruleArguments: ["allow-null-check"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleArguments: ["smart"], + ruleName: "eqeqeq", + notices: [ + 'Option "smart" allows for comparing two literal values, evaluating the value of typeof and null comparisons.', + ], + }, + ], + }); + }); + + test("conversion with an allow-undefined-check argument", () => { + const result = convertTripleEquals({ + ruleArguments: ["allow-undefined-check"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleArguments: ["smart"], + ruleName: "eqeqeq", + notices: [ + 'Option "allow-undefined-check" is not supported by ESLint. Option "smart" is the closest.', + 'Option "smart" allows for comparing two literal values, evaluating the value of typeof and null comparisons.', + ], + }, + ], + }); + }); +}); diff --git a/src/rules/converters/triple-equals.ts b/src/rules/converters/triple-equals.ts new file mode 100644 index 000000000..26c6975c3 --- /dev/null +++ b/src/rules/converters/triple-equals.ts @@ -0,0 +1,48 @@ +import { RuleConverter } from "../converter"; + +export const convertTripleEquals: RuleConverter = tslintRule => { + const getRuleOptions = () => { + const smartOptionNotice = + 'Option "smart" allows for comparing two literal values, evaluating the value of typeof and null comparisons.'; + + if ( + tslintRule.ruleArguments.length !== 0 && + tslintRule.ruleArguments[0] === "allow-null-check" + ) { + return { + arguments: ["smart"], + notices: [smartOptionNotice], + }; + } + + if ( + tslintRule.ruleArguments.length !== 0 && + tslintRule.ruleArguments[0] === "allow-undefined-check" + ) { + return { + arguments: ["smart"], + notices: [ + 'Option "allow-undefined-check" is not supported by ESLint. Option "smart" is the closest.', + smartOptionNotice, + ], + }; + } + + return { + arguments: ["always"], + notices: [], + }; + }; + + const options = getRuleOptions(); + + return { + rules: [ + { + ruleName: "eqeqeq", + ruleArguments: options.arguments, + notices: options.notices, + }, + ], + }; +};