Skip to content

Commit abb17d2

Browse files
deerawanJosh Goldberg
authored and
Josh Goldberg
committed
add converter trailing-comma (#242)
* add converter trailing-comma and unit test (#186) * add more typings and display notices
1 parent e453505 commit abb17d2

File tree

3 files changed

+539
-0
lines changed

3 files changed

+539
-0
lines changed

src/rules/converters.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ import { convertSpaceBeforeFunctionParen } from "./converters/space-before-funct
118118
import { convertSpaceWithinParens } from "./converters/space-within-parens";
119119
import { convertStrictBooleanExpressions } from "./converters/strict-boolean-expressions";
120120
import { convertSwitchDefault } from "./converters/switch-default";
121+
import { convertTrailingComma } from "./converters/trailing-comma";
121122
import { convertTripleEquals } from "./converters/triple-equals";
122123
import { convertTypedefWhitespace } from "./converters/typedef-whitespace";
123124
import { convertTypeLiteralDelimiter } from "./converters/type-literal-delimiter";
@@ -253,6 +254,7 @@ export const converters = new Map([
253254
["space-within-parens", convertSpaceWithinParens],
254255
["strict-boolean-expressions", convertStrictBooleanExpressions],
255256
["switch-default", convertSwitchDefault],
257+
["trailing-comma", convertTrailingComma],
256258
["triple-equals", convertTripleEquals],
257259
["type-literal-delimiter", convertTypeLiteralDelimiter],
258260
["typedef-whitespace", convertTypedefWhitespace],
Lines changed: 359 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,359 @@
1+
import { convertTrailingComma } from "../trailing-comma";
2+
3+
describe(convertTrailingComma, () => {
4+
test("conversion without arguments", () => {
5+
const result = convertTrailingComma({
6+
ruleArguments: [],
7+
});
8+
9+
expect(result).toEqual({
10+
rules: [
11+
{
12+
ruleName: "comma-dangle",
13+
},
14+
],
15+
});
16+
});
17+
18+
describe("conversion with arguments using string values", () => {
19+
const testCases = [
20+
{
21+
argument: {
22+
singleline: "never",
23+
},
24+
expectedRuleArguments: [],
25+
},
26+
{
27+
argument: {
28+
singleline: "always",
29+
},
30+
expectedRuleArguments: [],
31+
},
32+
{
33+
argument: {
34+
multiline: "never",
35+
},
36+
expectedRuleArguments: [],
37+
},
38+
{
39+
argument: {
40+
multiline: "always",
41+
},
42+
expectedRuleArguments: ["always-multiline"],
43+
},
44+
{
45+
argument: {
46+
singleline: "never",
47+
multiline: "never",
48+
},
49+
expectedRuleArguments: [],
50+
},
51+
{
52+
argument: {
53+
singleline: "never",
54+
multiline: "always",
55+
},
56+
expectedRuleArguments: ["always-multiline"],
57+
},
58+
{
59+
argument: {
60+
singleline: "always",
61+
multiline: "never",
62+
},
63+
expectedRuleArguments: [],
64+
},
65+
{
66+
argument: {
67+
singleline: "always",
68+
multiline: "always",
69+
},
70+
expectedRuleArguments: ["always"],
71+
},
72+
];
73+
74+
testCases.forEach(testCase => {
75+
test(`conversion with arguments ${JSON.stringify(testCase.argument)}`, () => {
76+
const result = convertTrailingComma({
77+
ruleArguments: [testCase.argument],
78+
});
79+
80+
expect(result).toEqual({
81+
rules: [
82+
{
83+
ruleName: "comma-dangle",
84+
...(testCase.expectedRuleArguments.length !== 0 && {
85+
ruleArguments: testCase.expectedRuleArguments,
86+
}),
87+
},
88+
],
89+
});
90+
});
91+
});
92+
});
93+
94+
describe("conversion with arguments using object values", () => {
95+
const testCases = [
96+
{
97+
argument: {
98+
singleline: "never",
99+
multiline: {
100+
objects: "always",
101+
arrays: "always",
102+
functions: "always",
103+
imports: "always",
104+
exports: "always",
105+
},
106+
},
107+
expectedRuleArguments: [
108+
{
109+
arrays: "always-multiline",
110+
objects: "always-multiline",
111+
functions: "always-multiline",
112+
imports: "always-multiline",
113+
exports: "always-multiline",
114+
},
115+
],
116+
},
117+
{
118+
argument: {
119+
singleline: "always",
120+
multiline: {
121+
objects: "always",
122+
arrays: "always",
123+
functions: "always",
124+
imports: "always",
125+
exports: "always",
126+
},
127+
},
128+
expectedRuleArguments: [
129+
{
130+
arrays: "always",
131+
objects: "always",
132+
functions: "always",
133+
imports: "always",
134+
exports: "always",
135+
},
136+
],
137+
},
138+
{
139+
argument: {
140+
singleline: {
141+
objects: "never",
142+
arrays: "never",
143+
functions: "never",
144+
imports: "never",
145+
exports: "never",
146+
},
147+
},
148+
expectedRuleArguments: [
149+
{
150+
arrays: "never",
151+
objects: "never",
152+
functions: "never",
153+
imports: "never",
154+
exports: "never",
155+
},
156+
],
157+
},
158+
{
159+
argument: {
160+
singleline: {
161+
objects: "never",
162+
arrays: "never",
163+
functions: "never",
164+
},
165+
multiline: {
166+
objects: "never",
167+
arrays: "never",
168+
functions: "never",
169+
},
170+
},
171+
expectedRuleArguments: [
172+
{
173+
arrays: "never",
174+
objects: "never",
175+
functions: "never",
176+
},
177+
],
178+
},
179+
{
180+
argument: {
181+
multiline: {
182+
objects: "always",
183+
arrays: "always",
184+
functions: "always",
185+
imports: "always",
186+
exports: "always",
187+
},
188+
},
189+
expectedRuleArguments: [
190+
{
191+
arrays: "always-multiline",
192+
objects: "always-multiline",
193+
functions: "always-multiline",
194+
imports: "always-multiline",
195+
exports: "always-multiline",
196+
},
197+
],
198+
},
199+
{
200+
argument: {
201+
singleline: {
202+
objects: "always",
203+
arrays: "always",
204+
functions: "always",
205+
imports: "always",
206+
exports: "always",
207+
},
208+
multiline: {
209+
objects: "always",
210+
arrays: "always",
211+
functions: "always",
212+
imports: "always",
213+
exports: "always",
214+
},
215+
},
216+
expectedRuleArguments: [
217+
{
218+
arrays: "always",
219+
objects: "always",
220+
functions: "always",
221+
imports: "always",
222+
exports: "always",
223+
},
224+
],
225+
},
226+
{
227+
argument: {
228+
singleline: {
229+
objects: "always",
230+
arrays: "always",
231+
},
232+
multiline: {
233+
objects: "always",
234+
arrays: "always",
235+
functions: "always",
236+
},
237+
},
238+
expectedRuleArguments: [
239+
{
240+
arrays: "always",
241+
objects: "always",
242+
functions: "always-multiline",
243+
},
244+
],
245+
},
246+
];
247+
248+
testCases.forEach(testCase => {
249+
test(`conversion with arguments ${JSON.stringify(testCase.argument)}`, () => {
250+
const result = convertTrailingComma({
251+
ruleArguments: [testCase.argument],
252+
});
253+
254+
expect(result).toEqual({
255+
rules: [
256+
{
257+
ruleName: "comma-dangle",
258+
...(testCase.expectedRuleArguments.length && {
259+
ruleArguments: testCase.expectedRuleArguments,
260+
}),
261+
},
262+
],
263+
});
264+
});
265+
});
266+
});
267+
268+
describe("conversion with not supported config", () => {
269+
const testCases = [
270+
{
271+
argument: {
272+
esSpecCompliant: true,
273+
},
274+
expectedRuleArguments: [],
275+
expectedNotices: ["ESLint does not support config property esSpecCompliant"],
276+
},
277+
{
278+
argument: {
279+
singleline: {
280+
typeLiterals: "ignore",
281+
},
282+
},
283+
expectedRuleArguments: [{}],
284+
expectedNotices: ["ESLint does not support config property typeLiterals"],
285+
},
286+
{
287+
argument: {
288+
multiline: {
289+
typeLiterals: "ignore",
290+
},
291+
},
292+
expectedRuleArguments: [{}],
293+
expectedNotices: ["ESLint does not support config property typeLiterals"],
294+
},
295+
{
296+
argument: {
297+
esSpecCompliant: true,
298+
singleline: {
299+
typeLiterals: "always",
300+
},
301+
},
302+
expectedRuleArguments: [{}],
303+
expectedNotices: [
304+
"ESLint does not support config property esSpecCompliant",
305+
"ESLint does not support config property typeLiterals",
306+
],
307+
},
308+
{
309+
argument: {
310+
esSpecCompliant: false,
311+
multiline: {
312+
typeLiterals: "always-multiline",
313+
},
314+
},
315+
expectedRuleArguments: [{}],
316+
expectedNotices: [
317+
"ESLint does not support config property esSpecCompliant",
318+
"ESLint does not support config property typeLiterals",
319+
],
320+
},
321+
{
322+
argument: {
323+
esSpecCompliant: false,
324+
singleline: {
325+
typeLiterals: "ignore",
326+
},
327+
multiline: {
328+
typeLiterals: "ignore",
329+
},
330+
},
331+
expectedRuleArguments: [{}],
332+
expectedNotices: [
333+
"ESLint does not support config property esSpecCompliant",
334+
"ESLint does not support config property typeLiterals",
335+
],
336+
},
337+
];
338+
339+
testCases.forEach(testCase => {
340+
test(`conversion with arguments ${JSON.stringify(testCase.argument)}`, () => {
341+
const result = convertTrailingComma({
342+
ruleArguments: [testCase.argument],
343+
});
344+
345+
expect(result).toEqual({
346+
rules: [
347+
{
348+
ruleName: "comma-dangle",
349+
...(testCase.expectedRuleArguments.length && {
350+
ruleArguments: testCase.expectedRuleArguments,
351+
}),
352+
notices: testCase.expectedNotices,
353+
},
354+
],
355+
});
356+
});
357+
});
358+
});
359+
});

0 commit comments

Comments
 (0)