Skip to content

Commit 9cc263f

Browse files
cthompson527Josh Goldberg
authored and
Josh Goldberg
committed
added only-arrow-functions converter and unit tests (#197)
1 parent 2f6c160 commit 9cc263f

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

src/rules/converters.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import { convertNoVoidExpression } from "./converters/no-void-expression";
8686
import { convertObjectLiteralKeyQuotes } from "./converters/object-literal-key-quotes";
8787
import { convertObjectLiteralShorthand } from "./converters/object-literal-shorthand";
8888
import { convertOneVariablePerDeclaration } from "./converters/one-variable-per-declaration";
89+
import { convertOnlyArrowFunctions } from "./converters/only-arrow-functions";
8990
import { convertPreferConst } from "./converters/prefer-const";
9091
import { convertPreferForOf } from "./converters/prefer-for-of";
9192
import { convertPreferFunctionOverMethod } from "./converters/prefer-function-over-method";
@@ -203,6 +204,7 @@ export const converters = new Map([
203204
["object-literal-key-quotes", convertObjectLiteralKeyQuotes],
204205
["object-literal-shorthand", convertObjectLiteralShorthand],
205206
["one-variable-per-declaration", convertOneVariablePerDeclaration],
207+
["only-arrow-functions", convertOnlyArrowFunctions],
206208
["prefer-const", convertPreferConst],
207209
["prefer-function-over-method", convertPreferFunctionOverMethod],
208210
["prefer-readonly", convertPreferReadonly],
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { RuleConverter } from "../converter";
2+
3+
export const convertOnlyArrowFunctions: RuleConverter = tslintRule => {
4+
const notices: string[] = [];
5+
6+
if (tslintRule.ruleArguments.includes("allow-declarations")) {
7+
notices.push("ESLint does not support allowing standalone function declarations.");
8+
}
9+
10+
if (tslintRule.ruleArguments.includes("allow-named-functions")) {
11+
notices.push(
12+
"ESLint does not support allowing named functions defined with the function keyword.",
13+
);
14+
}
15+
16+
return {
17+
rules: [
18+
{
19+
...(notices.length > 0 && { notices }),
20+
ruleName: "prefer-arrow/prefer-arrow-functions",
21+
},
22+
],
23+
plugins: ["prefer-arrow"],
24+
};
25+
};
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { convertOnlyArrowFunctions } from "../only-arrow-functions";
2+
3+
describe(convertOnlyArrowFunctions, () => {
4+
test("conversion without arguments", () => {
5+
const result = convertOnlyArrowFunctions({
6+
ruleArguments: [],
7+
});
8+
9+
expect(result).toEqual({
10+
rules: [
11+
{
12+
ruleName: "prefer-arrow/prefer-arrow-functions",
13+
},
14+
],
15+
plugins: ["prefer-arrow"],
16+
});
17+
});
18+
19+
test("conversion with allow-declarations argument", () => {
20+
const result = convertOnlyArrowFunctions({
21+
ruleArguments: ["allow-declarations"],
22+
});
23+
24+
expect(result).toEqual({
25+
rules: [
26+
{
27+
notices: ["ESLint does not support allowing standalone function declarations."],
28+
ruleName: "prefer-arrow/prefer-arrow-functions",
29+
},
30+
],
31+
plugins: ["prefer-arrow"],
32+
});
33+
});
34+
35+
test("conversion with allow-named-functions argument", () => {
36+
const result = convertOnlyArrowFunctions({
37+
ruleArguments: ["allow-named-functions"],
38+
});
39+
40+
expect(result).toEqual({
41+
rules: [
42+
{
43+
notices: [
44+
"ESLint does not support allowing named functions defined with the function keyword.",
45+
],
46+
ruleName: "prefer-arrow/prefer-arrow-functions",
47+
},
48+
],
49+
plugins: ["prefer-arrow"],
50+
});
51+
});
52+
53+
test("conversion with all arguments", () => {
54+
const result = convertOnlyArrowFunctions({
55+
ruleArguments: ["allow-declarations", "allow-named-functions"],
56+
});
57+
58+
expect(result).toEqual({
59+
rules: [
60+
{
61+
notices: [
62+
"ESLint does not support allowing standalone function declarations.",
63+
"ESLint does not support allowing named functions defined with the function keyword.",
64+
],
65+
ruleName: "prefer-arrow/prefer-arrow-functions",
66+
},
67+
],
68+
plugins: ["prefer-arrow"],
69+
});
70+
});
71+
});

0 commit comments

Comments
 (0)