是否有 es-lint 或类似规则来禁用“extends”

Max*_*ber 2 javascript class ecmascript-6

我想使用 ES2015 类,但不希望任何继承进入我们的代码库。是否有一个自定义的 eslint 规则,比如“不扩展”或“不继承”,所以人们不能这样做class A extends B {}

我对此进行了相当多的搜索,但我认为我可能使用了错误的搜索词。

Nic*_*bal 5

您可以使用no-restricted-syntax规则来禁止继承,如下所示(此处以 JS 语法实现):

rules: {
  'no-restricted-syntax': [
    'error',
    {
      selector: 'ClassDeclaration[superClass]',
      message: "Extending other classes via inheritance isn't allowed. Use composition instead.",
    },
  ],
},
Run Code Online (Sandbox Code Playgroud)

message是可选的,但有助于 DX。