Webstorm: javascript code-complete for dynamically generated class methods

How to annotate a dynamically generated class method in Webstorm so it shows up in the autocomplete?

So far I was able to accomplish similar thing for properties:

/**
 * @property prop1
 * @property prop2
 */

class MyClass{
    //empty class
}

Now if I type "new MyClass().pr" then Webstorm shows both prop1 and prop2 in the code completion popup.

What is the correct syntax for methods?

It seems @name should do the trick, but Webstorm doesn't recognize it for autocompletion.

Or maybe it's not about annotations but there is a different way of accomplishing that?

Any IDEas?

 

Here is a quick example of how I generate them:

 

const methodsConfig = {
    method1:"some instructions 1",
    method2:"some instructions 2",
}

class MyClass{
    generateMethods(){
        Object.keys(methodsConfig).forEach(methodName => {
            MyClass.prototype[methodName] = (...args) => {
                //..further logic here
            }
        }
    }
}
6 comments
Comment actions Permalink

You ca try using @property for methods. Like:

/**
 * @property prop1
 * @property prop2
 * @property {function(string)} method1
 */

class MyClass{}
0
Comment actions Permalink

Cool, thanks Elena, it now shows the parameters in the code-completion popup which is awesome! That pretty much solves my question.

 

Just wondering though if it's possible to show it as a method in Webstorm somehow or Webstrom doesn't support that?

While defining a signature this way, it seems it's possible to show either a list of variables or their types but not the both - not a big deal for my case, but still interested if it's possible to get closer here?

 

 

 

 

0
Comment actions Permalink

Nothing else comes to my mind, sorry:(

0
Comment actions Permalink

Elena Pogorelova

It seems like it just recently stopped working, any ideas? 

I've update to the following version recently and deleted .idea file:

WebStorm 2020.1.1
Build #WS-201.7223.93, built on April 30, 2020

Do I need to set any option for it to work again? 

0
Comment actions Permalink

it still works for me in WS-201.7223.93:

1
Comment actions Permalink

agh, found the problem: I put a global method between the annotations and the class declaration which broke it:

 

/**
 * @property prop1
 * @property prop2
 * @property {function(string)} method1
 */

function someMethod(){} //<- putting it here eventually breaks the declaration as it's no longer attributed to the class
class MyClass{}

 

Removing the method from there fixed the problem!

 

Thanks for looking into it!

1

Please sign in to leave a comment.