]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/no-underscore-dangle.md
import 7.12.1 upstream release
[pve-eslint.git] / eslint / docs / rules / no-underscore-dangle.md
CommitLineData
eb39fafa
DC
1# disallow dangling underscores in identifiers (no-underscore-dangle)
2
3As far as naming conventions for identifiers go, dangling underscores may be the most polarizing in JavaScript. Dangling underscores are underscores at either the beginning or end of an identifier, such as:
4
5```js
6var _foo;
7```
8
9There is actually a long history of using dangling underscores to indicate "private" members of objects in JavaScript (though JavaScript doesn't have truly private members, this convention served as a warning). This began with SpiderMonkey adding nonstandard methods such as `__defineGetter__()`. The intent with the underscores was to make it obvious that this method was special in some way. Since that time, using a single underscore prefix has become popular as a way to indicate "private" members of objects.
10
11Whether or not you choose to allow dangling underscores in identifiers is purely a convention and has no effect on performance, readability, or complexity. It's purely a preference.
12
13## Rule Details
14
15This rule disallows dangling underscores in identifiers.
16
17Examples of **incorrect** code for this rule:
18
19```js
20/*eslint no-underscore-dangle: "error"*/
21
22var foo_;
23var __proto__ = {};
24foo._bar();
25```
26
27Examples of **correct** code for this rule:
28
29```js
30/*eslint no-underscore-dangle: "error"*/
31
32var _ = require('underscore');
33var obj = _.contains(items, item);
34obj.__proto__ = {};
35var file = __filename;
6f036462
TL
36function foo(_bar) {};
37const foo = { onClick(_bar) {} };
38const foo = (_bar) => {};
eb39fafa
DC
39```
40
41## Options
42
43This rule has an object option:
44
6f036462
TL
45- `"allow"` allows specified identifiers to have dangling underscores
46- `"allowAfterThis": false` (default) disallows dangling underscores in members of the `this` object
47- `"allowAfterSuper": false` (default) disallows dangling underscores in members of the `super` object
48- `"allowAfterThisConstructor": false` (default) disallows dangling underscores in members of the `this.constructor` object
49- `"enforceInMethodNames": false` (default) allows dangling underscores in method names
50- `"allowFunctionParams": true` (default) allows dangling underscores in function parameter names
eb39fafa
DC
51
52### allow
53
54Examples of additional **correct** code for this rule with the `{ "allow": ["foo_", "_bar"] }` option:
55
56```js
57/*eslint no-underscore-dangle: ["error", { "allow": ["foo_", "_bar"] }]*/
58
59var foo_;
60foo._bar();
61```
62
63### allowAfterThis
64
65Examples of **correct** code for this rule with the `{ "allowAfterThis": true }` option:
66
67```js
68/*eslint no-underscore-dangle: ["error", { "allowAfterThis": true }]*/
69
70var a = this.foo_;
71this._bar();
72```
73
74### allowAfterSuper
75
76Examples of **correct** code for this rule with the `{ "allowAfterSuper": true }` option:
77
78```js
79/*eslint no-underscore-dangle: ["error", { "allowAfterSuper": true }]*/
80
81var a = super.foo_;
82super._bar();
83```
84
85### allowAfterThisConstructor
86
87Examples of **correct** code for this rule with the `{ "allowAfterThisConstructor": true }` option:
88
89```js
90/*eslint no-underscore-dangle: ["error", { "allowAfterThisConstructor": true }]*/
91
92var a = this.constructor.foo_;
93this.constructor._bar();
94```
95
96### enforceInMethodNames
97
98Examples of **incorrect** code for this rule with the `{ "enforceInMethodNames": true }` option:
99
100```js
101/*eslint no-underscore-dangle: ["error", { "enforceInMethodNames": true }]*/
102
103class Foo {
104 _bar() {}
105}
106
107class Foo {
108 bar_() {}
109}
110
111const o = {
112 _bar() {}
113};
114
115const o = {
116 bar_() = {}
117};
118```
119
6f036462
TL
120### allowFunctionParams
121
122Examples of **incorrect** code for this rule with the `{ "allowFunctionParams": false }` option:
123
124```js
125/*eslint no-underscore-dangle: ["error", { "allowFunctionParams": false }]*/
126
127function foo (_bar) {}
128function foo (_bar = 0) {}
129function foo (..._bar) {}
130
131const foo = function onClick (_bar) {}
132const foo = function onClick (_bar = 0) {}
133const foo = function onClick (..._bar) {}
134
135const foo = (_bar) => {};
136const foo = (_bar = 0) => {};
137const foo = (..._bar) => {};
138```
139
eb39fafa
DC
140## When Not To Use It
141
142If you want to allow dangling underscores in identifiers, then you can safely turn this rule off.