9

WebStorm does a very good job of resolving functions which are returned from CommonJS modules as methods (and reads JsDoc associated with them), like for instance:

// utils/valid.js
/**
 * Returns true no matter what.
 * @param {HTMLElement} element
 * @return {boolean}
 */
function isValid(element) {
    return true;
}
module.exports.isValid = isValid; // exports property

Such a function is then correctly provided in code completion and inline documentation mechanisms when such a module is required in another file.

// main.js
var isValid = require('./utils/isValid').isValid; // works well

However, this fails when the function is returned directly as module exports

// utils/valid.js
module.exports = isValid; // exports object is a function

So when such a module is required, WebStorm seems to not know what it is:

// main.js
var isValid = require('./utils/isValid'); // doesn't work

This is very common in our project and changing all module.exports to plain objects is not an option. Is there any way for fix this issue in WebStorm?

Paul S.
  • 1,583
  • 1
  • 11
  • 14
  • how do you use it? Please provide the complete sample – lena Jun 01 '15 at 16:54
  • 1
    I added a few more lines in the samples, however how I actually use the module is not related to the issue - the main point is that eg. pressing Ctrl+Q should display inline documentation for the module regardless of the way which was used to define it https://www.jetbrains.com/webstorm/help/viewing-inline-documentation.html – Paul S. Jun 03 '15 at 13:40
  • I agree with Paul S, how he uses it isn't related to the issue. I too am having this problem, did you get any further with it Paul? – Dean Feb 24 '16 at 12:04
  • Unfortunately not. However, my opinion on this is that although the above is technically correct, it's architecturally defective. The split of modules is too fine-grained, but also regardless of granularity it's better to stick to the convention of returning a plain object with methods (even if it's just a single method). – Paul S. Feb 26 '16 at 14:31

1 Answers1

0

Create a macro to switch between the two semantic forms:

<iframe width="854" height="480" src="https://www.youtube.com/embed/J3YX1WIScAk" frameborder="0" allowfullscreen></iframe>

References

Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265