1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
# Enforce line-break rules for AMD dependencies (one-dependency-per-line)
When more than a handful of dependencies are required in a module, the list can quickly become too long to fit on one line. Inconsistent line break usage can also make it hard to read and maintain long lists of dependencies.
## Rule Details
This rule aims to enforce consistent line-break usage in dependency lists.
### Options
The rule takes one option, an object, which has two keys, `paths` and `names`. These keys allow individual control over the line-break rules for, respectively, the dependency path list and the list of names (arguments) passed to the module definition function. Each key can either be a string `"always"` or `"never"`, or a numeric value representing the maximum number of dependencies each list can contain before enforcing the one-dependency-per-line rule. The default is `{ "paths": "always", "names": "always" }`.
```json
"one-dependency-per-line": [2, { "paths": "always", "names": "never" }]
"one-dependency-per-line": [2, { "paths": 3, "names": 3 }]
```
The following examples demonstate the behavior of these options.
#### { "paths": "always", "names": "always" }
When the mode is set to `"always"`, each dependency must be placed on a separate line.
In this mode, the following pattern is considered a warning:
```js
require(['jquery', 'underscore', 'backbone'], function ($, _, Backbone) {
/* ... */
});
```
Whereas, the following pattern is *not* considered a warning:
```js
require([
'jquery',
'underscore',
'backbone'
], function (
$,
_,
Backbone
) {
/* ... */
});
```
#### { "paths": "never", "names": "never" }
When the mode is set to `"never"`, dependency lists must appear on one line.
In this mode, the following pattern is considered a warning:
```js
require([
'jquery',
'underscore',
'backbone'
], function (
$,
_,
Backbone
) {
/* ... */
});
```
Whereas, the following pattern is *not* considered a warning:
```js
require(['jquery', 'underscore', 'backbone'], function ($, _, Backbone) {
/* ... */
});
```
#### { "paths": 3, "names": 3 }
When a number value is provided, it defines the maximum number of dependencies a list can contain before line-breaking is enforced for all dependencies in that list. This allows for very short dependency lists to appear on a single line, but longer ones to spaced appropriately.
In this mode, the following patterns are considered warnings:
```js
// "one-dependency-per-line": [2, { "paths": "always", "names": 3 }]
require([
'jquery',
'underscore',
'backbone',
'some/other/module'
], function ($, _, Backbone, SomeOtherModule) {
/* ... */
});
// "one-dependency-per-line": [2, { "paths": 2, "names": 2 }]
require(['jquery', 'underscore', 'backbone'], function ($, _, Backbone) {
/* ... */
});
```
Whereas, the following patterns are *not* considered warnings:
```js
// "one-dependency-per-line": [2, { "paths": "always", "names": 3 }]
require([
'jquery',
'underscore',
'backbone',
'some/other/module'
], function (
$,
_,
Backbone,
SomeOtherModule
) {
/* ... */
});
// "one-dependency-per-line": [2, { "paths": 2, "names": 2 }]
require(['jquery', 'underscore'], function ($, _) {
/* ... */
});
```
## When Not To Use It
If your project will not be following any consistent line-break pattern for dependencies, it is safe to turn off this rule.
## Further Reading
* [Define a Module](http://requirejs.org/docs/api.html#define)
|