Skip to content

Commit dd5138c

Browse files
bouzuyaJosh Goldberg
authored and
Josh Goldberg
committed
fix space-before-function-paren (#293)
* fix `space-before-function-paren` * chmod +x bin/tslint-to-eslint-config
1 parent c6c3e88 commit dd5138c

File tree

3 files changed

+86
-29
lines changed

3 files changed

+86
-29
lines changed

bin/tslint-to-eslint-config

100644100755
File mode changed.
Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
11
import { RuleConverter } from "../converter";
22

3-
const NOT_SUPPORTED_OPTIONS = ["constructor", "method"];
3+
const SUPPORTED_OPTIONS: string[] = ["anonymous", "asyncArrow", "named"];
44

5-
function isExistedESLintOption(rule: string) {
6-
return !NOT_SUPPORTED_OPTIONS.includes(rule);
7-
}
5+
type ObjectArgument = Record<string, "always" | "never">;
86

9-
export const convertSpaceBeforeFunctionParen: RuleConverter = tslintRule => {
10-
const ruleArguments = tslintRule.ruleArguments.filter(isExistedESLintOption);
11-
const notices = NOT_SUPPORTED_OPTIONS.reduce<string[]>((acc, option) => {
12-
if (tslintRule.ruleArguments.includes(option)) {
13-
acc.push(`Option "${option}" is not supported by ESLint.`);
14-
}
15-
return acc;
16-
}, []);
7+
const isObjectArgument = (argument: unknown): argument is ObjectArgument =>
8+
typeof argument === "object" && argument !== null;
179

18-
return {
19-
rules: [
20-
{
21-
...(tslintRule.ruleArguments.length !== 0 && {
22-
ruleArguments,
10+
export const convertSpaceBeforeFunctionParen: RuleConverter = tslintRule => {
11+
const argument: unknown = tslintRule.ruleArguments[0];
12+
const ruleName = "space-before-function-paren";
13+
if (argument === "always" || argument === "never") {
14+
return { rules: [{ ruleArguments: [argument], ruleName }] };
15+
}
16+
if (isObjectArgument(argument)) {
17+
const notices = Object.keys(argument)
18+
.filter(option => !SUPPORTED_OPTIONS.includes(option))
19+
.map(option => `Option "${option}" is not supported by ESLint.`);
20+
const filtered = Object.keys(argument)
21+
.filter(option => SUPPORTED_OPTIONS.includes(option))
22+
.reduce<ObjectArgument>((obj, option) => {
23+
return { ...obj, [option]: argument[option] };
24+
}, {});
25+
return {
26+
rules: [
27+
{
2328
...(notices.length !== 0 && { notices }),
24-
}),
25-
ruleName: "space-before-function-paren",
26-
},
27-
],
28-
};
29+
...(Object.keys(filtered).length !== 0 && {
30+
ruleArguments: [filtered],
31+
}),
32+
ruleName,
33+
},
34+
],
35+
};
36+
}
37+
return { rules: [{ ruleName }] };
2938
};

src/rules/converters/tests/space-before-function-paren.test.ts

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,45 @@ describe(convertSpaceBeforeFunctionParen, () => {
1515
});
1616
});
1717

18-
test("conversion with an argument", () => {
18+
test("conversion with an argument (always)", () => {
1919
const result = convertSpaceBeforeFunctionParen({
20-
ruleArguments: ["anonymous"],
20+
ruleArguments: ["always"],
2121
});
2222

2323
expect(result).toEqual({
2424
rules: [
2525
{
26-
ruleArguments: ["anonymous"],
26+
ruleArguments: ["always"],
27+
ruleName: "space-before-function-paren",
28+
},
29+
],
30+
});
31+
});
32+
33+
test("conversion with an argument (never)", () => {
34+
const result = convertSpaceBeforeFunctionParen({
35+
ruleArguments: ["never"],
36+
});
37+
38+
expect(result).toEqual({
39+
rules: [
40+
{
41+
ruleArguments: ["never"],
42+
ruleName: "space-before-function-paren",
43+
},
44+
],
45+
});
46+
});
47+
48+
test("conversion with an argument (object)", () => {
49+
const result = convertSpaceBeforeFunctionParen({
50+
ruleArguments: [{ anonymous: "never" }],
51+
});
52+
53+
expect(result).toEqual({
54+
rules: [
55+
{
56+
ruleArguments: [{ anonymous: "never" }],
2757
ruleName: "space-before-function-paren",
2858
},
2959
],
@@ -32,7 +62,15 @@ describe(convertSpaceBeforeFunctionParen, () => {
3262

3363
test("conversion with all existing arguments", () => {
3464
const result = convertSpaceBeforeFunctionParen({
35-
ruleArguments: ["anonymous", "named", "asyncArrow", "method", "constructor"],
65+
ruleArguments: [
66+
{
67+
anonymous: "never",
68+
asyncArrow: "always",
69+
constructor: "never",
70+
method: "never",
71+
named: "never",
72+
},
73+
],
3674
});
3775

3876
expect(result).toEqual({
@@ -42,7 +80,13 @@ describe(convertSpaceBeforeFunctionParen, () => {
4280
'Option "constructor" is not supported by ESLint.',
4381
'Option "method" is not supported by ESLint.',
4482
],
45-
ruleArguments: ["anonymous", "named", "asyncArrow"],
83+
ruleArguments: [
84+
{
85+
anonymous: "never",
86+
asyncArrow: "always",
87+
named: "never",
88+
},
89+
],
4690
ruleName: "space-before-function-paren",
4791
},
4892
],
@@ -51,7 +95,12 @@ describe(convertSpaceBeforeFunctionParen, () => {
5195

5296
test('conversion with not supported options ["method", "constructor"]', () => {
5397
const result = convertSpaceBeforeFunctionParen({
54-
ruleArguments: ["method", "constructor"],
98+
ruleArguments: [
99+
{
100+
constructor: "never",
101+
method: "never",
102+
},
103+
],
55104
});
56105

57106
expect(result).toEqual({
@@ -61,7 +110,6 @@ describe(convertSpaceBeforeFunctionParen, () => {
61110
'Option "constructor" is not supported by ESLint.',
62111
'Option "method" is not supported by ESLint.',
63112
],
64-
ruleArguments: [],
65113
ruleName: "space-before-function-paren",
66114
},
67115
],

0 commit comments

Comments
 (0)