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.

19 comments
Comment actions Permalink

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)

0
Comment actions Permalink

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:

function foo(bar) {
  bar
.field1 = bar.field2 * 2;
}


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?

0
Comment actions Permalink

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

0
Comment actions Permalink

I don't understand.  Can you give an example and more details on how I would do this?  Thanks.

0
Comment actions Permalink

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

0
Comment actions Permalink

Thank you for the reply.  Interesting.  So I created this json file:

{
  "blah":{
    "field1":1,
    "field2":1
  }
}


Now the code

function foo(bar) {
  bar.field1 = bar.field2 * 2;
}


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?

1
Comment actions Permalink

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;

0
Comment actions Permalink

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.


0
Comment actions Permalink

So far validation / completion is not completely strict (e.g. it will suggest other members or resolve to other members)

0
Comment actions Permalink

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?

App.BarType = function () {
  return {
    field1: 1,
    field2: 1,
    field3: 1
  }
};


Later code in other file:

function (/*App.BarType*/ bar) {

  bar.field2 = bar.field1 * 2;

}

function foo(bars) {
  var /*App.BarType*/ bar = bars[0];
  bar.field2 = bar.field1 * 2;
}


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.

0
Comment actions Permalink

Related question.  If I have:

App.BarType = function () {
  return {
    some_column: 1,
    another_column: 1
  }
};

function foo(/*App.BarType*/ bar) {
  bar.some_colunm = 99;
}


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?

0
Comment actions Permalink

WebStorm recognizes array types as well but also adds some extra variants at the top

0
Comment actions Permalink

please create ticket about validation in YouTrack and we may continue discussion in that place

0
Comment actions Permalink

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!

0
Comment actions Permalink

I upgraded to the newest EAP and it looks like it does handle the array of type like I was hoping.

For example:

function SomeType() {
  return {
    fieldOne:1,
    fieldTwo:1
  }
}

var /*SomeType*/ row;
row.fieldOne = row.fieldTwo;
row.fieldOne = row.fieldThree;

var /*SomeType[]*/ rows;
rows[0].fieldOne = rows[0].fieldTwo;
rows[0].fieldOne = rows[0].fieldThree;


Has highlights for unresolved variables on row.fieldThree and rows[0].fieldThree.  This is exactly what I would expect.  Good job!

0
Comment actions Permalink

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.

0
Comment actions Permalink

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

0
Comment actions Permalink

Sounds good.  Thank you for the reply.

0

Please sign in to leave a comment.