10000 fix(eslint-plugin): [no-explicit-any] Fix ignoreRestArgs for interfac… · ajmacd/typescript-eslint@22e9ae5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 22e9ae5

Browse files
Dilatorilybradzacher
authored andcommitted
fix(eslint-plugin): [no-explicit-any] Fix ignoreRestArgs for interfaces (typescript-eslint#777)
1 parent 24dac45 commit 22e9ae5

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

packages/eslint-plugin/docs/rules/no-explicit-any.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ const baz1 = function (...args: any[]) {}
128128
const baz2 = function (...args: readonly any[]) {}
129129
const baz3 = function (...args: Array<any>) {}
130130
const baz4 = function (...args: ReadonlyArray<any>) {}
131+
132+
interface Qux1 { (...args: any[]): void; }
133+
interface Qux2 { (...args: readonly any[]): void; }
134+
interface Qux3 { (...args: Array<any>): void; }
135+
interface Qux4 { (...args: ReadonlyArray<any>): void; }
136+
137+
function quux1(fn: (...args: any[]) => void): void {}
138+
function quux2(fn: (...args: readonly any[]) => void): void {}
139+
function quux3(fn: (...args: Array<any>) => void): void {}
140+
function quux4(fn: (...args: ReadonlyArray<any>) => void): void {}
141+
142+
function quuz1(): ((...args: any[]) => void) {}
143+
function quuz2(): ((...args: readonly any[]) => void) {}
144+
function quuz3(): ((...args: Array<any>) => void) {}
145+
function quuz4(): ((...args: ReadonlyArray<any>) => void) {}
131146
```
132147

133148
Examples of **correct** code for the `{ "ignoreRestArgs": true }` option:
@@ -149,6 +164,21 @@ const baz1 = function (...args: any[]) {}
149164
const baz2 = function (...args: readonly any[]) {}
150165
const baz3 = function (...args: Array<any>) {}
151166
const baz4 = function (...args: ReadonlyArray<any>) {}
167+
168+
interface Qux1 { (...args: any[]): void; }
169+
interface Qux2 { (...args: readonly any[]): void; }
170+
interface Qux3 { (...args: Array<any>): void; }
171+
interface Qux4 { (...args: ReadonlyArray<any>): void; }
172+
173+
function quux1(fn: (...args: any[]) => void): void {}
174+
function quux2(fn: (...args: readonly any[]) => void): void {}
175+
function quux3(fn: (...args: Array<any>) => void): void {}
176+
function quux4(fn: (...args: ReadonlyArray<any>) => void): void {}
177+
178+
function quuz1(): ((...args: any[]) => void) {}
179+
function quuz2(): ((...args: readonly any[]) => void) {}
180+
function quuz3(): ((...args: Array<any>) => void) {}
181+
function quuz4(): ((...args: ReadonlyArray<any>) => void) {}
152182
```
153183

154184
## When Not To Use It

packages/eslint-plugin/src/rules/no-explicit-any.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,16 @@ export default util.createRule<Options, MessageIds>({
5151
/**
5252
* Checks if the node is an arrow function, function declaration or function expression
5353
* @param node the node to be validated.
54-
* @returns true if the node is an arrow function, function declaration or function expression
54+
* @returns true if the node is an arrow function, function declaration, function expression, function type, or call signature
5555
* @private
5656
*/
5757
function isNodeValidFunction(node: TSESTree.Node): boolean {
5858
return [
5959
AST_NODE_TYPES.ArrowFunctionExpression,
6060
AST_NODE_TYPES.FunctionDeclaration,
6161
AST_NODE_TYPES.FunctionExpression,
62+
AST_NODE_TYPES.TSFunctionType,
63+
AST_NODE_TYPES.TSCallSignatureDeclaration,
6264
].includes(node.type);
6365
}
6466

packages/eslint-plugin/tests/rules/no-explicit-any.test.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,54 @@ type obj = {
189189
code: `const baz4 = (...args: ReadonlyArray<any>) => {}`,
190190
options: [{ ignoreRestArgs: true }],
191191
},
192+
{
193+
code: `interface Qux1 { (...args: any[]): void; }`,
194+
options: [{ ignoreRestArgs: true }],
195+
},
196+
{
197+
code: `interface Qux2 { (...args: readonly any[]): void; }`,
198+
options: [{ ignoreRestArgs: true }],
199+
},
200+
{
201+
code: `interface Qux3 { (...args: Array<any>): void; }`,
202+
options: [{ ignoreRestArgs: true }],
203+
},
204+
{
205+
code: `interface Qux4 { (...args: ReadonlyArray<any>): void; }`,
206+
options: [{ ignoreRestArgs: true }],
207+
},
208+
{
209+
code: `function quux1(fn: (...args: any[]) => void): void {}`,
210+
options: [{ ignoreRestArgs: true }],
211+
},
212+
{
213+
code: `function quux2(fn: (...args: readonly any[]) => void): void {}`,
214+
options: [{ ignoreRestArgs: true }],
215+
},
216+
{
217+
code: `function quux3(fn: (...args: Array<any>) => void): void {}`,
218+
options: [{ ignoreRestArgs: true }],
219+
},
220+
{
221+
code: `function quux4(fn: (...args: ReadonlyArray<any>) => void): void {}`,
222+
options: [{ ignoreRestArgs: true }],
223+
},
224+
{
225+
code: `function quuz1(): ((...args: any[]) => void) {}`,
226+
options: [{ ignoreRestArgs: true }],
227+
},
228+
{
229+
code: `function quuz2(): ((...args: readonly any[]) => void) {}`,
230+
options: [{ ignoreRestArgs: true }],
231+
},
232+
{
233+
code: `function quuz3(): ((...args: Array<any>) => void) {}`,
234+
options: [{ ignoreRestArgs: true }],
235+
},
236+
{
237+
code: `function quuz4(): ((...args: ReadonlyArray<any>) => void) {}`,
238+
options: [{ ignoreRestArgs: true }],
239+
},
192240
],
193241
invalid: ([
194242
{
@@ -787,6 +835,39 @@ type obj = {
787835
},
788836
],
789837
},
838+
{
839+
code: `interface Qux5 { (...args: any): void; }`,
840+
options: [{ ignoreRestArgs: true }],
841+
errors: [
842+
{
843+
messageId: 'unexpectedAny',
844+
line: 1,
845+
column: 28,
846+
},
847+
],
848+
},
849+
{
850+
code: `function quux5(fn: (...args: any) => void): void {}`,
851+
options: [{ ignoreRestArgs: true }],
852+
errors: [
853+
{
854+
messageId: 'unexpectedAny',
855+
line: 1,
856+
column: 30,
857+
},
858+
],
859+
},
860+
{
861+
code: `function quuz5(): ((...args: any) => void) {}`,
862+
options: [{ ignoreRestArgs: true }],
863+
errors: [
864+
{
865+
messageId: 'unexpectedAny',
866+
line: 1,
867+
column: 30,
868+
},
869+
],
870+
},
790871
] as InvalidTestCase[]).reduce<InvalidTestCase[]>((acc, testCase) => {
791872
acc.push(testCase);
792873
const options = testCase.options || [];

0 commit comments

Comments
 (0)
0