Skip to content

Commit

Permalink
no-common-js: allow conditional requires
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonxia23 authored and ljharb committed Oct 16, 2017
1 parent 523789f commit 1b990d0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/rules/no-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ module.exports = {
},
'CallExpression': function (call) {
if (context.getScope().type !== 'module') return
if (
call.parent.type !== 'ExpressionStatement'
&& call.parent.type !== 'VariableDeclarator'
) return

if (call.callee.type !== 'Identifier') return
if (call.callee.name !== 'require') return
Expand Down
1 change: 1 addition & 0 deletions tests/src/rules/no-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ruleTester.run('no-commonjs', require('rules/no-commonjs'), {

// allowed requires
{ code: 'function a() { var x = require("y"); }' }, // nested requires allowed
{ code: 'var a = c && require("b")' }, // conditional requires allowed
{ code: 'require.resolve("help")' }, // methods of require are allowed
{ code: 'require.ensure([])' }, // webpack specific require.ensure is allowed
{ code: 'require([], function(a, b, c) {})' }, // AMD require is allowed
Expand Down

0 comments on commit 1b990d0

Please sign in to comment.