0

I try to use Eslint to find useless dependencies injection in my Angular/Ionic components.

Example:

import { BasicDataService } from '../../providers/basic-data.service';

@Component({
    selector: 'app-login',
    templateUrl: './login.page.html',
    styleUrls: ['./login.page.scss'],
})
export class LoginPage implements OnInit {
    constructor(
        private bd: BasicDataService,
    ) {}
}

The property bd is defined in the constructor but then it is not used, how could Eslint highlight it?

My .eslintrc.json so far is:

{
    "env": {
        "browser": true,
        "es2021": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:@typescript-eslint/eslint-recommended",
        "plugin:@typescript-eslint/recommended"
    ],
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "ecmaVersion": 12,
        "sourceType": "module"
    },
    "plugins": ["@typescript-eslint"],
    "rules": {}
}


In one previous project, I used the rule @typescript-eslint/no-unused-vars-experimental but it seems it has been removed recently.

Thanks!

Pierre
  • 1,044
  • 15
  • 27
  • You can `@typescript-eslint/no-unused-vars` instead of `@typescript-eslint/no-unused-vars-experimental` which is depricated and removed as you mentioned. – deepakchethan Nov 21 '21 at 03:03
  • Thanks for your answer, yes I tried ```"@typescript-eslint/no-unused-vars": 2``` but nothing appears around *db* – Pierre Nov 21 '21 at 03:18
  • @typescript-eslint/no-unused-vars is by default set as enabled. So you may check in your compiler eslint server is running or not. You may check debug console (for vs code) for this. – Alimur Razi Rana Nov 21 '21 at 04:16
  • Yes, Eslint is running, I can see other errors found by ESLint in both Sublime Text and VS Code, including other variables not used (like a simple: const aa = 1; ). When I execute Eslint in terminal, it works fine, found the errors I expect, except those dependencies injections not used. – Pierre Nov 21 '21 at 09:16

1 Answers1

1

There is no lint ESLint rule which does analysis of TS's private class properties.

TS itself can do this though via its noUnusedLocals compiler option. Though note that this will also match unused variables.

https://www.typescriptlang.org/tsconfig#noUnusedLocals

Brad Zacher
  • 2,915
  • 18
  • 31
  • Thanks, indeed at least I could let Typescript find for the non-used private class properties, that is very helpful. – Pierre Dec 21 '21 at 06:17
  • The difficult thing about linting for unused private properties is that they're not truly private and there a ways that you can access them outside of the class using square-bracket syntax (`myClass['privateProp']`) - which is why it's best left to TS which can do rich type-analysis of such cases! – Brad Zacher Dec 21 '21 at 23:02