diff --git a/bin/tslint-to-eslint-config b/bin/tslint-to-eslint-config old mode 100644 new mode 100755 diff --git a/src/rules/converters/space-before-function-paren.ts b/src/rules/converters/space-before-function-paren.ts index 916a5984b..a52916c47 100644 --- a/src/rules/converters/space-before-function-paren.ts +++ b/src/rules/converters/space-before-function-paren.ts @@ -1,29 +1,38 @@ import { RuleConverter } from "../converter"; -const NOT_SUPPORTED_OPTIONS = ["constructor", "method"]; +const SUPPORTED_OPTIONS: string[] = ["anonymous", "asyncArrow", "named"]; -function isExistedESLintOption(rule: string) { - return !NOT_SUPPORTED_OPTIONS.includes(rule); -} +type ObjectArgument = Record; -export const convertSpaceBeforeFunctionParen: RuleConverter = tslintRule => { - const ruleArguments = tslintRule.ruleArguments.filter(isExistedESLintOption); - const notices = NOT_SUPPORTED_OPTIONS.reduce((acc, option) => { - if (tslintRule.ruleArguments.includes(option)) { - acc.push(`Option "${option}" is not supported by ESLint.`); - } - return acc; - }, []); +const isObjectArgument = (argument: unknown): argument is ObjectArgument => + typeof argument === "object" && argument !== null; - return { - rules: [ - { - ...(tslintRule.ruleArguments.length !== 0 && { - ruleArguments, +export const convertSpaceBeforeFunctionParen: RuleConverter = tslintRule => { + const argument: unknown = tslintRule.ruleArguments[0]; + const ruleName = "space-before-function-paren"; + if (argument === "always" || argument === "never") { + return { rules: [{ ruleArguments: [argument], ruleName }] }; + } + if (isObjectArgument(argument)) { + const notices = Object.keys(argument) + .filter(option => !SUPPORTED_OPTIONS.includes(option)) + .map(option => `Option "${option}" is not supported by ESLint.`); + const filtered = Object.keys(argument) + .filter(option => SUPPORTED_OPTIONS.includes(option)) + .reduce((obj, option) => { + return { ...obj, [option]: argument[option] }; + }, {}); + return { + rules: [ + { ...(notices.length !== 0 && { notices }), - }), - ruleName: "space-before-function-paren", - }, - ], - }; + ...(Object.keys(filtered).length !== 0 && { + ruleArguments: [filtered], + }), + ruleName, + }, + ], + }; + } + return { rules: [{ ruleName }] }; }; diff --git a/src/rules/converters/tests/space-before-function-paren.test.ts b/src/rules/converters/tests/space-before-function-paren.test.ts index b83285fe5..77663d453 100644 --- a/src/rules/converters/tests/space-before-function-paren.test.ts +++ b/src/rules/converters/tests/space-before-function-paren.test.ts @@ -15,15 +15,45 @@ describe(convertSpaceBeforeFunctionParen, () => { }); }); - test("conversion with an argument", () => { + test("conversion with an argument (always)", () => { const result = convertSpaceBeforeFunctionParen({ - ruleArguments: ["anonymous"], + ruleArguments: ["always"], }); expect(result).toEqual({ rules: [ { - ruleArguments: ["anonymous"], + ruleArguments: ["always"], + ruleName: "space-before-function-paren", + }, + ], + }); + }); + + test("conversion with an argument (never)", () => { + const result = convertSpaceBeforeFunctionParen({ + ruleArguments: ["never"], + }); + + expect(result).toEqual({ + rules: [ + { + ruleArguments: ["never"], + ruleName: "space-before-function-paren", + }, + ], + }); + }); + + test("conversion with an argument (object)", () => { + const result = convertSpaceBeforeFunctionParen({ + ruleArguments: [{ anonymous: "never" }], + }); + + expect(result).toEqual({ + rules: [ + { + ruleArguments: [{ anonymous: "never" }], ruleName: "space-before-function-paren", }, ], @@ -32,7 +62,15 @@ describe(convertSpaceBeforeFunctionParen, () => { test("conversion with all existing arguments", () => { const result = convertSpaceBeforeFunctionParen({ - ruleArguments: ["anonymous", "named", "asyncArrow", "method", "constructor"], + ruleArguments: [ + { + anonymous: "never", + asyncArrow: "always", + constructor: "never", + method: "never", + named: "never", + }, + ], }); expect(result).toEqual({ @@ -42,7 +80,13 @@ describe(convertSpaceBeforeFunctionParen, () => { 'Option "constructor" is not supported by ESLint.', 'Option "method" is not supported by ESLint.', ], - ruleArguments: ["anonymous", "named", "asyncArrow"], + ruleArguments: [ + { + anonymous: "never", + asyncArrow: "always", + named: "never", + }, + ], ruleName: "space-before-function-paren", }, ], @@ -51,7 +95,12 @@ describe(convertSpaceBeforeFunctionParen, () => { test('conversion with not supported options ["method", "constructor"]', () => { const result = convertSpaceBeforeFunctionParen({ - ruleArguments: ["method", "constructor"], + ruleArguments: [ + { + constructor: "never", + method: "never", + }, + ], }); expect(result).toEqual({ @@ -61,7 +110,6 @@ describe(convertSpaceBeforeFunctionParen, () => { 'Option "constructor" is not supported by ESLint.', 'Option "method" is not supported by ESLint.', ], - ruleArguments: [], ruleName: "space-before-function-paren", }, ],