From 2c28eac0de5d5f4d51d50b1f834c5d34698c8669 Mon Sep 17 00:00:00 2001 From: "Maurice T. Meyer" Date: Wed, 2 Oct 2019 20:19:05 +0200 Subject: [PATCH 1/4] Added missing quotemark converter --- src/rules/converters.ts | 3 +- src/rules/converters/quotemark.ts | 32 +++++++ src/rules/converters/tests/quotemark.test.ts | 87 ++++++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 src/rules/converters/quotemark.ts create mode 100644 src/rules/converters/tests/quotemark.test.ts diff --git a/src/rules/converters.ts b/src/rules/converters.ts index f1a6289b0..533a5c112 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 { convertQuotemark } from "./converters/quotemark"; /** * Keys TSLint rule names to their ESLint rule converters. @@ -205,6 +206,7 @@ export const converters = new Map([ ["no-octal-literal", convertNoOctalLiteral], ["no-regex-spaces", convertNoRegexSpaces], ["no-unnecessary-semicolons", convertNoUnnecessarySemicolons], + ["quotemark", convertQuotemark], // 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... @@ -221,7 +223,6 @@ export const converters = new Map([ // ["no-trailing-whitespace", convertNoTrailingWhitespace], // no-trailing-spaces // ["no-unused-expression", convertNoUnusedExpression], // no-unused-expressions // ["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... diff --git a/src/rules/converters/quotemark.ts b/src/rules/converters/quotemark.ts new file mode 100644 index 000000000..2ad627dfa --- /dev/null +++ b/src/rules/converters/quotemark.ts @@ -0,0 +1,32 @@ +import { RuleConverter } from "../converter"; + +export const convertQuotemark: RuleConverter = tslintRule => { + const notices: string[] = []; + const ruleArguments: any[] = []; + + ["jsx-single", "jsx-double", "avoid-template"].forEach(rule => { + if (tslintRule.ruleArguments.includes(rule)) { + notices.push(`Option "${rule}" is not supported by ESLint.`); + } + }); + + ["single", "double", "backtick"].forEach(rule => { + if (tslintRule.ruleArguments.includes(rule)) { + ruleArguments.push(rule); + } + }); + + if (tslintRule.ruleArguments.includes("avoid-escape")) { + ruleArguments.push({ avoidEscape: true }); + } + + return { + rules: [ + { + notices, + ruleArguments, + ruleName: "@typescript-eslint/quotes", + }, + ], + }; +}; diff --git a/src/rules/converters/tests/quotemark.test.ts b/src/rules/converters/tests/quotemark.test.ts new file mode 100644 index 000000000..7df4d3788 --- /dev/null +++ b/src/rules/converters/tests/quotemark.test.ts @@ -0,0 +1,87 @@ +import { convertQuotemark } from "../quotemark"; + +describe(convertQuotemark, () => { + test("conversion without arguments", () => { + const result = convertQuotemark({ + ruleArguments: [], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "@typescript-eslint/quotes", + ruleArguments: [], + notices: [], + }, + ], + }); + }); + + test("conversion with an arguments", () => { + const result = convertQuotemark({ + ruleArguments: ["double"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "@typescript-eslint/quotes", + ruleArguments: ["double"], + notices: [], + }, + ], + }); + }); + + test("conversion with an avoid-escape argument", () => { + const result = convertQuotemark({ + ruleArguments: ["avoid-escape"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "@typescript-eslint/quotes", + ruleArguments: [{ avoidEscape: true }], + notices: [], + }, + ], + }); + }); + + test("conversion with multiple arguments", () => { + const result = convertQuotemark({ + ruleArguments: ["single", "avoid-template"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "@typescript-eslint/quotes", + ruleArguments: ["single"], + notices: ['Option "avoid-template" is not supported by ESLint.'], + }, + ], + }); + }); + + test("conversion with unsupported arguments", () => { + const result = convertQuotemark({ + ruleArguments: ["jsx-single", "jsx-double", "avoid-template"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "@typescript-eslint/quotes", + ruleArguments: [], + notices: [ + 'Option "jsx-single" is not supported by ESLint.', + 'Option "jsx-double" is not supported by ESLint.', + 'Option "avoid-template" is not supported by ESLint.', + ], + }, + ], + }); + }); +}); From 09395d4138e4efcd9a353b08b61776f96b295b46 Mon Sep 17 00:00:00 2001 From: "Maurice T. Meyer" Date: Thu, 3 Oct 2019 14:35:53 +0200 Subject: [PATCH 2/4] Fixed a typo and renamed a test --- src/rules/converters/tests/quotemark.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rules/converters/tests/quotemark.test.ts b/src/rules/converters/tests/quotemark.test.ts index 7df4d3788..a5aeb3849 100644 --- a/src/rules/converters/tests/quotemark.test.ts +++ b/src/rules/converters/tests/quotemark.test.ts @@ -17,7 +17,7 @@ describe(convertQuotemark, () => { }); }); - test("conversion with an arguments", () => { + test("conversion with an argument", () => { const result = convertQuotemark({ ruleArguments: ["double"], }); @@ -49,7 +49,7 @@ describe(convertQuotemark, () => { }); }); - test("conversion with multiple arguments", () => { + test("conversion with arguments", () => { const result = convertQuotemark({ ruleArguments: ["single", "avoid-template"], }); From ff454e9669d3e208a2894d58061a5d7b125f11f9 Mon Sep 17 00:00:00 2001 From: "Maurice T. Meyer" Date: Thu, 3 Oct 2019 14:40:39 +0200 Subject: [PATCH 3/4] Refactored some variable names to better reflect the usage, replaced any with a more specific type --- src/rules/converters/quotemark.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/rules/converters/quotemark.ts b/src/rules/converters/quotemark.ts index 2ad627dfa..01fc3f175 100644 --- a/src/rules/converters/quotemark.ts +++ b/src/rules/converters/quotemark.ts @@ -2,17 +2,17 @@ import { RuleConverter } from "../converter"; export const convertQuotemark: RuleConverter = tslintRule => { const notices: string[] = []; - const ruleArguments: any[] = []; + const ruleArguments: Array = []; - ["jsx-single", "jsx-double", "avoid-template"].forEach(rule => { - if (tslintRule.ruleArguments.includes(rule)) { - notices.push(`Option "${rule}" is not supported by ESLint.`); + ["jsx-single", "jsx-double", "avoid-template"].forEach(option => { + if (tslintRule.ruleArguments.includes(option)) { + notices.push(`Option "${option}" is not supported by ESLint.`); } }); - ["single", "double", "backtick"].forEach(rule => { - if (tslintRule.ruleArguments.includes(rule)) { - ruleArguments.push(rule); + ["single", "double", "backtick"].forEach(option => { + if (tslintRule.ruleArguments.includes(option)) { + ruleArguments.push(option); } }); From 2cf1316aaed52b5a0c2501baba7594a57f60a23b Mon Sep 17 00:00:00 2001 From: "Maurice T. Meyer" Date: Thu, 3 Oct 2019 14:47:38 +0200 Subject: [PATCH 4/4] Making eslint stage happy by defining a custom type --- src/rules/converters/quotemark.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/rules/converters/quotemark.ts b/src/rules/converters/quotemark.ts index 01fc3f175..3c864df7a 100644 --- a/src/rules/converters/quotemark.ts +++ b/src/rules/converters/quotemark.ts @@ -1,8 +1,9 @@ import { RuleConverter } from "../converter"; +type QuotemarkRule = string | { avoidEscape: true }; export const convertQuotemark: RuleConverter = tslintRule => { const notices: string[] = []; - const ruleArguments: Array = []; + const ruleArguments: QuotemarkRule[] = []; ["jsx-single", "jsx-double", "avoid-template"].forEach(option => { if (tslintRule.ruleArguments.includes(option)) {