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] ignoreRestArgs #548

Merged
merged 4 commits into from Jun 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/eslint-plugin/docs/rules/no-explicit-any.md
Expand Up @@ -87,6 +87,23 @@ function greet(param: Array<string>): string {}
function greet(param: Array<string>): Array<string> {}
```

### Options

This rule accepts a single object option with the following default configuration:

```json
{
"@typescript-eslint/no-explicit-any": [
"error",
{
"ignoreRestArgs": false
}
]
}
```

- `ignoreRestArgs: true` will disallow usages of `any` as a type declaration except rest spread parameters.

## When Not To Use It

If an unknown type or a library without typings is used
Expand Down
61 changes: 58 additions & 3 deletions packages/eslint-plugin/src/rules/no-explicit-any.ts
@@ -1,3 +1,7 @@
import {
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import * as util from '../util';

export default util.createRule({
Expand All @@ -12,12 +16,63 @@ export default util.createRule({
messages: {
unexpectedAny: 'Unexpected any. Specify a different type.',
},
schema: [],
schema: [
{
type: 'object',
additionalProperties: false,
properties: {
ignoreRestArgs: {
type: 'boolean',
},
},
},
],
},
defaultOptions: [],
create(context) {
defaultOptions: [
{
ignoreRestArgs: false,
},
],
create(context, [{ ignoreRestArgs }]) {
/**
* Checks if the node parent is a function declaration
* @param node the node to be validated.
* @returns true if the node parent is a function declaration
* @private
*/
function isParentFunctionDeclaration(node: TSESTree.Node): boolean {
return (
typeof node.parent !== 'undefined' &&
node.parent.type === AST_NODE_TYPES.FunctionDeclaration
);
}

/**
* Checks if the node great granparent is a rest element
* @param node the node to be validated.
* @returns true if the node great granparent is a rest element
* @private
*/
function isGreatGrandparentRestElementInFunctionDeclaration(
node: TSESTree.Node,
): boolean {
return (
typeof node.parent !== 'undefined' &&
typeof node.parent.parent !== 'undefined' &&
typeof node.parent.parent.parent !== 'undefined' &&
node.parent.parent.parent.type === AST_NODE_TYPES.RestElement &&
isParentFunctionDeclaration(node.parent.parent.parent)
);
}

return {
TSAnyKeyword(node) {
if (
ignoreRestArgs &&
isGreatGrandparentRestElementInFunctionDeclaration(node)
) {
return;
}
context.report({
node,
messageId: 'unexpectedAny',
Expand Down
24 changes: 24 additions & 0 deletions packages/eslint-plugin/tests/rules/no-explicit-any.test.ts
Expand Up @@ -129,6 +129,15 @@ type obj = {
message: string & Array<Array<string>>;
}
`,
// https://github.com/eslint/typescript-eslint-parser/issues/397
{
code: `
function foo(a: number, ...rest: any[]): void {
return;
}
`,
options: [{ ignoreRestArgs: true }],
},
],
invalid: [
{
Expand Down Expand Up @@ -679,5 +688,20 @@ type obj = {
},
],
},
{
// https://github.com/eslint/typescript-eslint-parser/issues/397
code: `
function foo(a: number, ...rest: any[]): void {
return;
}
`,
errors: [
{
messageId: 'unexpectedAny',
line: 2,
column: 42,
},
],
},
],
});