How to make WebStorm resolve modules which are functions?

半城伤御伤魂 提交于 2020-01-01 08:49:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!