no-unused-vars

禁止未使用的变量

配置文件 中的 "extends": "eslint:recommended" 属性启用了该规则

由于重构不完整,已声明但未在代码中任何地方使用的变量很可能是错误。这样的变量会占用代码中的空间,并可能导致读者混淆。

规则详情

该规则旨在消除未使用的变量、函数和函数参数。

如果以下任何一项为真,则认为使用变量 foo

    rA+wO7xcB1QaHEMWEkmPaNZsao6Hjp8Y2lDFgY67KYTNCdo/RbMqfII9Xg62jlCfiqWxZ8bnwQN5RNdZUGeqyZtzhHA5/YVX+1Q2S5r0Of49fKDNc3DYefaegBpp6M19Rfc5Nh2BvI2kzWgecbeJ1IOyygOSC2KGtsecB1X3RPugVZnTbLGT4wf+cY/m/ziv11IJnh9QdNg0YbN8186fmrz8RpijgbXA4wcpl0AeqeLybbTHKehDdLgpB2zsLtQln7U8VZXhrvMdNGVnSuA+piGyEbTlNtf+BYkgju0IQ4EOD61xCk1bskRDX6Xllf3qndp5JIn3wzmlf/krq8cj17xF3NQuGetrLxFI3/YqMEVfdcLbZ50iBJfhyD1klgVS8kOWgzmNmye9/nqOR4jdvZgwKRqyESTkfg75hNPuV2U=

5+EOuu9MjqFPjgqDgWelIQqasGS3wjQuB2MP4SdeQCnMHD193UtkuTAIF2IXLGyJGLr6vWaW7aRNfMUDf4jxnpDcwHMEkPJviZuNL8tLyFtKfAKg1ZPaDsfdVS3xNAtU9qK62YRnb73ZXaBK1V8/a8+En4HLslMCHtJIyJWwcco=

Wk+Mu7oFIccqpNSe5orR05ZG/BDV40hHIKzF0SoJnVcuS8r6evQjx3oyoFn9+fZT

/*eslint no-unused-vars: "error"*/
/*global some_unused_var*/

// It checks variables you have defined as global
some_unused_var = 42;

var x;

// Write-only variables are not considered as used.
var y = 10;
y = 5;

// A read for a modification of itself is not considered as used.
var z = 0;
z = z + 1;

// By default, unused arguments cause warnings.
(function(foo) {
    return 5;
})();

// Unused recursive functions also cause warnings.
function fact(n) {
    if (n < 2) return 1;
    return n * fact(n - 1);
}

// When a function definition destructures an array, unused entries from the array also cause warnings.
function getY([x, y]) {
    return y;
}

UubGaThNGVSAZCEeqgdAAx/QE1uwW/De5ytjPBnKCKqeOsDZpJS0MvQSJQtY0WPI

/*eslint no-unused-vars: "error"*/

var x = 10;
alert(x);

// foo is considered used here
myFunc(function foo() {
    // ...
}.bind(this));

(function(foo) {
    return foo;
})();

var myFunc;
myFunc = setTimeout(function() {
    // myFunc is considered used
    myFunc();
}, 50);

// Only the second argument from the destructured array is used.
function getY([, y]) {
    return y;
}

exported

eMf7FBHQpNxFmxQFnIWgFjIErAHkdUrfcVKT+Ci8Tl4L6gkhtb3tLgkJsPRsYfVJPMctbn3yXXvEGtE6GPUKp/x2rHWJSjvjt2rLizn3yPv7G2GcBMt0F+/Inol/YVNERZZ4PRCiAlGaqMflsBYrygg58SG+Uk7U5WzrgY3YpNX30/uy2oovHvD03ZQFlhk5slSHS+kdfmlyhSx+TTs+H2dWticVH6Ibrjt56lmx0mEiRT3PYkB7rfjmFYPpan6I38B1hTZaxceVyLOjCuL/rRiXVEucbgT9j71FLuNsdkvqzF7ogzpDkVLhPA+ydMzTYJ7BK0Suqwq6iPFbCbvOv1Z182THqmg2dAROpzDaKoXAi4OqrGKXchI2Ai7IDrk5

/rivtr76VE2FY4YTJFpLaT1QoCvIfD9APf9etR65z1hHWiCA6UOamcOWOSriQkEjuGWnwMGoaebDESjcagBXRUzRFIxukCR+fArTIrK5OvE=

    AHNU24G9m10WeG4hFdEGTlsxv3XdjAXfWV77U18IpR30+9KxNewQNIvervScPj7CIzzoDFTUpyiCdzPxLb30UfPo/w9DMLhk/oHRXFNnHksmEI7KNP35rYRL6xN0emsaIZufuv4Aj7NzRoAmlFr7HR0WSuYpMPXBEnDVt4jIuizg+vzQeX+hVwFzXyTAi0oG9QHrl9D3t1IpYqeS/5+o0Oy2RlBwuKR373GI38sNkfEaejPVw8X5kGyo1h41nZpMCSRMhdeQHWnecofCn4dEUxHI0OML2BCNYuo43oHGtwI=

QME6dpOp9U7duejl2VMwqkQGarlixaNI2F2PVhwlEgQxBcCWJc+ruG5QOst88E5X1H5kTK7RDIR0lnJdVeawP2a07O+A7+Pu4d94rN36oClZDpaS/zFAGkYBu7Un7mocNFpYHMNEQvvSM8IYZ8bop8rFSlXgYxx1SdU9y6AuzX0=

/izGxolNhqMKT6WY8olaIqVcr/yePO4NQk53+B5Yambe1RKCtbP1BlLKlRVEhKHsCRFSpI341p+qtGWaWGCOrEZEbngFUJEXcvwyB32lr8w=

/* exported global_var */

var global_var = 42;

选项

tPbp+QbdlmdjNko4kZxXBkPrcDaGxjaaJDZ+RGuQe+SMB5yCl/X8dIPo6Tzf+5rs6RkF67pYN5uFWAaikKtVjn+mFVhxwKUHTn2lj847hmBW5EUUi6ZP4tX16Slfd5baJwZpDv6qy3agvPsgSPOkzQiSghbuFrE1LHZeMPWKLsk651gV9WBuOX3TWz04bLtDknNiOT4M3iUMtOMNOEBVfA==

hutJgMfalMULXfxCI/yn8z3mcfVwJ2g5XwheUmVjqt1hKyTvb8I4cqWp05RAaRzL0WfIJW7bmI5tOHhw2YOahDCkczaPrdLXQPXvlgYn0i9TucBmovumGlyq3ZyXjJwkTFNCo1ftkTs14gipTK0Y6JveFurQfcvbxo1XS7elh2c=

{
    "rules": {
        "no-unused-vars": ["error", { "vars": "all", "args": "after-used", "ignoreRestSiblings": false }]
    }
}

vars

z+Hn67PEb2PJfxdmNDjtMIP0ryQhHc0evU/G7EjJnetd4GYpMWyfaBIEjnaDpihV

    Az24cWfAB11ESX5VUAa68IZ7AdhPD+XdwFSCVD0O+KHWgDvDZA4Xmgs7zdZCLOBGJnvH3rK1wehVF+04WBE/+j+SvP7KMZ9yTl9I807eLRdkbgQ5h2+IZ9lywC952XQWAhBnUGTinxMNetxZOCwLSWbueb/2dviuL46f4ff95ZWneO5siHl9pDn+QrcmvUzI5nM/zECQmRpRpewKsSxLOpUmvo7RCMaq/0yvzJ8bbFw1VASkcNtHM0I53gw9T7vovIpBYHvA/uBX7o0R9HFO2qhNJlMer48uWCnca+bfVf36fUZZikVaxvPwznqo5sHE

vars: local

uRBGR4t+vgJwwURwUoi7Q6QSxdpuF2oTZwJpGUV1N7U//4CB6TtAdNTAmj2edwygEAgDLJVZ3qABfqKG3Ztiqg==

/*eslint no-unused-vars: ["error", { "vars": "local" }]*/
/*global some_unused_var */

some_unused_var = 42;

varsIgnorePattern

GAIYJJHUgEutY+iiW1M7ueK8mTO2/0CysArliCzhgoxLxXeSVavB5l8IscFyM90HHDAXcev9F/l8lZOJPoUTHAq+Fz+uPoeNKY7/+PFtt0h2szxkAomNF5vlk03NgIDTeAialG4YciOnGfC6eO+VDIzxNyKU0d0hfBOSzi8k+IYQozOYlpcOlox9X1dYw8OriYOVJj0rv1c8/DQWAdP8Ggr/if8FRev5vQlBMKQ92CeehJ0QoLuqiYhE0rLu4cbBGwNDj7s6qVS44qSAywakfA==

rXtJzdzkTJ7oGPh6XEVzNJCapxvecmmAV/Fb7d8CdUcIDJJuXGqIxNoRTO/BbAo2AlF2ZbpBa9sJGuePJHgFGCk7AA9bMo5cB/OwbFowhILxZd9hEAOABmwtsEE5Xqof

/*eslint no-unused-vars: ["error", { "varsIgnorePattern": "[iI]gnored" }]*/

var firstVarIgnored = 1;
var secondVar = 2;
console.log(secondVar);

args

X57p6TYK+SEFtL1aZPiuFAG6m/RC03z915PmeCo4LKWoOuAFys7ptUjLfF189DbK

    IAp7+YnXHluz9kn3pjMGkILlL/X4FcCkZa0pfkCLxdXNgJTDapHlsp7xmDITtYtxod9UCoPHG1eMTXIPeLDYWJeT37CqLhyPTcLD4fmK4bYp1Uw1L+UeWSxBeEfALM2/h3XkuQBNcjb1XHyMAd2E4K+7KAebxhtLnGyqGOmYz7uRiXzKdVVjB4bn2B80NPDBq9cODXidayFxGDHerIQClxgjNSzfa63QIPppZn1H2WFpaOvQxTF1OOZ1ADd1eej5+wFgM36iBWvWiy5wYdE6WK/ufgPVlVX8O9mb0X2aA1zm02rcMR5BMGmX3xZWBd/5zYPke5tlRad+cloTyk2GMq3D8W79pOeu7nG+v4nMvHphuA5Tk5UOww4V+YTYVY3ANppI6nftp2vWKCLpiom9YDDQTnRAbUK+z/z0rngileI=

args: after-used

eqHB0w7lE25hskemPRrxEPHLQ+N4MCk4KB3+HtEhZi7ncNl0e58AETxU/US6DCL3TW6NUyrmkItSZUpN7xFEjGmRMjoV6cTbYL5TG1xMVgY=

/*eslint no-unused-vars: ["error", { "args": "after-used" }]*/

// 2 errors, for the parameters after the last used parameter (bar)
// "baz" is defined but never used
// "qux" is defined but never used
(function(foo, bar, baz, qux) {
    return bar;
})();

eqHB0w7lE25hskemPRrxEPHLQ+N4MCk4KB3+HtEhZi7ncNl0e58AETxU/US6DCL31EnAkwldTVQOXURbS/vmuro+85wQ7m1ZLE047xyanyA=

/*eslint no-unused-vars: ["error", {"args": "after-used"}]*/

(function(foo, bar, baz, qux) {
    return qux;
})();

args: all

2NGbWaQET/uBXt1BX40d8TrYzRMa0MD+VlIGSjGUqPdaeslHB6oPVMRxHSUrfNw9BUcfIRhhwQxBNOOT/gW8XA==

/*eslint no-unused-vars: ["error", { "args": "all" }]*/

// 2 errors
// "foo" is defined but never used
// "baz" is defined but never used
(function(foo, bar, baz) {
    return bar;
})();

args: none

2NGbWaQET/uBXt1BX40d8UHGj+HftDeKBD46a+WxKrO+Tr0CF6BV1WIyZGmKKvDp/Q4airzsO7L0DxUkjvsRwQ==

/*eslint no-unused-vars: ["error", { "args": "none" }]*/

(function(foo, bar, baz) {
    return bar;
})();

ignoreRestSiblings

9WIg10Z73RexihEK3nWskTslPczZYkrKMEMagTC6gZEHyyQPexRMjlLMRp3cFpRm4o19jKMCV9ItvLPKDbAPAjknp8KGvEeJdtLCb9vNzcCSi6Lyen23p84LnuMYn/W02rnSoq3vC6en0SFtEdAQu6s4XmewnN00/NX6ZebFJlEhESmKLFKM2vS/qQTudugFRYMKInBtEkGD766uNjB6ItJa0YxmRcZY4AomN45eDUu//Go7k+ohYLMF1fXWFyyhq4Z3DW8LVs0iff6B8Z7wkZHbstMa+8y5xVKCawOtQqYaQ9IBgAPPscEGY2ln8UMa69utpIGderG3qEWgmaEKSSd/eg32O1SAjCxqz/grKbhgJdjHiiYZghwsP8c6AGsMeBDae/9Bme/lwqcOYdx4cFqClG0sUeFIL1L+1uyyL4C91yvlkFeAU6KQZ2y5AGd4

4cFkLgz25f8+GR2dS/rWRHF3sPD52pe+qABPGF6/vZvL3iYqn7wzaIVjAxSm8EjtgzqiR7Jgfcz0PR8AqqrmAkDc9Z5h7BqhyFqE/b+z2FA=

/*eslint no-unused-vars: ["error", { "ignoreRestSiblings": true }]*/
// 'foo' and 'bar' were ignored because they have a rest property sibling.
var { foo, ...coords } = data;

var bar;
({ bar, ...coords } = data);

argsIgnorePattern

PBWMVbmjwMqKQbYTlNOkvKuX35Z7Iq/sF903lknA3Oxc+CQikEOX+7hWRmU2QJqTCbjfO0nG2nHYXKX0u6lRpCcOCIm/ZijhoZrOEHIU/nU6c2cc6LMjJBC8KLgekSRkeo6OxnHAp0dM1ri8adqFdjUmwcP/uyUou+1aZ765KgXP8WzBICHIFbdcDB8gXSjHe3UlFNqluzYRdewoW7w0cqbpgsisM4Ydwny11s7d6uY=

P+Os9K7edEqqOZz5yGo7jy2wTevW3VBnnUNvtqO5fuscYYMKyUsU7tekfKkZxvpPtW3eUiGQQeqHs+EmwhBwgzc/Oixtw5IihFb6He6vzxk=

/*eslint no-unused-vars: ["error", { "argsIgnorePattern": "^_" }]*/

function foo(x, _y) {
    return x + 1;
}
foo();

destructuredArrayIgnorePattern

4vzn5efkz5BxrWwtz172CPDspvwBY4n0xN3EC/jW1UkjIfyTDNw2lrtwIaZ9bHfyXooFEbypS1wmalTCUWxiOba0sbzEuSMdlkJi5cabUNfHVyVSndGcXZVix/27s78NUeyk3qcB9OolgV9kCaMuR0p1X4ciTbZpWoAibhADtTtcf3fLk/jBgQdUB+dUROqcw0V8S3a0M6m+aQI1uaTbpmVZLHtBr1JBFqp3cumF93hv11r/fDDY5zXQRn0ihEtt+3r9366OrYHBpyJV7ckqzA==

HAcw5Q8DQjH2qyI0A79eovjQmLVyxA/iRFN8PrqUoKz1Pg3d59al1HqsphMrJiscs19LoZxdCtPriV3k+lL1o4qsQQKsBeFM2QhD9vAfds1CoSHHqpyKcvDW0CKwrxNs

/*eslint no-unused-vars: ["error", { "destructuredArrayIgnorePattern": "^_" }]*/

const [a, _b, c] = ["a", "b", "c"];
console.log(a+c);

const { x: [_a, foo] } = bar;
console.log(foo);

function baz([_c, x]) {
    x;
}
baz();

function test({p: [_q, r]}) {
    r;
}
test();

let _m, n;
foo.forEach(item => {
    [_m, n] = item;
    console.log(n);
});

let _o, p;
_o = 1;
[_o, p] = foo;
p;

caughtErrors

BSzi8PV8Qwl/x2+PRTPeivk9S5mYzYlYsLw80RGqmZCOcRNzgLyQaUS8rFKOTHwrQy0KqhGO6tnUUKcSsqrLU6Qw6uQyTaN0vmuLvGMQd8Q=

7hgayPzINeCj2pTf0TxXx17h39Ig/JJ1uqvdDoD+nHg=

    jlsxfqkJr2zx1omZa9ja/j6eoeIv8CcTCLtja0RLooubsh8+M16EFsWS31LZKj+VCKUKggNV6Ici+cms9nCpGMEhw+C+NcgUN0k6/P87PZUsWrM64EfVQqVigt6DGD1kSgymqcFqYt49iGnv7XRkHqUxhP+oGMIJA3y73TyboNbduMvvbKgGP1ctE7gx4Skw

caughtErrors: none

PWNw6zrCcL6UBKC9kdjKVEj1kqZTdcqztyFWrbN8rkub9nQ7mTEz7feNinbaHQibmgtR/3CNuj/hM46tglbITg==

WI8Y6R7/W+5qbF3GEC/wt/qr+g0KW8cSH9xxFGMjYtzj0nTeK15kE4RL6RpyxG1zNeVi9qlkC7RN20ylOw6z2knhQo67+ryqwDcYkKJCcpo=

/*eslint no-unused-vars: ["error", { "caughtErrors": "none" }]*/

try {
    //...
} catch (err) {
    console.error("errors");
}

caughtErrors: all

WI8Y6R7/W+5qbF3GEC/wtyW7lNo5ssxAWm/AOyV4i0ddPaFxWHNXHRx2Tz5pxGUrB164Eu1q45/lU8IchU92pdh3U1NBcj7W5oTTDwJRQO8=

/*eslint no-unused-vars: ["error", { "caughtErrors": "all" }]*/

// 1 error
// "err" is defined but never used
try {
    //...
} catch (err) {
    console.error("errors");
}

caughtErrorsIgnorePattern

BSzi8PV8Qwl/x2+PRTPeijyaS+ppjuScLS4YGEZlC98ddlbnp2NY0MsgZjNkysBprobpGFH4RsnJweKnssYsq2AWmi+x/uYItjbki+g20GFMso0Jelx0O4P/0G/TQ3ak0xs4wG661LpDeGAlqIfwqC/guc2L+GDoB78Hjdo9+1OEjRcT4eT1+GdBd/+WJmGSm9an0TjgN5LsJBXJQovL+HgHoxTrYvdo6ofB9+6ijOdbXsVOOGdXTWoRgFdjkkv7

WI8Y6R7/W+5qbF3GEC/wt9H2o/Rqk5rVmUTAdfMUqSinqK47qxfq4svOcFHld9E1Z2xkzF/HLpYj5KjHfTAmp2pW5KvexKFACrLyuUt0h2mppEh8o2ZZ0TJ9B+bmsBCm

/*eslint no-unused-vars: ["error", { "caughtErrorsIgnorePattern": "^ignore" }]*/

try {
    //...
} catch (ignoreErr) {
    console.error("errors");
}

何时不使用

SffVY7Lc4VmPP9wWdc2cAtQh6tzdjqcDz1jKb0ssVOX3E7TZOQcEm33KIcL2zCJjTOcBZFyMvfnV+zVXNjHfTwBm/l4ZWDjG8f7Skksn+vPSW7W9hFGXyaSwzPl+a1ZuXx9551BOeviWuwhChJfjGQ==