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

fix(eslint-plugin): [no-explicit-any] Fix ignoreRestArgs for interfaces #777

Merged
merged 1 commit into from Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
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