ccalvert
ccalvert

Reputation: 4476

Define or suppress WebStorm Code Analysis for a specific object

I'm having trouble with the WebStorm code analysis tool.

In a node express server I send an object:

var configSummary = {
  'siteDirs': siteDirs,
  etc...
};
res.status(200).send(configSummary);

In a web app I use jQuery to ask the express server to send back a JSON object:

$.getJSON('/makers/config', function(configSummary) {
  configSummary.siteDirs.forEach(etc...
})

The code runs without error, but the WebStorm code analysis annotator for my web app quite reasonably complains that configSummary.siteDirs is an unresolved variable. I know how to suppress the error in the editor with a comment, but I don't like that solution. Instead, I would like to teach WebStorm about the configSummary object or tell it to ignore that "type" in the client side JavaScript file. How can I do that?

Upvotes: 0

Views: 85

Answers (1)

lena
lena

Reputation: 93858

In cases when the actual data is only known at runtime (for example, when data is a value set through ajax call), it can't re resolved during static analysis, thus the error.

It's not possible to suppress analysis for specific error type - you can only suppress it for statement using comments. But you can let the IDE know what your data looks like. Possible solution using JSDoc annotations:

/**
 * @typedef {Object} configSummary
 * @property {Object} siteDirs
  */
...

$.getJSON('/makers/config', function (/** configSummary */ configSummary) {
    configSummary.siteDirs.forEach(...)
})

See also https://youtrack.jetbrains.com/issue/WEB-17419#comment=27-1058451, http://devnet.jetbrains.com/message/5504337#5504337 for other possible workarounds. You can open any of .js library files from plugins/JavaScriptLanguage/lib/JavaScriptLanguage.jar!/com/intellij/lang/javascript/index/predefined/ to see what stub definitions look like

Upvotes: 2

Related Questions