so many unresolved variables and functions and types
I've started trying to go through all the warnings on a big javascript project using my shiny new WebStorm. The problem is that unresolved variables/functions/types can't be supressed on a statement-by-statement level like a lot of the other stuff. This is reported here.
Since that route is closed off, I wonder if there's a way to tell WebStorm how to understand that this certain object has this variable/type/function. Because I've got a ton of these and it's really limiting the usefullness of WebStorm to not be able to make them go away for the ones I know are wrong and leave the legitimate errors there.
Please sign in to leave a comment.
Before trying to foce the validation lets examine how the types were defined and where the definitions are located (e.g. they should be in the same opened project)
Thanks for the reply, Maxim. As you've read my bug report as well, you know some of the kinds of things I'm doing. For example:
Nowhere is bar defined, because it comes from an external source via AJAX/JSON. Is there some way to indicate to WebStorm was the structure should contain?
In this case it is enough to put simple sample JSON file (with necessary fields) under content (you can create special content root for that).
I don't understand. Can you give an example and more details on how I would do this? Thanks.
If your code in project have e.g. GetVariable undefined, then adding json file with following content will fix the problem:
{
"GetVariable":1
}
to avoid mixing such code with production one an separate content root can be created
Thank you for the reply. Interesting. So I created this json file:
Now the code
No longer syntax highlights field2. But this seems a little odd. WS has no way of knowing that "blah" is the same thing as "bar". I suppose it's just guessing and can't help it if it gets it wrong?
That was last resort for JSON derived members. If you really have some structure behind JSON you can annotate your code with comments like following:
function foo(/*BarType*/bar) {
bar.field1 = bar.field2 * 2;
}
And put such definitions in file outside production code (but under some content root)
function BarType() {}
BarType.prototype.field2 = 1;
Now THAT is something I was looking for. Thanks! Yes, I think this would be quite helpful in finding errors in my code. I was looking for something like this before
but I just couldn't figure out where to look.
So far validation / completion is not completely strict (e.g. it will suggest other members or resolve to other members)
Good to know. But it looks like it first shows the ones that do belong, and labels them with the right type. I have even more respect for WebStorm now. :)
Anything wrong with doing it in this format?
Later code in other file:
It seems like WebStorm accepts this but I just thought I'd ask in case there was any hidden traps.
Interesting! It also looks like you accept "/*App.BarType[]*/ bars" and it will know that the variable is an array. But it doesn't look like it understand how to complete the code for bars[0].(something). Maybe in the future?
Is there a help topic you can point me at for these features? I have searched the help but can't find the right place. I am using the EAP. Thanks again.
Related question. If I have:
is there some way for me to get WS to warn me that "some_colunm" is unknown for App.BarType, thus making me realize I made a typo? Right now, it doesn't seem to do any checking on the parts that are on the left of the assignment, only those on the right. This is completely valid Javascript, yes. But it's not what I meant to type.
If this isn't a current feature, is it worth me suggesting it or do you think it'd just be too complicated for WS to handle?
WebStorm recognizes array types as well but also adds some extra variants at the top
please create ticket about validation in YouTrack and we may continue discussion in that place
Okay, logged here:
http://youtrack.jetbrains.net/issue/WI-8221
My point was that it recognized that it was an array, but not that it was an array of a certain defined type. Does that make sense?
And can you point me to where these features of type hints we've been talking about are in the online help? Thanks!
I upgraded to the newest EAP and it looks like it does handle the array of type like I was hoping.
For example:
Has highlights for unresolved variables on row.fieldThree and rows[0].fieldThree. This is exactly what I would expect. Good job!
Did this feature change in a recent EAP? A couple of EAPs ago, I started getting warnings again on unresolved functions/variables, even though I still have the stub JSON file with the variable in it. It was working perfectly before.
Yes, since user content root can have very large json files IDEA does not index them anymore.
Same functionality can be achieved via dummy js file with anonymous function expression returning the json literal
Sounds good. Thank you for the reply.
Webstorm already uses typescript declaration files from libraries, so why not make one for our own application?
create a file named myapp.d.ts:
then in client code
Just like the solution of Maxim, this will provide autocomplete, when you add the type in semi-comment.
Otherwise, this solution just removes the WebStorm error warnings about undeclared variables.
Possibly because it was nine years ago and that feature didn't exist? :D Can't really tell you, as I don't really do much web dev these days and haven't even used webstorm for years.