Is it possible to disallow string enums via ESLint?

huangapple go评论65阅读模式
英文:

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
{
  &quot;rules&quot;: {
    &quot;no-restricted-syntax&quot;: [
      &quot;error&quot;,
      {
        &quot;selector&quot;: &quot;TSEnumDeclaration &gt; TSEnumMember &gt; Literal&quot;,
        &quot;message&quot;: &quot;Use a string union type instead.&quot;
      }
    ]
  }
}

When linting, it will flag only the literal of a declaration with the defined message.

E.g.:

enum test1 {
  member11,
  member12 = &quot;test12&quot;,
}

&cross;

enum test2 {
  member21 = &quot;test21&quot;,
  member22 = &quot;test22&quot;,
}

&cross;

enum test3 {
  member31 = 31,
  member32,
}

&cross;

enum test4 {
  member41,
  member42,
}

&check;

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:

{
  &quot;rules&quot;: {
    &quot;no-restricted-syntax&quot;: [
      &quot;warn&quot;,
      {
        &quot;selector&quot;: &quot;TSEnumDeclaration&quot;,
        &quot;message&quot;: &quot;Verify enums don&#39;t contain StringLiteral values&quot;
      }
    ]
  }
}

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.

huangapple
  • 本文由 发表于 2023年4月13日 23:32:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定