Skip to content

Latest commit

 

History

History
404 lines (334 loc) · 8.03 KB

member-delimiter-style.md

File metadata and controls

404 lines (334 loc) · 8.03 KB

Enforces a member delimiter style in interfaces and type literals.

Enforces a consistent member delimiter style in interfaces and type literals. There are three member delimiter styles primarily used in TypeScript:

  • Semicolon style (default, preferred in TypeScript).
interface Foo {
    name: string;
    greet(): void; // last semicolon can be ignored
}

type Bar = {
    name: string;
    greet(): void; // last semicolon can be ignored
}
  • Comma style (JSON style).
interface Foo {
    name: string,
    greet(): void, // last comma can be ignored
}

type Bar = {
    name: string,
    greet(): void, // last comma can be ignored
}
  • Linebreak style.
interface Foo {
    name: string
    greet(): void
}

type Bar = {
    name: string
    greet(): void
}

The 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.

Rule Details

This rule aims to standardise the way interface and type literal members are delimited.

Options

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, except in single line interfaces or type literals, in which case the delimiter of the last member can be omitted.

The rule can also take one or more of the following options:

  • "delimiter": "semi", (default) use this to require a semicolon.
  • "delimiter": "comma", use this to require a comma.
  • "delimiter": "none", use this to require a linebreak.
  • "requireLast": true, (default) use this to require a delimiter for all members of the interface and/or type literal.
  • "requireLast": false, use this to ignore the last member of the interface and/or type literal.
  • "ignoreSingleLine": true, (default) use this to override the requireLast in single line declarations.
  • "ignoreSingleLine": false, use this to enfore the requireLast in single line declarations.
  • "overrides", overrides the default options for interfaces and type literals.

defaults

Examples of incorrect code for this rule with the defaults { delimiter: "semi", requireLast: true, ignoreSingleLine: true } or no option at all:

// missing semicolon delimiter
interface Foo {
    name: string
    greet(): string
}

// using incorrect delimiter
interface Bar {
    name: string,
    greet(): string,
}

// missing last member delimiter
interface Baz {
    name: string;
    greet(): string
}

// missing semicolon delimiter
type Foo = {
    name: string
    greet(): string
}

// using incorrect delimiter
type Bar = {
    name: string,
    greet(): string,
}

// missing last member delimiter
type Baz = {
    name: string,
    greet(): string
}

Examples of correct code for this rule with the default { delimiter: "semi", requireLast: true, ignoreSingleLine: true }:

interface Foo {
    name: string;
    greet(): string;
}

interface Foo { name: string }

interface Foo { name: string; }

type Bar = {
    name: string;
    greet(): string;
}

type Bar = { name: string }

type Bar = { name: string; }

delimiter - semi

Examples of incorrect code for this rule with { delimiter: "semi" }:

// missing semicolon delimiter
interface Foo {
    name: string
    greet(): string
}

// using incorrect delimiter
interface Bar {
    name: string,
    greet(): string,
}

Examples of correct code for this rule with { delimiter: "semi" }:

// with requireLast = true/false
interface Foo {
    name: string;
    greet(): string;
}

type Bar = {
    name: string;
    greet(): string;
}

// with requireLast = false
interface Foo {
    name: string;
    greet(): string
}

type Bar = {
    name: string;
    greet(): string
}

delimiter - comma

Examples of incorrect code for this rule with { delimiter: "comma" }:

// missing comma delimiter
interface Foo {
    name: string
    greet(): string
}

// using incorrect delimiter
interface Bar {
    name: string;
    greet(): string;
}

Examples of correct code for this rule with { delimiter: "comma" }:

// with requireLast = true/false
interface Foo {
    name: string,
    greet(): string,
}

type Bar = {
    name: string,
    greet(): string,
}

// with requireLast = false
interface Foo {
    name: string,
    greet(): string
}

type Bar = {
    name: string,
    greet(): string
}

delimiter - none

Examples of incorrect code for this rule with { delimiter: "none" }:

// using incorrect delimiter
interface Foo {
    name: string;
    greet(): string;
}

// using incorrect delimiter
interface Bar {
    name: string,
    greet(): string,
}

Examples of correct code for this rule with { delimiter: "none" }:

interface Foo {
    name: string
    greet(): string
}

type Bar = {
    name: string
    greet(): string
}

requireLast

Examples of incorrect code for this rule with { requireLast: true }:

// using incorrect delimiter
interface Foo {
    name: string;
    greet(): string
}

// using incorrect delimiter
type Bar = {
    name: string,
    greet(): string
}

Examples of correct code for this rule with { requireLast: true }:

interface Foo {
    name: string;
    greet(): string;
}

type Bar = {
    name: string,
    greet(): string,
}

Examples of correct code for this rule with { requireLast: false }:

interface Foo {
    name: string
    greet(): string
}

interface Bar {
    name: string;
    greet(): string
}

interface Baz {
    name: string;
    greet(): string;
}

type Foo = {
    name: string
    greet(): string
}

type Bar = {
    name: string,
    greet(): string
}

type Baz = {
    name: string,
    greet(): string,
}

ignoreSingleLine

Examples of incorrect code for this rule with { ignoreSingleLine: true }:

// using incorrect delimiter
interface Foo { name: string, }

// using incorrect delimiter
type Bar = { name: string, }

Examples of correct code for this rule with { ignoreSingleLine: true }:

// can have a delimiter or not
interface Foo { name: string }

interface Foo { name: string; }

// can have a delimiter or not
type Bar = { name: string }

type Bar = { name: string; }

Examples of incorrect code for this rule with { ignoreSingleLine: false }:

// missing delimiter
interface Foo { name: string }

// missing delimiter
type Bar = { name: string }

Examples of correct code for this rule with { ignoreSingleLine: false }:

interface Foo { name: string; }

type Bar = { name: string; }

overrides - interface

Examples of incorrect code for this rule with { delimiter: "comma", requireLast: true, overrides: { interface: { delimiter: "semi" } } }:

// expecting a semicolon
interface Foo {
    name: string,
    greet(): string,
}

// this is fine, using default
type Bar = {
    name: string,
    greet(): string,
}

Examples of correct code for this rule with { delimiter: "comma", requireLast: true, overrides: { interface: { delimiter: "semi" } } }:

// this is fine, using override
interface Foo {
    name: string;
    greet(): string;
}

// this is fine, using default
type Bar = {
    name: string,
    greet(): string,
}

overrides - typeLiteral

Examples of incorrect code for this rule with { delimiter: "semi", requireLast: true, overrides: { typeLiteral: { delimiter: "comma", requireLast: false } } }:

// this is fine, using default
interface Foo {
    name: string;
    greet(): string;
}

// expecting a comma
type Bar = {
    name: string;
    greet(): string
}

Examples of correct code for this rule with { delimiter: "semi", requireLast: true, overrides: { typeLiteral: { delimiter: "comma", requireLast: false } } }:

// this is fine, using default
interface Foo {
    name: string;
    greet(): string;
}

// this is fine, using override
type Bar = {
    name: string,
    greet(): string
}

When Not To Use It

If you don't care about enforcing a consistent member delimiter in interfaces and type literals, then you will not need this rule.