Skip to content

Commit 255d807

Browse files
authored
Respect the original TSLint configuration of ban-types (#353)
* 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 * Use the Record type instead of traditional dict decralation Fix: #353 (comment) * Simplify a variable name for banned-types Fix: #353 (comment) * Rename an unclear variable name for banned-type to self-descriptive name Fix: #353 (comment) * Use short-hand notation for variable substitution Fix: #353 (comment) * Check whether a rule is array object or not on `ban-types` converter
1 parent 5051f98 commit 255d807

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-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 bannedTypes: Record<string, ConvertBanTypeArgument | null> = {};
9+
10+
for (const rule of tslintRule.ruleArguments) {
11+
if (!Array.isArray(rule)) {
12+
break;
13+
}
14+
15+
const [bannedType, message] = rule;
16+
if (!bannedType) {
17+
break;
18+
}
19+
20+
bannedTypes[bannedType] = message ? { message } : null;
21+
}
22+
23+
const ruleArguments =
24+
Object.keys(bannedTypes).length === 0 ? undefined : [{ types: bannedTypes }];
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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,73 @@ 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+
});
72+
73+
test("conversion with duplicated arguments", () => {
74+
const result = convertBanTypes({
75+
ruleArguments: ["!!!this-is-not-array!!!"],
76+
});
77+
78+
expect(result).toEqual({
79+
rules: [
80+
{
81+
ruleName: "@typescript-eslint/ban-types",
82+
},
83+
],
84+
});
85+
});
1786
});

0 commit comments

Comments
 (0)