简体   繁体   English

为Typescript编写自定义ESlint规则时如何隔离构造函数参数

[英]how to isolate constructor parameters when writing custom ESlint rule for Typescript

My goal is to alphabetically sort the constructor parameters using "quick fix" from eslint (I'm trying to write this custom rule in order to do that)我的目标是使用 eslint 中的“快速修复”按字母顺序对构造函数参数进行排序(我正在尝试编写此自定义规则以执行此操作)

and for now I just want to 'isolate' the constructor parameters and have eslint display squiggly lines and my message when I hover over them, but I don't know how to get that by just looking at an AST of this example code here: https://astexplorer.net/#/gist/dc0def03c26658b1bfa5d8743f9a9f91/70365c30bee24bbec8289744ef4d33cf42268cb8现在我只想“隔离”构造函数参数,并在我将鼠标悬停在它们上面时让 eslint 显示波浪线和我的message ,但我不知道如何通过查看此示例代码的 AST 来获得它: https://astexplorer.net/#/gist/dc0def03c26658b1bfa5d8743f9a9f91/70365c30bee24bbec8289744ef4d33cf42268cb8

Which one do I choose?我选择哪一个? and how do I make sure it only checks ones in the constructor?以及如何确保它只检查构造函数中的那些?

//example code used in AST explorer      
constructor(
        private _appService: AppService,
        private _authService: AuthService,
        private _formBuilder: FormBuilder,
        private _manageScenarioService: ManageScenarioService,
      ) {}

this is what I have so far: the problem with this is it highlights all identifiers, and not just the ones within the consturctor这就是我到目前为止所拥有的:问题在于它突出显示了所有标识符,而不仅仅是构造函数中的标识符

 import { Rule } from 'eslint';
    export function diSortRule(context: Rule.RuleContext): Rule.RuleListener {
      return {
        Identifier(node) {
          context.report({
            node,
            message: 'this pops up here',
          });
        },
      };
    }

any help would be so appreciated!!任何帮助将不胜感激!

I know it is long time ago already but I hope it can still come handy to you or others.我知道这已经是很久以前的事了,但我希望它仍然可以对您或其他人有用。 We just published a rule which is autofixable and it sorts constructor parameters in classes decorated by one of @Component() , @Directive() , @Injectable() , @Pipe() .我们刚刚发布了一个可自动修复的规则,它对由@Component()@Directive()@Injectable()@Pipe()之一修饰的类中的构造函数参数进行排序。

You can use it as:您可以将其用作:

  1. install our plugin: npm install --save-dev @erento/eslint-plugin-erento-rules安装我们的插件: npm install --save-dev @erento/eslint-plugin-erento-rules

  2. Add this to your .eslintrc将此添加到您的 .eslintrc

     "plugins": [ "@erento/erento-rules" ], "rules": { "@erento/erento-rules/injectable-order": "error" }
  3. run your ESLint linter运行你的 ESLint linter

More to this rule is here: https://github.com/erento/eslint-plugin-erento-rules/blob/master/src/rules/injectable-order.md更多关于这条规则的信息在这里: https : //github.com/erento/eslint-plugin-erento-rules/blob/master/src/rules/injectable-order.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM