Skip to content

Commit 8441504

Browse files
authored
Add converter for typedef property (#1370)
* feat: Add converter for `typedef` rule (#937) * style: Condense repetitive code This also uses `Set` to improve time complexity of searching to `O(n)` time * test: Fix to use `@jest/globals` * test: Add case for all argument conversions * feat: Complete `typedef` support This adds full support to `explicit-function-return-type` and `explicit-module-boundary-type`
1 parent 6f393cd commit 8441504

File tree

3 files changed

+246
-0
lines changed

3 files changed

+246
-0
lines changed

src/converters/lintConfigs/rules/ruleConverters.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ import { convertTemplateUseTrackByFunction } from "./ruleConverters/template-use
321321
import { convertTrailingComma } from "./ruleConverters/trailing-comma";
322322
import { convertTripleEquals } from "./ruleConverters/triple-equals";
323323
import { convertTypeLiteralDelimiter } from "./ruleConverters/type-literal-delimiter";
324+
import { convertTypedef } from "./ruleConverters/typedef";
324325
import { convertTypedefWhitespace } from "./ruleConverters/typedef-whitespace";
325326
import { convertTypeofCompare } from "./ruleConverters/typeof-compare";
326327
import { convertUnderscoreConsistentInvocation } from "./ruleConverters/underscore-consistent-invocation";
@@ -669,6 +670,7 @@ export const ruleConverters = new Map([
669670
["trailing-comma", convertTrailingComma],
670671
["triple-equals", convertTripleEquals],
671672
["type-literal-delimiter", convertTypeLiteralDelimiter],
673+
["typedef", convertTypedef],
672674
["typedef-whitespace", convertTypedefWhitespace],
673675
["typeof-compare", convertTypeofCompare],
674676
["underscore-consistent-invocation", convertUnderscoreConsistentInvocation],
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
import { describe, expect, test } from "@jest/globals";
2+
3+
import { convertTypedef } from "../typedef";
4+
5+
describe("convertTypedef", () => {
6+
test("conversion without arguments", () => {
7+
const result = convertTypedef({
8+
ruleArguments: [],
9+
});
10+
11+
expect(result).toEqual({
12+
rules: [
13+
{
14+
ruleName: "@typescript-eslint/typedef",
15+
},
16+
{
17+
ruleName: "@typescript-eslint/explicit-function-return-type",
18+
},
19+
{
20+
ruleName: "@typescript-eslint/explicit-module-boundary-types",
21+
},
22+
],
23+
});
24+
});
25+
26+
test("conversion with a few arguments", () => {
27+
const result = convertTypedef({
28+
ruleArguments: [
29+
"parameter",
30+
"variable-declaration-ignore-function",
31+
"array-destructuring",
32+
],
33+
});
34+
35+
expect(result).toEqual({
36+
rules: [
37+
{
38+
ruleArguments: [
39+
{
40+
parameter: true,
41+
variableDeclarationIgnoreFunction: true,
42+
arrayDestructuring: true,
43+
},
44+
],
45+
ruleName: "@typescript-eslint/typedef",
46+
},
47+
{
48+
ruleName: "@typescript-eslint/explicit-function-return-type",
49+
},
50+
{
51+
ruleName: "@typescript-eslint/explicit-module-boundary-types",
52+
},
53+
],
54+
});
55+
});
56+
57+
test("conversion with all arguments", () => {
58+
const result = convertTypedef({
59+
ruleArguments: [
60+
"parameter",
61+
"arrow-parameter",
62+
"property-declaration",
63+
"variable-declaration",
64+
"variable-declaration-ignore-function",
65+
"member-variable-declaration",
66+
"object-destructuring",
67+
"array-destructuring",
68+
],
69+
});
70+
71+
expect(result).toEqual({
72+
rules: [
73+
{
74+
ruleArguments: [
75+
{
76+
parameter: true,
77+
arrowParameter: true,
78+
propertyDeclaration: true,
79+
variableDeclaration: true,
80+
variableDeclarationIgnoreFunction: true,
81+
memberVariableDeclaration: true,
82+
objectDestructuring: true,
83+
arrayDestructuring: true,
84+
},
85+
],
86+
ruleName: "@typescript-eslint/typedef",
87+
},
88+
{
89+
ruleName: "@typescript-eslint/explicit-function-return-type",
90+
},
91+
{
92+
ruleName: "@typescript-eslint/explicit-module-boundary-types",
93+
},
94+
],
95+
});
96+
});
97+
98+
test("conversion with call-signature", () => {
99+
const result = convertTypedef({
100+
ruleArguments: ["call-signature"],
101+
});
102+
103+
expect(result).toEqual({
104+
notices: [
105+
"ESLint does not differentiate between the call signatures of arrow and non-arrow functions. Both will be checked",
106+
],
107+
rules: [
108+
{
109+
ruleName: "@typescript-eslint/typedef",
110+
},
111+
{
112+
ruleArguments: [
113+
{
114+
allowExpressions: false,
115+
allowTypedFunctionExpressions: false,
116+
allowHigherOrderFunctions: false,
117+
allowDirectConstAssertionInArrowFunctions: true,
118+
allowConciseArrowFunctionExpressionsStartingWithVoid: true,
119+
},
120+
],
121+
ruleName: "@typescript-eslint/explicit-function-return-type",
122+
},
123+
{
124+
ruleArguments: [
125+
{
126+
allowArgumentsExplicitlyTypedAsAny: true,
127+
allowDirectConstAssertionInArrowFunctions: true,
128+
allowHigherOrderFunctions: false,
129+
allowTypedFunctionExpressions: false,
130+
},
131+
],
132+
ruleName: "@typescript-eslint/explicit-module-boundary-types",
133+
},
134+
],
135+
});
136+
});
137+
138+
test("conversion with arrow-call-signature", () => {
139+
const result = convertTypedef({
140+
ruleArguments: ["arrow-call-signature"],
141+
});
142+
143+
expect(result).toEqual({
144+
notices: [
145+
"ESLint does not differentiate between the call signatures of arrow and non-arrow functions. Both will be checked",
146+
],
147+
rules: [
148+
{
149+
ruleName: "@typescript-eslint/typedef",
150+
},
151+
{
152+
ruleArguments: [
153+
{
154+
allowExpressions: false,
155+
allowTypedFunctionExpressions: false,
156+
allowHigherOrderFunctions: false,
157+
allowDirectConstAssertionInArrowFunctions: true,
158+
allowConciseArrowFunctionExpressionsStartingWithVoid: true,
159+
},
160+
],
161+
ruleName: "@typescript-eslint/explicit-function-return-type",
162+
},
163+
{
164+
ruleArguments: [
165+
{
166+
allowArgumentsExplicitlyTypedAsAny: true,
167+
allowDirectConstAssertionInArrowFunctions: true,
168+
allowHigherOrderFunctions: false,
169+
allowTypedFunctionExpressions: false,
170+
},
171+
],
172+
ruleName: "@typescript-eslint/explicit-module-boundary-types",
173+
},
174+
],
175+
});
176+
});
177+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { RuleConverter } from "../ruleConverter";
2+
3+
export const convertTypedef: RuleConverter = (tslintRule) => {
4+
const typedefRule: Record<string, boolean> = {};
5+
const functionReturnRule: Record<string, boolean> = {};
6+
const moduleBoundaryRule: Record<string, boolean> = {};
7+
8+
const originalArguments = new Set(tslintRule.ruleArguments);
9+
10+
const argumentEquivalents = {
11+
parameter: "parameter",
12+
"arrow-parameter": "arrowParameter",
13+
"property-declaration": "propertyDeclaration",
14+
"variable-declaration": "variableDeclaration",
15+
"variable-declaration-ignore-function": "variableDeclarationIgnoreFunction",
16+
"member-variable-declaration": "memberVariableDeclaration",
17+
"object-destructuring": "objectDestructuring",
18+
"array-destructuring": "arrayDestructuring",
19+
};
20+
21+
for (const [tslintArgument, eslintArgument] of Object.entries(argumentEquivalents)) {
22+
if (originalArguments.has(tslintArgument)) typedefRule[eslintArgument] = true;
23+
}
24+
25+
const checksFunctionCallSignture =
26+
originalArguments.has("arrow-call-signature") || originalArguments.has("call-signature");
27+
if (checksFunctionCallSignture) {
28+
functionReturnRule.allowExpressions = false;
29+
functionReturnRule.allowTypedFunctionExpressions = false;
30+
functionReturnRule.allowHigherOrderFunctions = false;
31+
functionReturnRule.allowDirectConstAssertionInArrowFunctions = true;
32+
functionReturnRule.allowConciseArrowFunctionExpressionsStartingWithVoid = true;
33+
34+
moduleBoundaryRule.allowArgumentsExplicitlyTypedAsAny = true;
35+
moduleBoundaryRule.allowDirectConstAssertionInArrowFunctions = true;
36+
moduleBoundaryRule.allowHigherOrderFunctions = false;
37+
moduleBoundaryRule.allowTypedFunctionExpressions = false;
38+
}
39+
40+
return {
41+
...(checksFunctionCallSignture && {
42+
notices: [
43+
"ESLint does not differentiate between the call signatures of arrow and non-arrow functions. Both will be checked",
44+
],
45+
}),
46+
rules: [
47+
{
48+
...(Object.keys(typedefRule).length !== 0 && {
49+
ruleArguments: [typedefRule],
50+
}),
51+
ruleName: "@typescript-eslint/typedef",
52+
},
53+
{
54+
...(Object.keys(functionReturnRule).length !== 0 && {
55+
ruleArguments: [functionReturnRule],
56+
}),
57+
ruleName: "@typescript-eslint/explicit-function-return-type",
58+
},
59+
{
60+
...(Object.keys(moduleBoundaryRule).length !== 0 && {
61+
ruleArguments: [moduleBoundaryRule],
62+
}),
63+
ruleName: "@typescript-eslint/explicit-module-boundary-types",
64+
},
65+
],
66+
};
67+
};

0 commit comments

Comments
 (0)