Skip to content

Commit a00e533

Browse files
committed
Respect ban-types configuration of TSLint
In the past, this tool didn't respect the TSLint configuration of `ban-types`, it means even if TSLint specified `ban-types` with the argument, generated ESLint configuration doesn't contain the detailed configuration of that. For example, when the TSLint configuration is like the following: ``` { "rules": { "ban-types": [true, ["Object", "Use {} instead."], ] } } ``` then, older tool generates the following ESLint configuration: ``` "rules": { "@typescript-eslint/ban-types": "error" } ``` According to the official documentation, probably this generated result doesn't make sense because each rule requires the type name (and the message). https://github.com/typescript-eslint/typescript-eslint/blob/f3160b471f8247e157555b6cf5b40a1f6ccdc233/packages/eslint-plugin/docs/rules/ban-types.md So this commit makes the generated ESLint configuration to be suitable to the certainly working configuration, like so: ``` "rules": { "@typescript-eslint/ban-types": [ "error", { "types": { "Object": { "message": "Use {} instead." } } } ] } ``` See also: https://palantir.github.io/tslint/rules/ban-types/ Related ticket: #352
1 parent 5051f98 commit a00e533

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

src/rules/converters/ban-types.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
11
import { RuleConverter } from "../converter";
22

3-
export const convertBanTypes: RuleConverter = () => {
3+
export const convertBanTypes: RuleConverter = tslintRule => {
4+
type ConvertBanTypeArgument = {
5+
message: string;
6+
};
7+
8+
const bannedTypesObj: { [key: string]: ConvertBanTypeArgument | null } = {};
9+
10+
for (const rule of tslintRule.ruleArguments) {
11+
if (!Array.isArray(rule)) {
12+
break;
13+
}
14+
15+
const typ = rule[0];
16+
if (!typ) {
17+
break;
18+
}
19+
20+
bannedTypesObj[typ] = rule[1]
21+
? {
22+
message: rule[1],
23+
}
24+
: null;
25+
}
26+
27+
const ruleArguments =
28+
Object.keys(bannedTypesObj).length === 0 ? undefined : [{ types: bannedTypesObj }];
29+
430
return {
531
rules: [
632
{
733
ruleName: "@typescript-eslint/ban-types",
34+
...{ ruleArguments },
835
},
936
],
1037
};

src/rules/converters/tests/ban-types.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,58 @@ describe(convertBanTypes, () => {
1414
],
1515
});
1616
});
17+
18+
test("conversion with arguments", () => {
19+
const result = convertBanTypes({
20+
ruleArguments: [
21+
["Object", "Use {} instead."],
22+
["String"],
23+
["Number"],
24+
["Boolean", "Use 'boolean' instead."],
25+
],
26+
});
27+
28+
expect(result).toEqual({
29+
rules: [
30+
{
31+
ruleName: "@typescript-eslint/ban-types",
32+
ruleArguments: [
33+
{
34+
types: {
35+
Object: {
36+
message: "Use {} instead.",
37+
},
38+
String: null,
39+
Number: null,
40+
Boolean: {
41+
message: "Use 'boolean' instead.",
42+
},
43+
},
44+
},
45+
],
46+
},
47+
],
48+
});
49+
});
50+
51+
test("conversion with duplicated arguments", () => {
52+
const result = convertBanTypes({
53+
ruleArguments: [["Object", "Use {} instead."], ["Object"]],
54+
});
55+
56+
expect(result).toEqual({
57+
rules: [
58+
{
59+
ruleName: "@typescript-eslint/ban-types",
60+
ruleArguments: [
61+
{
62+
types: {
63+
Object: null,
64+
},
65+
},
66+
],
67+
},
68+
],
69+
});
70+
});
1771
});

0 commit comments

Comments
 (0)