]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-mixed-requires.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / no-mixed-requires.md
1 # disallow `require` calls to be mixed with regular variable declarations (no-mixed-requires)
2
3 This rule was **deprecated** in ESLint v7.0.0. Please use the corresponding rule in [`eslint-plugin-node`](https://github.com/mysticatea/eslint-plugin-node).
4
5 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.
6
7 ## Rule Details
8
9 When this rule is enabled, each `var` statement must satisfy the following conditions:
10
11 * either none or all variable declarations must be require declarations (default)
12 * all require declarations must be of the same type (grouping)
13
14 This rule distinguishes between six kinds of variable declaration types:
15
16 * `core`: declaration of a required [core module][1]
17 * `file`: declaration of a required [file module][2]
18 * `module`: declaration of a required module from the [node_modules folder][3]
19 * `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)
20 * `uninitialized`: a declaration that is not initialized
21 * `other`: any other kind of declaration
22
23 In this document, the first four types are summed up under the term *require declaration*.
24
25 ```js
26 var fs = require('fs'), // "core" \
27 async = require('async'), // "module" |- these are "require declaration"s
28 foo = require('./foo'), // "file" |
29 bar = require(getName()), // "computed" /
30 baz = 42, // "other"
31 bam; // "uninitialized"
32 ```
33
34 ## Options
35
36 This rule can have an object literal option whose two properties have `false` values by default.
37
38 Configuring this rule with one boolean option `true` is deprecated.
39
40 Examples of **incorrect** code for this rule with the default `{ "grouping": false, "allowCall": false }` options:
41
42 ```js
43 /*eslint no-mixed-requires: "error"*/
44
45 var fs = require('fs'),
46 i = 0;
47
48 var async = require('async'),
49 debug = require('diagnostics').someFunction('my-module'),
50 eslint = require('eslint');
51 ```
52
53 Examples of **correct** code for this rule with the default `{ "grouping": false, "allowCall": false }` options:
54
55 ```js
56 /*eslint no-mixed-requires: "error"*/
57
58 // only require declarations (grouping off)
59 var eventEmitter = require('events').EventEmitter,
60 myUtils = require('./utils'),
61 util = require('util'),
62 bar = require(getBarModuleName());
63
64 // only non-require declarations
65 var foo = 42,
66 bar = 'baz';
67
68 // always valid regardless of grouping because all declarations are of the same type
69 var foo = require('foo' + VERSION),
70 bar = require(getBarModuleName()),
71 baz = require();
72 ```
73
74 ### grouping
75
76 Examples of **incorrect** code for this rule with the `{ "grouping": true }` option:
77
78 ```js
79 /*eslint no-mixed-requires: ["error", { "grouping": true }]*/
80
81 // invalid because of mixed types "core" and "module"
82 var fs = require('fs'),
83 async = require('async');
84
85 // invalid because of mixed types "file" and "unknown"
86 var foo = require('foo'),
87 bar = require(getBarModuleName());
88 ```
89
90 ### allowCall
91
92 Examples of **incorrect** code for this rule with the `{ "allowCall": true }` option:
93
94 ```js
95 /*eslint no-mixed-requires: ["error", { "allowCall": true }]*/
96
97 var async = require('async'),
98 debug = require('diagnostics').someFunction('my-module'), /* allowCall doesn't allow calling any function */
99 eslint = require('eslint');
100 ```
101
102 Examples of **correct** code for this rule with the `{ "allowCall": true }` option:
103
104 ```js
105 /*eslint no-mixed-requires: ["error", { "allowCall": true }]*/
106
107 var async = require('async'),
108 debug = require('diagnostics')('my-module'),
109 eslint = require('eslint');
110 ```
111
112 ## Known Limitations
113
114 * The implementation is not aware of any local functions with the name `require` that may shadow Node.js' global `require`.
115
116 * 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.
117 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.
118
119 ## When Not To Use It
120
121 If you use a pattern such as [UMD][4] where the `require`d modules are not loaded in variable declarations, this rule will obviously do nothing for you.
122
123 [1]: https://nodejs.org/api/modules.html#modules_core_modules
124 [2]: https://nodejs.org/api/modules.html#modules_file_modules
125 [3]: https://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders
126 [4]: https://github.com/umdjs/umd