Skip to content

Commit 07be509

Browse files
da-snapJosh Goldberg
authored and
Josh Goldberg
committed
Add missing converter: file-name-casing (#204)
* Add missing converter: file-name-casing Linked to issue: #161 * Add test case for coverage * Fix linting issues * Fixes after review. #161 * Add notice for changed behavior * Change map to object * Correct notice about the 'ignore' case * Specify map typing
1 parent 8170793 commit 07be509

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

src/rules/converters.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { convertCurly } from "./converters/curly";
1313
import { convertCyclomaticComplexity } from "./converters/cyclomatic-complexity";
1414
import { convertEofline } from "./converters/eofline";
1515
import { convertMemberAccess } from "./converters/member-access";
16+
import { convertFileNameCasing } from "./converters/file-name-casing";
1617
import { convertForin } from "./converters/forin";
1718
import { convertFunctionConstructor } from "./converters/function-constructor";
1819
import { convertIncrementDecrement } from "./converters/increment-decrement";
@@ -128,6 +129,7 @@ export const converters = new Map([
128129
["curly", convertCurly],
129130
["cyclomatic-complexity", convertCyclomaticComplexity],
130131
["eofline", convertEofline],
132+
["file-name-casing", convertFileNameCasing],
131133
["forin", convertForin],
132134
["function-constructor", convertFunctionConstructor],
133135
["increment-decrement", convertIncrementDecrement],
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { RuleConverter } from "../converter";
2+
3+
const IGNORE_CASE_NOTICE = "ESLint (Unicorn plugin) does not support the 'ignore' case.";
4+
const CASING_BY_FILETYPE_CHANGE =
5+
"ESLint (Unicorn Plugin) does not support file name casing by file type, so all previously configured casings are now allowed.";
6+
const CASES_MAP: { [s: string]: string } = {
7+
"camel-case": "camelCase",
8+
"pascal-case": "pascalCase",
9+
"kebab-case": "kebabCase",
10+
"snake-case": "snakeCase",
11+
};
12+
13+
export const convertFileNameCasing: RuleConverter = tslintRule => {
14+
return {
15+
rules: [
16+
{
17+
ruleName: "unicorn/filename-case",
18+
...collectArguments(tslintRule.ruleArguments),
19+
},
20+
],
21+
plugins: ["unicorn"],
22+
};
23+
};
24+
25+
const collectArguments = (ruleArguments: any[]) => {
26+
const notices: string[] = [];
27+
const foundCases: { [k: string]: boolean } = {};
28+
29+
if (ruleArguments.length === 0 || ruleArguments[0] === false || ruleArguments.length < 2) {
30+
return undefined;
31+
}
32+
33+
const casings = ruleArguments[1];
34+
if (typeof casings === "string") {
35+
if (casings === "ignore") {
36+
notices.push(IGNORE_CASE_NOTICE);
37+
} else {
38+
foundCases[CASES_MAP[casings]] = true;
39+
}
40+
}
41+
42+
if (ruleArguments[1] instanceof Object) {
43+
notices.push(CASING_BY_FILETYPE_CHANGE);
44+
for (const casing in casings) {
45+
if (casings[casing] === "ignore") {
46+
notices.push(IGNORE_CASE_NOTICE);
47+
} else {
48+
foundCases[CASES_MAP[casings[casing]]] = true;
49+
}
50+
}
51+
}
52+
53+
return {
54+
...(notices.length > 0 && { notices }),
55+
ruleArguments: [
56+
{
57+
cases: foundCases,
58+
},
59+
],
60+
};
61+
};
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { convertFileNameCasing } from "../file-name-casing";
2+
3+
describe(convertFileNameCasing, () => {
4+
test("conversion without parameter", () => {
5+
const result = convertFileNameCasing({
6+
ruleArguments: [],
7+
});
8+
9+
expect(result).toEqual({
10+
rules: [
11+
{
12+
ruleName: "unicorn/filename-case",
13+
},
14+
],
15+
plugins: ["unicorn"],
16+
});
17+
});
18+
19+
test("conversion without string value", () => {
20+
const result = convertFileNameCasing({
21+
ruleArguments: [true, "camel-case"],
22+
});
23+
24+
expect(result).toEqual({
25+
rules: [
26+
{
27+
ruleName: "unicorn/filename-case",
28+
ruleArguments: [
29+
{
30+
cases: {
31+
camelCase: true,
32+
},
33+
},
34+
],
35+
},
36+
],
37+
plugins: ["unicorn"],
38+
});
39+
});
40+
41+
test("conversion with object for filetypes", () => {
42+
const result = convertFileNameCasing({
43+
ruleArguments: [true, { ".ts": "ignore", ".tsx": "pascal-case", ".js": "snake-case" }],
44+
});
45+
46+
expect(result).toEqual({
47+
rules: [
48+
{
49+
notices: [
50+
"ESLint (Unicorn Plugin) does not support file name casing by file type, so all previously configured casings are now allowed.",
51+
"ESLint (Unicorn plugin) does not support the 'ignore' case.",
52+
],
53+
ruleName: "unicorn/filename-case",
54+
ruleArguments: [
55+
{
56+
cases: {
57+
snakeCase: true,
58+
pascalCase: true,
59+
},
60+
},
61+
],
62+
},
63+
],
64+
plugins: ["unicorn"],
65+
});
66+
});
67+
68+
test("conversion with ignore as case", () => {
69+
const result = convertFileNameCasing({
70+
ruleArguments: [true, "ignore"],
71+
});
72+
73+
expect(result).toEqual({
74+
rules: [
75+
{
76+
notices: ["ESLint (Unicorn plugin) does not support the 'ignore' case."],
77+
ruleName: "unicorn/filename-case",
78+
ruleArguments: [
79+
{
80+
cases: {},
81+
},
82+
],
83+
},
84+
],
85+
plugins: ["unicorn"],
86+
});
87+
});
88+
89+
test("conversion with ignore in object", () => {
90+
const result = convertFileNameCasing({
91+
ruleArguments: [true, { ".ts": "ignore" }],
92+
});
93+
94+
expect(result).toEqual({
95+
rules: [
96+
{
97+
notices: [
98+
"ESLint (Unicorn Plugin) does not support file name casing by file type, so all previously configured casings are now allowed.",
99+
"ESLint (Unicorn plugin) does not support the 'ignore' case.",
100+
],
101+
ruleName: "unicorn/filename-case",
102+
ruleArguments: [
103+
{
104+
cases: {},
105+
},
106+
],
107+
},
108+
],
109+
plugins: ["unicorn"],
110+
});
111+
});
112+
});

0 commit comments

Comments
 (0)