no-mixed-requires
不允许 require 调用与常规变量声明混合
This rule was deprecated in ESLint v7.0.0. It will be removed in v11.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 声明(默认)
- 所有要求声明必须是同一类型(分组)
该规则区分了六种变量声明类型:
🌐 This rule distinguishes between six kinds of variable declaration types:
core:必需的 核心模块 声明file:必需的 文件模块 声明module:从 node_modules 文件夹 声明所需的模块computed:无法确定其类型的必需模块的声明(因为它是计算的或因为 require 是在没有参数的情况下调用的)uninitialized:未初始化的声明other:任何其他类型的声明
在本文档中,前四种类型归纳在术语require 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。 - 在内部,核心模块的列表是通过
require("repl")._builtinLibs检索的。如果你在 ESLint 和你的应用中使用不同版本的 Node.js,那么每个版本的核心模块列表可能会不同。上述提到的_builtinLibs属性在 0.8 版本中可用,对于更早的版本,将使用硬编码的模块名称列表作为回退。如果你的 Node.js 版本早于 0.6,该列表可能不准确。
何时不使用
🌐 When Not To Use It
如果你使用像 马里兰大学 这样的模式,而 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 中引入。