no-mixed-requires

不允许 require 调用与常规变量声明混合

该规则在 ESLint v7.0.0 中已弃用。请使用 eslint-plugin-n 中的相应规则。

¥This rule was deprecated in ESLint v7.0.0. Please use the corresponding rule in eslint-plugin-n.

在 Node.js 社区中,通常习惯将调用 require 模块的初始化与其他变量声明分开,有时还按模块类型对它们进行分组。此规则可帮助你执行此约定。

¥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.

规则详情

¥Rule Details

启用此规则后,每条 var 语句必须满足以下条件:

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

  • none 或 all 变量声明必须是 require 声明(默认)

    ¥either none or all variable declarations must be require declarations (default)

  • 所有要求声明必须是同一类型(分组)

    ¥all require declarations must be of the same type (grouping)

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

¥This rule distinguishes between six kinds of variable declaration types:

  • core:要求的 核心模块 声明

    ¥core: declaration of a required core module

  • file:要求的 文件模块 声明

    ¥file: declaration of a required file module

  • modulenode_modules 文件夹 中所需模块的声明

    ¥module: declaration of a required module from the node_modules folder

  • computed:无法确定其类型的必需模块的声明(因为它是计算的或因为 require 是在没有参数的情况下调用的)

    ¥computed: declaration of a required module whose type could not be determined (either because it is computed or because require was called without an argument)

  • uninitialized:未初始化的声明

    ¥uninitialized: a declaration that is not initialized

  • other:任何其他类型的声明

    ¥other: any other kind of declaration

在本文档中,前四种类型在术语要求声明下进行了总结。

¥In this document, the first four types are summed up under the term 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

此规则可以有一个对象字面选项,其两个属性默认具有 false 值。

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

不推荐使用一个布尔选项 true 配置此规则。

¥Configuring this rule with one boolean option true is deprecated.

使用默认 { "grouping": false, "allowCall": false } 选项的此规则的错误代码示例:

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

在线运行
/*eslint no-mixed-requires: "error"*/

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

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

使用默认 { "grouping": false, "allowCall": false } 选项的此规则的正确代码示例:

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

在线运行
/*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

使用 { "grouping": true } 选项的此规则的错误代码示例:

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

在线运行
/*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

使用 { "allowCall": true } 选项的此规则的错误代码示例:

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

在线运行
/*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');

使用 { "allowCall": true } 选项的此规则的正确代码示例:

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

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

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

已知限制

¥Known Limitations

  • 该实现不知道任何名称为 require 的局部函数可能会影响 Node.js 的全局 require

    ¥The implementation is not aware of any local functions with the name require that may shadow Node.js’ global require.

  • 在内部,核心模块列表通过 require("repl")._builtinLibs 检索。如果你为 ESLint 和你的应用使用不同版本的 Node.js,则每个版本的核心模块列表可能会有所不同。上面提到的 _builtinLibs 属性在 0.8 中可用,对于早期版本,模块名称的硬编码列表用作后备。如果你的 Node.js 版本早于 0.6,则该列表可能不准确。

    ¥Internally, the list of core modules is retrieved via require("repl")._builtinLibs. If you use different versions of Node.js for ESLint and your application, the list of core modules for each version may be different. The above mentioned _builtinLibs property became available in 0.8, for earlier versions a hardcoded list of module names is used as a fallback. If your version of Node.js is older than 0.6 that list may be inaccurate.

何时不使用

¥When Not To Use It

如果你使用诸如 UMD 之类的模式,其中 required 模块未在变量声明中加载,则此规则显然对你没有任何帮助。

¥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.

版本

此规则是在 ESLint v0.0.9 中引入。

资源

ESLint 中文网
粤ICP备13048890号