Skip to content

Commit

Permalink
[no-commonjs] add allowConditionalRequire option
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoji Chen committed Aug 4, 2019
1 parent 654dc10 commit 8b1c71c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
19 changes: 17 additions & 2 deletions docs/rules/no-commonjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,30 @@ If `allowRequire` option is set to `true`, `require` calls are valid:

```js
/*eslint no-commonjs: [2, { allowRequire: true }]*/
var mod = require('./mod');
```

but `module.exports` is reported as usual.

### Allow conditional require

By default, conditional requires are allowed:

```js
var a = b && require("c")

if (typeof window !== "undefined") {
require('that-ugly-thing');
}

var fs = null;
try {
fs = require("fs")
} catch (error) {}
```

but `module.exports` is reported as usual.
If the `allowConditionalRequire` option is set to `false`, they will be reported.

This is useful for conditional requires.
If you don't rely on synchronous module loading, check out [dynamic import](https://github.com/airbnb/babel-plugin-dynamic-import-node).

### Allow primitive modules
Expand Down
7 changes: 6 additions & 1 deletion src/rules/no-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ function allowRequire(node, options) {
return options.allowRequire
}

function allowConditionalRequire(node, options) {
return options.allowConditionalRequire === undefined ? true : options.allowConditionalRequire
}

function validateScope(scope) {
if (scope.variableScope.type === 'module') return true
return false
Expand Down Expand Up @@ -52,6 +56,7 @@ const schemaObject = {
properties: {
allowPrimitiveModules: { 'type': 'boolean' },
allowRequire: { 'type': 'boolean' },
allowConditionalRequire: { 'type': 'boolean' },
},
additionalProperties: false,
}
Expand Down Expand Up @@ -117,7 +122,7 @@ module.exports = {

if (allowRequire(call, options)) return

if (isConditional(call.parent)) return
if (allowConditionalRequire(call, options) && isConditional(call.parent)) return

// keeping it simple: all 1-string-arg `require` calls are reported
context.report({
Expand Down
13 changes: 13 additions & 0 deletions tests/src/rules/no-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ ruleTester.run('no-commonjs', require('rules/no-commonjs'), {
{ code: 'var x = require("x")', errors: [ { message: IMPORT_MESSAGE }] },
{ code: 'x = require("x")', errors: [ { message: IMPORT_MESSAGE }] },
{ code: 'require("x")', errors: [ { message: IMPORT_MESSAGE }] },

{ code: 'if (typeof window !== "undefined") require("x")',
options: [{ allowConditionalRequire: false }],
errors: [ { message: IMPORT_MESSAGE }],
},
{ code: 'if (typeof window !== "undefined") { require("x") }',
options: [{ allowConditionalRequire: false }],
errors: [ { message: IMPORT_MESSAGE }],
},
{ code: 'try { require("x") } catch (error) {}',
options: [{ allowConditionalRequire: false }],
errors: [ { message: IMPORT_MESSAGE }],
},
]),

// exports
Expand Down

0 comments on commit 8b1c71c

Please sign in to comment.