Reputation: 1769
I'm making a node.js project, and work in Webstorm, where functions are usually highlighted in yellow. However in the following scenario, fnA
is not recognized as a function in file b.
//file a.js
module.exports = fnA;
/**
* Converts val to someOtherVal
* @param val
* @return someOtherVal
*/
function fnA( val ){
//Do something with val....
return someOtherVal
}
//file b.js
var fnA = requires('./a.js');
function fnB() {
var c = fnA(3); //<- not recognized as a function
}
However if I wrap the function in a namespace, it works properly:
//a.js
module.exports = fnA;
/**
* Converts val to someOtherVal
* @param val
* @memberof core
*/
function fnA( val ){
//Do something with val....
return someOtherVal
}
__________________________________________________
//b.js
/**
* @namespace
*/
var core = {
fnA : requires('./a.js')
}
function b() {
var c = core.fnA(3); //Recognized as a function
}
I would like to avoid wrapping the function in a namespace - what is the proper way to jsdoc the function in the first example?
Upvotes: 1
Views: 1424
Reputation: 1
I ran into a similar problem and i figured i had mixed commonJS and ES6 in my module. I had something like this
//I used commonJS here
const {foo} = require('bar')
//At the same time used ES6 syntax here which confused the interpreter
import {bar} from 'foo'
//file a.js
module.exports = fnA;
/**
* Converts val to someOtherVal
* @param val
* @return someOtherVal
*/
function fnA( val ){
//Do something with val....
return someOtherVal
}
//fix=> i simply reverted to commonJS and everything worked fine
So be careful not to mix syntax in your module as this could confuse the interpreter as it may prefer ES6 over commonJS and you end up contorting code by wrapping in namespace as was your case up there.
Upvotes: 0
Reputation: 709
same issue in JSDoc 3.6.6 (functions defined with an export not recognized)
Workaround: Define functions without export and export an object.
//file.js
const function1 = () => {
...
}
const function2 = () => {
...
}
export {
function1,
function2
}
Upvotes: 0