问题
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?
回答1:
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
Using Macros in the Editor - Help | WebStorm
Komodo Edit autocompletion JS object literal
来源:https://stackoverflow.com/questions/30576454/how-to-make-webstorm-resolve-modules-which-are-functions