5
votes

My PhpStorm / WebStorm IDE keeps telling, that the class and constructor are unused for some classes (not for all). I have already set the JavaScript language version to ECMAScript6 and enabled Node.js support in settings. The code in general works fine too. For example ...

File TestClass.js:

module.exports = class Test {
    constructor() {
        console.log("Test.constructor");
    }

    test() {
        console.log("Test.test");
    }
}

File Test.js:

let Test = require("./TestClass");
let inst = new Test();
inst.test();

With this, code inspections keeps telling me:

Unused class TestClass
Unused method constructor

Is there anything wrong or is there a way to suppress the warning? The IDE in general displays no option to suppress this.

Even another problem seems to be, that anonymous classed causes inspection problems too. If I rewrite module.exports = class Test { to module.exports = class { even the method test() inside the class will be marked as unused. I haven't found a way to prevent this too ...

1
I think, you can suppress "unused class and methods" inspection in File - Preferences - Code Inspection - JS. I'm not sure with the path, because I worked with PhpStorm 6 month ago, but it was something like that.Jirka Picek
@JirkaPicek But this will disable the inspection for the whole project. That would work, but has a bigger impact. I would like to have the inspection, but to work correct. ;)eisbehr
@Gimby Sure, that has to be enabled. But that is already enabled in my project. I've extended the question to this.eisbehr
I know you found solution already, but if you would like to disable just some specific inspections in future, there is in ReSharper in Alt+Enter menu Inspection... -> Disable once with comment -> Disable <Your selection>. I guess something simillar is in PhpStorm.Jirka Picek
@JirkaPicek Yes, that was one thing I was searching for at first. But it seems like there is no option to suppress such warnings. In know the Alt+Enter function, but there is nothing happen on this lines. There is even no suppress entry in the context menu of the warning.eisbehr

1 Answers

10
votes

It seems that I just needed to ask here, to find the answer by myself. ;)

The problem seems to belong to the inline export of the class. When creating the class first, and then export it afterwards by the class name resolves the problem.

So I just had to rewrite my class files like this:

class Test { // instead of module.exports = class Test {
    constructor() {
        console.log("Test.constructor");
    }
}

module.exports = Test; // put the export to an extra line