英文:
Is it possible to disallow string enums via ESLint?
问题
ESLint是否可以配置来标记字符串enum
的声明?例如:
enum Foo {
Bar = "bar",
Baz = "baz",
}
这与no-mixed-enums
规则不同。我已经查看了no-restricted-syntax
,但据我所知,它不涵盖@typescript-eslint
规则。
一些背景信息:
- 枚举,特别是字符串枚举,通常被认为是反模式,如这个Stack Overflow帖子中简要阐述的那样。
- 目前,我希望允许普通的(未分配整数)枚举,所以我不希望对关键字本身进行代码检查。
- 任何标记枚举值的赋值的代码检查配置也将起作用。
最终,我希望所有字符串enum
声明都可以通过自动修复成为字符串联合type
,即:
type Foo = "bar" | "baz";
也许有插件可以帮助处理这些情况吗?
英文:
Can ESLint be configured to flag the declaration of a string enum
? E.g.:
enum Foo {
Bar = "bar",
Baz = "baz",
}
This is different from the no-mixed-enums
rule. I have looked into no-restricted-syntax
, but that doesn't cover @typescript-eslint
rules as far as I know.
Some context:
- Enums, particularly string enums are often considered anti-patterns, as is succinctly elaborated in this Stack Overflow post.
- For now I would like to allow plain (unassigned integer) enums, so I wouldn't want to lint against the keyword itself.
- Any linter config that flags any assignation of enum values would also work.
Eventually I would want all string enum
declarations to be auto-fixable by a string union type
, i.e.:
type Foo = "bar" | "baz";
Is there perhaps a plugin that can help with any of this?
答案1
得分: 2
{
"rules": {
"no-restricted-syntax": [
"error",
{
"selector": "TSEnumDeclaration > TSEnumMember > Literal",
"message": "Use a string union type instead."
}
]
}
}
enum test1 {
member11,
member12 = "test12",
}
enum test2 {
member21 = "test21",
member22 = "test22",
}
enum test3 {
member31 = 31,
member32,
}
enum test4 {
member41,
member42,
}
<details>
<summary>英文:</summary>
As [suggested][1] by @anothermh, drilling down into the AST of an `enum` with `@typescript-eslint/parser` reveals which nodes to use in the selector. Link to [gist][2].
The following rule works as intended:
```json
{
"rules": {
"no-restricted-syntax": [
"error",
{
"selector": "TSEnumDeclaration > TSEnumMember > Literal",
"message": "Use a string union type instead."
}
]
}
}
When linting, it will flag only the literal of a declaration with the defined message.
E.g.:
enum test1 {
member11,
member12 = "test12",
}
✗
enum test2 {
member21 = "test21",
member22 = "test22",
}
✗
enum test3 {
member31 = 31,
member32,
}
✗
enum test4 {
member41,
member42,
}
✓
This is sufficient for my purposes currently. Then there are ways to automate the fixing of these errors by replacing the entire expression (for another time).
答案2
得分: 1
{
"rules": {
"no-restricted-syntax": [
"warn",
{
"selector": "TSEnumDeclaration",
"message": "确保枚举不包含 StringLiteral 值"
}
]
}
}
这是你要翻译的内容。
英文:
In the simplest form you can use no-restricted-syntax
:
{
"rules": {
"no-restricted-syntax": [
"warn",
{
"selector": "TSEnumDeclaration",
"message": "Verify enums don't contain StringLiteral values"
}
]
}
}
It's much more complex to write a linter rule for specific types as evidenced by typescript-eslint/no-mixed-enums. My advice would be to start with evaluating that code if you want a specific ruleset that will disallow only StringLiteral values.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论