Skip to content

Commit

Permalink
fix(eslint-plugin): [no-explicit-any] Fix ignoreRestArgs for interfac…
Browse files Browse the repository at this point in the history
…es (#777)
  • Loading branch information
Dilatorily authored and bradzacher committed Jul 29, 2019
1 parent 24dac45 commit 22e9ae5
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
30 changes: 30 additions & 0 deletions packages/eslint-plugin/docs/rules/no-explicit-any.md
Expand Up @@ -128,6 +128,21 @@ const baz1 = function (...args: any[]) {}
const baz2 = function (...args: readonly any[]) {}
const baz3 = function (...args: Array<any>) {}
const baz4 = function (...args: ReadonlyArray<any>) {}

interface Qux1 { (...args: any[]): void; }
interface Qux2 { (...args: readonly any[]): void; }
interface Qux3 { (...args: Array<any>): void; }
interface Qux4 { (...args: ReadonlyArray<any>): void; }

function quux1(fn: (...args: any[]) => void): void {}
function quux2(fn: (...args: readonly any[]) => void): void {}
function quux3(fn: (...args: Array<any>) => void): void {}
function quux4(fn: (...args: ReadonlyArray<any>) => void): void {}

function quuz1(): ((...args: any[]) => void) {}
function quuz2(): ((...args: readonly any[]) => void) {}
function quuz3(): ((...args: Array<any>) => void) {}
function quuz4(): ((...args: ReadonlyArray<any>) => void) {}
```

Examples of **correct** code for the `{ "ignoreRestArgs": true }` option:
Expand All @@ -149,6 +164,21 @@ const baz1 = function (...args: any[]) {}
const baz2 = function (...args: readonly any[]) {}
const baz3 = function (...args: Array<any>) {}
const baz4 = function (...args: ReadonlyArray<any>) {}

interface Qux1 { (...args: any[]): void; }
interface Qux2 { (...args: readonly any[]): void; }
interface Qux3 { (...args: Array<any>): void; }
interface Qux4 { (...args: ReadonlyArray<any>): void; }

function quux1(fn: (...args: any[]) => void): void {}
function quux2(fn: (...args: readonly any[]) => void): void {}
function quux3(fn: (...args: Array<any>) => void): void {}
function quux4(fn: (...args: ReadonlyArray<any>) => void): void {}

function quuz1(): ((...args: any[]) => void) {}
function quuz2(): ((...args: readonly any[]) => void) {}
function quuz3(): ((...args: Array<any>) => void) {}
function quuz4(): ((...args: ReadonlyArray<any>) => void) {}
```

## When Not To Use It
Expand Down
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/rules/no-explicit-any.ts
Expand Up @@ -51,14 +51,16 @@ export default util.createRule<Options, MessageIds>({
/**
* Checks if the node is an arrow function, function declaration or function expression
* @param node the node to be validated.
* @returns true if the node is an arrow function, function declaration or function expression
* @returns true if the node is an arrow function, function declaration, function expression, function type, or call signature
* @private
*/
function isNodeValidFunction(node: TSESTree.Node): boolean {
return [
AST_NODE_TYPES.ArrowFunctionExpression,
AST_NODE_TYPES.FunctionDeclaration,
AST_NODE_TYPES.FunctionExpression,
AST_NODE_TYPES.TSFunctionType,
AST_NODE_TYPES.TSCallSignatureDeclaration,
].includes(node.type);
}

Expand Down
81 changes: 81 additions & 0 deletions packages/eslint-plugin/tests/rules/no-explicit-any.test.ts
Expand Up @@ -189,6 +189,54 @@ type obj = {
code: `const baz4 = (...args: ReadonlyArray<any>) => {}`,
options: [{ ignoreRestArgs: true }],
},
{
code: `interface Qux1 { (...args: any[]): void; }`,
options: [{ ignoreRestArgs: true }],
},
{
code: `interface Qux2 { (...args: readonly any[]): void; }`,
options: [{ ignoreRestArgs: true }],
},
{
code: `interface Qux3 { (...args: Array<any>): void; }`,
options: [{ ignoreRestArgs: true }],
},
{
code: `interface Qux4 { (...args: ReadonlyArray<any>): void; }`,
options: [{ ignoreRestArgs: true }],
},
{
code: `function quux1(fn: (...args: any[]) => void): void {}`,
options: [{ ignoreRestArgs: true }],
},
{
code: `function quux2(fn: (...args: readonly any[]) => void): void {}`,
options: [{ ignoreRestArgs: true }],
},
{
code: `function quux3(fn: (...args: Array<any>) => void): void {}`,
options: [{ ignoreRestArgs: true }],
},
{
code: `function quux4(fn: (...args: ReadonlyArray<any>) => void): void {}`,
options: [{ ignoreRestArgs: true }],
},
{
code: `function quuz1(): ((...args: any[]) => void) {}`,
options: [{ ignoreRestArgs: true }],
},
{
code: `function quuz2(): ((...args: readonly any[]) => void) {}`,
options: [{ ignoreRestArgs: true }],
},
{
code: `function quuz3(): ((...args: Array<any>) => void) {}`,
options: [{ ignoreRestArgs: true }],
},
{
code: `function quuz4(): ((...args: ReadonlyArray<any>) => void) {}`,
options: [{ ignoreRestArgs: true }],
},
],
invalid: ([
{
Expand Down Expand Up @@ -787,6 +835,39 @@ type obj = {
},
],
},
{
code: `interface Qux5 { (...args: any): void; }`,
options: [{ ignoreRestArgs: true }],
errors: [
{
messageId: 'unexpectedAny',
line: 1,
column: 28,
},
],
},
{
code: `function quux5(fn: (...args: any) => void): void {}`,
options: [{ ignoreRestArgs: true }],
errors: [
{
messageId: 'unexpectedAny',
line: 1,
column: 30,
},
],
},
{
code: `function quuz5(): ((...args: any) => void) {}`,
options: [{ ignoreRestArgs: true }],
errors: [
{
messageId: 'unexpectedAny',
line: 1,
column: 30,
},
],
},
] as InvalidTestCase[]).reduce<InvalidTestCase[]>((acc, testCase) => {
acc.push(testCase);
const options = testCase.options || [];
Expand Down

0 comments on commit 22e9ae5

Please sign in to comment.