Search code examples
node.jsphpstormwebstormjetbrains-idees6-class

Why is the JetBrains IDE keeps saying Class and Constructor are unused?


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


Solution

  • 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