no-underscore-dangle
Disallow dangling underscores in identifiers
This rule is currently frozen and is not accepting changes.
As 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:
var _foo;
There is a long history of marking “private” members with dangling underscores in JavaScript, beginning with SpiderMonkey adding nonstandard methods such as __defineGetter__()
. Since that time, using a single underscore prefix has become the most popular convention for indicating a member is not part of the public interface of an object.
It is recommended to use the formal private class features introduced in ECMAScript 2022 for encapsulating private data and methods rather than relying on naming conventions.
Allowing dangling underscores in identifiers is purely a convention and has no effect on performance, readability, or complexity. They do not have the same encapsulation benefits as private class features, even with this rule enabled.
Rule Details
This rule disallows dangling underscores in identifiers.
Examples of incorrect code for this rule:
/*eslint no-underscore-dangle: "error"*/
var ;
var ;
();
Examples of correct code for this rule:
/*eslint no-underscore-dangle: "error"*/
var _ = require('underscore');
var obj = _.contains(items, item);
obj.__proto__ = {};
var file = __filename;
function foo(_bar) {};
const bar = { onClick(_bar) {} };
const baz = (_bar) => {};
Options
This rule has an object option:
"allow"
allows specified identifiers to have dangling underscores"allowAfterThis": false
(default) disallows dangling underscores in members of thethis
object"allowAfterSuper": false
(default) disallows dangling underscores in members of thesuper
object"allowAfterThisConstructor": false
(default) disallows dangling underscores in members of thethis.constructor
object"enforceInMethodNames": false
(default) allows dangling underscores in method names"enforceInClassFields": false
(default) allows dangling underscores in es2022 class fields names"allowInArrayDestructuring": true
(default) allows dangling underscores in variable names assigned by array destructuring"allowInObjectDestructuring": true
(default) allows dangling underscores in variable names assigned by object destructuring"allowFunctionParams": true
(default) allows dangling underscores in function parameter names
allow
Examples of additional correct code for this rule with the { "allow": ["foo_", "_bar"] }
option:
/*eslint no-underscore-dangle: ["error", { "allow": ["foo_", "_bar"] }]*/
var foo_;
foo._bar();
allowAfterThis
Examples of correct code for this rule with the { "allowAfterThis": true }
option:
/*eslint no-underscore-dangle: ["error", { "allowAfterThis": true }]*/
var a = this.foo_;
this._bar();
allowAfterSuper
Examples of correct code for this rule with the { "allowAfterSuper": true }
option:
/*eslint no-underscore-dangle: ["error", { "allowAfterSuper": true }]*/
class Foo extends Bar {
doSomething() {
var a = super.foo_;
super._bar();
}
}
allowAfterThisConstructor
Examples of correct code for this rule with the { "allowAfterThisConstructor": true }
option:
/*eslint no-underscore-dangle: ["error", { "allowAfterThisConstructor": true }]*/
var a = this.constructor.foo_;
this.constructor._bar();
enforceInMethodNames
Examples of incorrect code for this rule with the { "enforceInMethodNames": true }
option:
/*eslint no-underscore-dangle: ["error", { "enforceInMethodNames": true }]*/
class Foo {
}
class Bar {
}
const o1 = {
};
const o2 = {
};
enforceInClassFields
Examples of incorrect code for this rule with the { "enforceInClassFields": true }
option:
/*eslint no-underscore-dangle: ["error", { "enforceInClassFields": true }]*/
class Foo {
}
class Bar {
}
class Baz {
}
class Qux {
}
class FooBar {
}
allowInArrayDestructuring
Examples of incorrect code for this rule with the { "allowInArrayDestructuring": false }
option:
/*eslint no-underscore-dangle: ["error", { "allowInArrayDestructuring": false }]*/
const ;
const ;
const ;
allowInObjectDestructuring
Examples of incorrect code for this rule with the { "allowInObjectDestructuring": false }
option:
/*eslint no-underscore-dangle: ["error", { "allowInObjectDestructuring": false }]*/
const ;
const ;
Examples of correct code for this rule with the { "allowInObjectDestructuring": false }
option:
/*eslint no-underscore-dangle: ["error", { "allowInObjectDestructuring": false }]*/
const { foo, bar, _baz: { a, b } } = collection;
const { qux, xyz, _baz: baz } = collection;
allowFunctionParams
Examples of incorrect code for this rule with the { "allowFunctionParams": false }
option:
/*eslint no-underscore-dangle: ["error", { "allowFunctionParams": false }]*/
function foo1 () {}
function foo2 () {}
function foo3 () {}
const foo4 = function onClick () {}
const foo5 = function onClick () {}
const foo6 = function onClick () {}
const foo7 = () => {};
const foo8 = () => {};
const foo9 = () => {};
When Not To Use It
If you want to allow dangling underscores in identifiers, then you can safely turn this rule off.
Version
This rule was introduced in ESLint v0.0.9.