From a1b100d304e271736251986c405238d3f29faa1b Mon Sep 17 00:00:00 2001 From: Cory Thompson Date: Thu, 3 Oct 2019 23:02:15 -0500 Subject: [PATCH] added only-arrow-functions converter and unit tests --- src/rules/converters.ts | 2 + src/rules/converters/only-arrow-functions.ts | 25 +++++++ .../tests/only-arrow-functions.test.ts | 71 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 src/rules/converters/only-arrow-functions.ts create mode 100644 src/rules/converters/tests/only-arrow-functions.test.ts diff --git a/src/rules/converters.ts b/src/rules/converters.ts index 782e17f47..f1509a708 100644 --- a/src/rules/converters.ts +++ b/src/rules/converters.ts @@ -82,6 +82,7 @@ import { convertNoVarRequires } from "./converters/no-var-requires"; import { convertObjectLiteralKeyQuotes } from "./converters/object-literal-key-quotes"; import { convertObjectLiteralShorthand } from "./converters/object-literal-shorthand"; import { convertOneVariablePerDeclaration } from "./converters/one-variable-per-declaration"; +import { convertOnlyArrowFunctions } from "./converters/only-arrow-functions"; import { convertPreferConst } from "./converters/prefer-const"; import { convertPreferForOf } from "./converters/prefer-for-of"; import { convertPreferFunctionOverMethod } from "./converters/prefer-function-over-method"; @@ -193,6 +194,7 @@ export const converters = new Map([ ["object-literal-key-quotes", convertObjectLiteralKeyQuotes], ["object-literal-shorthand", convertObjectLiteralShorthand], ["one-variable-per-declaration", convertOneVariablePerDeclaration], + ["only-arrow-functions", convertOnlyArrowFunctions], ["prefer-const", convertPreferConst], ["prefer-function-over-method", convertPreferFunctionOverMethod], ["prefer-readonly", convertPreferReadonly], diff --git a/src/rules/converters/only-arrow-functions.ts b/src/rules/converters/only-arrow-functions.ts new file mode 100644 index 000000000..17fa60abf --- /dev/null +++ b/src/rules/converters/only-arrow-functions.ts @@ -0,0 +1,25 @@ +import { RuleConverter } from "../converter"; + +export const convertOnlyArrowFunctions: RuleConverter = tslintRule => { + const notices: string[] = []; + + if (tslintRule.ruleArguments.includes("allow-declarations")) { + notices.push("ESLint does not support allowing standalone function declarations."); + } + + if (tslintRule.ruleArguments.includes("allow-named-functions")) { + notices.push( + "ESLint does not support allowing named functions defined with the function keyword.", + ); + } + + return { + rules: [ + { + ...(notices.length > 0 && { notices }), + ruleName: "prefer-arrow/prefer-arrow-functions", + }, + ], + plugins: ["prefer-arrow"], + }; +}; diff --git a/src/rules/converters/tests/only-arrow-functions.test.ts b/src/rules/converters/tests/only-arrow-functions.test.ts new file mode 100644 index 000000000..decbb63c3 --- /dev/null +++ b/src/rules/converters/tests/only-arrow-functions.test.ts @@ -0,0 +1,71 @@ +import { convertOnlyArrowFunctions } from "../only-arrow-functions"; + +describe(convertOnlyArrowFunctions, () => { + test("conversion without arguments", () => { + const result = convertOnlyArrowFunctions({ + ruleArguments: [], + }); + + expect(result).toEqual({ + rules: [ + { + ruleName: "prefer-arrow/prefer-arrow-functions", + }, + ], + plugins: ["prefer-arrow"], + }); + }); + + test("conversion with allow-declarations argument", () => { + const result = convertOnlyArrowFunctions({ + ruleArguments: ["allow-declarations"], + }); + + expect(result).toEqual({ + rules: [ + { + notices: ["ESLint does not support allowing standalone function declarations."], + ruleName: "prefer-arrow/prefer-arrow-functions", + }, + ], + plugins: ["prefer-arrow"], + }); + }); + + test("conversion with allow-named-functions argument", () => { + const result = convertOnlyArrowFunctions({ + ruleArguments: ["allow-named-functions"], + }); + + expect(result).toEqual({ + rules: [ + { + notices: [ + "ESLint does not support allowing named functions defined with the function keyword.", + ], + ruleName: "prefer-arrow/prefer-arrow-functions", + }, + ], + plugins: ["prefer-arrow"], + }); + }); + + test("conversion with all arguments", () => { + const result = convertOnlyArrowFunctions({ + ruleArguments: ["allow-declarations", "allow-named-functions"], + }); + + expect(result).toEqual({ + rules: [ + { + notices: [ + "ESLint does not support allowing standalone function declarations.", + "ESLint does not support allowing named functions defined with the function keyword.", + ], + ruleName: "prefer-arrow/prefer-arrow-functions", + }, + ], + plugins: ["prefer-arrow"], + }); + }); +});