Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): addno-implicit-any-catch
rule#2202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
18 commits Select commitHold shift + click to select a range
d61ed9d
feat(typescript-estree): allow catch clause to have a type
phiresky81fbc88
feat(eslint-plugin): add no-implicit-any-catch rule
phireskya19c245
fix(eslint-plugin): [no-implicit-any-catch] disable recommended for now
phiresky076e8ef
fix(typescript-estree): add tests for catch clause type syntax
phiresky4fbde9b
Update packages/eslint-plugin/docs/rules/no-implicit-any-catch.md
phiresky4ba7d61
fix(typescript-estree): ignore catch clause type fixture from babel
phireskycb5be10
fix(typescript-estree): add updated snapshots for catch clause type
phiresky75533af
fix(typescript-estree): no-implicit-any-cache: add commented out type…
phiresky3aa4c07
chore: upgrade typescript for catch clause type
phireskyce0a8a3
Merge remote-tracking branch 'upstream/master'
phireskyd89ccd3
update to 4.0.0-beta and fix errors
bradzacher27af1e5
Merge remote-tracking branch 'upstream/master'
phiresky22282b1
fix yarn lockfile not freezing
bradzacher61dcd4e
fix type error
bradzacheracc2bde
update snaps
bradzacher92f0d95
Merge branch 'master' into pr/phiresky/2202
bradzachereb14de8
fix recommended config
bradzacher3f724c4
update readme
bradzacherFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionspackages/eslint-plugin/README.md
This file contains hidden or 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
76 changes: 76 additions & 0 deletionspackages/eslint-plugin/docs/rules/no-implicit-any-catch.md
This file contains hidden or 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,76 @@ | ||
# Disallow usage of the implicit `any` type in catch clauses (`no-implicit-any-catch`) | ||
TypeScript 4.0 added support for adding an explicit `any` or `unknown` type annotation on a catch clause variable. | ||
By default, TypeScript will type a catch clause variable as `any`, so explicitly annotating it as `unknown` can add a lot of safety to your codebase. | ||
The `noImplicitAny` flag in TypeScript does not cover this for backwards compatibility reasons. | ||
## Rule Details | ||
This rule requires an explicit type to be declared on a catch clause variable. | ||
The following pattern is considered a warning: | ||
```ts | ||
try { | ||
// ... | ||
} catch (e) { | ||
// ... | ||
} | ||
``` | ||
The following pattern is **_not_** considered a warning: | ||
<!-- TODO: prettier currently removes the type annotations, re-enable this once prettier is updated --> | ||
<!-- prettier-ignore-start --> | ||
```ts | ||
try { | ||
// ... | ||
} catch (e: unknown) { | ||
// ... | ||
} | ||
``` | ||
<!-- prettier-ignore-end --> | ||
## Options | ||
The rule accepts an options object with the following properties: | ||
```ts | ||
type Options = { | ||
// if false, disallow specifying `: any` as the error type as well. See also `no-explicit-any` | ||
allowExplicitAny: boolean; | ||
}; | ||
const defaults = { | ||
allowExplicitAny: false, | ||
}; | ||
``` | ||
### `allowExplicitAny` | ||
The follow is is **_not_** considered a warning with `{ allowExplicitAny: true }` | ||
<!-- TODO: prettier currently removes the type annotations, re-enable this once prettier is updated --> | ||
<!-- prettier-ignore-start --> | ||
```ts | ||
try { | ||
// ... | ||
} catch (e: any) { | ||
// ... | ||
} | ||
``` | ||
<!-- prettier-ignore-end --> | ||
## When Not To Use It | ||
If you are not using TypeScript 4.0 (or greater), then you will not be able to use this rule, annotations on catch clauses is not supported. | ||
## Further Reading | ||
- [TypeScript 4.0 Beta Release Notes](https://devblogs.microsoft.com/typescript/announcing-typescript-4-0-beta/#unknown-on-catch) |
1 change: 1 addition & 0 deletionspackages/eslint-plugin/src/configs/all.ts
This file contains hidden or 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
2 changes: 2 additions & 0 deletionspackages/eslint-plugin/src/rules/index.ts
This file contains hidden or 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
95 changes: 95 additions & 0 deletionspackages/eslint-plugin/src/rules/no-implicit-any-catch.ts
This file contains hidden or 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,95 @@ | ||
import * as util from '../util'; | ||
import { | ||
TSESLint, | ||
AST_NODE_TYPES, | ||
} from '@typescript-eslint/experimental-utils'; | ||
export type Options = [ | ||
{ | ||
allowExplicitAny: boolean; | ||
}, | ||
]; | ||
export type MessageIds = | ||
| 'implicitAnyInCatch' | ||
| 'explicitAnyInCatch' | ||
| 'suggestExplicitUnknown'; | ||
export default util.createRule<Options, MessageIds>({ | ||
name: 'no-implicit-any-catch', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Disallow usage of the implicit `any` type in catch clauses', | ||
category: 'Best Practices', | ||
recommended: false, | ||
suggestion: true, | ||
}, | ||
fixable: 'code', | ||
messages: { | ||
implicitAnyInCatch: 'Implicit any in catch clause', | ||
explicitAnyInCatch: 'Explicit any in catch clause', | ||
suggestExplicitUnknown: | ||
'Use `unknown` instead, this will force you to explicitly, and safely assert the type is correct.', | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
allowExplicitAny: { | ||
type: 'boolean', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
defaultOptions: [ | ||
{ | ||
allowExplicitAny: false, | ||
}, | ||
], | ||
create(context, [{ allowExplicitAny }]) { | ||
return { | ||
CatchClause(node): void { | ||
if (!node.param) { | ||
return; // ignore catch without variable | ||
} | ||
if (!node.param.typeAnnotation) { | ||
context.report({ | ||
node, | ||
messageId: 'implicitAnyInCatch', | ||
suggest: [ | ||
{ | ||
messageId: 'suggestExplicitUnknown', | ||
fix(fixer): TSESLint.RuleFix { | ||
return fixer.insertTextAfter(node.param!, ': unknown'); | ||
}, | ||
}, | ||
], | ||
}); | ||
} else if ( | ||
!allowExplicitAny && | ||
node.param.typeAnnotation.typeAnnotation.type === | ||
AST_NODE_TYPES.TSAnyKeyword | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'explicitAnyInCatch', | ||
suggest: [ | ||
{ | ||
messageId: 'suggestExplicitUnknown', | ||
fix(fixer): TSESLint.RuleFix { | ||
return fixer.replaceText( | ||
node.param!.typeAnnotation!, | ||
': unknown', | ||
); | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
78 changes: 78 additions & 0 deletionspackages/eslint-plugin/tests/rules/no-implicit-any-catch.test.ts
This file contains hidden or 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,78 @@ | ||
/* eslint-disable eslint-comments/no-use */ | ||
// TODO - prettier currently removes the type annotations, re-enable this once prettier is updated | ||
/* eslint "@typescript-eslint/internal/plugin-test-formatting": ["error", { formatWithPrettier: false }] */ | ||
/* eslint-enable eslint-comments/no-use */ | ||
import rule from '../../src/rules/no-implicit-any-catch'; | ||
import { RuleTester } from '../RuleTester'; | ||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
ruleTester.run('no-implicit-any-catch', rule, { | ||
valid: [ | ||
` | ||
try { | ||
} catch (e1: unknown) {} | ||
`, | ||
{ | ||
code: ` | ||
try { | ||
} catch (e2: any) {} | ||
`, | ||
options: [{ allowExplicitAny: true }], | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
try { | ||
} catch (e3) {} | ||
`.trim(), | ||
errors: [ | ||
{ | ||
line: 2, | ||
column: 3, | ||
messageId: 'implicitAnyInCatch', | ||
endLine: 2, | ||
endColumn: 16, | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestExplicitUnknown', | ||
output: ` | ||
try { | ||
} catch (e3: unknown) {} | ||
`.trim(), | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
try { | ||
} catch (e4: any) {} | ||
`.trim(), | ||
options: [{ allowExplicitAny: false }], | ||
errors: [ | ||
{ | ||
line: 2, | ||
column: 3, | ||
messageId: 'explicitAnyInCatch', | ||
endLine: 2, | ||
endColumn: 21, | ||
suggestions: [ | ||
{ | ||
messageId: 'suggestExplicitUnknown', | ||
output: ` | ||
try { | ||
} catch (e4: unknown) {} | ||
`.trim(), | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.