非常教程

Eslint参考手册

规则 | Rules

no-unused-vars

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

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

规则细节

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

如果以下任一情况属实,则认为使用变量:

  • 它代表一个被称为(doSomething())的函数,
  • 它被读取(var y = x
  • 它作为参数传递给一个函数(doSomething(x)
  • 它是在传递给另一个函数(doSomething(function() { foo(); }))的函数内读取的,

如果一个变量只被赋值给()或声明,那么该变量被视为使用var x = 5

此规则的错误代码示例:

/*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;
}

此规则的正确代码示例:

/*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 descructured array is used.
function getY([, y]) {
    return y;
}

exported

在 CommonJS 或 ECMAScript 模块以外的环境中,您可以使用var创建一个可供其他脚本使用的全局变量。您可以使用/* exported variableName */注释块来指示此变量正在导出,因此不应被视为未使用。

请注意,/* exported */对以下任何内容都没有影响:

  • 当环境是node或时commonjs
  • parserOptions.sourceTypemodule
  • ecmaFeatures.globalReturntrue

该行注释// exported variableName将不起作用,因为exported不是特定行。

正确/* exported variableName */操作代码示例:

/* exported global_var */

var global_var = 42;

选项

这条规则需要一个参数,它可以是一个字符串或一个对象。字符串设置与vars属性的设置相同(如下所述)。

默认情况下,这个规则是启用all变量和after-used参数的选项。

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

vars

vars选项有两个设置:

  • all检查所有变量的使用情况,包括全局范围内的变量。这是默认设置。
  • local 只检查使用本地声明的变量,但将允许全局变量未被使用。

vars: local

选项的正确代码示例{ "vars": "local" }

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

some_unused_var = 42;

varsIgnorePattern

varsIgnorePattern选项指定不检查用法的例外:名称与正则表达式模式匹配的变量。例如,名称包含ignored或的变量Ignored

选项的正确代码示例{ "varsIgnorePattern": "[iI]gnored" }

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

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

args

args选项有三个设置:

  • after-used - 只有最后一个参数必须使用。例如,这允许您为函数使用两个命名参数,并且只要您使用第二个参数,ESLint 就不会警告您第一个参数。这是默认设置。
  • all - 必须使用所有命名的参数。
  • none - 不检查参数。

args: after-used

不正确的代码为默认{ "args": "after-used" }选项的示例:

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

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

默认选项的正确代码示例{ "args": "after-used" }

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

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

args: all

选项的错误代码示例{ "args": "all" }

/*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

选项的正确代码示例{ "args": "none" }

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

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

ignoreRestSiblings

ignoreRestSiblings选项是一个布尔值(默认:)false。使用Rest属性可以从对象中“省略”属性,但默认情况下,兄弟属性被标记为“未使用”。启用此选项后,其他属性的兄弟会被忽略。

选项的正确代码示例{ "ignoreRestSiblings": true }

/*eslint no-unused-vars: ["error", { "ignoreRestSiblings": true }]*/
// 'type' is ignored because it has a rest property sibling.
var { type, ...coords } = data;

argsIgnorePattern

argsIgnorePattern选项指定不检查用法的例外:名称与正则表达式模式匹配的参数。例如,名称以下划线开头的变量。

选项的正确代码示例{ "argsIgnorePattern": "^_" }

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

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

caughtErrors

caughtErrors选项用于catch块参数验证。

它有两个设置:

  • none - 不检查错误对象。这是默认设置。
  • all - 必须使用所有命名的参数。

caughtErrors: none

不指定此规则等同于将其分配给none

选项的正确代码示例{ "caughtErrors": "none" }

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

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

caughtErrors: all

选项的错误代码示例{ "caughtErrors": "all" }

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

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

caughtErrorsIgnorePattern

caughtErrorsIgnorePattern选项指定不检查用法的异常:捕获其名称与正则表达式模式匹配的参数。例如,名称以字符串'ignore'开头的变量。

选项的正确代码示例{ "caughtErrorsIgnorePattern": "^ignore" }

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

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

何时不使用它

如果您不想收到关于未使用的变量或函数参数的通知,可以放心地关闭此规则。

版本

该规则在 ESLint 0.0.9中引入。

资源

  • Rule source
  • Documentation source

规则 | Rules相关

1.accessor-pairs
2.array-bracket-newline
3.array-bracket-spacing
4.array-callback-return
5.array-element-newline
6.arrow-body-style
7.arrow-parens
8.arrow-spacing
9.block-scoped-var
10.block-spacing
11.brace-style
12.callback-return
13.camelcase
14.capitalized-comments
15.class-methods-use-this
16.comma-dangle
17.comma-spacing
18.comma-style
19.complexity
20.computed-property-spacing
21.consistent-return
22.consistent-this
23.constructor-super
24.curly
25.default-case
26.dot-location
27.dot-notation
28.eol-last
29.eqeqeq
30.for-direction
31.func-call-spacing
32.func-name-matching
33.func-names
34.func-style
35.function-paren-newline
36.generator-star
37.generator-star-spacing
38.getter-return
39.global-require
40.global-strict
41.guard-for-in
42.handle-callback-err
43.id-blacklist
44.id-length
45.id-match
46.implicit-arrow-linebreak
47.indent
48.indent-legacy
49.init-declarations
50.jsx-quotes
51.key-spacing
52.keyword-spacing
53.line-comment-position
54.linebreak-style
55.lines-around-comment
56.lines-around-directive
57.lines-between-class-members
58.max-depth
59.max-len
60.max-lines
61.max-nested-callbacks
62.max-params
63.max-statements
64.max-statements-per-line
65.multiline-comment-style
66.multiline-ternary
67.new-cap
68.new-parens
69.newline-after-var
70.newline-before-return
71.newline-per-chained-call
72.no-alert
73.no-array-constructor
74.no-arrow-condition
75.no-await-in-loop
76.no-bitwise
77.no-buffer-constructor
78.no-caller
79.no-case-declarations
80.no-catch-shadow
81.no-class-assign
82.no-comma-dangle
83.no-compare-neg-zero
84.no-cond-assign
85.no-confusing-arrow
86.no-console
87.no-const-assign
88.no-constant-condition
89.no-continue
90.no-control-regex
91.no-debugger
92.no-delete-var
93.no-div-regex
94.no-dupe-args
95.no-dupe-class-members
96.no-dupe-keys
97.no-duplicate-case
98.no-duplicate-imports
99.no-else-return
100.no-empty
Eslint

ESLint 是一个代码规范和错误检查工具,有以下几个特性。所有东西都是可以插拔的。你可以调用任意的 rule api 或者 formatter api 去打包或者定义 rule or formatter。任意的 rule 都是独立的。没有特定的 coding style,你可以自己配置。

主页 https://eslint.org/
源码 https://github.com/eslint/eslint
发布版本 4.12.0

Eslint目录

1.指南 | Guide
2.规则 | Rules