Skip to content

Commit 3afd2d2

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 3afd2d2

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/rules/converters/ban-types.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
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+
const typ = rule[0];
12+
if (!typ) {
13+
break;
14+
}
15+
16+
bannedTypesObj[typ] = rule[1]
17+
? {
18+
message: rule[1],
19+
}
20+
: null;
21+
}
22+
23+
const ruleArguments =
24+
Object.keys(bannedTypesObj).length === 0 ? undefined : [{ types: bannedTypesObj }];
25+
426
return {
527
rules: [
628
{
729
ruleName: "@typescript-eslint/ban-types",
30+
...{ ruleArguments },
831
},
932
],
1033
};

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,59 @@ 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+
[], // empty array: this should be ignored
26+
],
27+
});
28+
29+
expect(result).toEqual({
30+
rules: [
31+
{
32+
ruleName: "@typescript-eslint/ban-types",
33+
ruleArguments: [
34+
{
35+
types: {
36+
Object: {
37+
message: "Use {} instead.",
38+
},
39+
String: null,
40+
Number: null,
41+
Boolean: {
42+
message: "Use 'boolean' instead.",
43+
},
44+
},
45+
},
46+
],
47+
},
48+
],
49+
});
50+
});
51+
52+
test("conversion with duplicated arguments", () => {
53+
const result = convertBanTypes({
54+
ruleArguments: [["Object", "Use {} instead."], ["Object"]],
55+
});
56+
57+
expect(result).toEqual({
58+
rules: [
59+
{
60+
ruleName: "@typescript-eslint/ban-types",
61+
ruleArguments: [
62+
{
63+
types: {
64+
Object: null,
65+
},
66+
},
67+
],
68+
},
69+
],
70+
});
71+
});
1772
});

0 commit comments

Comments
 (0)