no-underscore-dangle
禁止在标识符中使用尾随下划线
此规则目前已冻结,不接受功能请求。
就标识符的命名约定而言,尾随下划线可能是 JavaScript 中最具争议的。尾随下划线是指在标识符的开头或结尾处的下划线,例如
var _foo;
在 JavaScript 中使用尾随下划线标记“私有”成员有着悠久的历史,最早可以追溯到 SpiderMonkey 添加了非标准方法,如 __defineGetter__()
。从那时起,使用单个下划线前缀已成为最流行的约定,用于指示成员不是对象的公共接口的一部分。
建议使用 ECMAScript 2022 中引入的正式私有类特性来封装私有数据和方法,而不是依赖于命名约定。
在标识符中允许尾随下划线纯粹是一种约定,对性能、可读性或复杂性没有影响。即使启用了此规则,它们也不具有与私有类特性相同的封装优势。
规则详情
此规则禁止在标识符中使用尾随下划线。
此规则的错误代码示例
/*eslint no-underscore-dangle: "error"*/
var ;
var ;
();
此规则的正确代码示例
/*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) => {};
选项
此规则有一个对象选项
"allow"
允许指定的标识符带有尾随下划线"allowAfterThis": false
(默认) 禁止在this
对象的成员中使用尾随下划线"allowAfterSuper": false
(默认) 禁止在super
对象的成员中使用尾随下划线"allowAfterThisConstructor": false
(默认) 禁止在this.constructor
对象的成员中使用尾随下划线"enforceInMethodNames": false
(默认) 允许在方法名称中使用尾随下划线"enforceInClassFields": false
(默认) 允许在 es2022 类字段名称中使用尾随下划线"allowInArrayDestructuring": true
(默认) 允许在数组解构赋值的变量名中使用尾随下划线"allowInObjectDestructuring": true
(默认) 允许在对象解构赋值的变量名中使用尾随下划线"allowFunctionParams": true
(默认) 允许在函数参数名中使用尾随下划线
allow
使用 { "allow": ["foo_", "_bar"] }
选项时,此规则的额外正确代码示例
/*eslint no-underscore-dangle: ["error", { "allow": ["foo_", "_bar"] }]*/
var foo_;
foo._bar();
allowAfterThis
使用 { "allowAfterThis": true }
选项时,此规则的正确代码示例
/*eslint no-underscore-dangle: ["error", { "allowAfterThis": true }]*/
var a = this.foo_;
this._bar();
allowAfterSuper
使用 { "allowAfterSuper": true }
选项时,此规则的正确代码示例
/*eslint no-underscore-dangle: ["error", { "allowAfterSuper": true }]*/
class Foo extends Bar {
doSomething() {
var a = super.foo_;
super._bar();
}
}
allowAfterThisConstructor
使用 { "allowAfterThisConstructor": true }
选项时,此规则的正确代码示例
/*eslint no-underscore-dangle: ["error", { "allowAfterThisConstructor": true }]*/
var a = this.constructor.foo_;
this.constructor._bar();
enforceInMethodNames
使用 { "enforceInMethodNames": true }
选项时,此规则的错误代码示例
/*eslint no-underscore-dangle: ["error", { "enforceInMethodNames": true }]*/
class Foo {
}
class Bar {
}
const o1 = {
};
const o2 = {
};
enforceInClassFields
使用 { "enforceInClassFields": true }
选项时,此规则的错误代码示例
/*eslint no-underscore-dangle: ["error", { "enforceInClassFields": true }]*/
class Foo {
}
class Bar {
}
class Baz {
}
class Qux {
}
class FooBar {
}
allowInArrayDestructuring
使用 { "allowInArrayDestructuring": false }
选项时,此规则的错误代码示例
/*eslint no-underscore-dangle: ["error", { "allowInArrayDestructuring": false }]*/
const ;
const ;
const ;
allowInObjectDestructuring
使用 { "allowInObjectDestructuring": false }
选项时,此规则的错误代码示例
/*eslint no-underscore-dangle: ["error", { "allowInObjectDestructuring": false }]*/
const ;
const ;
使用 { "allowInObjectDestructuring": false }
选项时,此规则的正确代码示例
/*eslint no-underscore-dangle: ["error", { "allowInObjectDestructuring": false }]*/
const { foo, bar, _baz: { a, b } } = collection;
const { qux, xyz, _baz: baz } = collection;
allowFunctionParams
使用 { "allowFunctionParams": false }
选项时,此规则的错误代码示例
/*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 = () => {};
何时不使用它
如果您想在标识符中允许尾随下划线,那么您可以安全地关闭此规则。
版本
此规则在 ESLint v0.0.9 中引入。