disallow require calls to be mixed with regular variable declarations (no-mixed-requires)

禁止 require 调用与普通变量声明混合使用 (no-mixed-requires)

In the Node.js community it is often customary to separate initializations with calls to require modules from other variable declarations, sometimes also grouping them by the type of module. This rule helps you enforce this convention.

在 Node.js 社区,通常习惯把调用 require 模块的初始化和其它变量声明分开,有时也根据模块类型对它们进行分组。该规则帮助你强制执行这种约定。

Rule Details

When this rule is enabled, each var statement must satisfy the following conditions:

当启用该规则时,每个 var 语句必须满足以下条件:

This rule distinguishes between six kinds of variable declaration types:

该规则区分六种类型的变量声明:

In this document, the first four types are summed up under the term require declaration.

在本文中,前四个类型属于 require declaration

var fs = require('fs'),        // "core"     \
    async = require('async'),  // "module"   |- these are "require declaration"s
    foo = require('./foo'),    // "file"     |
    bar = require(getName()),  // "computed" /
    baz = 42,                  // "other"
    bam;                       // "uninitialized"

Options

This rule can have an object literal option whose two properties have false values by default.

该规则有一个选项,是个对象,它的两个属性值默认为 false

Configuring this rule with one boolean option true is deprecated.

使用一个布尔类型的选项 true 配置此规则已被弃用。

Examples of incorrect code for this rule with the default { "grouping": false, "allowCall": false } options:

默认选项 { "grouping": false, "allowCall": false }错误 代码示例:

/*eslint no-mixed-requires: "error"*/

var fs = require('fs'),
    i = 0;

var async = require('async'),
    debug = require('diagnostics').someFunction('my-module'),
    eslint = require('eslint');

Examples of correct code for this rule with the default { "grouping": false, "allowCall": false } options:

默认选项 { "grouping": false, "allowCall": false }正确 代码示例:

/*eslint no-mixed-requires: "error"*/

// only require declarations (grouping off)
var eventEmitter = require('events').EventEmitter,
    myUtils = require('./utils'),
    util = require('util'),
    bar = require(getBarModuleName());

// only non-require declarations
var foo = 42,
    bar = 'baz';

// always valid regardless of grouping because all declarations are of the same type
var foo = require('foo' + VERSION),
    bar = require(getBarModuleName()),
    baz = require();

grouping

Examples of incorrect code for this rule with the { "grouping": true } option:

选项 { "grouping": true }错误 代码示例:

/*eslint no-mixed-requires: ["error", { "grouping": true }]*/

// invalid because of mixed types "core" and "module"
var fs = require('fs'),
    async = require('async');

// invalid because of mixed types "file" and "unknown"
var foo = require('foo'),
    bar = require(getBarModuleName());

allowCall

Examples of incorrect code for this rule with the { "allowCall": true } option:

选项 { "allowCall": true }错误 代码示例:

/*eslint no-mixed-requires: ["error", { "allowCall": true }]*/

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

Examples of correct code for this rule with the { "allowCall": true } option:

选项 { "allowCall": true }正确 代码示例:

/*eslint no-mixed-requires: ["error", { "allowCall": true }]*/

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

Known Limitations

When Not To Use It

If you use a pattern such as UMD where the required modules are not loaded in variable declarations, this rule will obviously do nothing for you.

如果你使用了类似 UMD 的模式,其 require 的模块在变量声明时不会被加载,该规则明显对你没什么用。

Version

This rule was introduced in ESLint 0.0.9.

该规则在 ESLint 0.0.9 中被引入。

Resources