-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4b1292a
Add enum-const-style rule
dc63ba0
Merge branch 'master' of git://github.com/typescript-eslint/typescrip…
3715b5f
fix(eslint-plugin) fix coverage
ffccf2a
Merge branch 'master' into mohsen1--enum-style
armano2 1b54a47
fix: add rule to docs, and configs
armano2 5c9a28f
Merge branch 'master' into mohsen1--enum-style
armano2 59874c8
test: add missing test cases
armano2 e69261e
fix: correct linting issue
armano2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
ONE, | ||
TWO, | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule with `always` config: | ||
|
||
```ts | ||
const Foo { | ||
ONE, | ||
TWO, | ||
} | ||
``` | ||
Comment on lines
+11
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the examples are the wrong way around There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
packages/eslint-plugin/tests/rules/enum-const-style.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
}, | ||
], | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.