我在 angular2+ 中使用 lint,如何按字母顺序对构造函数参数进行排序?

I am using lint with angular2+, How can I sort the constructor parameters alphabetically?

我想在这里对构造函数参数进行排序。我现在正在将 TSLINT 与 Angular9 一起使用。有没有会员排序之类的规则可以使用

constructor(
    // Sort these
    private readonly router: Router,
    private readonly _changeDetector: ChangeDetectorRef,
    private readonly favoritesService: FavoritesService,
    private readonly jsonService: JsonService,
    private readonly messageService: MessageService,
    public readonly helperService: HelperService,
    public readonly dialog: MatDialog,
    public readonly activatedRoute: ActivatedRoute,
  ) {
    //
  }

我目前正在使用这些规则,但这些规则不会对构造函数参数进行排序。

"member-ordering": [
      true,
      {
        "alphabetize": true,
        "order": [
          "private-static-field",
          "protected-static-field",
          "public-static-field",

          "private-instance-field",
          "protected-instance-field",
          "public-instance-field",

          "private-constructor",
          "protected-constructor",
          "public-constructor",

          "private-static-method",
          "protected-static-method",
          "public-static-method",

          "private-instance-method",
          "protected-instance-method",
          "public-instance-method"
        ]
      }
    ],

检查这条规则:https://palantir.github.io/tslint/rules/member-ordering/

然后您可以将此规则添加到您的配置中:

"member-ordering": [
  true,
  {
    "alphabetize": true,
    "order": [
      "public-static-field",
      "public-instance-field",
      "public-constructor",
      "private-static-field",
      "private-instance-field",
      "private-constructor",
      "public-instance-method",
      "protected-instance-method",
      "private-instance-method"
    ]
  }
]

它也有自动修复功能。

这不是问题的真正答案,但我仍然认为值得一提:

不要(过分)关心构造函数参数的顺序

IMO 你不应该太在意构造函数中参数的顺序 - 特别是如果你永远不会自己调用它,因为 Angular 会为你处理。

Here 您可以找到有关该主题的扩展讨论。

如果您想符合现有的 API 或公开您的函数并想让它们更简洁,那么排序可能很重要。 在常规函数中,排序可能更容易理解函数调用,或者可能只是感觉 "more natural"。所有这些都不适用于没有人会调用的构造函数。

对参数进行排序(按字母顺序)不会增加代码的可读性。

考虑以下两个片段:

constructor(
  private readonly router: Router,
  private readonly _changeDetector: ChangeDetectorRef,
  private readonly favoritesService: FavoritesService,
  private readonly jsonService: JsonService,
  private readonly messageService: MessageService,
  public readonly helperService: HelperService,
  public readonly dialog: MatDialog,
  public readonly activatedRoute: ActivatedRoute,
) {}

对比

constructor(
  private readonly _changeDetector: ChangeDetectorRef,
  public readonly activatedRoute: ActivatedRoute,
  public readonly dialog: MatDialog,
  private readonly favoritesService: FavoritesService,
  public readonly helperService: HelperService,
  private readonly jsonService: JsonService,
  private readonly messageService: MessageService,
  private readonly router: Router,
) {}

他们哪个更好?如果有的话,它将是第一个,因为它将 privatepublic 声明放在一起。

其他订购选项

如果你坚持对你的论点进行排序,我会想到两种可能性

  1. 组相关参数(例如路由器和激活路由)
  2. 按框架分组与自己分组

TSLint

据我所知 - 并在最后几分钟尝试 google - 没有规则可以帮助你实现你的目标 - 破坏了我的论点,即似乎没有人认为有任何需要。

如果您真的想执行该规则,请查看 creating your own rules