no-mixed-requires
此规则在 ESLint v7.0.0 中已弃用。请使用 eslint-plugin-node
中的相应规则。
在 Node.js 社区中,通常习惯将调用 require
模块的初始化与其他变量声明分开,有时还按模块类型对它们进行分组。此规则可帮助您执行此约定。
规则详情
启用此规则后,每条 var
语句必须满足以下条件:
- none 或 all 变量声明必须是 require 声明(默认)
- 所有要求声明必须是同一类型(分组)
该规则区分了六种变量声明类型:
- FrRM57xYU4dvKU81EozqiIBJ7RkRMVbJuwxG98pIKWa6L12YrXuabdS8TNiCdv33wdFA2zNXldgbgRxgUZLyxTNAxwc9wwtqD/s4fptBb6dMLS9bX+q+o3XKgeFcwglt9g8HJVwtFpRvAR6Si7jiUYdDUbHeFCyAu9ZJmjaWUG3FpDFEp3X9FJ8OXRYgT/pnRb5K4acObIk3Y6tWWWFKanXgSWxQ5gnfVMcSV0fMM6xPQ24DFehdlwv8AP36cOCaDeUXahQLTHU3MyhOc0420VPHOpczTBpYD6XAZol/btQc4hXyysu8PD1Zj/ceUkmK08VBJ3qR+ALyr9yUcSeWKdQG6TfupFpiUKuJbpE2pIs92ZB809/Glj8Q4jcpLkgOzeSGBZQGfrZngHQAXYCWYB4Zr8cT2Hy+Q1GBFU8ktg1dk/VvSz3b+EZcmNfZ9hNLZBYpMuHeabiI8uc7bJCyXd18866LOPlGmDFQaDXrbWd7+HAMnT05s5p5mykzM439FxxS7CmbtsOVz0E7qnfDmNt6bbvuvL4ygASPknP4RpqTWkfZt7WWAuVahC8By6PzBi+ZGRjhSmJ17zOEklxgudFWcqYGIBwoXUTQf8SeKbTnkQLSeslNRvKzvh3NJa6fagjFqwp5dn71nYFSYrfEEuR14MbZUKLYG6SuM0HosP38ICQCTnrYyl5qOrGhkCEhlvfm/VT37A29VtMPt8PUTzNLmUOXOxRrO0cWjkYa1La1nukWkkYXNfWRMsEYt9ka6BpKz+LyCe0psvpzSPXLZbvWxS0egzQHYyETPalYKL9iwHouSiHzxzsMz5rM2PjyBtZbBFkcrGRQKx1y3hmOcg==
5NHnc6fhghs5vNZbIvr6HB4ADvGMoy66QiV/viGh+JSr7WAzN69vMNBfSCaTkNZnfwGCwR/4+OuiJ5jwyuJmOr6Rzq5ogE9ouZqd41ykaJ4=
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"
选项
pDSmzSxlBjG3F8gvt/L6HyGOGLW3Fs1KzRYDlxa4peVhWGID4bPbamA2aKgMPGzgKFBwFIKW6C7JjXHHWBarCsrshhp1fhjnkhejkVpmOIaxVrtE9/2em7WaPXfYIGsgaM7YpFYKqHEkf9BpDBDuGw==
2fqeg/jXYEFxLjEbjsR1oY+sDxdK3ODb0c5YrYIyPueoJc62xmC9a/JQY8DoQ9buEPiErmcdJ9EMq4ZOmuacBlEnBe7SKl6tWV4UV0q9+GM=
pdhvGSNJbUDyQjJJdpNBf97XPBqKEyZjhs4ypQz3WWduonUiOHA0KHejO3Ji7xdfCiJgWtyy/rVyhjqhd/Jd7tGj69M7DCcRmbMA/lx1Jxb8DVd8wzS0jZbQFnrRgvEnmBqPV6ymRNw+lVef1if2jA==
/*eslint no-mixed-requires: "error"*/
var fs = require('fs'),
i = 0;
var async = require('async'),
debug = require('diagnostics').someFunction('my-module'),
eslint = require('eslint');
pdhvGSNJbUDyQjJJdpNBf97XPBqKEyZjhs4ypQz3WWduonUiOHA0KHejO3Ji7xdfCiJgWtyy/rVyhjqhd/Jd7tGj69M7DCcRmbMA/lx1JxbbzfRWSQdzC4D2/UuvjGJ0cmzeiaIVrm1OavGk4Ynjbw==
/*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
yqV+SV60QvsOsYXkYvZsZW0Uyez5p3M4zb5Yu+M5K+cBs2mK7FYYnp6qkPgRKMsmaUhPgSYYQBLtJYXN8k24zzfVt3qni3uh4PLzI+Sfg658cA6oX7b3c/x9r6dBD14M
/*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
yqV+SV60QvsOsYXkYvZsZW7fIQRkdI0KbtE3VQa4JONdLSltZIymcE0Uio5nThAzjLvsmG+H0ywc1GDVJ3BPEXmpzLiN41i56pWX9d8pSS7fGv4YAEOpe1ZCpkgNdlSk
/*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');
yqV+SV60QvsOsYXkYvZsZW7fIQRkdI0KbtE3VQa4JONdLSltZIymcE0Uio5nThAzAQ26JDKt82aMxheJYgIigEZL4+6uMZvxs+jFmHM8flMiXfIJvMtp9h+3SAl7zL2z
/*eslint no-mixed-requires: ["error", { "allowCall": true }]*/
var async = require('async'),
debug = require('diagnostics')('my-module'),
eslint = require('eslint');
已知限制
- yYF+wdsEy4Xz1WgxDdrSx/FExBsYjec/WpCgllGsTedGBiXW/6VGBc2aioiTZzzyGEImW1DCTua3HjbsY/Iv3VencWkYpGj+8C1PvY9hs7p3MgiMUp3IUO/K5Nyvjb8lG7Nyiw3HErUs9nLvSqNiy6taDedpiQOnWXf7JZJbpWmWVbCyz2Xf57NPIQ0mPZAj7n0ZV4DkifFzE5gNB+loRsZfgusSqpKfAfB8aiq44LvbSDa3eAU79xw6stlKy9gjPXwdUHqGS6iMLqSMWftxiTiJVuJyN4vubB9gbPUXp6DyZkFyfpKmW9doVDX2XnJyKDDWcfZ6IGbeTMrVAjHu5/c/HLTBr2RDW+GQOxaItmINhMm/VAGibX5AmZB/WZVPNny6nkGMSfUI4EBJ1QT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