Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix space-before-function-paren #293

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 }] };
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
}
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