Specifying to Eclipse/JSDT the type of a Javascript variable
Asked Answered
N

1

5

I'm trying to use Eclipse for some server-side Javascript development.

The API I use has a function doStuff(string, object) (names changed to protect the guilty) that returns a value of differing types (subclasses of one type) depending on the (values of) the arguments passed it.

I have built a Javascript library to describe this function:

/**
  * function doStuff(s, o)
  * @memberOf Global
  * @param {String} s
  * @param {Object} o
  * @type ResultType
  * @returns {ResultType}
  */
doStuff = function(str, obj} {return new ResultType();}

Because it can return several types, I have declared it as returning the base type. However, that means Eclipse doesn't know what type it really is, and so I get later spurious errors when trying to access fields of that object.

So there can be FooResultType, BarResultType, each of which are ResultTypes, but have additional fields/functions

Is there any way around this? Can I somehow annotate the variable holding the returned value so that Eclipse knows what type it really is?

I've tried (with and without braces around FooResultType)

/**
  * @type FooResultType
  */
  v = doStuff("stringvalue", someObject);

but this makes no difference.

(There are other questions in this area, but nothing that address this issue, I think)

Nightspot answered 3/7, 2011 at 17:51 Comment(3)
Maybe I'm off-topic, but judging by your examples, what you're trying to achieve, and the editor you're using, I think you should try GWT.Runstadler
a) I can't - this is an embedded Rhino JS interpreter inside a weblogic app, and the scripts have to be written in Javascript b) isn't GWT for browser-side? This is server-side.Nightspot
Oh, yes, GWT will generate JS, but it's pretty client-side specific (browser sniffing, etc), so yeah. You can't really use it. I somehow missed the server-side mention in your question.Runstadler
N
7

(Answering my own question)

The below does work. The key seems to be the "var" - only by declaring a variable can you get JSDT to recognise it has the specified type. My suspicion is that JSDT can only manage one type per variable, although of course being Javascript the type can change arbitarily.

/**
  * @returns {FooResultType}
  */
  var v = doStuff("stringvalue", someObject);

It also seems to require @returns rather than @type, although it's difficult to know what is and is not supported by JSDT - it's not well documented and experimentation is needed. Small changes seem to make unexpected differences sometimes.

Nightspot answered 6/7, 2011 at 12:19 Comment(4)
Is there a way to add variable definition as well?Alexisaley
Did you have to install JSDT for this annotation support, or is it out-of-box eclipse? Are these annotations documented anywhere?Obliquely
It was a while ago so I don't recall which Eclipse download I used. BUt you need JSDT installed for sure. The JSDoc annotations are documented in various places (try google) but which exact ones JSDT supports isn't at all clear.Nightspot
Also, if anything, support for this seems to have got more limited in recent versions of Eclipse.Nightspot

© 2022 - 2024 — McMap. All rights reserved.