Skip to content

Commit d70969f

Browse files
author
Josh Goldberg
authored
Added converter for max-func-body-length (#1066)
1 parent bb27bc1 commit d70969f

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

src/converters/lintConfigs/rules/ruleConverters.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { convertJSDocFormat } from "./ruleConverters/jsdoc-format";
2727
import { convertLabelPosition } from "./ruleConverters/label-position";
2828
import { convertLinebreakStyle } from "./ruleConverters/linebreak-style";
2929
import { convertMaxClassesPerFile } from "./ruleConverters/max-classes-per-file";
30+
import { convertMaxFuncBodyLength } from "./ruleConverters/max-func-body-length";
3031
import { convertMaxFileLineCount } from "./ruleConverters/max-file-line-count";
3132
import { convertMaxLineLength } from "./ruleConverters/max-line-length";
3233
import { convertMemberAccess } from "./ruleConverters/member-access";
@@ -276,6 +277,7 @@ export const ruleConverters = new Map([
276277
["label-position", convertLabelPosition],
277278
["linebreak-style", convertLinebreakStyle],
278279
["max-classes-per-file", convertMaxClassesPerFile],
280+
["max-func-body-length", convertMaxFuncBodyLength],
279281
["max-file-line-count", convertMaxFileLineCount],
280282
["max-line-length", convertMaxLineLength],
281283
["member-access", convertMemberAccess],
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { isNumber } from "lodash";
2+
3+
import { RuleConverter } from "../ruleConverter";
4+
5+
const parseExtras = (ruleArguments: any[]) => {
6+
if (ruleArguments.length === 0) {
7+
return {};
8+
}
9+
10+
const [max] = ruleArguments;
11+
if (typeof max === "number") {
12+
return {
13+
ruleArguments: [max],
14+
}
15+
}
16+
17+
const notices = [
18+
"ESLint's max-statements rule only supports a single maximum function length."
19+
];
20+
21+
if (max["ignore-comments"]) {
22+
notices.push("ESLint's max-statements rule does not have an option to ignore comments.")
23+
}
24+
25+
return {
26+
notices,
27+
ruleArguments: [
28+
Math.max(...Object.values(max as Record<string, number | string>).filter(isNumber)),
29+
]
30+
}
31+
}
32+
33+
export const convertMaxFuncBodyLength: RuleConverter = (tslintRule) => {
34+
return {
35+
rules: [
36+
{
37+
...parseExtras(tslintRule.ruleArguments),
38+
ruleName: "max-statements",
39+
},
40+
],
41+
};
42+
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { convertMaxFuncBodyLength } from "../max-func-body-length";
2+
3+
describe(convertMaxFuncBodyLength, () => {
4+
test("conversion without arguments", () => {
5+
const result = convertMaxFuncBodyLength({
6+
ruleArguments: [],
7+
});
8+
9+
expect(result).toEqual({
10+
rules: [
11+
{
12+
ruleName: "max-statements",
13+
},
14+
],
15+
});
16+
});
17+
18+
test("conversion with a max number", () => {
19+
const result = convertMaxFuncBodyLength({
20+
ruleArguments: [10],
21+
});
22+
23+
expect(result).toEqual({
24+
rules: [
25+
{
26+
ruleArguments: [10],
27+
ruleName: "max-statements",
28+
},
29+
],
30+
});
31+
});
32+
33+
test("conversion with a max object", () => {
34+
const result = convertMaxFuncBodyLength({
35+
ruleArguments: [{
36+
"ctor-body-length": 15,
37+
"func-body-length": 5,
38+
}],
39+
});
40+
41+
expect(result).toEqual({
42+
rules: [
43+
{
44+
notices: [
45+
"ESLint's max-statements rule only supports a single maximum function length."
46+
],
47+
ruleArguments: [15],
48+
ruleName: "max-statements",
49+
},
50+
],
51+
});
52+
});
53+
54+
test("conversion with the ignore-comments option", () => {
55+
const result = convertMaxFuncBodyLength({
56+
ruleArguments: [{
57+
"ctor-body-length": 15,
58+
"func-body-length": 5,
59+
"ignore-comments": true,
60+
}],
61+
});
62+
63+
expect(result).toEqual({
64+
rules: [
65+
{
66+
notices: [
67+
"ESLint's max-statements rule only supports a single maximum function length.",
68+
"ESLint's max-statements rule does not have an option to ignore comments."
69+
],
70+
ruleArguments: [15],
71+
ruleName: "max-statements",
72+
},
73+
],
74+
});
75+
});
76+
});

0 commit comments

Comments
 (0)