Missing exports qualifier¶
ID: js/node/missing-exports-qualifierKind: problemSecurity severity: Severity: errorPrecision: highTags: - quality - reliability - correctness - frameworks/node.jsQuery suites: - javascript-code-quality.qls - javascript-security-and-quality.qls
Click to see the query in the CodeQL repository
Referencing an otherwise undeclared global variable in a module that exports a definition of the same name is confusing and may indicate a bug.
Recommendation¶
If the global variable reference is intentional, consider adding a JSLint/*global...*/ directive or an externs declaration to declare the variable.
If the global variable reference is unintentional, qualifying the reference withexports will make it refer to the exported definition instead.
Example¶
In the following example, the module exports two functionscheckOne andcheckList. The latter is also stored in a variable of the same name that is local to the module, but the former is not. Hence the callcheckOne(xs[i]) on line 7 does not refer to the function defined on line 1, but to an otherwise undeclared global variable also calledcheckOne.
exports.checkOne=function(x){if(!x)thrownewError();};varcheckList=exports.checkList=function(xs){for(vari=0;i<xs.length;++i)checkOne(xs[i]);};
Assuming that the intention is to call thecheckOne function defined on line 1, the call should be qualified withexports like this:
exports.checkOne=function(x){if(!x)thrownewError();};varcheckList=exports.checkList=function(xs){for(vari=0;i<xs.length;++i)exports.checkOne(xs[i]);};
References¶
Node.js:Modules.
JSLint Help:JSLint Directives.
Closure Compiler:Advanced Compilation and Externs.