Skip to content

Commit ea0f36a

Browse files
fix(no-implicit-any-catch): proper parenthesis for two-param catchError (#230)
We're assuming that if `callback` has more than one parameter (i.e. `restParams.length > 0`), then all the parameters should already be wrapped in parenthesis (or else it's invalid JS syntax), so we can safely insert `: unknown` and nothing else. Resolves #224
1 parent 0601c7f commit ea0f36a

File tree

2 files changed

+132
-2
lines changed

2 files changed

+132
-2
lines changed

src/rules/no-implicit-any-catch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export const noImplicitAnyCatchRule = ruleCreator({
8484
isArrowFunctionExpression(callback)
8585
|| isFunctionExpression(callback)
8686
) {
87-
const [param] = callback.params;
87+
const [param, ...restParams] = callback.params;
8888
if (!param) {
8989
return;
9090
}
@@ -128,7 +128,7 @@ export const noImplicitAnyCatchRule = ruleCreator({
128128
}
129129
} else {
130130
function fix(fixer: eslint.RuleFixer) {
131-
if (isParenthesised(sourceCode, param)) {
131+
if (isParenthesised(sourceCode, param) || restParams.length > 0) {
132132
return fixer.insertTextAfter(param, ': unknown');
133133
}
134134
return [

tests/rules/no-implicit-any-catch.test.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,26 @@ ruleTester({ types: true }).run('no-implicit-any-catch', noImplicitAnyCatchRule,
9595
);
9696
`,
9797
},
98+
{
99+
code: stripIndent`
100+
// arrow; explicit unknown and caught; default option
101+
import { throwError, catchError, Observable } from "rxjs";
102+
103+
throwError(new Error("Kaboom!")).pipe(
104+
catchError((error: unknown, caught: Observable<unknown>) => console.error(error)),
105+
);
106+
`,
107+
},
108+
{
109+
code: stripIndent`
110+
// non-arrow; explicit unknown and caught; default option
111+
import { throwError, catchError, Observable } from "rxjs";
112+
113+
throwError(new Error("Kaboom!")).pipe(
114+
catchError(function (error: unknown, caught: Observable<unknown>) { console.error(error); }),
115+
);
116+
`,
117+
},
98118
{
99119
code: stripIndent`
100120
// subscribe; arrow; explicit unknown; default option
@@ -117,6 +137,28 @@ ruleTester({ types: true }).run('no-implicit-any-catch', noImplicitAnyCatchRule,
117137
);
118138
`,
119139
},
140+
{
141+
code: stripIndent`
142+
// subscribe; arrow; explicit unknown and caught; default option
143+
import { throwError, catchError, Observable } from "rxjs";
144+
145+
throwError(new Error("Kaboom!")).subscribe(
146+
undefined,
147+
(error: unknown, caught: Observable<unknown>) => console.error(error)
148+
);
149+
`,
150+
},
151+
{
152+
code: stripIndent`
153+
// subscribe; arrow; explicit any and caught; default option
154+
import { throwError, catchError, Observable } from "rxjs";
155+
156+
throwError(new Error("Kaboom!")).subscribe(
157+
undefined,
158+
(error: any, caught: Observable<unknown>) => console.error(error)
159+
);
160+
`,
161+
},
120162
{
121163
code: stripIndent`
122164
// subscribe observer; arrow; explicit unknown; default option
@@ -137,6 +179,26 @@ ruleTester({ types: true }).run('no-implicit-any-catch', noImplicitAnyCatchRule,
137179
});
138180
`,
139181
},
182+
{
183+
code: stripIndent`
184+
// subscribe observer; arrow; explicit unknown and caught; default option
185+
import { throwError, catchError, Observable } from "rxjs";
186+
187+
throwError(new Error("Kaboom!")).subscribe({
188+
error: (error: unknown, caught: Observable<unknown>) => console.error(error)
189+
});
190+
`,
191+
},
192+
{
193+
code: stripIndent`
194+
// subscribe observer; arrow; explicit any and caught; default option
195+
import { throwError, catchError, Observable } from "rxjs";
196+
197+
throwError(new Error("Kaboom!")).subscribe({
198+
error: (error: any, caught: Observable<unknown>) => console.error(error)
199+
});
200+
`,
201+
},
140202
{
141203
code: stripIndent`
142204
// tap; arrow; explicit unknown; default option
@@ -272,6 +334,40 @@ ruleTester({ types: true }).run('no-implicit-any-catch', noImplicitAnyCatchRule,
272334
],
273335
},
274336
),
337+
fromFixture(
338+
stripIndent`
339+
// arrow; implicit any and caught
340+
import { throwError, catchError } from "rxjs";
341+
342+
throwError(new Error("Kaboom!")).pipe(
343+
catchError((error, caught) => console.error(error)),
344+
~~~~~ [implicitAny suggest]
345+
);
346+
`,
347+
{
348+
output: stripIndent`
349+
// arrow; implicit any and caught
350+
import { throwError, catchError } from "rxjs";
351+
352+
throwError(new Error("Kaboom!")).pipe(
353+
catchError((error: unknown, caught) => console.error(error)),
354+
);
355+
`,
356+
suggestions: [
357+
{
358+
messageId: 'suggestExplicitUnknown',
359+
output: stripIndent`
360+
// arrow; implicit any and caught
361+
import { throwError, catchError } from "rxjs";
362+
363+
throwError(new Error("Kaboom!")).pipe(
364+
catchError((error: unknown, caught) => console.error(error)),
365+
);
366+
`,
367+
},
368+
],
369+
},
370+
),
275371
fromFixture(
276372
stripIndent`
277373
// non-arrow; implicit any
@@ -309,6 +405,40 @@ ruleTester({ types: true }).run('no-implicit-any-catch', noImplicitAnyCatchRule,
309405
],
310406
},
311407
),
408+
fromFixture(
409+
stripIndent`
410+
// non-arrow; implicit any and caught
411+
import { throwError, catchError } from "rxjs";
412+
413+
throwError(new Error("Kaboom!")).pipe(
414+
catchError(function (error, caught) { console.error(error); })
415+
~~~~~ [implicitAny suggest]
416+
);
417+
`,
418+
{
419+
output: stripIndent`
420+
// non-arrow; implicit any and caught
421+
import { throwError, catchError } from "rxjs";
422+
423+
throwError(new Error("Kaboom!")).pipe(
424+
catchError(function (error: unknown, caught) { console.error(error); })
425+
);
426+
`,
427+
suggestions: [
428+
{
429+
messageId: 'suggestExplicitUnknown',
430+
output: stripIndent`
431+
// non-arrow; implicit any and caught
432+
import { throwError, catchError } from "rxjs";
433+
434+
throwError(new Error("Kaboom!")).pipe(
435+
catchError(function (error: unknown, caught) { console.error(error); })
436+
);
437+
`,
438+
},
439+
],
440+
},
441+
),
312442
fromFixture(
313443
stripIndent`
314444
// arrow; explicit any; explicit option

0 commit comments

Comments
 (0)