Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[no-unused-var] varsIgnorePattern not working for this module definition #2648

Closed
3 tasks done
HolgerJeromin opened this issue Oct 8, 2020 · 11 comments · Fixed by #2768
Closed
3 tasks done

[no-unused-var] varsIgnorePattern not working for this module definition #2648

HolgerJeromin opened this issue Oct 8, 2020 · 11 comments · Fixed by #2768
Labels
bug Something isn't working good first issue Good for newcomers has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@HolgerJeromin
Copy link

HolgerJeromin commented Oct 8, 2020

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

module.exports = {
    "root": true,
    "parser": "@typescript-eslint/parser",
    "plugins": [        "@typescript-eslint"    ],
    "rules": {
        "no-unused-vars": "off",
        "@typescript-eslint/no-unused-vars": ["error", {
            "vars": "all",
            "varsIgnorePattern": "Hmi",
            "args": "none"
        }]
    }

}
module Hmi {
    export interface myType  {
    }
    export function getType(): Hmi.myType | null {
        return null; 
     // return Hmi.System.getMyType();
    }
}

Or even shorter:

module Hmi {
    export class myClass {
    }
    console.log(Hmi.myClass);  // Register call in reality
}

Expected Result

No rule violation.

  1. Because the module is used.
  2. Because I even added the name Hmi into varsIgnorePattern

Actual Result

.\node_modules\.bin\eslint.cmd  file.ts

file.ts
  1:8  error  'Hmi' is defined but never used  @typescript-eslint/no-unused-vars

✖ 1 problem (1 error, 0 warnings)

Additional Info
This examples are working!

export module Hmi {   // <-- export added!
    export interface myType  {
    }
    export function getType(): Hmi.myType | null {
        return null;
    }
}
module Hmi {
    export interface myType  {
    }
    export function getType():  null {  // <-- myType removed
        return null;
    }
}

Versions

  "@typescript-eslint/eslint-plugin": "4.4.0",
  "@typescript-eslint/parser": "4.4.0",
  "eslint": "6.8.0", and "7.10.0"
  "typescript": "4.0.3",
@HolgerJeromin HolgerJeromin added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Oct 8, 2020
@bradzacher
Copy link
Member

No rule violation.
Because the module is used.

Your example code doesn't include a usage.
Please update your example appropriately

@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for maintainers to take a look labels Oct 8, 2020
@HolgerJeromin
Copy link
Author

This is a minimal example for the behavior in our 460000 loc project with TS compiler option "module": "none".
I adjusted the examples a bit.
I added a console call to the example which is a register call in reality.
But in my code I really have files which are only such wrappers of an internal API. They are called from customer code.

It is fine if eslint can not detect usage of the object. But at least the entry varsIgnorePattern should make him happy.

@bradzacher
Copy link
Member

I ask because the usage is very important.
When you provide an example as part of a bug report, that example needs to be a self-contained reproduction of the issue. If it's not, then there's no way I can repro, and thus no way I can help you.
So thank you for updating.

In this case - in your updated example...

Usages from within the namespace do not count, and are not considered by the rule.
This is done on purpose. Or else you could have orphaned, self-referencing namespaces left in your codebase.

You get the same functionality with functions:

/* eslint no-unused-vars: ["error"] */

function foo() {
  return foo(); // Error: 'foo' is defined but never used. (no-unused-vars)
}

eslint demo

@ghost

This comment has been minimized.

@bradzacher

This comment has been minimized.

@HolgerJeromin
Copy link
Author

Usages from within the namespace do not count, and are not considered by the rule.
This is done on purpose. Or else you could have orphaned, self-referencing namespaces left in your codebase.

Ok, I am perfectly fine with that.

Another example from my code base:

module Hmi.System { 
    export class Init { 
        public static run() {
             // Run the HMI
        }
    }
    Hmi.System.Init.run(); // This code kick starts the main code.
}

I have no problem adding "varsIgnorePattern": "Hmi" at the main eslint config.
But starting every ts file with
module Hmi { // eslint-disable-line @typescript-eslint/no-unused-vars
is not very sexy.

@bradzacher
Copy link
Member

I guess I have a few questions here before I can provide a good course of action:

  • why are you using module/namespace syntax at all?
  • why are you using classes as static function containers? (instead of an object, or another namespace)
  • why are you nesting the run call within the namespace scope, instead of the parent (root) scope?

@HolgerJeromin
Copy link
Author

why are you using module/namespace syntax at all?
why are you using classes as static function containers? (instead of an object, or another namespace)

It is a technical dept in our code (which started with TS 1.6) we want to change in the (far?) future.

why are you nesting the run call within the namespace scope, instead of the parent (root) scope?

You are right. This can be adjusted with some files (but not in all).

Sorry if I repeat myself. But I see varsIgnorePattern as a flag like "whatever the code is structured this var should never ever be reported".

@bradzacher
Copy link
Member

Yeah sometimes this can be solved in the code, sometimes it needs config. The more information I have, the easier it is for me to figure out what the best solution could be for your use case.

In this case, yes likely using varsIgnorePattern to ignore Hmi is the best course of action for your codebase.

Though I would definitely recommend prioritising off of (what I assume are) global non-exported namespaces and onto ES6 modules 🙂

@bradzacher bradzacher added fix: user error issue was fixed by correcting the configuration / correcting the code and removed awaiting response Issues waiting for a reply from the OP or another party labels Oct 14, 2020
@HolgerJeromin
Copy link
Author

HolgerJeromin commented Oct 15, 2020

In this case, yes likely using varsIgnorePattern to ignore Hmi is the best course of action for your codebase.

I think you did not understood what I am trying to say since four messages:
"varsIgnorePattern": "Hmi"
Does not ignore the var Hmi in my example (and real) code.

@HolgerJeromin HolgerJeromin changed the title [no-unused-var] False positive for module definition [no-unused-var] varsIgnorePattern not working for this module definition Oct 15, 2020
@bradzacher bradzacher added bug Something isn't working good first issue Good for newcomers and removed fix: user error issue was fixed by correcting the configuration / correcting the code labels Oct 15, 2020
@bradzacher
Copy link
Member

I did not understand that was what you meant, no sorry.

The bug is here. Our extensions to rule doesn't consider varsIgnorePattern for namespace decls that are self-referenced

if (isOnlySelfReferenced) {
context.report({
node: variable.identifiers[0],
messageId: 'unusedVar',
data: {
varName: variable.name,
action: 'defined',
additional: '',
},
});
}

Should be a simple fix. happy to accept a PR.

@bradzacher bradzacher reopened this Oct 15, 2020
@bradzacher bradzacher added the has pr there is a PR raised to close this label Nov 16, 2020
bradzacher added a commit that referenced this issue Nov 16, 2020
I wanted to avoid doing this, but not doing this restricts our logic too much - it causes problems when we want to consider reporting on things that the base rule wouldn't report on.

Fixes #2714
Fixes #2648
Closes #2679
bradzacher added a commit that referenced this issue Nov 22, 2020
I wanted to avoid doing this, but not doing this restricts our logic too much - it causes problems when we want to consider reporting on things that the base rule wouldn't report on.

Fixes #2714
Fixes #2648
Closes #2679
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working good first issue Good for newcomers has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
2 participants