Skip to content

feat(eslint-plugin): add "enum-const-style" rule #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ Pro Tip: For larger codebases you may want to consider splitting our linting int
| [`@typescript-eslint/ban-types`](./docs/rules/ban-types.md) | Bans specific types from being used | :heavy_check_mark: | :wrench: | |
| [`@typescript-eslint/consistent-type-assertions`](./docs/rules/consistent-type-assertions.md) | Enforces consistent usage of type assertions | :heavy_check_mark: | | |
| [`@typescript-eslint/consistent-type-definitions`](./docs/rules/consistent-type-definitions.md) | Consistent with type definition either `interface` or `type` | | :wrench: | |
| [`@typescript-eslint/enum-const-style`](./docs/rules/enum-const-style.md) | Enforce const enum style | | | |
| [`@typescript-eslint/explicit-function-return-type`](./docs/rules/explicit-function-return-type.md) | Require explicit return types on functions and class methods | :heavy_check_mark: | | |
| [`@typescript-eslint/explicit-member-accessibility`](./docs/rules/explicit-member-accessibility.md) | Require explicit accessibility modifiers on class properties and methods | | | |
| [`@typescript-eslint/explicit-module-boundary-types`](./docs/rules/explicit-module-boundary-types.md) | Require explicit return and argument types on exported functions' and classes' public class methods | | | |
Expand Down
61 changes: 61 additions & 0 deletions packages/eslint-plugin/docs/rules/enum-const-style.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Enforce const enum style (`enum-const-style`)

This rule enforces consistent usage of `const enum`.

## Rule Details

This rule aims to standardize usage of const enums.

## Options

### Default config: `never`

```JSON
{
"enum-const-style": ["error", "never"]
}
```

Examples of **incorrect** code for this rule with `never` config:

```ts
enum Foo {
ONE,
TWO,
}
```

Examples of **correct** code for this rule with `never` config:

```ts
const enum Foo {
ONE,
TWO,
}
```

### `always` config

Only const enums are allowed

Examples of **incorrect** code for this rule with `always` config:

```ts
enum const Foo {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
enum const Foo {
const enum Foo {

ONE,
TWO,
}
```

Examples of **correct** code for this rule with `always` config:

```ts
const Foo {
ONE,
TWO,
}
```
Comment on lines +11 to +57
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the examples are the wrong way around

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i haven't read docs, but i did small refactoring in rule source code


## When Not To Use It

If you don't want to regulate usage of `const enum`s.
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/configs/all.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@typescript-eslint/consistent-type-definitions": "error",
"default-param-last": "off",
"@typescript-eslint/default-param-last": "error",
"@typescript-eslint/enum-const-style": "error",
"@typescript-eslint/explicit-function-return-type": "error",
"@typescript-eslint/explicit-member-accessibility": "error",
"@typescript-eslint/explicit-module-boundary-types": "error",
Expand Down
44 changes: 44 additions & 0 deletions packages/eslint-plugin/src/rules/enum-const-style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { TSESTree } from '@typescript-eslint/experimental-utils';
import * as util from '../util';

type Messages = 'noNonConstEnums' | 'noConstEnums';
type Options = ['always' | 'never'];

export default util.createRule<Options, Messages>({
name: 'enum-const-style',
meta: {
type: 'problem',
docs: {
description: 'Enforce const enum style',
category: 'Stylistic Issues',
recommended: false,
},
messages: {
noNonConstEnums: 'enums are forbidden. Use const enums',
noConstEnums: 'const enums are forbidden. Use enum',
},
schema: [
{
enum: ['always', 'never'],
},
],
},
defaultOptions: ['never'],
create(context, [options]) {
return {
TSEnumDeclaration(node: TSESTree.TSEnumDeclaration): void {
if (options === 'always' && !node.const) {
context.report({
node,
messageId: 'noNonConstEnums',
});
} else if (options === 'never' && node.const) {
context.report({
node,
messageId: 'noConstEnums',
});
}
},
};
},
});
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import classNameCasing from './class-name-casing';
import consistentTypeAssertions from './consistent-type-assertions';
import consistentTypeDefinitions from './consistent-type-definitions';
import defaultParamLast from './default-param-last';
import enumConstStyle from './enum-const-style';
import explicitFunctionReturnType from './explicit-function-return-type';
import explicitMemberAccessibility from './explicit-member-accessibility';
import explicitModuleBoundaryTypes from './explicit-module-boundary-types';
Expand Down Expand Up @@ -92,6 +93,7 @@ export default {
'consistent-type-assertions': consistentTypeAssertions,
'consistent-type-definitions': consistentTypeDefinitions,
'default-param-last': defaultParamLast,
'enum-const-style': enumConstStyle,
'explicit-function-return-type': explicitFunctionReturnType,
'explicit-member-accessibility': explicitMemberAccessibility,
'explicit-module-boundary-types': explicitModuleBoundaryTypes,
Expand Down
81 changes: 81 additions & 0 deletions packages/eslint-plugin/tests/rules/enum-const-style.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import rule from '../../src/rules/enum-const-style';
import { RuleTester } from '../RuleTester';

const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
});

ruleTester.run('enum-style', rule, {
valid: [
'enum Foo {}',
'enum Foo { FOO }',
'enum Foo { FOO = 1, BAR = 2 }',
'enum Foo { FOO = "FOO", BAR = "BAR" }',
{
code: 'const enum Foo {}',
options: ['always'],
},
{
code: 'const enum Foo { FOO }',
options: ['always'],
},
{
code: 'const enum Foo { FOO = 1, BAR = 2 }',
options: ['always'],
},
{
code: 'const enum Foo { FOO = "FOO", BAR = "BAR" }',
options: ['always'],
},
{
code: 'enum Foo {}',
options: ['never'],
},
{
code: 'enum Foo { FOO }',
options: ['never'],
},
{
code: 'enum Foo { FOO = 1, BAR = 2 }',
options: ['never'],
},
{
code: 'enum Foo { FOO = "FOO", BAR = "BAR" }',
options: ['never'],
},
],
invalid: [
{
code: 'const enum Foo {}',
errors: [
{
messageId: 'noConstEnums',
line: 1,
column: 1,
},
],
},
{
code: 'const enum Foo {}',
errors: [
{
messageId: 'noConstEnums',
line: 1,
column: 1,
},
],
options: ['never'],
},
{
code: 'enum Foo {}',
errors: [
{
messageId: 'noNonConstEnums',
line: 1,
column: 1,
},
],
options: ['always'],
},
],
});