docs(linter): Fix/improve the configuration docs for no-unused-vars#17188
docs(linter): Fix/improve the configuration docs for no-unused-vars#17188graphite-app[bot] merged 1 commit intomainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes and improves the configuration documentation for the no-unused-vars linter rule by enabling full schema generation for previously skipped options and correcting the serialization format for enum values.
Key changes:
- Removes
#[schemars(skip)]attributes from pattern fields (vars_ignore_pattern, args_ignore_pattern, caught_errors_ignore_pattern, destructured_array_ignore_pattern) to generate complete documentation - Changes enum serialization from
camelCasetokebab-caseforVarsOption,ArgsOption, andCaughtErrorsto align with the actual parsing logic - Adds
JsonSchemaderive toIgnorePatternenum to support schema generation for pattern fields
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
CodSpeed Performance ReportMerging #17188 will not alter performanceComparing Summary
Footnotes
|
Merge activity
|
…17188) Previously we were skipping the generation of config schema/docs for several options due to the previous Regex issues we had. Now that those are fixed, we should render the docs fully. This also renders default values now, and fixes the casing of the values you can pass to the `args` option, which should always use kebab-case. It also fixes `caughtErrors`, which was listed as a boolean despite taking an enum. The docs are below: ----- ## Configuration This rule accepts a configuration object with the following properties: ### args type: `"after-used" | "all" | "none"` Controls how unused arguments are checked. This option has three settings: 1. `after-used` - Unused positional arguments that occur before the last used argument will not be checked, but all named arguments and all positional arguments after the last used argument will be checked. This is the default setting. 2. `all` - All named arguments must be used. 3. `none` - Do not check arguments. #### `"after-used"` Unused positional arguments that occur before the last used argument will not be checked, but all named arguments and all positional arguments after the last used argument will be checked. #### `"all"` All named arguments must be used #### `"none"` Do not check arguments ### argsIgnorePattern Specifies exceptions to this rule for unused arguments. Arguments whose names match this pattern will be ignored. By default, this pattern is `^_` unless options are configured with an object. In this case it will default to [`None`]. Note that this behavior deviates from both ESLint and TypeScript-ESLint, which never provide a default pattern. #### Example Examples of **correct** code for this option when the pattern is `^_`: ```javascript function foo(_a, b) { console.log(b); } foo(1, 2); ``` ### caughtErrors type: `"all" | "none"` Used for `catch` block validation. It has two settings: - `none` - do not check error objects. This is the default setting. - `all` - all named arguments must be used. `none` corresponds to `false`, while `all` corresponds to `true`. ### caughtErrorsIgnorePattern Specifies exceptions to this rule for errors caught within a `catch` block. Variables declared within a `catch` block whose names match this pattern will be ignored. #### Example Examples of **correct** code when the pattern is `^ignore`: ```javascript try { // ... } catch (ignoreErr) { console.error("Error caught in catch block"); } ``` ### destructuredArrayIgnorePattern This option specifies exceptions within destructuring patterns that will not be checked for usage. Variables declared within array destructuring whose names match this pattern will be ignored. By default this pattern is unset. #### Example Examples of **correct** code for this option, when the pattern is `^_`: ```javascript const [a, _b, c] = ["a", "b", "c"]; console.log(a + c); const { x: [_a, foo], } = bar; console.log(foo); let _m, n; foo.forEach((item) => { [_m, n] = item; console.log(n); }); ``` ### ignoreClassWithStaticInitBlock type: `boolean` default: `false` The `ignoreClassWithStaticInitBlock` option is a boolean. Static initialization blocks allow you to initialize static variables and execute code during the evaluation of a class definition, meaning the static block code is executed without creating a new instance of the class. When set to `true`, this option ignores classes containing static initialization blocks. #### Example Examples of **incorrect** code for the `{ "ignoreClassWithStaticInitBlock": true }` option ```javascript /* no-unused-vars: ["error", { "ignoreClassWithStaticInitBlock": true }]*/ class Foo { static myProperty = "some string"; static mymethod() { return "some string"; } } class Bar { static { let baz; // unused variable } } ``` Examples of **correct** code for the `{ "ignoreClassWithStaticInitBlock": true }` option ```javascript /* no-unused-vars: ["error", { "ignoreClassWithStaticInitBlock": true }]*/ class Foo { static { let bar = "some string"; console.log(bar); } } ``` ### ignoreRestSiblings type: `boolean` default: `false` Using a Rest property it is possible to "omit" properties from an object, but by default the sibling properties are marked as "unused". With this option enabled the rest property's siblings are ignored. #### Example Examples of **correct** code when this option is set to `true`: ```js // 'foo' and 'bar' were ignored because they have a rest property sibling. var { foo, ...coords } = data; var bar; ({ bar, ...coords } = data); ``` ### ignoreUsingDeclarations type: `boolean` default: `false` When set to `true`, the rule will ignore variables declared with `using` or `await using` declarations, even if they are unused. This is useful when working with resources that need to be disposed via the explicit resource management proposal, where the primary purpose is the disposal side effect rather than using the resource. #### Example Examples of **correct** code for the `{ "ignoreUsingDeclarations": true }` option: ```javascript /* no-unused-vars: ["error", { "ignoreUsingDeclarations": true }]*/ using resource = getResource(); await using anotherResource = getAnotherResource(); ``` ### reportUsedIgnorePattern type: `boolean` default: `false` The `reportUsedIgnorePattern` option is a boolean. Using this option will report variables that match any of the valid ignore pattern options (`varsIgnorePattern`, `argsIgnorePattern`, `caughtErrorsIgnorePattern`, or `destructuredArrayIgnorePattern`) if they have been used. #### Example Examples of **incorrect** code for the `{ "reportUsedIgnorePattern": true }` option: ```javascript /* no-unused-vars: ["error", { "reportUsedIgnorePattern": true, "varsIgnorePattern": "[iI]gnored" }]*/ var firstVarIgnored = 1; var secondVar = 2; console.log(firstVarIgnored, secondVar); ``` Examples of **correct** code for the `{ "reportUsedIgnorePattern": true }` option: ```javascript /* no-unused-vars: ["error", { "reportUsedIgnorePattern": true, "varsIgnorePattern": "[iI]gnored" }]*/ var firstVar = 1; var secondVar = 2; console.log(firstVar, secondVar); ``` ### reportVarsOnlyUsedAsTypes type: `boolean` default: `false` The `reportVarsOnlyUsedAsTypes` option is a boolean. If `true`, the rule will also report variables that are only used as types. #### Examples Examples of **incorrect** code for the `{ "reportVarsOnlyUsedAsTypes": true }` option: ```javascript /* no-unused-vars: ["error", { "reportVarsOnlyUsedAsTypes": true }] */ const myNumber: number = 4; export type MyNumber = typeof myNumber ``` Examples of **correct** code for the `{ "reportVarsOnlyUsedAsTypes": true }` option: ```javascript export type MyNumber = number; ``` Note: even with `{ "reportVarsOnlyUsedAsTypes": false }`, cases where the value is only used a type within itself will still be reported: ```javascript function foo(): typeof foo {} ``` ### vars type: `"all" | "local"` Controls how usage of a variable in the global scope is checked. This option has two settings: 1. `all` checks all variables for usage, including those in the global scope. This is the default setting. 2. `local` checks only that locally-declared variables are used but will allow global variables to be unused. #### `"all"` All variables are checked for usage, including those in the global scope. #### `"local"` Checks only that locally-declared variables are used but will allow global variables to be unused. ### varsIgnorePattern Specifies exceptions to this rule for unused variables. Variables whose names match this pattern will be ignored. By default, this pattern is `^_` unless options are configured with an object. In this case it will default to [`None`]. Note that this behavior deviates from both ESLint and TypeScript-ESLint, which never provide a default pattern. #### Example Examples of **correct** code for this option when the pattern is `^_`: ```javascript var _a = 10; var b = 10; console.log(b); ```
13976b6 to
9b10c80
Compare
Previously we were skipping the generation of config schema/docs for several options due to the previous Regex issues we had. Now that those are fixed, we should render the docs fully.
This also renders default values now, and fixes the casing of the values you can pass to the
argsoption, which should always use kebab-case. It also fixescaughtErrors, which was listed as a boolean despite taking an enum.The docs are below:
Configuration
This rule accepts a configuration object with the following properties:
args
type:
"after-used" | "all" | "none"Controls how unused arguments are checked.
This option has three settings:
after-used- Unused positional arguments that occur before the lastused argument will not be checked, but all named arguments and all
positional arguments after the last used argument will be checked.
This is the default setting.
all- All named arguments must be used.none- Do not check arguments."after-used"Unused positional arguments that occur before the last used argument
will not be checked, but all named arguments and all positional
arguments after the last used argument will be checked.
"all"All named arguments must be used
"none"Do not check arguments
argsIgnorePattern
Specifies exceptions to this rule for unused arguments. Arguments whose
names match this pattern will be ignored.
By default, this pattern is
^_unless options are configured with anobject. In this case it will default to [
None]. Note that thisbehavior deviates from both ESLint and TypeScript-ESLint, which never
provide a default pattern.
Example
Examples of correct code for this option when the pattern is
^_:caughtErrors
type:
"all" | "none"Used for
catchblock validation.It has two settings:
none- do not check error objects. This is the default setting.all- all named arguments must be used.nonecorresponds tofalse, whileallcorresponds totrue.caughtErrorsIgnorePattern
Specifies exceptions to this rule for errors caught within a
catchblock.Variables declared within a
catchblock whose names match this patternwill be ignored.
Example
Examples of correct code when the pattern is
^ignore:destructuredArrayIgnorePattern
This option specifies exceptions within destructuring patterns that will
not be checked for usage. Variables declared within array destructuring
whose names match this pattern will be ignored.
By default this pattern is unset.
Example
Examples of correct code for this option, when the pattern is
^_:ignoreClassWithStaticInitBlock
type:
booleandefault:
falseThe
ignoreClassWithStaticInitBlockoption is a boolean. Staticinitialization blocks allow you to initialize static variables and
execute code during the evaluation of a class definition, meaning
the static block code is executed without creating a new instance
of the class. When set to
true, this option ignores classescontaining static initialization blocks.
Example
Examples of incorrect code for the
{ "ignoreClassWithStaticInitBlock": true }optionExamples of correct code for the
{ "ignoreClassWithStaticInitBlock": true }optionignoreRestSiblings
type:
booleandefault:
falseUsing a Rest property it is possible to "omit" properties from an
object, but by default the sibling properties are marked as "unused".
With this option enabled the rest property's siblings are ignored.
Example
Examples of correct code when this option is set to
true:ignoreUsingDeclarations
type:
booleandefault:
falseWhen set to
true, the rule will ignore variables declared withusingorawait usingdeclarations, even if they are unused.This is useful when working with resources that need to be disposed
via the explicit resource management proposal, where the primary
purpose is the disposal side effect rather than using the resource.
Example
Examples of correct code for the
{ "ignoreUsingDeclarations": true }option:reportUsedIgnorePattern
type:
booleandefault:
falseThe
reportUsedIgnorePatternoption is a boolean.Using this option will report variables that match any of the valid
ignore pattern options (
varsIgnorePattern,argsIgnorePattern,caughtErrorsIgnorePattern, ordestructuredArrayIgnorePattern) ifthey have been used.
Example
Examples of incorrect code for the
{ "reportUsedIgnorePattern": true }option:Examples of correct code for the
{ "reportUsedIgnorePattern": true }option:reportVarsOnlyUsedAsTypes
type:
booleandefault:
falseThe
reportVarsOnlyUsedAsTypesoption is a boolean.If
true, the rule will also report variables that are only used as types.Examples
Examples of incorrect code for the
{ "reportVarsOnlyUsedAsTypes": true }option:Examples of correct code for the
{ "reportVarsOnlyUsedAsTypes": true }option:Note: even with
{ "reportVarsOnlyUsedAsTypes": false }, cases where the value isonly used a type within itself will still be reported:
vars
type:
"all" | "local"Controls how usage of a variable in the global scope is checked.
This option has two settings:
allchecks all variables for usage, including those in the globalscope. This is the default setting.
localchecks only that locally-declared variables are used but willallow global variables to be unused.
"all"All variables are checked for usage, including those in the global scope.
"local"Checks only that locally-declared variables are used but will allow
global variables to be unused.
varsIgnorePattern
Specifies exceptions to this rule for unused variables. Variables whose
names match this pattern will be ignored.
By default, this pattern is
^_unless options are configured with anobject. In this case it will default to [
None]. Note that thisbehavior deviates from both ESLint and TypeScript-ESLint, which never
provide a default pattern.
Example
Examples of correct code for this option when the pattern is
^_: