Skip to content

Commit

Permalink
no-commonjs: Fix false positive when exports is in scope
Browse files Browse the repository at this point in the history
Fixes #940
  • Loading branch information
Adrian Helvik authored and ljharb committed Nov 29, 2017
1 parent b839641 commit 7383977
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/rules/no-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ module.exports = {

// exports.
if (node.object.name === 'exports') {
context.report({ node, message: EXPORT_MESSAGE })
const isInScope = context.getScope()
.variables
.some(variable => variable.name === 'exports')
if (! isInScope) {
context.report({ node, message: EXPORT_MESSAGE })
}
}

},
Expand Down
9 changes: 9 additions & 0 deletions tests/src/rules/no-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ ruleTester.run('no-commonjs', require('rules/no-commonjs'), {
// exports
{ code: 'export default "x"', parserOptions: { sourceType: 'module' } },
{ code: 'export function house() {}', parserOptions: { sourceType: 'module' } },
{
code:
'function someFunc() {\n'+
' const exports = someComputation();\n'+
'\n'+
' expect(exports.someProp).toEqual({ a: \'value\' });\n'+
'}',
parserOptions: { sourceType: 'module' },
},

// allowed requires
{ code: 'function a() { var x = require("y"); }' }, // nested requires allowed
Expand Down

0 comments on commit 7383977

Please sign in to comment.