2 * @fileoverview Rule to enforce grouped require statements for Node.JS
3 * @author Raphael Pigulla
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
17 description
: "disallow `require` calls to be mixed with regular variable declarations",
18 category
: "Node.js and CommonJS",
20 url
: "https://eslint.org/docs/rules/no-mixed-requires"
39 additionalProperties
: false
46 noMixRequire
: "Do not mix 'require' and other declarations.",
47 noMixCoreModuleFileComputed
: "Do not mix core, module, file and computed requires."
53 const options
= context
.options
[0];
57 if (typeof options
=== "object") {
58 grouping
= options
.grouping
;
59 allowCall
= options
.allowCall
;
65 * Returns the list of built-in modules.
66 * @returns {string[]} An array of built-in Node.js modules.
68 function getBuiltinModules() {
71 * This list is generated using:
72 * `require("repl")._builtinLibs.concat('repl').sort()`
73 * This particular list is as per nodejs v0.12.2 and iojs v0.7.1
76 "assert", "buffer", "child_process", "cluster", "crypto",
77 "dgram", "dns", "domain", "events", "fs", "http", "https",
78 "net", "os", "path", "punycode", "querystring", "readline",
79 "repl", "smalloc", "stream", "string_decoder", "tls", "tty",
80 "url", "util", "v8", "vm", "zlib"
84 const BUILTIN_MODULES
= getBuiltinModules();
86 const DECL_REQUIRE
= "require",
87 DECL_UNINITIALIZED
= "uninitialized",
90 const REQ_CORE
= "core",
92 REQ_MODULE
= "module",
93 REQ_COMPUTED
= "computed";
96 * Determines the type of a declaration statement.
97 * @param {ASTNode} initExpression The init node of the VariableDeclarator.
98 * @returns {string} The type of declaration represented by the expression.
100 function getDeclarationType(initExpression
) {
101 if (!initExpression
) {
104 return DECL_UNINITIALIZED
;
107 if (initExpression
.type
=== "CallExpression" &&
108 initExpression
.callee
.type
=== "Identifier" &&
109 initExpression
.callee
.name
=== "require"
112 // "var x = require('util');"
116 initExpression
.type
=== "CallExpression" &&
117 initExpression
.callee
.type
=== "CallExpression"
120 // "var x = require('diagnose')('sub-module');"
121 return getDeclarationType(initExpression
.callee
);
123 if (initExpression
.type
=== "MemberExpression") {
125 // "var x = require('glob').Glob;"
126 return getDeclarationType(initExpression
.object
);
134 * Determines the type of module that is loaded via require.
135 * @param {ASTNode} initExpression The init node of the VariableDeclarator.
136 * @returns {string} The module type.
138 function inferModuleType(initExpression
) {
139 if (initExpression
.type
=== "MemberExpression") {
141 // "var x = require('glob').Glob;"
142 return inferModuleType(initExpression
.object
);
144 if (initExpression
.arguments
.length
=== 0) {
146 // "var x = require();"
150 const arg
= initExpression
.arguments
[0];
152 if (arg
.type
!== "Literal" || typeof arg
.value
!== "string") {
154 // "var x = require(42);"
158 if (BUILTIN_MODULES
.indexOf(arg
.value
) !== -1) {
160 // "var fs = require('fs');"
163 if (/^\.{0,2}\//u.test(arg
.value
)) {
165 // "var utils = require('./utils');"
169 // "var async = require('async');"
175 * Check if the list of variable declarations is mixed, i.e. whether it
176 * contains both require and other declarations.
177 * @param {ASTNode} declarations The list of VariableDeclarators.
178 * @returns {boolean} True if the declarations are mixed, false if not.
180 function isMixed(declarations
) {
183 declarations
.forEach(declaration
=> {
184 const type
= getDeclarationType(declaration
.init
);
186 contains
[type
] = true;
190 contains
[DECL_REQUIRE
] &&
191 (contains
[DECL_UNINITIALIZED
] || contains
[DECL_OTHER
])
196 * Check if all require declarations in the given list are of the same
198 * @param {ASTNode} declarations The list of VariableDeclarators.
199 * @returns {boolean} True if the declarations are grouped, false if not.
201 function isGrouped(declarations
) {
204 declarations
.forEach(declaration
=> {
205 if (getDeclarationType(declaration
.init
) === DECL_REQUIRE
) {
206 found
[inferModuleType(declaration
.init
)] = true;
210 return Object
.keys(found
).length
<= 1;
216 VariableDeclaration(node
) {
218 if (isMixed(node
.declarations
)) {
221 messageId
: "noMixRequire"
223 } else if (grouping
&& !isGrouped(node
.declarations
)) {
226 messageId
: "noMixCoreModuleFileComputed"