Linux websever 5.15.0-153-generic #163-Ubuntu SMP Thu Aug 7 16:37:18 UTC 2025 x86_64
Apache/2.4.52 (Ubuntu)
: 192.168.3.70 | : 192.168.1.99
Cant Read [ /etc/named.conf ]
8.1.2-1ubuntu2.23
urlab
www.github.com/MadExploits
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
CPANEL RESET
CREATE WP USER
README
+ Create Folder
+ Create File
/
var /
www /
html /
cqt /
node_modules /
eslint /
lib /
rules /
[ HOME SHELL ]
Name
Size
Permission
Action
utils
[ DIR ]
drwxr-xr-x
accessor-pairs.js
10.95
KB
-rw-r--r--
array-bracket-newline.js
7.93
KB
-rw-r--r--
array-bracket-spacing.js
7.88
KB
-rw-r--r--
array-callback-return.js
12.6
KB
-rw-r--r--
array-element-newline.js
8.86
KB
-rw-r--r--
arrow-body-style.js
10.12
KB
-rw-r--r--
arrow-parens.js
6.95
KB
-rw-r--r--
arrow-spacing.js
4.51
KB
-rw-r--r--
block-scoped-var.js
3.42
KB
-rw-r--r--
block-spacing.js
5.04
KB
-rw-r--r--
brace-style.js
7.41
KB
-rw-r--r--
callback-return.js
5.74
KB
-rw-r--r--
camelcase.js
10.12
KB
-rw-r--r--
capitalized-comments.js
9.07
KB
-rw-r--r--
class-methods-use-this.js
6.24
KB
-rw-r--r--
comma-dangle.js
11.38
KB
-rw-r--r--
comma-spacing.js
5.75
KB
-rw-r--r--
comma-style.js
10
KB
-rw-r--r--
complexity.js
4.89
KB
-rw-r--r--
computed-property-spacing.js
6.55
KB
-rw-r--r--
consistent-return.js
5.6
KB
-rw-r--r--
consistent-this.js
4.33
KB
-rw-r--r--
constructor-super.js
11.77
KB
-rw-r--r--
curly.js
11.48
KB
-rw-r--r--
default-case-last.js
1.08
KB
-rw-r--r--
default-case.js
2.26
KB
-rw-r--r--
default-param-last.js
1.65
KB
-rw-r--r--
dot-location.js
3.32
KB
-rw-r--r--
dot-notation.js
5.08
KB
-rw-r--r--
eol-last.js
3.48
KB
-rw-r--r--
eqeqeq.js
4.76
KB
-rw-r--r--
for-direction.js
4.11
KB
-rw-r--r--
func-call-spacing.js
8.1
KB
-rw-r--r--
func-name-matching.js
8.63
KB
-rw-r--r--
func-names.js
5.01
KB
-rw-r--r--
func-style.js
5.21
KB
-rw-r--r--
function-call-argument-newline...
4.12
KB
-rw-r--r--
function-paren-newline.js
9.96
KB
-rw-r--r--
generator-star-spacing.js
6.17
KB
-rw-r--r--
getter-return.js
5.62
KB
-rw-r--r--
global-require.js
2.78
KB
-rw-r--r--
grouped-accessor-pairs.js
7.08
KB
-rw-r--r--
guard-for-in.js
1.88
KB
-rw-r--r--
handle-callback-err.js
3.17
KB
-rw-r--r--
id-blacklist.js
6.54
KB
-rw-r--r--
id-denylist.js
6.21
KB
-rw-r--r--
id-length.js
5.48
KB
-rw-r--r--
id-match.js
8.99
KB
-rw-r--r--
implicit-arrow-linebreak.js
3.07
KB
-rw-r--r--
indent-legacy.js
34.88
KB
-rw-r--r--
indent.js
64.92
KB
-rw-r--r--
index.js
17.43
KB
-rw-r--r--
init-declarations.js
3.88
KB
-rw-r--r--
jsx-quotes.js
3.07
KB
-rw-r--r--
key-spacing.js
20.77
KB
-rw-r--r--
keyword-spacing.js
19.94
KB
-rw-r--r--
line-comment-position.js
3.65
KB
-rw-r--r--
linebreak-style.js
3.49
KB
-rw-r--r--
lines-around-comment.js
15.13
KB
-rw-r--r--
lines-around-directive.js
6.85
KB
-rw-r--r--
lines-between-class-members.js
9.89
KB
-rw-r--r--
logical-assignment-operators.j...
18.6
KB
-rw-r--r--
max-classes-per-file.js
1.81
KB
-rw-r--r--
max-depth.js
3.55
KB
-rw-r--r--
max-len.js
13.33
KB
-rw-r--r--
max-lines-per-function.js
5.91
KB
-rw-r--r--
max-lines.js
4.3
KB
-rw-r--r--
max-nested-callbacks.js
2.63
KB
-rw-r--r--
max-params.js
3
KB
-rw-r--r--
max-statements-per-line.js
6.58
KB
-rw-r--r--
max-statements.js
4.47
KB
-rw-r--r--
multiline-comment-style.js
17.96
KB
-rw-r--r--
multiline-ternary.js
6.39
KB
-rw-r--r--
new-cap.js
7.04
KB
-rw-r--r--
new-parens.js
3.09
KB
-rw-r--r--
newline-after-var.js
8.64
KB
-rw-r--r--
newline-before-return.js
6.96
KB
-rw-r--r--
newline-per-chained-call.js
3.79
KB
-rw-r--r--
no-alert.js
3.75
KB
-rw-r--r--
no-array-constructor.js
4.88
KB
-rw-r--r--
no-async-promise-executor.js
1007
B
-rw-r--r--
no-await-in-loop.js
2.44
KB
-rw-r--r--
no-bitwise.js
2.88
KB
-rw-r--r--
no-buffer-constructor.js
1.82
KB
-rw-r--r--
no-caller.js
1.06
KB
-rw-r--r--
no-case-declarations.js
1.82
KB
-rw-r--r--
no-catch-shadow.js
2.45
KB
-rw-r--r--
no-class-assign.js
1.53
KB
-rw-r--r--
no-compare-neg-zero.js
1.62
KB
-rw-r--r--
no-cond-assign.js
4.72
KB
-rw-r--r--
no-confusing-arrow.js
3.16
KB
-rw-r--r--
no-console.js
6.09
KB
-rw-r--r--
no-const-assign.js
1.8
KB
-rw-r--r--
no-constant-binary-expression....
17.72
KB
-rw-r--r--
no-constant-condition.js
4.36
KB
-rw-r--r--
no-constructor-return.js
1.16
KB
-rw-r--r--
no-continue.js
784
B
-rw-r--r--
no-control-regex.js
3.74
KB
-rw-r--r--
no-debugger.js
793
B
-rw-r--r--
no-delete-var.js
852
B
-rw-r--r--
no-div-regex.js
1.22
KB
-rw-r--r--
no-dupe-args.js
1.95
KB
-rw-r--r--
no-dupe-class-members.js
2.82
KB
-rw-r--r--
no-dupe-else-if.js
4.1
KB
-rw-r--r--
no-dupe-keys.js
3.87
KB
-rw-r--r--
no-duplicate-case.js
1.72
KB
-rw-r--r--
no-duplicate-imports.js
9.79
KB
-rw-r--r--
no-else-return.js
13.09
KB
-rw-r--r--
no-empty-character-class.js
1.89
KB
-rw-r--r--
no-empty-function.js
5.22
KB
-rw-r--r--
no-empty-pattern.js
1.8
KB
-rw-r--r--
no-empty-static-block.js
1007
B
-rw-r--r--
no-empty.js
2.39
KB
-rw-r--r--
no-eq-null.js
1.09
KB
-rw-r--r--
no-eval.js
7.34
KB
-rw-r--r--
no-ex-assign.js
1.27
KB
-rw-r--r--
no-extend-native.js
5.29
KB
-rw-r--r--
no-extra-bind.js
5.87
KB
-rw-r--r--
no-extra-boolean-cast.js
10.34
KB
-rw-r--r--
no-extra-label.js
4.17
KB
-rw-r--r--
no-extra-parens.js
42.57
KB
-rw-r--r--
no-extra-semi.js
4.52
KB
-rw-r--r--
no-fallthrough.js
6.8
KB
-rw-r--r--
no-floating-decimal.js
2.51
KB
-rw-r--r--
no-func-assign.js
1.82
KB
-rw-r--r--
no-global-assign.js
2.37
KB
-rw-r--r--
no-implicit-coercion.js
12.3
KB
-rw-r--r--
no-implicit-globals.js
4.52
KB
-rw-r--r--
no-implied-eval.js
4.24
KB
-rw-r--r--
no-import-assign.js
6.03
KB
-rw-r--r--
no-inline-comments.js
2.63
KB
-rw-r--r--
no-inner-declarations.js
3.15
KB
-rw-r--r--
no-invalid-regexp.js
5.82
KB
-rw-r--r--
no-invalid-this.js
4.38
KB
-rw-r--r--
no-irregular-whitespace.js
7.67
KB
-rw-r--r--
no-iterator.js
1.08
KB
-rw-r--r--
no-label-var.js
2.01
KB
-rw-r--r--
no-labels.js
3.29
KB
-rw-r--r--
no-lone-blocks.js
3.24
KB
-rw-r--r--
no-lonely-if.js
3.23
KB
-rw-r--r--
no-loop-func.js
6.87
KB
-rw-r--r--
no-loss-of-precision.js
6.91
KB
-rw-r--r--
no-magic-numbers.js
10.33
KB
-rw-r--r--
no-misleading-character-class....
15.96
KB
-rw-r--r--
no-mixed-operators.js
6.98
KB
-rw-r--r--
no-mixed-requires.js
6.23
KB
-rw-r--r--
no-mixed-spaces-and-tabs.js
3.3
KB
-rw-r--r--
no-multi-assign.js
1.3
KB
-rw-r--r--
no-multi-spaces.js
4.48
KB
-rw-r--r--
no-multi-str.js
1.57
KB
-rw-r--r--
no-multiple-empty-lines.js
5.45
KB
-rw-r--r--
no-native-reassign.js
2.7
KB
-rw-r--r--
no-negated-condition.js
2.37
KB
-rw-r--r--
no-negated-in-lhs.js
1.3
KB
-rw-r--r--
no-nested-ternary.js
960
B
-rw-r--r--
no-new-func.js
2.34
KB
-rw-r--r--
no-new-native-nonconstructor.j...
1.71
KB
-rw-r--r--
no-new-object.js
1.82
KB
-rw-r--r--
no-new-require.js
1.55
KB
-rw-r--r--
no-new-symbol.js
1.69
KB
-rw-r--r--
no-new-wrappers.js
1.43
KB
-rw-r--r--
no-new.js
905
B
-rw-r--r--
no-nonoctal-decimal-escape.js
4.46
KB
-rw-r--r--
no-obj-calls.js
2.41
KB
-rw-r--r--
no-object-constructor.js
2.85
KB
-rw-r--r--
no-octal-escape.js
1.15
KB
-rw-r--r--
no-octal.js
851
B
-rw-r--r--
no-param-reassign.js
6.21
KB
-rw-r--r--
no-path-concat.js
1.96
KB
-rw-r--r--
no-plusplus.js
2.61
KB
-rw-r--r--
no-process-env.js
1.6
KB
-rw-r--r--
no-process-exit.js
1.69
KB
-rw-r--r--
no-promise-executor-return.js
6.52
KB
-rw-r--r--
no-proto.js
1.07
KB
-rw-r--r--
no-prototype-builtins.js
4.61
KB
-rw-r--r--
no-redeclare.js
4.32
KB
-rw-r--r--
no-regex-spaces.js
5.15
KB
-rw-r--r--
no-restricted-exports.js
5.5
KB
-rw-r--r--
no-restricted-globals.js
3.72
KB
-rw-r--r--
no-restricted-imports.js
19.02
KB
-rw-r--r--
no-restricted-modules.js
5.98
KB
-rw-r--r--
no-restricted-properties.js
5.81
KB
-rw-r--r--
no-restricted-syntax.js
1.66
KB
-rw-r--r--
no-return-assign.js
2.21
KB
-rw-r--r--
no-return-await.js
4.35
KB
-rw-r--r--
no-script-url.js
1.48
KB
-rw-r--r--
no-self-assign.js
4.56
KB
-rw-r--r--
no-self-compare.js
1.67
KB
-rw-r--r--
no-sequences.js
3.92
KB
-rw-r--r--
no-setter-return.js
5.59
KB
-rw-r--r--
no-shadow-restricted-names.js
2.61
KB
-rw-r--r--
no-shadow.js
16.95
KB
-rw-r--r--
no-spaced-func.js
2.54
KB
-rw-r--r--
no-sparse-arrays.js
1.45
KB
-rw-r--r--
no-sync.js
1.84
KB
-rw-r--r--
no-tabs.js
2.39
KB
-rw-r--r--
no-template-curly-in-string.js
989
B
-rw-r--r--
no-ternary.js
785
B
-rw-r--r--
no-this-before-super.js
8.83
KB
-rw-r--r--
no-throw-literal.js
1.06
KB
-rw-r--r--
no-trailing-spaces.js
5.91
KB
-rw-r--r--
no-unassigned-vars.js
1.92
KB
-rw-r--r--
no-undef-init.js
2.35
KB
-rw-r--r--
no-undef.js
1.85
KB
-rw-r--r--
no-undefined.js
1.82
KB
-rw-r--r--
no-underscore-dangle.js
9.31
KB
-rw-r--r--
no-unexpected-multiline.js
3.4
KB
-rw-r--r--
no-unmodified-loop-condition.j...
9.8
KB
-rw-r--r--
no-unneeded-ternary.js
5.8
KB
-rw-r--r--
no-unreachable-loop.js
4.96
KB
-rw-r--r--
no-unreachable.js
7.32
KB
-rw-r--r--
no-unsafe-finally.js
3.26
KB
-rw-r--r--
no-unsafe-negation.js
3.88
KB
-rw-r--r--
no-unsafe-optional-chaining.js
5.26
KB
-rw-r--r--
no-unused-expressions.js
5.39
KB
-rw-r--r--
no-unused-labels.js
3.79
KB
-rw-r--r--
no-unused-private-class-member...
6.13
KB
-rw-r--r--
no-unused-vars.js
47.41
KB
-rw-r--r--
no-use-before-define.js
11.59
KB
-rw-r--r--
no-useless-assignment.js
18.88
KB
-rw-r--r--
no-useless-backreference.js
7.48
KB
-rw-r--r--
no-useless-call.js
2.71
KB
-rw-r--r--
no-useless-catch.js
1.28
KB
-rw-r--r--
no-useless-computed-key.js
5.8
KB
-rw-r--r--
no-useless-concat.js
2.9
KB
-rw-r--r--
no-useless-constructor.js
6.82
KB
-rw-r--r--
no-useless-escape.js
10.81
KB
-rw-r--r--
no-useless-rename.js
4.81
KB
-rw-r--r--
no-useless-return.js
11.67
KB
-rw-r--r--
no-var.js
11.19
KB
-rw-r--r--
no-void.js
1.33
KB
-rw-r--r--
no-warning-comments.js
5.54
KB
-rw-r--r--
no-whitespace-before-property....
3.79
KB
-rw-r--r--
no-with.js
757
B
-rw-r--r--
nonblock-statement-body-positi...
4.31
KB
-rw-r--r--
object-curly-newline.js
9.88
KB
-rw-r--r--
object-curly-spacing.js
10.16
KB
-rw-r--r--
object-property-newline.js
3.67
KB
-rw-r--r--
object-shorthand.js
17.59
KB
-rw-r--r--
one-var-declaration-per-line.j...
2.92
KB
-rw-r--r--
one-var.js
17.5
KB
-rw-r--r--
operator-assignment.js
7.36
KB
-rw-r--r--
operator-linebreak.js
8.69
KB
-rw-r--r--
padded-blocks.js
8.7
KB
-rw-r--r--
padding-line-between-statement...
16.59
KB
-rw-r--r--
prefer-arrow-callback.js
11.39
KB
-rw-r--r--
prefer-const.js
15.41
KB
-rw-r--r--
prefer-destructuring.js
8.72
KB
-rw-r--r--
prefer-exponentiation-operator...
6.57
KB
-rw-r--r--
prefer-named-capture-group.js
4.93
KB
-rw-r--r--
prefer-numeric-literals.js
4.4
KB
-rw-r--r--
prefer-object-has-own.js
3.71
KB
-rw-r--r--
prefer-object-spread.js
9.27
KB
-rw-r--r--
prefer-promise-reject-errors.j...
4.51
KB
-rw-r--r--
prefer-reflect.js
3.74
KB
-rw-r--r--
prefer-regex-literals.js
14.05
KB
-rw-r--r--
prefer-rest-params.js
2.96
KB
-rw-r--r--
prefer-spread.js
2.5
KB
-rw-r--r--
prefer-template.js
10.29
KB
-rw-r--r--
quote-props.js
9.89
KB
-rw-r--r--
quotes.js
10.68
KB
-rw-r--r--
radix.js
5.42
KB
-rw-r--r--
require-atomic-updates.js
9.08
KB
-rw-r--r--
require-await.js
4.46
KB
-rw-r--r--
require-unicode-regexp.js
7.57
KB
-rw-r--r--
require-yield.js
1.68
KB
-rw-r--r--
rest-spread-spacing.js
3.65
KB
-rw-r--r--
semi-spacing.js
7.21
KB
-rw-r--r--
semi-style.js
5.12
KB
-rw-r--r--
semi.js
13.01
KB
-rw-r--r--
sort-imports.js
8.4
KB
-rw-r--r--
sort-keys.js
5.74
KB
-rw-r--r--
sort-vars.js
3.24
KB
-rw-r--r--
space-before-blocks.js
6.18
KB
-rw-r--r--
space-before-function-paren.js
5.13
KB
-rw-r--r--
space-in-parens.js
9.1
KB
-rw-r--r--
space-infix-ops.js
5.75
KB
-rw-r--r--
space-unary-ops.js
10.39
KB
-rw-r--r--
spaced-comment.js
10.98
KB
-rw-r--r--
strict.js
8.91
KB
-rw-r--r--
switch-colon-spacing.js
4.59
KB
-rw-r--r--
symbol-description.js
1.66
KB
-rw-r--r--
template-curly-spacing.js
4.24
KB
-rw-r--r--
template-tag-spacing.js
2.99
KB
-rw-r--r--
unicode-bom.js
1.68
KB
-rw-r--r--
use-isnan.js
6.96
KB
-rw-r--r--
valid-typeof.js
4.16
KB
-rw-r--r--
vars-on-top.js
4.2
KB
-rw-r--r--
wrap-iife.js
6.75
KB
-rw-r--r--
wrap-regex.js
2.12
KB
-rw-r--r--
yield-star-spacing.js
3.9
KB
-rw-r--r--
yoda.js
9.48
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : no-unused-vars.js
/** * @fileoverview Rule to flag declared but unused variables * @author Ilya Volodin */ "use strict"; //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ const astUtils = require("./utils/ast-utils"); //------------------------------------------------------------------------------ // Typedefs //------------------------------------------------------------------------------ /** * A simple name for the types of variables that this rule supports * @typedef {'array-destructure'|'catch-clause'|'parameter'|'variable'} VariableType */ /** * Bag of data used for formatting the `unusedVar` lint message. * @typedef {Object} UnusedVarMessageData * @property {string} varName The name of the unused var. * @property {'defined'|'assigned a value'} action Description of the vars state. * @property {string} additional Any additional info to be appended at the end. */ /** * Bag of data used for formatting the `usedIgnoredVar` lint message. * @typedef {Object} UsedIgnoredVarMessageData * @property {string} varName The name of the unused var. * @property {string} additional Any additional info to be appended at the end. */ //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ /** @type {import('../types').Rule.RuleModule} */ module.exports = { meta: { type: "problem", docs: { description: "Disallow unused variables", recommended: true, url: "https://eslint.org/docs/latest/rules/no-unused-vars", }, hasSuggestions: true, schema: [ { oneOf: [ { enum: ["all", "local"], }, { type: "object", properties: { vars: { enum: ["all", "local"], }, varsIgnorePattern: { type: "string", }, args: { enum: ["all", "after-used", "none"], }, ignoreRestSiblings: { type: "boolean", }, argsIgnorePattern: { type: "string", }, caughtErrors: { enum: ["all", "none"], }, caughtErrorsIgnorePattern: { type: "string", }, destructuredArrayIgnorePattern: { type: "string", }, ignoreClassWithStaticInitBlock: { type: "boolean", }, ignoreUsingDeclarations: { type: "boolean", }, reportUsedIgnorePattern: { type: "boolean", }, }, additionalProperties: false, }, ], }, ], messages: { unusedVar: "'{{varName}}' is {{action}} but never used{{additional}}.", usedIgnoredVar: "'{{varName}}' is marked as ignored but is used{{additional}}.", removeVar: "Remove unused variable '{{varName}}'.", }, }, create(context) { const sourceCode = context.sourceCode; const REST_PROPERTY_TYPE = /^(?:RestElement|(?:Experimental)?RestProperty)$/u; const config = { vars: "all", args: "after-used", ignoreRestSiblings: false, caughtErrors: "all", ignoreClassWithStaticInitBlock: false, ignoreUsingDeclarations: false, reportUsedIgnorePattern: false, }; const firstOption = context.options[0]; if (firstOption) { if (typeof firstOption === "string") { config.vars = firstOption; } else { config.vars = firstOption.vars || config.vars; config.args = firstOption.args || config.args; config.ignoreRestSiblings = firstOption.ignoreRestSiblings || config.ignoreRestSiblings; config.caughtErrors = firstOption.caughtErrors || config.caughtErrors; config.ignoreClassWithStaticInitBlock = firstOption.ignoreClassWithStaticInitBlock || config.ignoreClassWithStaticInitBlock; config.ignoreUsingDeclarations = firstOption.ignoreUsingDeclarations || config.ignoreUsingDeclarations; config.reportUsedIgnorePattern = firstOption.reportUsedIgnorePattern || config.reportUsedIgnorePattern; if (firstOption.varsIgnorePattern) { config.varsIgnorePattern = new RegExp( firstOption.varsIgnorePattern, "u", ); } if (firstOption.argsIgnorePattern) { config.argsIgnorePattern = new RegExp( firstOption.argsIgnorePattern, "u", ); } if (firstOption.caughtErrorsIgnorePattern) { config.caughtErrorsIgnorePattern = new RegExp( firstOption.caughtErrorsIgnorePattern, "u", ); } if (firstOption.destructuredArrayIgnorePattern) { config.destructuredArrayIgnorePattern = new RegExp( firstOption.destructuredArrayIgnorePattern, "u", ); } } } /** * Determines what variable type a def is. * @param {Object} def the declaration to check * @returns {VariableType} a simple name for the types of variables that this rule supports */ function defToVariableType(def) { /* * This `destructuredArrayIgnorePattern` error report works differently from the catch * clause and parameter error reports. _Both_ the `varsIgnorePattern` and the * `destructuredArrayIgnorePattern` will be checked for array destructuring. However, * for the purposes of the report, the currently defined behavior is to only inform the * user of the `destructuredArrayIgnorePattern` if it's present (regardless of the fact * that the `varsIgnorePattern` would also apply). If it's not present, the user will be * informed of the `varsIgnorePattern`, assuming that's present. */ if ( config.destructuredArrayIgnorePattern && def.name.parent.type === "ArrayPattern" ) { return "array-destructure"; } switch (def.type) { case "CatchClause": return "catch-clause"; case "Parameter": return "parameter"; default: return "variable"; } } /** * Gets a given variable's description and configured ignore pattern * based on the provided variableType * @param {VariableType} variableType a simple name for the types of variables that this rule supports * @throws {Error} (Unreachable) * @returns {[string | undefined, string | undefined]} the given variable's description and * ignore pattern */ function getVariableDescription(variableType) { let pattern; let variableDescription; switch (variableType) { case "array-destructure": pattern = config.destructuredArrayIgnorePattern; variableDescription = "elements of array destructuring"; break; case "catch-clause": pattern = config.caughtErrorsIgnorePattern; variableDescription = "caught errors"; break; case "parameter": pattern = config.argsIgnorePattern; variableDescription = "args"; break; case "variable": pattern = config.varsIgnorePattern; variableDescription = "vars"; break; default: throw new Error( `Unexpected variable type: ${variableType}`, ); } if (pattern) { pattern = pattern.toString(); } return [variableDescription, pattern]; } /** * Generates the message data about the variable being defined and unused, * including the ignore pattern if configured. * @param {Variable} unusedVar eslint-scope variable object. * @returns {UnusedVarMessageData} The message data to be used with this unused variable. */ function getDefinedMessageData(unusedVar) { const def = unusedVar.defs && unusedVar.defs[0]; let additionalMessageData = ""; if (def) { const [variableDescription, pattern] = getVariableDescription( defToVariableType(def), ); if (pattern && variableDescription) { additionalMessageData = `. Allowed unused ${variableDescription} must match ${pattern}`; } } return { varName: unusedVar.name, action: "defined", additional: additionalMessageData, }; } /** * Generate the warning message about the variable being * assigned and unused, including the ignore pattern if configured. * @param {Variable} unusedVar eslint-scope variable object. * @returns {UnusedVarMessageData} The message data to be used with this unused variable. */ function getAssignedMessageData(unusedVar) { const def = unusedVar.defs && unusedVar.defs[0]; let additionalMessageData = ""; if (def) { const [variableDescription, pattern] = getVariableDescription( defToVariableType(def), ); if (pattern && variableDescription) { additionalMessageData = `. Allowed unused ${variableDescription} must match ${pattern}`; } } return { varName: unusedVar.name, action: "assigned a value", additional: additionalMessageData, }; } /** * Generate the warning message about a variable being used even though * it is marked as being ignored. * @param {Variable} variable eslint-scope variable object * @param {VariableType} variableType a simple name for the types of variables that this rule supports * @returns {UsedIgnoredVarMessageData} The message data to be used with * this used ignored variable. */ function getUsedIgnoredMessageData(variable, variableType) { const [variableDescription, pattern] = getVariableDescription(variableType); let additionalMessageData = ""; if (pattern && variableDescription) { additionalMessageData = `. Used ${variableDescription} must not match ${pattern}`; } return { varName: variable.name, additional: additionalMessageData, }; } //-------------------------------------------------------------------------- // Helpers //-------------------------------------------------------------------------- const STATEMENT_TYPE = /(?:Statement|Declaration)$/u; /** * Determines if a given variable is being exported from a module. * @param {Variable} variable eslint-scope variable object. * @returns {boolean} True if the variable is exported, false if not. * @private */ function isExported(variable) { const definition = variable.defs[0]; if (definition) { let node = definition.node; if (node.type === "VariableDeclarator") { node = node.parent; } else if (definition.type === "Parameter") { return false; } return node.parent.type.indexOf("Export") === 0; } return false; } /** * Determines if a given variable uses the explicit resource management protocol. * @param {Variable} variable eslint-scope variable object. * @returns {boolean} True if the variable is declared with "using" or "await using" * @private */ function usesExplicitResourceManagement(variable) { const [definition] = variable.defs; return ( definition?.type === "Variable" && (definition.parent.kind === "using" || definition.parent.kind === "await using") ); } /** * Checks whether a node is a sibling of the rest property or not. * @param {ASTNode} node a node to check * @returns {boolean} True if the node is a sibling of the rest property, otherwise false. */ function hasRestSibling(node) { return ( node.type === "Property" && node.parent.type === "ObjectPattern" && REST_PROPERTY_TYPE.test(node.parent.properties.at(-1).type) ); } /** * Determines if a variable has a sibling rest property * @param {Variable} variable eslint-scope variable object. * @returns {boolean} True if the variable has a sibling rest property, false if not. * @private */ function hasRestSpreadSibling(variable) { if (config.ignoreRestSiblings) { const hasRestSiblingDefinition = variable.defs.some(def => hasRestSibling(def.name.parent), ); const hasRestSiblingReference = variable.references.some(ref => hasRestSibling(ref.identifier.parent), ); return hasRestSiblingDefinition || hasRestSiblingReference; } return false; } /** * Determines if a reference is a read operation. * @param {Reference} ref An eslint-scope Reference * @returns {boolean} whether the given reference represents a read operation * @private */ function isReadRef(ref) { return ref.isRead(); } /** * Determine if an identifier is referencing an enclosing function name. * @param {Reference} ref The reference to check. * @param {ASTNode[]} nodes The candidate function nodes. * @returns {boolean} True if it's a self-reference, false if not. * @private */ function isSelfReference(ref, nodes) { let scope = ref.from; while (scope) { if (nodes.includes(scope.block)) { return true; } scope = scope.upper; } return false; } /** * Gets a list of function definitions for a specified variable. * @param {Variable} variable eslint-scope variable object. * @returns {ASTNode[]} Function nodes. * @private */ function getFunctionDefinitions(variable) { const functionDefinitions = []; variable.defs.forEach(def => { const { type, node } = def; // FunctionDeclarations if (type === "FunctionName") { functionDefinitions.push(node); } // FunctionExpressions if ( type === "Variable" && node.init && (node.init.type === "FunctionExpression" || node.init.type === "ArrowFunctionExpression") ) { functionDefinitions.push(node.init); } }); return functionDefinitions; } /** * Checks the position of given nodes. * @param {ASTNode} inner A node which is expected as inside. * @param {ASTNode} outer A node which is expected as outside. * @returns {boolean} `true` if the `inner` node exists in the `outer` node. * @private */ function isInside(inner, outer) { return ( inner.range[0] >= outer.range[0] && inner.range[1] <= outer.range[1] ); } /** * Checks whether a given node is unused expression or not. * @param {ASTNode} node The node itself * @returns {boolean} The node is an unused expression. * @private */ function isUnusedExpression(node) { const parent = node.parent; if (parent.type === "ExpressionStatement") { return true; } if (parent.type === "SequenceExpression") { const isLastExpression = parent.expressions.at(-1) === node; if (!isLastExpression) { return true; } return isUnusedExpression(parent); } return false; } /** * If a given reference is left-hand side of an assignment, this gets * the right-hand side node of the assignment. * * In the following cases, this returns null. * * - The reference is not the LHS of an assignment expression. * - The reference is inside of a loop. * - The reference is inside of a function scope which is different from * the declaration. * @param {eslint-scope.Reference} ref A reference to check. * @param {ASTNode} prevRhsNode The previous RHS node. * @returns {ASTNode|null} The RHS node or null. * @private */ function getRhsNode(ref, prevRhsNode) { const id = ref.identifier; const parent = id.parent; const refScope = ref.from.variableScope; const varScope = ref.resolved.scope.variableScope; const canBeUsedLater = refScope !== varScope || astUtils.isInLoop(id); /* * Inherits the previous node if this reference is in the node. * This is for `a = a + a`-like code. */ if (prevRhsNode && isInside(id, prevRhsNode)) { return prevRhsNode; } if ( parent.type === "AssignmentExpression" && isUnusedExpression(parent) && id === parent.left && !canBeUsedLater ) { return parent.right; } return null; } /** * Checks whether a given function node is stored to somewhere or not. * If the function node is stored, the function can be used later. * @param {ASTNode} funcNode A function node to check. * @param {ASTNode} rhsNode The RHS node of the previous assignment. * @returns {boolean} `true` if under the following conditions: * - the funcNode is assigned to a variable. * - the funcNode is bound as an argument of a function call. * - the function is bound to a property and the object satisfies above conditions. * @private */ function isStorableFunction(funcNode, rhsNode) { let node = funcNode; let parent = funcNode.parent; while (parent && isInside(parent, rhsNode)) { switch (parent.type) { case "SequenceExpression": if (parent.expressions.at(-1) !== node) { return false; } break; case "CallExpression": case "NewExpression": return parent.callee !== node; case "AssignmentExpression": case "TaggedTemplateExpression": case "YieldExpression": return true; default: if (STATEMENT_TYPE.test(parent.type)) { /* * If it encountered statements, this is a complex pattern. * Since analyzing complex patterns is hard, this returns `true` to avoid false positive. */ return true; } } node = parent; parent = parent.parent; } return false; } /** * Checks whether a given Identifier node exists inside of a function node which can be used later. * * "can be used later" means: * - the function is assigned to a variable. * - the function is bound to a property and the object can be used later. * - the function is bound as an argument of a function call. * * If a reference exists in a function which can be used later, the reference is read when the function is called. * @param {ASTNode} id An Identifier node to check. * @param {ASTNode} rhsNode The RHS node of the previous assignment. * @returns {boolean} `true` if the `id` node exists inside of a function node which can be used later. * @private */ function isInsideOfStorableFunction(id, rhsNode) { const funcNode = astUtils.getUpperFunction(id); return ( funcNode && isInside(funcNode, rhsNode) && isStorableFunction(funcNode, rhsNode) ); } /** * Checks whether a given reference is a read to update itself or not. * @param {eslint-scope.Reference} ref A reference to check. * @param {ASTNode} rhsNode The RHS node of the previous assignment. * @returns {boolean} The reference is a read to update itself. * @private */ function isReadForItself(ref, rhsNode) { const id = ref.identifier; const parent = id.parent; return ( ref.isRead() && // self update. e.g. `a += 1`, `a++` ((parent.type === "AssignmentExpression" && parent.left === id && isUnusedExpression(parent) && !astUtils.isLogicalAssignmentOperator(parent.operator)) || (parent.type === "UpdateExpression" && isUnusedExpression(parent)) || // in RHS of an assignment for itself. e.g. `a = a + 1` (rhsNode && isInside(id, rhsNode) && !isInsideOfStorableFunction(id, rhsNode))) ); } /** * Determine if an identifier is used either in for-in or for-of loops. * @param {Reference} ref The reference to check. * @returns {boolean} whether reference is used in the for-in loops * @private */ function isForInOfRef(ref) { let target = ref.identifier.parent; // "for (var ...) { return; }" if (target.type === "VariableDeclarator") { target = target.parent.parent; } if ( target.type !== "ForInStatement" && target.type !== "ForOfStatement" ) { return false; } // "for (...) { return; }" if (target.body.type === "BlockStatement") { target = target.body.body[0]; // "for (...) return;" } else { target = target.body; } // For empty loop body if (!target) { return false; } return target.type === "ReturnStatement"; } /** * Determines if the variable is used. * @param {Variable} variable The variable to check. * @returns {boolean} True if the variable is used * @private */ function isUsedVariable(variable) { if (variable.eslintUsed) { return true; } const functionNodes = getFunctionDefinitions(variable); const isFunctionDefinition = functionNodes.length > 0; let rhsNode = null; return variable.references.some(ref => { if (isForInOfRef(ref)) { return true; } const forItself = isReadForItself(ref, rhsNode); rhsNode = getRhsNode(ref, rhsNode); return ( isReadRef(ref) && !forItself && !( isFunctionDefinition && isSelfReference(ref, functionNodes) ) ); }); } /** * Checks whether the given variable is after the last used parameter. * @param {eslint-scope.Variable} variable The variable to check. * @returns {boolean} `true` if the variable is defined after the last * used parameter. */ function isAfterLastUsedArg(variable) { const def = variable.defs[0]; const params = sourceCode.getDeclaredVariables(def.node); const posteriorParams = params.slice(params.indexOf(variable) + 1); // If any used parameters occur after this parameter, do not report. return !posteriorParams.some( v => v.references.length > 0 || v.eslintUsed, ); } /** * Gets an array of variables without read references. * @param {Scope} scope an eslint-scope Scope object. * @param {Variable[]} unusedVars an array that saving result. * @returns {Variable[]} unused variables of the scope and descendant scopes. * @private */ function collectUnusedVariables(scope, unusedVars) { const variables = scope.variables; const childScopes = scope.childScopes; let i, l; if (scope.type !== "global" || config.vars === "all") { for (i = 0, l = variables.length; i < l; ++i) { const variable = variables[i]; // skip a variable of class itself name in the class scope if ( scope.type === "class" && scope.block.id === variable.identifiers[0] ) { continue; } // skip function expression names if (scope.functionExpressionScope) { continue; } // skip variables marked with markVariableAsUsed() if ( !config.reportUsedIgnorePattern && variable.eslintUsed ) { continue; } // skip implicit "arguments" variable if ( scope.type === "function" && variable.name === "arguments" && variable.identifiers.length === 0 ) { continue; } // explicit global variables don't have definitions. const def = variable.defs[0]; if (def) { const type = def.type; const refUsedInArrayPatterns = variable.references.some( ref => ref.identifier.parent.type === "ArrayPattern", ); // skip elements of array destructuring patterns if ( (def.name.parent.type === "ArrayPattern" || refUsedInArrayPatterns) && config.destructuredArrayIgnorePattern && config.destructuredArrayIgnorePattern.test( def.name.name, ) ) { if ( config.reportUsedIgnorePattern && isUsedVariable(variable) ) { context.report({ node: def.name, messageId: "usedIgnoredVar", data: getUsedIgnoredMessageData( variable, "array-destructure", ), }); } continue; } if (type === "ClassName") { const hasStaticBlock = def.node.body.body.some( node => node.type === "StaticBlock", ); if ( config.ignoreClassWithStaticInitBlock && hasStaticBlock ) { continue; } } // skip catch variables if (type === "CatchClause") { if (config.caughtErrors === "none") { continue; } // skip ignored parameters if ( config.caughtErrorsIgnorePattern && config.caughtErrorsIgnorePattern.test( def.name.name, ) ) { if ( config.reportUsedIgnorePattern && isUsedVariable(variable) ) { context.report({ node: def.name, messageId: "usedIgnoredVar", data: getUsedIgnoredMessageData( variable, "catch-clause", ), }); } continue; } } else if (type === "Parameter") { // skip any setter argument if ( (def.node.parent.type === "Property" || def.node.parent.type === "MethodDefinition") && def.node.parent.kind === "set" ) { continue; } // if "args" option is "none", skip any parameter if (config.args === "none") { continue; } // skip ignored parameters if ( config.argsIgnorePattern && config.argsIgnorePattern.test(def.name.name) ) { if ( config.reportUsedIgnorePattern && isUsedVariable(variable) ) { context.report({ node: def.name, messageId: "usedIgnoredVar", data: getUsedIgnoredMessageData( variable, "parameter", ), }); } continue; } // if "args" option is "after-used", skip used variables if ( config.args === "after-used" && astUtils.isFunction(def.name.parent) && !isAfterLastUsedArg(variable) ) { continue; } } else { // skip ignored variables if ( config.varsIgnorePattern && config.varsIgnorePattern.test(def.name.name) ) { if ( config.reportUsedIgnorePattern && isUsedVariable(variable) ) { context.report({ node: def.name, messageId: "usedIgnoredVar", data: getUsedIgnoredMessageData( variable, "variable", ), }); } continue; } } } if ( !isUsedVariable(variable) && !isExported(variable) && !( config.ignoreUsingDeclarations && usesExplicitResourceManagement(variable) ) && !hasRestSpreadSibling(variable) ) { unusedVars.push(variable); } } } for (i = 0, l = childScopes.length; i < l; ++i) { collectUnusedVariables(childScopes[i], unusedVars); } return unusedVars; } /** * fixes unused variables * @param {Object} fixer fixer object * @param {Object} unusedVar unused variable to fix * @returns {Object} fixer object */ function handleFixes(fixer, unusedVar) { const id = unusedVar.identifiers[0]; const parent = id.parent; const parentType = parent.type; const tokenBefore = sourceCode.getTokenBefore(id); const tokenAfter = sourceCode.getTokenAfter(id); const isFunction = astUtils.isFunction; const isLoop = astUtils.isLoop; const allWriteReferences = unusedVar.references.filter(ref => ref.isWrite(), ); /** * get range from token before of a given node * @param {ASTNode} node node of identifier * @param {number} skips number of token to skip * @returns {number} start range of token before the identifier */ function getPreviousTokenStart(node, skips) { return sourceCode.getTokenBefore(node, skips).range[0]; } /** * get range to token after of a given node * @param {ASTNode} node node of identifier * @param {number} skips number of token to skip * @returns {number} end range of token after the identifier */ function getNextTokenEnd(node, skips) { return sourceCode.getTokenAfter(node, skips).range[1]; } /** * get the value of token before of a given node * @param {ASTNode} node node of identifier * @returns {string} value of token before the identifier */ function getTokenBeforeValue(node) { return sourceCode.getTokenBefore(node).value; } /** * get the value of token after of a given node * @param {ASTNode} node node of identifier * @returns {string} value of token after the identifier */ function getTokenAfterValue(node) { return sourceCode.getTokenAfter(node).value; } /** * Check if an array has a single element with null as other element. * @param {ASTNode} node ArrayPattern node * @returns {boolean} true if array has single element with other null elements */ function hasSingleElement(node) { return node.elements.filter(e => e !== null).length === 1; } /** * check whether import specifier has an import of particular type * @param {ASTNode} node ImportDeclaration node * @param {string} type type of import to check * @returns {boolean} true if import specifier has import of specified type */ function hasImportOfCertainType(node, type) { return node.specifiers.some(e => e.type === type); } /** * Check whether declaration is safe to remove or not * @param {ASTNode} nextToken next token of unused variable * @param {ASTNode} prevToken previous token of unused variable * @returns {boolean} true if declaration is not safe to remove */ function isDeclarationNotSafeToRemove(nextToken, prevToken) { return ( nextToken.type === "String" || (prevToken && !astUtils.isSemicolonToken(prevToken) && !astUtils.isOpeningBraceToken(prevToken)) ); } /** * give fixes for unused variables in function parameters * @param {ASTNode} node node to check * @returns {Object} fixer object */ function fixFunctionParameters(node) { const parentNode = node.parent; if (isFunction(parentNode)) { // remove unused function parameter if there is only a single parameter if (parentNode.params.length === 1) { return fixer.removeRange(node.range); } // remove first unused function parameter when there are multiple parameters if ( getTokenBeforeValue(node) === "(" && getTokenAfterValue(node) === "," ) { return fixer.removeRange([ node.range[0], getNextTokenEnd(node), ]); } // remove unused function parameters except first one when there are multiple parameters return fixer.removeRange([ getPreviousTokenStart(node), node.range[1], ]); } return null; } /** * fix unused variable declarations and function parameters * @param {ASTNode} node parent node to identifier * @returns {Object} fixer object */ function fixVariables(node) { const parentNode = node.parent; // remove unused declared variables such as var a = b; or var a = b, c; if (parentNode.type === "VariableDeclarator") { // skip variable in for (const [ foo ] of bar); if (isLoop(parentNode.parent.parent)) { return null; } /* * remove unused declared variable with single declaration such as 'var a = b;' * remove complete declaration when there is an unused variable in 'const { a } = foo;', same for arrays. */ if (parentNode.parent.declarations.length === 1) { // if next token is a string it could become a directive if node is removed -> no suggestion. const nextToken = sourceCode.getTokenAfter( parentNode.parent, ); // if previous token exists and is not ";" or "{" not sure about ASI rules -> no suggestion. const prevToken = sourceCode.getTokenBefore( parentNode.parent, ); if ( nextToken && isDeclarationNotSafeToRemove(nextToken, prevToken) ) { return null; } return fixer.removeRange(parentNode.parent.range); } /* * remove unused declared variable with multiple declaration except first one such as 'var a = b, c = d;' * fix 'let bar = "hello", { a } = foo;' to 'let bar = "hello";' if 'a' is unused, same for arrays. */ if (getTokenBeforeValue(parentNode) === ",") { return fixer.removeRange([ getPreviousTokenStart(parentNode), parentNode.range[1], ]); } /* * remove first unused declared variable when there are multiple declarations * fix 'let { a } = foo, bar = "hello";' to 'let bar = "hello";' if 'a' is unused, same for arrays. */ return fixer.removeRange([ parentNode.range[0], getNextTokenEnd(parentNode), ]); } // fixes [{a: {k}}], [{a: [k]}] if (getTokenBeforeValue(node) === ":") { if (parentNode.parent.type === "ObjectPattern") { // eslint-disable-next-line no-use-before-define -- due to interdependency of functions return fixObjectWithValueSeparator(node); } } // fix unused function parameters return fixFunctionParameters(node); } /** * fix nested object like { a: { b } } * @param {ASTNode} node parent node to check * @returns {Object} fixer object */ function fixNestedObjectVariable(node) { const parentNode = node.parent; // fix for { a: { b: { c: { d } } } } if ( parentNode.parent.parent.parent.type === "ObjectPattern" && parentNode.parent.properties.length === 1 ) { return fixNestedObjectVariable(parentNode.parent); } // fix for { a: { b } } if (parentNode.parent.type === "ObjectPattern") { // fix for unused variables in destructured object with single property in variable declaration and function parameter if (parentNode.parent.properties.length === 1) { return fixVariables(parentNode.parent); } // fix for first unused property when there are multiple properties such as '{ a: { b }, c }' if (getTokenBeforeValue(parentNode) === "{") { return fixer.removeRange([ parentNode.range[0], getNextTokenEnd(parentNode), ]); } // fix for unused property except first one when there are multiple properties such as '{ k, a: { b } }' return fixer.removeRange([ getPreviousTokenStart(parentNode), parentNode.range[1], ]); } return null; } /** * fix unused variables in array and nested array * @param {ASTNode} node parent node to check * @returns {Object} fixer object */ function fixNestedArrayVariable(node) { const parentNode = node.parent; // fix for nested arrays [[ a ]] if ( parentNode.parent.type === "ArrayPattern" && hasSingleElement(parentNode) ) { return fixNestedArrayVariable(parentNode); } if (hasSingleElement(parentNode)) { // fixes { a: [{ b }] } or { a: [[ b ]] } if (getTokenBeforeValue(parentNode) === ":") { return fixVariables(parentNode); } // fixes [a, ...[[ b ]]] or [a, ...[{ b }]] if (parentNode.parent.type === "RestElement") { // eslint-disable-next-line no-use-before-define -- due to interdependency of functions return fixRestInPattern(parentNode.parent); } // fix unused variables in destructured array in variable declaration or function parameter return fixVariables(parentNode); } // remove last unused array element if ( getTokenBeforeValue(node) === "," && getTokenAfterValue(node) === "]" ) { return fixer.removeRange([ getPreviousTokenStart(node), node.range[1], ]); } // remove unused array element return fixer.removeRange(node.range); } /** * fix cases like {a: {k}} or {a: [k]} * @param {ASTNode} node parent node to check * @returns {Object} fixer object */ function fixObjectWithValueSeparator(node) { const parentNode = node.parent.parent; // fix cases like [{a : { b }}] or [{a : [ b ]}] if ( parentNode.parent.type === "ArrayPattern" && parentNode.properties.length === 1 ) { return fixNestedArrayVariable(parentNode); } // fix cases like {a: {k}} or {a: [k]} return fixNestedObjectVariable(node); } /** * fix ...[[a]] or ...[{a}] like patterns * @param {ASTNode} node parent node to check * @returns {Object} fixer object */ function fixRestInPattern(node) { const parentNode = node.parent; // fix ...[[a]] or ...[{a}] in function parameters if (isFunction(parentNode)) { if (parentNode.params.length === 1) { return fixer.removeRange(node.range); } return fixer.removeRange([ getPreviousTokenStart(node), node.range[1], ]); } // fix rest in nested array pattern like [[a, ...[b]]] if (parentNode.type === "ArrayPattern") { // fix [[...[b]]] if (hasSingleElement(parentNode)) { if (parentNode.parent.type === "ArrayPattern") { return fixNestedArrayVariable(parentNode); } // fix 'const [...[b]] = foo; and function foo([...[b]]) {} return fixVariables(parentNode); } // fix [[a, ...[b]]] return fixer.removeRange([ getPreviousTokenStart(node), node.range[1], ]); } return null; } // skip fix when variable has references that would be left behind if ( allWriteReferences.some( ref => ref.identifier.range[0] !== id.range[0], ) ) { return null; } // remove declared variables such as var a; or var a, b; if (parentType === "VariableDeclarator") { if (parent.parent.declarations.length === 1) { // prevent fix of variable in forOf and forIn loops. if ( isLoop(parent.parent.parent) && parent.parent.parent.body !== parent.parent ) { return null; } // removes only variable not semicolon in 'if (foo()) var bar;' or in 'loops' or in 'with' statement. if ( parent.parent.parent.type === "IfStatement" || isLoop(parent.parent.parent) || (parent.parent.parent.type === "WithStatement" && parent.parent.parent.body === parent.parent) ) { return fixer.replaceText(parent.parent, ";"); } // if next token is a string it could become a directive if node is removed -> no suggestion. const nextToken = sourceCode.getTokenAfter(parent.parent); // if previous token exists and is not ";" or "{" not sure about ASI rules -> no suggestion. const prevToken = sourceCode.getTokenBefore(parent.parent); if ( nextToken && isDeclarationNotSafeToRemove(nextToken, prevToken) ) { return null; } // remove unused declared variable with single declaration like 'var a = b;' return fixer.removeRange(parent.parent.range); } // remove unused declared variable with multiple declaration except first one like 'var a = b, c = d;' if (tokenBefore.value === ",") { return fixer.removeRange([ tokenBefore.range[0], parent.range[1], ]); } // remove first unused declared variable when there are multiple declarations return fixer.removeRange([ parent.range[0], getNextTokenEnd(parent), ]); } // remove variables in object patterns if (parent.parent.type === "ObjectPattern") { if (parent.parent.properties.length === 1) { // fix [a, ...{b}] if (parent.parent.parent.type === "RestElement") { return fixRestInPattern(parent.parent.parent); } // fix [{ a }] if (parent.parent.parent.type === "ArrayPattern") { return fixNestedArrayVariable(parent.parent); } /* * var {a} = foo; * function a({a}) {} * fix const { a: { b } } = foo; */ return fixVariables(parent.parent); } // fix const { a:b } = foo; if (tokenBefore.value === ":") { // remove first unused variable in const { a:b } = foo; if ( getTokenBeforeValue(parent) === "{" && getTokenAfterValue(parent) === "," ) { return fixer.removeRange([ parent.range[0], getNextTokenEnd(parent), ]); } // remove unused variables in const { a: b, c: d } = foo; except first one return fixer.removeRange([ getPreviousTokenStart(parent), id.range[1], ]); } } // remove unused variables inside an array if (parentType === "ArrayPattern") { if (hasSingleElement(parent)) { // fix [a, ...[b]] if (parent.parent.type === "RestElement") { return fixRestInPattern(parent.parent); } // fix [ [a] ] if (parent.parent.type === "ArrayPattern") { return fixNestedArrayVariable(parent); } /* * fix var [a] = foo; * fix function foo([a]) {} * fix const { a: [b] } = foo; */ return fixVariables(parent); } // if "a" is unused in [a, b ,c] fixes to [, b, c] if (tokenBefore.value === "," && tokenAfter.value === ",") { return fixer.removeRange(id.range); } } // remove unused rest elements if (parentType === "RestElement") { // fix [a, ...rest] if (parent.parent.type === "ArrayPattern") { if (hasSingleElement(parent.parent)) { // fix [[...rest]] when there is only rest element if (parent.parent.parent.type === "ArrayPattern") { return fixNestedArrayVariable(parent.parent); } // fix 'const [...rest] = foo;' and 'function foo([...rest]) {}' return fixVariables(parent.parent); } // fix [a, ...rest] return fixer.removeRange([ getPreviousTokenStart(id, 1), id.range[1], ]); } // fix { a, ...rest} if (parent.parent.type === "ObjectPattern") { // fix 'const {...rest} = foo;' and 'function foo({...rest}) {}' if (parent.parent.properties.length === 1) { return fixVariables(parent.parent); } // fix { a, ...rest} when there are multiple properties return fixer.removeRange([ getPreviousTokenStart(id, 1), id.range[1], ]); } // fix function foo(...rest) {} if (isFunction(parent.parent)) { // remove unused rest in function parameter if there is only single parameter if (parent.parent.params.length === 1) { return fixer.removeRange(parent.range); } // remove unused rest in function parameter if there multiple parameter return fixer.removeRange([ getPreviousTokenStart(parent), parent.range[1], ]); } } if (parentType === "AssignmentPattern") { // fix [a = aDefault] if (parent.parent.type === "ArrayPattern") { return fixNestedArrayVariable(parent); } // fix {a = aDefault} if (parent.parent.parent.type === "ObjectPattern") { if (parent.parent.parent.properties.length === 1) { // fixes [{a = aDefault}] if ( parent.parent.parent.parent.type === "ArrayPattern" ) { return fixNestedArrayVariable(parent.parent.parent); } // fix 'const {a = aDefault} = foo;' and 'function foo({a = aDefault}) {}' return fixVariables(parent.parent.parent); } // fix unused 'a' in {a = aDefault} if it is the first property if ( getTokenBeforeValue(parent.parent) === "{" && getTokenAfterValue(parent.parent) === "," ) { return fixer.removeRange([ parent.parent.range[0], getNextTokenEnd(parent.parent), ]); } // fix unused 'b' in {a, b = aDefault} if it is not the first property return fixer.removeRange([ getPreviousTokenStart(parent.parent), parent.parent.range[1], ]); } // fix unused assignment patterns in function parameters if (isFunction(parent.parent)) { return fixFunctionParameters(parent); } } // remove unused functions if (parentType === "FunctionDeclaration" && parent.id === id) { return fixer.removeRange(parent.range); } // remove unused default import if (parentType === "ImportDefaultSpecifier") { // remove unused default import when there are not other imports if ( !hasImportOfCertainType(parent.parent, "ImportSpecifier") && !hasImportOfCertainType( parent.parent, "ImportNamespaceSpecifier", ) ) { return fixer.removeRange([ parent.range[0], parent.parent.source.range[0], ]); } // remove unused default import when there are other imports also return fixer.removeRange([id.range[0], tokenAfter.range[1]]); } if (parentType === "ImportSpecifier") { // remove unused imports when there is a single import if ( parent.parent.specifiers.filter( e => e.type === "ImportSpecifier", ).length === 1 ) { // remove unused import when there is no default import if ( !hasImportOfCertainType( parent.parent, "ImportDefaultSpecifier", ) ) { return fixer.removeRange(parent.parent.range); } // fixes "import foo from 'module';" to "import 'module';" return fixer.removeRange([ getPreviousTokenStart(parent, 1), tokenAfter.range[1], ]); } if (getTokenBeforeValue(parent) === "{") { return fixer.removeRange([ parent.range[0], getNextTokenEnd(parent), ]); } return fixer.removeRange([ getPreviousTokenStart(parent), parent.range[1], ]); } if (parentType === "ImportNamespaceSpecifier") { if ( hasImportOfCertainType( parent.parent, "ImportDefaultSpecifier", ) ) { return fixer.removeRange([ getPreviousTokenStart(parent), parent.range[1], ]); } // fixes "import * as foo from 'module';" to "import 'module';" return fixer.removeRange([ parent.range[0], parent.parent.source.range[0], ]); } // skip error in catch(error) variable if (parentType === "CatchClause") { return null; } // remove unused declared classes if (parentType === "ClassDeclaration") { return fixer.removeRange(parent.range); } // remove unused variable that is in a sequence [a,b] fixes to [a] if (tokenBefore?.value === ",") { return fixer.removeRange([tokenBefore.range[0], id.range[1]]); } // remove unused variable that is in a sequence inside function arguments and object pattern if (tokenAfter.value === ",") { // fix function foo(a, b) {} if (tokenBefore.value === "(") { return fixer.removeRange([ id.range[0], tokenAfter.range[1], ]); } // fix const {a, b} = foo; if (tokenBefore.value === "{") { return fixer.removeRange([ id.range[0], tokenAfter.range[1], ]); } } if ( parentType === "ArrowFunctionExpression" && parent.params.length === 1 && tokenAfter?.value !== ")" ) { return fixer.replaceText(id, "()"); } return fixer.removeRange(id.range); } //-------------------------------------------------------------------------- // Public //-------------------------------------------------------------------------- return { "Program:exit"(programNode) { const unusedVars = collectUnusedVariables( sourceCode.getScope(programNode), [], ); for (let i = 0, l = unusedVars.length; i < l; ++i) { const unusedVar = unusedVars[i]; // Report the first declaration. if (unusedVar.defs.length > 0) { // report last write reference, https://github.com/eslint/eslint/issues/14324 const writeReferences = unusedVar.references.filter( ref => ref.isWrite() && ref.from.variableScope === unusedVar.scope.variableScope, ); let referenceToReport; if (writeReferences.length > 0) { referenceToReport = writeReferences.at(-1); } context.report({ node: referenceToReport ? referenceToReport.identifier : unusedVar.identifiers[0], messageId: "unusedVar", data: unusedVar.references.some(ref => ref.isWrite(), ) ? getAssignedMessageData(unusedVar) : getDefinedMessageData(unusedVar), suggest: [ { messageId: "removeVar", data: { varName: unusedVar.name, }, fix(fixer) { return handleFixes(fixer, unusedVar); }, }, ], }); // If there are no regular declaration, report the first `/*globals*/` comment directive. } else if (unusedVar.eslintExplicitGlobalComments) { const directiveComment = unusedVar.eslintExplicitGlobalComments[0]; context.report({ node: programNode, loc: astUtils.getNameLocationInGlobalDirectiveComment( sourceCode, directiveComment, unusedVar.name, ), messageId: "unusedVar", data: getDefinedMessageData(unusedVar), }); } } }, }; }, };
Close