使用@typescript-eslint/naming-convention是否可以选择非静态类属性?

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

Using @typescript-eslint/naming-convention is it possible to select class properties that are not static?

问题

以下是代码部分的中文翻译:

export class ConsentOptionsWidget {
  public static NULL_CATEGORY_PLACEHOLDER = '选择订阅频道';
  public static EMAIL_CHANNEL = '电子邮件';
  public header_name!: string;
  public consentStatement!: string;
}
"@typescript-eslint/naming-convention": [
  "error",
  {
    "format": ["snake_case"],
    "modifiers": ["public"],
    "selector": "classProperty"
  }
]

我已经为您翻译了代码部分,如有需要,请告诉我是否还需要其他帮助。

英文:

I have the following typescript:

export class ConsentOptionsWidget {
  public static NULL_CATEGORY_PLACEHOLDER = 'Opt in to channel';
  public static EMAIL_CHANNEL = 'Email';
  public header_name!: string;
  public consentStatement!: string;
}

and my eslint rule:

"@typescript-eslint/naming-convention": [
  "error",
  {
    "format": ["snake_case"],
    "modifiers": ["public"],
    "selector": "classProperty"
  }
]

I only want the rule to apply to header_name and consentStatement with consentStatement generating an error. But, as is, consentStatement, NULL_CATEGORY_PLACEHOLDER and EMAIL_CHANNEL generate errors.

According to the project maintainers, even though the docs say "The name must match all of the modifiers" modifiers is actually a superset so public matches anything with public regardless of any other modifiers.

答案1

得分: 1

You could add another descriptor with no format requirement for public static properties.

"@typescript-eslint/naming-convention": [
  "error",
  {
    "format": ["snake_case"],
    "modifiers": ["public"],
    "selector": "classProperty"
  },
  {
    "modifiers": ["public", "static"],
    "selector": "classProperty"
  }
]

DEMO LINK

英文:

You could add another descriptor with no format requirement for public static properties.

"@typescript-eslint/naming-convention": [
  "error",
  {
    "format": ["snake_case"],
    "modifiers": ["public"],
    "selector": "classProperty"
  },
  {
    "modifiers": ["public", "static"],
    "selector": "classProperty"
  }
]

DEMO LINK

huangapple
  • 本文由 发表于 2023年4月4日 06:38:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924226.html
匿名

发表评论

匿名网友

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

确定