Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add new-unused-vars-experimental
- Loading branch information
1 parent
d785c61
commit 7d7fa02
Showing
10 changed files
with
1,890 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
packages/eslint-plugin/docs/rules/no-unused-vars-experimental.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Disallow unused variables and arguments (no-unused-vars-experimental) | ||
|
||
Variables that are declared and not used anywhere in the code are most likely an error due to incomplete refactoring. Such variables take up space in the code and can lead to confusion by readers. | ||
|
||
## Rule Details | ||
|
||
This rule leverages the typescript compiler's unused variable checks to report. This means that with all rule options set to `false`, it should report the same errors as if you used both the `noUnusedLocals` and `noUnusedParameters` compiler options. | ||
|
||
This rule is vastly different to, and maintains no compatability with the base eslint version of the rule. | ||
|
||
**_NOTE:_** you must disable the base rule as it can report incorrect errors | ||
|
||
```json | ||
{ | ||
"no-unused-vars": "off", | ||
"@typescript-eslint/no-unused-vars": ["error"] | ||
} | ||
``` | ||
|
||
## Supported Nodes | ||
|
||
This rule supports checks on the following features: | ||
|
||
- Declarations: | ||
- `var` / `const` / `let` | ||
- `function` | ||
- `class` | ||
- `enum` | ||
- `interface` | ||
- `type` | ||
- Class methods | ||
- Class properties and parameter properties | ||
- Function parameters | ||
- Generic type parameters | ||
- Import statements | ||
|
||
## Options | ||
|
||
```ts | ||
type Options = { | ||
ignoredNamesRegex?: string | boolean; | ||
ignoreArgsIfArgsAfterAreUsed?: boolean; | ||
}; | ||
``` | ||
|
||
### ignoredNamesRegex | ||
|
||
This option accepts a regex string to match names against. | ||
Any matched names will be ignored and have no errors reported. | ||
If you set it to false, it will never ignore any names. | ||
|
||
The default value is `'^_'` (i.e. matches any name prefixed with an underscore). | ||
|
||
Examples of valid code with `{ variables: { ignoredNamesRegex: '^_' } }`. | ||
|
||
```ts | ||
const _unusedVar = 'unused'; | ||
class _Unused { | ||
private _unused = 1; | ||
private _unusedMethod() {} | ||
} | ||
function _unusedFunction() {} | ||
enum _UnusedEnum { | ||
a = 1, | ||
} | ||
interface _UnusedInterface {} | ||
type _UnusedType = {}; | ||
``` | ||
|
||
**_NOTE:_** The typescript compiler automatically ignores imports, function arguments, type parameter declarations, and object destructuring variables prefixed with an underscore. | ||
As this is hard-coded into the compiler, we cannot change this. | ||
|
||
Examples of valid code based on the unchangable compiler settings | ||
|
||
```ts | ||
import _UnusedDefault, { _UnusedNamed } from 'foo'; | ||
export function foo(_unusedProp: string) {} | ||
export class Foo<_UnusedGeneric> {} | ||
const { prop: _unusedDesctructure } = foo; | ||
``` | ||
|
||
## ignoreArgsIfArgsAfterAreUsed | ||
|
||
When true, this option will ignore unused function arguments if the arguments proceeding arguments are used. | ||
|
||
Examples of invalid code with `{ ignoreArgsIfArgsAfterAreUsed: false }` | ||
|
||
```ts | ||
function foo(unused: string, used: number) { | ||
console.log(used); | ||
} | ||
|
||
class Foo { | ||
constructor(unused: string, public used: number) { | ||
console.log(used); | ||
} | ||
} | ||
``` | ||
|
||
Examples of valid code with `{ ignoreArgsIfArgsAfterAreUsed: true }` | ||
|
||
```ts | ||
function foo(unused: string, used: number) { | ||
console.log(used); | ||
} | ||
|
||
class Foo { | ||
constructor(unused: string, public used: number) { | ||
console.log(used); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.