Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): [no-explicit-any] Add an optional fixer #609

Merged
merged 10 commits into from Jun 28, 2019
2 changes: 1 addition & 1 deletion packages/eslint-plugin/README.md
Expand Up @@ -143,7 +143,7 @@ Then you should add `airbnb` (or `airbnb-base`) to your `extends` section of `.e
| [`@typescript-eslint/no-angle-bracket-type-assertion`](./docs/rules/no-angle-bracket-type-assertion.md) | Enforces the use of `as Type` assertions instead of `<Type>` assertions | :heavy_check_mark: | | |
| [`@typescript-eslint/no-array-constructor`](./docs/rules/no-array-constructor.md) | Disallow generic `Array` constructors | :heavy_check_mark: | :wrench: | |
| [`@typescript-eslint/no-empty-interface`](./docs/rules/no-empty-interface.md) | Disallow the declaration of empty interfaces | :heavy_check_mark: | | |
| [`@typescript-eslint/no-explicit-any`](./docs/rules/no-explicit-any.md) | Disallow usage of the `any` type | :heavy_check_mark: | | |
| [`@typescript-eslint/no-explicit-any`](./docs/rules/no-explicit-any.md) | Disallow usage of the `any` type | :heavy_check_mark: | :wrench: | |
| [`@typescript-eslint/no-extra-parens`](./docs/rules/no-extra-parens.md) | Disallow unnecessary parentheses | | :wrench: | |
| [`@typescript-eslint/no-extraneous-class`](./docs/rules/no-extraneous-class.md) | Forbids the use of classes as namespaces | | | |
| [`@typescript-eslint/no-floating-promises`](./docs/rules/no-floating-promises.md) | Requires Promise-like values to be handled appropriately. | | | :thought_balloon: |
Expand Down
15 changes: 15 additions & 0 deletions packages/eslint-plugin/docs/rules/no-explicit-any.md
Expand Up @@ -87,6 +87,21 @@ function greet(param: Array<string>): string {}
function greet(param: Array<string>): Array<string> {}
```

## Options

The rule accepts an options object with the following properties:

```ts
type Options = {
// if true, only when fix any to unknown
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
fixToUnknown: boolean;
};

const defaults = {
fixToUnknwon: false,
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
};
```

## When Not To Use It

If an unknown type or a library without typings is used
Expand Down
38 changes: 34 additions & 4 deletions packages/eslint-plugin/src/rules/no-explicit-any.ts
@@ -1,6 +1,14 @@
import * as util from '../util';
import { TSESLint } from '@typescript-eslint/experimental-utils';

export default util.createRule({
type Options = [
{
fixToUnknown: boolean;
}
];
type MessageIds = 'unexpectedAny';

export default util.createRule<Options, MessageIds>({
name: 'no-explicit-any',
meta: {
type: 'suggestion',
Expand All @@ -9,18 +17,40 @@ export default util.createRule({
category: 'Best Practices',
recommended: 'warn',
},
fixable: 'code',
messages: {
unexpectedAny: 'Unexpected any. Specify a different type.',
},
schema: [],
schema: [
{
type: 'object',
properties: {
fixToUnknown: {
type: 'boolean',
},
},
},
],
},
defaultOptions: [],
create(context) {
defaultOptions: [
{
fixToUnknown: false,
},
],

create(context, [option]) {
return {
TSAnyKeyword(node) {
let fix: TSESLint.ReportFixFunction | null = null;

if (option.fixToUnknown) {
fix = fixer => fixer.replaceText(node, 'unknown');
}

context.report({
node,
messageId: 'unexpectedAny',
fix,
});
},
};
Expand Down