Eliminating "unresolved variable" warnings on external (package.json) objects

I have a Node project with a Gruntfile.js and package.json in the root directory, and there's code in Gruntfile.js that loads package.json:

var pkg = grunt.file.readJSON("package.json");

and package.json contains a simple object definition:

{

     name: "project",

     author: "Jeff Parsons",

     ...

}

but references to that object in Gruntfile.js, like:

pkg.author

result in "unresolved variable" warnings in PhpStorm.  Is there some combination of JSDoc tags, for example, I could use to get PhpStorm to recognize all the properties that pkg contains?

Work-arounds I've seen involve doing things like creating dummy variables with all the same properties as the original JSON, but it would be nice to avoid duplicating what's already inside the package.json file.  And my problem isn't limited to "standard" package.json object properties -- I've also added custom properties to my JSON files, and having to duplicate those properties in another definition somewhere would be less than ideal.

Thanks.

2 comments
Comment actions Permalink

Hi Jeff,

if creating fake variables as shown in http://devnet.jetbrains.com/message/5504337#5504337 is not a suitable workaround for you, I can suggest using @property tags. Smth like:

/**
* @class
* @property {String} name
* @property {String} author
* ...
*/
var pkg = grunt.file.readJSON("package.json");


But you will still need to list all properties explicitly.

0
Comment actions Permalink

Thanks -- that works, and the advantage of a JSDoc solution is that it provides a natural location to document the properties I'm using.

Maybe someday there'll be some sort directive/pragma/tag (something related to JSDoc @type?) that can make this even simpler though.

Cheers.

0

Please sign in to leave a comment.