Skip to content

Commit

Permalink
feat(eslint-plugin): Improve "interface-name-prefix" to accept a priv…
Browse files Browse the repository at this point in the history
…ate name such as "_IAnimal"
  • Loading branch information
octogonz committed Aug 1, 2019
1 parent 73f8c79 commit 62b2b1f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
37 changes: 32 additions & 5 deletions packages/eslint-plugin/docs/rules/interface-name-prefix.md
@@ -1,18 +1,21 @@
# Require that interface names be prefixed with `I` (interface-name-prefix)

It can be hard to differentiate between classes and interfaces.
Prefixing interfaces with "I" can help telling them apart at a glance.
Interfaces often represent important software contracts, so it can be helpful to prefix their names with `I`.
The unprefixed name is then available for a class that provides a standard implementation of the interface.

## Rule Details

This rule enforces consistency of interface naming prefix conventions.
This rule enforces whether or not the `I` prefix is required for interface names.

## Options

This rule has a string option.

- `"never"` (default) disallows all interfaces being prefixed with `"I"`
- `"always"` requires all interfaces be prefixed with `"I"`
- `"never"` (default) disallows all interfaces being prefixed with `"I"` (or `"_I"`)
- `"always"` requires all interfaces be prefixed with `"I"` (or `"_I"`)

The `_` prefix is sometimes used to designate a private declaration, in which case a private interface might be
named `_IAnimal` instead of `IAnimal`. The rule recognizes both forms.

### never

Expand All @@ -24,6 +27,14 @@ The following patterns are considered warnings:
interface IAnimal {
name: string;
}

interface _IAnimal {
name: string;
}

interface IIguana {
name: string;
}
```

The following patterns are not warnings:
Expand All @@ -32,6 +43,10 @@ The following patterns are not warnings:
interface Animal {
name: string;
}

interface Iguana {
name: string;
}
```

### always
Expand All @@ -42,6 +57,10 @@ The following patterns are considered warnings:
interface Animal {
name: string;
}

interface Iguana {
name: string;
}
```

The following patterns are not warnings:
Expand All @@ -50,6 +69,14 @@ The following patterns are not warnings:
interface IAnimal {
name: string;
}

interface _IAnimal {
name: string;
}

interface IIguana {
name: string;
}
```

## When Not To Use It
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/interface-name-prefix.ts
Expand Up @@ -35,7 +35,7 @@ export default util.createRule<Options, MessageIds>({
return false;
}

return /^I[A-Z]/.test(name);
return /^_?I[A-Z]/.test(name);
}

return {
Expand Down
Expand Up @@ -22,6 +22,14 @@ interface IAnimal {
},
{
code: `
interface _IAnimal {
name: string;
}
`,
options: ['always'],
},
{
code: `
interface IIguana {
name: string;
}
Expand Down

0 comments on commit 62b2b1f

Please sign in to comment.