Skip to content
Permalink
Browse files

Update: Add "allowCall" option (fixes #4011)

Add new option “allowCall” to support nested invocation.
  • Loading branch information...
rpatil26 committed Jan 2, 2016
1 parent e04bacf commit 9c03abc25346c4e655efebaedd8c27f74e09c1a0
Showing with 41 additions and 11 deletions.
  1. +17 −2 docs/rules/no-mixed-requires.md
  2. +20 −7 lib/rules/no-mixed-requires.js
  3. +4 −2 tests/lib/rules/no-mixed-requires.js
@@ -11,11 +11,11 @@ When this rule is enabled, all `var` statements must satisfy the following condi

### Options

This rule comes with option called `grouping` which is turned off by default. You can set it in your `eslint.json`:
This rule comes with two boolean options. Both are turned off by default. You can set those in your `eslint.json`:

```json
{
"no-mixed-requires": [1, {"grouping": true}]
"no-mixed-requires": [1, {"grouping": true, "allowCall": true}]
}
```

@@ -99,6 +99,21 @@ var foo = require('foo'), /*error Do not mix core, module, file and
bar = require(getBarModuleName());
```

The following patterns are not considered problems when `allowCall` is turned on:

```js
var async = require('async'),
debug = require('diagnostics')('my-module'),
eslint = require('eslint');
```

The following patterns are always considered problems regardless of `allowCall`:

```js
var async = require('async'),
debug = require('diagnostics').someFunction('my-module'), /* Allow Call doesn't allow calling any function */
eslint = require('eslint');
```

## When Not To Use It

@@ -11,6 +11,17 @@

module.exports = function(context) {

var grouping = false,
allowCall = false,
options = context.options[0];

if (typeof options === "object") {
grouping = options.grouping;
allowCall = options.allowCall;
} else {
grouping = !!options;
}

/**
* Returns the list of built-in modules.
*
@@ -56,6 +67,12 @@ module.exports = function(context) {
) {
// "var x = require('util');"
return DECL_REQUIRE;
} else if (allowCall &&
initExpression.type === "CallExpression" &&
initExpression.callee.type === "CallExpression"
) {
// "var x = require('diagnose')('sub-module');"
return getDeclarationType(initExpression.callee);
} else if (initExpression.type === "MemberExpression") {
// "var x = require('glob').Glob;"
return getDeclarationType(initExpression.object);
@@ -140,13 +157,6 @@ module.exports = function(context) {
return {

"VariableDeclaration": function(node) {
var grouping = false;

if (typeof context.options[0] === "object") {
grouping = context.options[0].grouping;
} else {
grouping = !!context.options[0];
}

if (isMixed(node.declarations)) {
context.report(
@@ -175,6 +185,9 @@ module.exports.schema = [
"properties": {
"grouping": {
"type": "boolean"
},
"allowCall": {
"type": "boolean"
}
},
"additionalProperties": false
@@ -30,7 +30,8 @@ ruleTester.run("no-mixed-requires", rule, {
{ code: "var foo = require(42), bar = require(getName())", options: [true] },
{ code: "var fs = require('fs'), foo = require('./foo')", options: [{grouping: false}] },
{ code: "var foo = require('foo'), bar = require(getName())", options: [false] },
{ code: "var a;", options: [true] }
{ code: "var a;", options: [true] },
{ code: "var async = require('async'), debug = require('diagnostics')('my-module')", options: [{allowCall: true}]}
],
invalid: [
{ code: "var fs = require('fs'), foo = 42", options: [false], errors: [{ message: "Do not mix 'require' and other declarations.", type: "VariableDeclaration"}] },
@@ -41,6 +42,7 @@ ruleTester.run("no-mixed-requires", rule, {
{ code: "var exec = require('child_process').exec, foo = require('foo')", options: [true], errors: [{ message: "Do not mix core, module, file and computed requires.", type: "VariableDeclaration"}] },
{ code: "var fs = require('fs'), foo = require('./foo')", options: [true], errors: [{ message: "Do not mix core, module, file and computed requires.", type: "VariableDeclaration"}] },
{ code: "var foo = require('foo'), foo2 = require('./foo')", options: [true], errors: [{ message: "Do not mix core, module, file and computed requires.", type: "VariableDeclaration"}] },
{ code: "var foo = require('foo'), bar = require(getName())", options: [true], errors: [{ message: "Do not mix core, module, file and computed requires.", type: "VariableDeclaration"}] }
{ code: "var foo = require('foo'), bar = require(getName())", options: [true], errors: [{ message: "Do not mix core, module, file and computed requires.", type: "VariableDeclaration"}] },
{ code: "var async = require('async'), debug = require('diagnostics').someFun('my-module')", options: [{allowCall: true}], errors: [{ message: "Do not mix 'require' and other declarations.", type: "VariableDeclaration"}] }
]
});

0 comments on commit 9c03abc

Please sign in to comment.
You can’t perform that action at this time.