2
2
const { findVariable} = require ( '@eslint-community/eslint-utils' ) ;
3
3
const avoidCapture = require ( './utils/avoid-capture.js' ) ;
4
4
const { renameVariable} = require ( './fix/index.js' ) ;
5
- const { matches , methodCallSelector } = require ( './selectors /index.js' ) ;
5
+ const { isMethodCall } = require ( './ast /index.js' ) ;
6
6
7
7
const MESSAGE_ID = 'catch-error-name' ;
8
8
const messages = {
9
9
[ MESSAGE_ID ] : 'The catch parameter `{{originalName}}` should be named `{{fixedName}}`.' ,
10
10
} ;
11
11
12
- const selector = matches ( [
13
- // `try {} catch (foo) {}`
14
- [
15
- 'CatchClause' ,
16
- ' > ' ,
17
- 'Identifier.param' ,
18
- ] . join ( '' ) ,
19
- // - `promise.then(…, foo => {})`
20
- // - `promise.then(…, function(foo) {})`
21
- // - `promise.catch(foo => {})`
22
- // - `promise.catch(function(foo) {})`
23
- [
24
- matches ( [
25
- methodCallSelector ( { method : 'then' , argumentsLength : 2 } ) ,
26
- methodCallSelector ( { method : 'catch' , argumentsLength : 1 } ) ,
27
- ] ) ,
28
- ' > ' ,
29
- ':matches(FunctionExpression, ArrowFunctionExpression).arguments:last-child' ,
30
- ' > ' ,
31
- 'Identifier.params:first-child' ,
32
- ] . join ( '' ) ,
33
- ] ) ;
12
+ // - `promise.then(…, foo => {})`
13
+ // - `promise.then(…, function (foo) {}) `
14
+ // - `promise.catch(foo => {})`
15
+ // - `promise.catch(function(foo) {})`
16
+ const isPromiseCatchParameter = node =>
17
+ ( node . parent . type === 'FunctionExpression' || node . parent . type === 'ArrowFunctionExpression' )
18
+ && node . parent . params [ 0 ] === node
19
+ && (
20
+ isMethodCall ( node . parent . parent , {
21
+ method : 'then' ,
22
+ argumentsLength : 2 ,
23
+ optionalCall : false ,
24
+ optionalMember : false ,
25
+ } )
26
+ || isMethodCall ( node . parent . parent , {
27
+ method : 'catch' ,
28
+ argumentsLength : 1 ,
29
+ optionalCall : false ,
30
+ optionalMember : false ,
31
+ } )
32
+ )
33
+ && node . parent . parent . arguments . at ( - 1 ) === node . parent ;
34
34
35
35
/** @param {import('eslint').Rule.RuleContext } context */
36
36
const create = context => {
@@ -50,7 +50,14 @@ const create = context => {
50
50
|| name . endsWith ( expectedName . charAt ( 0 ) . toUpperCase ( ) + expectedName . slice ( 1 ) ) ;
51
51
52
52
return {
53
- [ selector ] ( node ) {
53
+ Identifier ( node ) {
54
+ if (
55
+ ! ( node . parent . type === 'CatchClause' && node . parent . param === node )
56
+ && ! isPromiseCatchParameter ( node )
57
+ ) {
58
+ return ;
59
+ }
60
+
54
61
const originalName = node . name ;
55
62
56
63
if (
0 commit comments