简体   繁体   中英

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)

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

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() .

You can use it as:

  1. install our plugin: npm install --save-dev @erento/eslint-plugin-erento-rules

  2. Add this to your .eslintrc

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

More to this rule is here: https://github.com/erento/eslint-plugin-erento-rules/blob/master/src/rules/injectable-order.md

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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