no-shadow
禁止在外部作用域中声明的隐藏变量中的变量声明
影子化是局部变量与其包含范围内的变量共享相同名称的过程。例如:
var a = 3;
function b() {
var a = 10;
}
在这种情况下,b()
内部的变量 a
将在全局范围内隐藏变量 a
。这可能会在阅读代码时造成混乱,并且无法访问全局变量。
规则详情
该规则旨在消除阴影变量声明。
此规则的错误代码示例:
/*eslint no-shadow: "error"*/
/*eslint-env es6*/
var a = 3;
function b() {
var a = 10;
}
var b = function () {
var a = 10;
}
function b(a) {
a = 10;
}
b(a);
if (true) {
let a = 5;
}
选项
RIRJKUz29INEex/qlHKVkPMuK7ZuPJLOkqYXJyknAfBwkqZ2oL+rk4jXxsOJhRehVVAKufAYeTexU0uKrwKlGpwH4WfV3UIB9OjtEhQnNn5K8vxwd1w3kPqSR/eW/AWCN8JH6e6UXy8VlPLWd5BTteTCxNqWIvOB7qSNfA+l7gEVcf027UjvBfaK0ChRKvT1hjrVHDA7ON1IcWFcWcMtp10nPybJjzwLFEVZ6q7fi/k=
{
"no-shadow": ["error", { "builtinGlobals": false, "hoist": "functions", "allow": [], "ignoreOnInitialization": false }]
}
builtinGlobals
fsIJjKSuEh79NDA6j6rE+oJSPLdLdn3pTVN/5iOAQhe3+HegNSxDM1IK4IEo4zu9v1YjEHCg55fCfdghxZIZZuuyHszXNArwScNj/0p22rxVUA0nXYsCIyr2JEu5sN5W0fEPj2YMtDY9Yhq/OZAeVE8N4gMiq9xdoO8oldujmcrc7Nkj3JQmXK3JNjtNKU9vrZfdrmwqRuE7d9CPEbUhR3JtmIccSCbeysMBBdtNyGYqtCa3rLfaLVXs8XM+YP71fongEd36eXHZN+Gk3VXAgiznfJAnB8g+JgylrOOAreg=
3yaKlrGaNCib2szuO2Z8o7/0eAZXJOU84uJWJhUzZD5BCK7L2ZovGQ4eMuoYTcAN9/CCpLADXDi0nZEYydbPZQk/xJI7BZIut0oZua9cBuY=
/*eslint no-shadow: ["error", { "builtinGlobals": true }]*/
function foo() {
var Object = 0;
}
hoist
tJydBF/QKldXFhDR7c4XnxNluRMY8xYk1MGixEdNfRwuNpEhba/BDQquwPucfxbI
- ZfmzyxGDCJKH1B3eVkt67Q3wb+zVemr4s2Fiobg6X+7bYUdLeMueNznx39ocq4jKdIDjt6Z6i5qixjryACTiUY3wo3UUN3U6vB0RTEwYVwOgFQaSL+sjlph+MEco5i5CCjziC0oVSdodSwzOR4OfJ+wagp+kgirrlhdjLplkPwuDBv8097UlWpgYbVnudS07whX3Grb0P6OBbph25irYEeeopxHIbSkc1+1DykP+eSZj6Vo5+azjBGi3n80scoEbHZKJ9kpDxvkXm927uhmXADYWCLMp2b9Qnmlpc/0P1wi4o7q8+S32PI7n4Qtq3aRTMu2GhzbGV0WCQh5K88SKLeoJqb0DAh0e5EVibnqQCDI=
hoist: functions
13LMgLE6Tl3R+WKswGosFgsJqxfjcVNtuIIZ7p1Q/Hwnu2j+W5Bk2h0d/zFzqrrz0e0aB7rIqSiU67/VLgxSD4gqIlYspQU19Rnw7koyHlM=
/*eslint no-shadow: ["error", { "hoist": "functions" }]*/
/*eslint-env es6*/
if (true) {
let b = 6;
}
function b() {}
/fqM8VxYnP8xi3woJzuwyUqtXjjDW75St9FynNjNtgmwYdvKAmgSfmHeTTmooLoyLOCWQea+vTiQzDoxur8ST+tquPQfunITdC6kdEvFlg1oIlGMT1Z1E0TH1q1gyoP0oBF8gY+o51U7slYwrpRESsPKZpvn3rHDgcxb+IUnoPM=
13LMgLE6Tl3R+WKswGosFgsJqxfjcVNtuIIZ7p1Q/Hwnu2j+W5Bk2h0d/zFzqrrzMURjMQwUdFRG0e8CkqPuZIkJDVjCWHAuQVOsizivzhI=
/*eslint no-shadow: ["error", { "hoist": "functions" }]*/
/*eslint-env es6*/
if (true) {
let a = 3;
}
let a = 5;
06Y+9R886tSG3SHU51SMdjrkq9MEsKKVE1xxkJgQI+4J+/eFP0FrPii0BK35K2Vcm2FbVjRj037n1hMTjSIHMmHYHhAA8XYJfUPVLzTvyelR3CdBXLziugkUWyZPvdQESJ+6Bwj5Pu7sa5xlLtB8faiLag/k9+J3IE9hFtaa1v0=
hoist: all
jqDADLyus5e98OP5SgeauPW1w4ayQCShQuF81ykRpEcqR5bgkLVE9FiEx03Qf46RtdPXScqOtnEIE2jnfPg/og==
/*eslint no-shadow: ["error", { "hoist": "all" }]*/
/*eslint-env es6*/
if (true) {
let a = 3;
let b = 6;
}
let a = 5;
function b() {}
hoist: never
jqDADLyus5e98OP5SgeauFrbs1yibLVnTKf1dLWnic6htiOdNb82SY1fj2yFPixtjbOwKqLHYILxtJRf8luP/0pI8yf3IbU9sw5r1zFkmIc=
/*eslint no-shadow: ["error", { "hoist": "never" }]*/
/*eslint-env es6*/
if (true) {
let a = 3;
let b = 6;
}
let a = 5;
function b() {}
06Y+9R886tSG3SHU51SMdjrkq9MEsKKVE1xxkJgQI+4J+/eFP0FrPii0BK35K2VcHfCBJpysWKDhwUPji0b1i9Q+FlxtH/y/njuTNnf58jcTs/l2b6Dxg9HJtwfhfbKFKUYItlCcKsx+WXt/cpRvOgRVV8azDKwMZ/02EHhj9K9w2v9iMIeKXjs2wYXCRyBP
allow
NL6wEhNqRBpE+3im+NUVZ514sfM6Y/DVSQi1QjgItY0WnnVdDmGKLCoJPL7kgtyeWodMrh/t50K8lTEYmVIVr5XD6xkuq06lj7a0cCCxedygZVTViTEUolJkPwGl+gA084nrVsdMWoNolczfgOfl6gx54bxX/ThcS0x6W31Hq6x/NMFmKYgY+PeerRKFwqqDlk9u8m3e0e1y0QAz2MKTdhk/h5dn5LoYdb4CoI00875wGSr7o9OLUPx20B8ZvyAU
dYZKO9Phy+70FDWoE9RUODzw8dYABpcnvxcs9k6RqtE5Bn8q+a2DJy33hfyEymoA2j6okSx3wrQCD0x6n763b+Tv2yHORlUMoOpzF1G4Yc4=
/*eslint no-shadow: ["error", { "allow": ["done"] }]*/
/*eslint-env es6*/
import async from 'async';
function foo(done) {
async.map([1, 2], function (e, done) {
done(null, e * 2)
}, done);
}
foo(function (err, result) {
console.log({ err, result });
});
ignoreOnInitialization
R5N5cO3wje7QGyRePCZtY5ITMrRIkJV4pKZAdbS1Byg9J1wqsZJ9OewIbKM1V05JQbxEa3L4w5p73+/XrMQzs1aamlsMgzl9eJps6U4PxJplRyzW9giQcBh+78I2PkULr1vgPIJeRQvcguDtQg62o7aM1qTByXH5r0i4EIzt7sngl4BMyh72BpTgPAKOEG1cGpLhB2v27IyIqm0wyL/8c+5AMlicoZLJIZwMuvHPB/aEfqeP2oDnbx0Z/aJIVLLDtrBfOHEuK3a6mbfxwakI37EHsuyLv9titpDDU3KT9U0=
QosjS2oVfFIJ5m/KqsdGUrNzBUHsBo8tFlehtiyTEFKdy1dwJplJlM8hNdSCqxD0CjTlPRaRD5jLzxsbcmEHo7yVqcSR/96XNyn4vpV9VxW3PYIyJcmzx4R/XwpN6sjnu/BMSGKO0St1WkT87VHrcg==
sDYzCiDKJKEYALZh2LpbgJHpZzkpXIejpAWADHPEicVN8VdrrtVPAXtTUv37SixKzUKzAClyZ8TME0DxfYbHqKvKfvVfhojan6ir0H8ZWExkPhNFDvMod6y8AddfmcTL
/*eslint no-shadow: ["error", { "ignoreOnInitialization": true }]*/
var x = x => x;
9dVfCLz8EZFbCy28LPVxgiIAkAcwxrCqoHQ3/Uy0P+2AISNVqKY0UzhRZbhoFiy+qwAmScfVlXBZJWUNRzXCNSdMD8M6B8ILAA1N4qWZNozpdj1O8UZA2caBZpsfawLE
sDYzCiDKJKEYALZh2LpbgJHpZzkpXIejpAWADHPEicUAjXVBJ9Fmm1v1dbqD9OGMIaHHKou5Z+l486P6LqlRl70sRJRO7CudiCVDYcltjbo=
/*eslint no-shadow: ["error", { "ignoreOnInitialization": true }]*/
var x = foo(x => x)
var y = (y => y)()
2hieXMI0cdhI7pQMDmnbFqNEQK/JbJSoB9NEbpBGSr9MfsHVxzDjA8n7fwyMRQJZtm/mkFpN9PeRM+Ogh/YYeChSt20IWIYINaVhNIlukASxuMx2VncxQtX/1479AOPgmnIOicrofNCbmNcleA06EjsTZAhQ7xOwn2hJ2AXVw+sjR9jw76PMA92O4Y4Cvr4F