Skip to content

Commit

Permalink
fix space-before-function-paren (#293)
Browse files Browse the repository at this point in the history
* fix `space-before-function-paren`

* chmod +x bin/tslint-to-eslint-config
  • Loading branch information
bouzuya authored and Josh Goldberg committed Dec 24, 2019
1 parent c6c3e88 commit dd5138c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 29 deletions.
Empty file modified bin/tslint-to-eslint-config 100644 → 100755
Empty file.
53 changes: 31 additions & 22 deletions 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<string, "always" | "never">;

export const convertSpaceBeforeFunctionParen: RuleConverter = tslintRule => {
const ruleArguments = tslintRule.ruleArguments.filter(isExistedESLintOption);
const notices = NOT_SUPPORTED_OPTIONS.reduce<string[]>((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<ObjectArgument>((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 }] };
};
62 changes: 55 additions & 7 deletions src/rules/converters/tests/space-before-function-paren.test.ts
Expand Up @@ -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",
},
],
Expand All @@ -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({
Expand All @@ -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",
},
],
Expand All @@ -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({
Expand All @@ -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",
},
],
Expand Down

0 comments on commit dd5138c

Please sign in to comment.