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