UNPKG

8.26 kBMarkdownView Raw
1# Require a specific member delimiter style for interfaces and type literals (member-delimiter-style)
2
3Enforces a consistent member delimiter style in interfaces and type literals. There are three member delimiter styles primarily used in TypeScript:
4- Semicolon style (default, preferred in TypeScript).
5```ts
6interface Foo {
7 name: string;
8 greet(): void; // last semicolon can be ignored
9}
10
11type Bar = {
12 name: string;
13 greet(): void; // last semicolon can be ignored
14}
15```
16
17- Comma style (JSON style).
18```ts
19interface Foo {
20 name: string,
21 greet(): void, // last comma can be ignored
22}
23
24type Bar = {
25 name: string,
26 greet(): void, // last comma can be ignored
27}
28```
29
30- Linebreak style.
31```ts
32interface Foo {
33 name: string
34 greet(): void
35}
36
37type Bar = {
38 name: string
39 greet(): void
40}
41```
42
43The rule also enforces the presence of the delimiter in the last member of the interface and/or type literal, allowing single line declarations to be ignored.
44
45## Rule Details
46
47This rule aims to standardise the way interface and type literal members are delimited.
48
49## Options
50
51This rule, in its default state, does not require any argument, in which case a **semicolon** is used as a
52delimiter and **all members** require a delimiter, except in single line interfaces or type literals, in which case
53the delimiter of the **last member** can be omitted.
54
55The rule can also take one or more of the following options:
56- `"delimiter": "semi"`, (default) use this to require a semicolon.
57- `"delimiter": "comma"`, use this to require a comma.
58- `"delimiter": "none"`, use this to require a linebreak.
59- `"requireLast": true`, (default) use this to require a delimiter for all members of the interface and/or type literal.
60- `"requireLast": false`, use this to ignore the last member of the interface and/or type literal.
61- `"ignoreSingleLine": true`, (default) use this to override the `requireLast` in single line declarations.
62- `"ignoreSingleLine": false`, use this to enfore the `requireLast` in single line declarations.
63- `"overrides"`, overrides the default options for **interfaces** and **type literals**.
64
65### defaults
66Examples of **incorrect** code for this rule with the defaults
67`{ delimiter: "semi", requireLast: true, ignoreSingleLine: true }` or no option at all:
68```ts
69// missing semicolon delimiter
70interface Foo {
71 name: string
72 greet(): string
73}
74
75// using incorrect delimiter
76interface Bar {
77 name: string,
78 greet(): string,
79}
80
81// missing last member delimiter
82interface Baz {
83 name: string;
84 greet(): string
85}
86
87// missing semicolon delimiter
88type Foo = {
89 name: string
90 greet(): string
91}
92
93// using incorrect delimiter
94type Bar = {
95 name: string,
96 greet(): string,
97}
98
99// missing last member delimiter
100type Baz = {
101 name: string,
102 greet(): string
103}
104```
105
106Examples of **correct** code for this rule with the default
107`{ delimiter: "semi", requireLast: true, ignoreSingleLine: true }`:
108```ts
109interface Foo {
110 name: string;
111 greet(): string;
112}
113
114interface Foo { name: string }
115
116interface Foo { name: string; }
117
118type Bar = {
119 name: string;
120 greet(): string;
121}
122
123type Bar = { name: string }
124
125type Bar = { name: string; }
126```
127
128### delimiter - semi
129Examples of **incorrect** code for this rule with `{ delimiter: "semi" }`:
130```ts
131// missing semicolon delimiter
132interface Foo {
133 name: string
134 greet(): string
135}
136
137// using incorrect delimiter
138interface Bar {
139 name: string,
140 greet(): string,
141}
142```
143
144Examples of **correct** code for this rule with `{ delimiter: "semi" }`:
145```ts
146// with requireLast = true/false
147interface Foo {
148 name: string;
149 greet(): string;
150}
151
152type Bar = {
153 name: string;
154 greet(): string;
155}
156
157// with requireLast = false
158interface Foo {
159 name: string;
160 greet(): string
161}
162
163type Bar = {
164 name: string;
165 greet(): string
166}
167```
168
169### delimiter - comma
170Examples of **incorrect** code for this rule with `{ delimiter: "comma" }`:
171```ts
172// missing comma delimiter
173interface Foo {
174 name: string
175 greet(): string
176}
177
178// using incorrect delimiter
179interface Bar {
180 name: string;
181 greet(): string;
182}
183```
184
185Examples of **correct** code for this rule with `{ delimiter: "comma" }`:
186```ts
187// with requireLast = true/false
188interface Foo {
189 name: string,
190 greet(): string,
191}
192
193type Bar = {
194 name: string,
195 greet(): string,
196}
197
198// with requireLast = false
199interface Foo {
200 name: string,
201 greet(): string
202}
203
204type Bar = {
205 name: string,
206 greet(): string
207}
208```
209
210### delimiter - none
211Examples of **incorrect** code for this rule with `{ delimiter: "none" }`:
212```ts
213// using incorrect delimiter
214interface Foo {
215 name: string;
216 greet(): string;
217}
218
219// using incorrect delimiter
220interface Bar {
221 name: string,
222 greet(): string,
223}
224```
225
226Examples of **correct** code for this rule with `{ delimiter: "none" }`:
227```ts
228interface Foo {
229 name: string
230 greet(): string
231}
232
233type Bar = {
234 name: string
235 greet(): string
236}
237```
238
239### requireLast
240Examples of **incorrect** code for this rule with `{ requireLast: true }`:
241```ts
242// using incorrect delimiter
243interface Foo {
244 name: string;
245 greet(): string
246}
247
248// using incorrect delimiter
249type Bar = {
250 name: string,
251 greet(): string
252}
253```
254
255Examples of **correct** code for this rule with `{ requireLast: true }`:
256```ts
257interface Foo {
258 name: string;
259 greet(): string;
260}
261
262type Bar = {
263 name: string,
264 greet(): string,
265}
266```
267
268Examples of **correct** code for this rule with `{ requireLast: false }`:
269```ts
270interface Foo {
271 name: string
272 greet(): string
273}
274
275interface Bar {
276 name: string;
277 greet(): string
278}
279
280interface Baz {
281 name: string;
282 greet(): string;
283}
284
285type Foo = {
286 name: string
287 greet(): string
288}
289
290type Bar = {
291 name: string,
292 greet(): string
293}
294
295type Baz = {
296 name: string,
297 greet(): string,
298}
299```
300
301### ignoreSingleLine
302Examples of **incorrect** code for this rule with `{ ignoreSingleLine: true }`:
303```ts
304// using incorrect delimiter
305interface Foo { name: string, }
306
307// using incorrect delimiter
308type Bar = { name: string, }
309```
310
311Examples of **correct** code for this rule with `{ ignoreSingleLine: true }`:
312```ts
313// can have a delimiter or not
314interface Foo { name: string }
315
316interface Foo { name: string; }
317
318// can have a delimiter or not
319type Bar = { name: string }
320
321type Bar = { name: string; }
322```
323
324Examples of **incorrect** code for this rule with `{ ignoreSingleLine: false }`:
325```ts
326// missing delimiter
327interface Foo { name: string }
328
329// missing delimiter
330type Bar = { name: string }
331```
332
333Examples of **correct** code for this rule with `{ ignoreSingleLine: false }`:
334```ts
335interface Foo { name: string; }
336
337type Bar = { name: string; }
338```
339
340### overrides - interface
341Examples of **incorrect** code for this rule with `{ delimiter: "comma", requireLast: true, overrides: { interface: { delimiter: "semi" } } }`:
342```ts
343// expecting a semicolon
344interface Foo {
345 name: string,
346 greet(): string,
347}
348
349// this is fine, using default
350type Bar = {
351 name: string,
352 greet(): string,
353}
354```
355
356Examples of **correct** code for this rule with `{ delimiter: "comma", requireLast: true, overrides: { interface: { delimiter: "semi" } } }`:
357```ts
358// this is fine, using override
359interface Foo {
360 name: string;
361 greet(): string;
362}
363
364// this is fine, using default
365type Bar = {
366 name: string,
367 greet(): string,
368}
369```
370
371### overrides - typeLiteral
372Examples of **incorrect** code for this rule with `{ delimiter: "semi", requireLast: true, overrides: { typeLiteral: { delimiter: "comma", requireLast: false } } }`:
373```ts
374// this is fine, using default
375interface Foo {
376 name: string;
377 greet(): string;
378}
379
380// expecting a comma
381type Bar = {
382 name: string;
383 greet(): string
384}
385```
386
387Examples of **correct** code for this rule with `{ delimiter: "semi", requireLast: true, overrides: { typeLiteral: { delimiter: "comma", requireLast: false } } }`:
388```ts
389// this is fine, using default
390interface Foo {
391 name: string;
392 greet(): string;
393}
394
395// this is fine, using override
396type Bar = {
397 name: string,
398 greet(): string
399}
400```
401
402## When Not To Use It
403
404If you don't care about enforcing a consistent member delimiter in interfaces and type literals, then you will not need this rule.