consistent-return
要求 return
语句始终或从不指定值
与强制函数返回指定类型值的静态类型语言不同,JavaScript 允许函数中的不同代码路径返回不同类型的值。
JavaScript 的一个令人困惑的方面是,如果以下任何一项为真,则函数会返回 undefined
:
- 它在退出之前不执行
return
语句 - 它执行没有明确指定值的
return
- 它执行
return undefined
- 它执行
return void
后跟一个表达式(例如,函数调用) - 它执行
return
后跟任何其他计算结果为undefined
的表达式
如果函数中的任何代码路径显式返回值,但某些代码路径没有显式返回值,则可能是输入错误,尤其是在大型函数中。在以下示例中:
- qRTz+N9MOzCZIkCGrkjI0SGYQjDQ8ihLo+zz9/zUivi5tS0LFyuDvA6Xu2fdyonTxh6XDYewAaeME0wkb2KbIVZKptSr7ZMpc2rKmbqfNrChRIumwZaPEPL47L3jBUFozBJwpRgrscOmy6nbf8kPkhLZ9f91O8LGUjItRlUl0ooEww0q4IBbMzBqznP46eO/HrYszWKQyqAWovM+RiiwMtyAGoZMUWcYZLgB1Qrp+iU=
function doSomething(condition) {
if (condition) {
return true;
} else {
return;
}
}
规则详情
nk9AR4lL0TJq9GTSYEuEZcEKmnAqWk7fYOIjagNzJkoak0EGnkY/VOCROxTs32Von+4d8rC8WmMDhUSCMcenvGZ4NfjLDnuQ/DmvjCRxMU2Sd/MsJcZtuEtRQsrDksDP/dTyeyzzCea+sXnPzPhGXsCfOrXyoIMsOlVTwQPnLiwYq0odANcI27aWlDJF883rBH1XKa/ypANaUKQ8qI6mdjTTvls/PR6RIQBDGKaOMZOhks2y4fjE68tIlxYsv1K7ndq9CUu6UZ/zYqGkxpKzPzNMxOvgDgvt180Ljo7xkdbbfMf1jMwjjijhqYl1kDbWO0NLA24AxYK9iDb8AS7fFX+QhJ4/HTBYKLTC6bnxi5A=
NrJkth/joQKVo0Sp81iI+A06i9GX/slD/1LexJqzFAoI1nFjlJjjk8Q+BydcruTg
/*eslint consistent-return: "error"*/
function doSomething(condition) {
if (condition) {
return true;
} else {
return;
}
}
function doSomething(condition) {
if (condition) {
return true;
}
}
DzO9VpSFHJiTjGCgt1KSooNhAIn1o64th84QTOygwWSy7fBgj4TGxtI1vaW0J9Ta
/*eslint consistent-return: "error"*/
function doSomething(condition) {
if (condition) {
return true;
} else {
return false;
}
}
function Foo() {
if (!(this instanceof Foo)) {
return new Foo();
}
this.a = 0;
}
选项
gOiAZnAcPh/ECcp3u5icNJSL4np3GSzA+XO+UhJI0YeSIyAdFhbPsJ6o9wB61EzW
- PqpPJHQycY9CZVp3isG7yeHh/FSrUttSVR34Xn3QjhvOdx5REf1I657uuGSCOY5I9PeDH/toPOr7DwOxg7DzrD/pGUvC5KDiVL9oV6n378LxeaX3BiQ1IXB+GuDgVdh9yUILb+haPxu4NSjiQRMIZ5G5we/j27GEiHDq/+yzG8F18cGYfz5y0O/tm7DqS0sbaFthjk7sMsnTI/w2i9qiCFmZsZlnfBI+glpA5uAPCJRCnuN+yZPInHqKaXjgGBT/EyxFcwSsl4KPAzDlqZKul6lamqeoixjcTtSJslTCgFPQpv9MD/bD7AK9jZueRdJx/Wg12rYWzytNqWm1MaX9KAl3GJ3B0Y+obdp7ujaYSdVG1BZE5iTiJxdF6wEyprBR
treatUndefinedAsUnspecified
qwlXDFeOGAub+EzAJFeRQXjjs7eoDAQrBYT27HBfT6khfp6Pu/XueGOe12mPom+stGKZrzE3Sku6tGr12xbQ2NqF25pWXNYxDVMaUoIkn6YuDRld3sbYNrONRcTDBAqvAQH0Hl+fggxAoyfOwCy+cw==
/*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": false }]*/
function foo(callback) {
if (callback) {
return void callback();
}
// no return statement
}
function bar(condition) {
if (condition) {
return undefined;
}
// no return statement
}
yqV+SV60QvsOsYXkYvZsZQ+NpoVwFXC3DZLqMSgjOQYETC14CDtQ4lsSl3/yns91YPVTqIugRVY8xCEs2V/wmOQdujHsSYWJ+XqCTgGsY5E1WTCYHYSEoDu/xDQmO2U1DwZHb1oPjewLDb1o8VeCww==
/*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": true }]*/
function foo(callback) {
if (callback) {
return void callback();
}
return true;
}
function bar(condition) {
if (condition) {
return undefined;
}
return true;
}
yqV+SV60QvsOsYXkYvZsZQ+NpoVwFXC3DZLqMSgjOQYETC14CDtQ4lsSl3/yns91YPVTqIugRVY8xCEs2V/wmGL/Giu8yCxAQZuYgFZoajk6OHyRYFK8V2NSOLybhVzIntQIpF74Hcpr4X2JZkRWwQ==
/*eslint consistent-return: ["error", { "treatUndefinedAsUnspecified": true }]*/
function foo(callback) {
if (callback) {
return void callback();
}
// no return statement
}
function bar(condition) {
if (condition) {
return undefined;
}
// no return statement
}
何时不使用
9OOdkW7zdOfS2Byvf6/srNsx4S4uUIjncYonqO7mHRuos0egMugxE+UczlkckCm49OLcqUxrD/7iB7hW5JcA5ZwMtY/z+l6Q2ik9tFTUXaDQ7YNW/RxgMJK5TWBNCIY8fAn8s//R/Kj8uX1cNqFLQb9+qBoUgqhjE2wlVOMnwTo=