Skip to content

Commit 95e551d

Browse files
authored
catch-error-name: Use simple selector (#2112)
1 parent 47397f7 commit 95e551d

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

rules/catch-error-name.js

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@
22
const {findVariable} = require('@eslint-community/eslint-utils');
33
const avoidCapture = require('./utils/avoid-capture.js');
44
const {renameVariable} = require('./fix/index.js');
5-
const {matches, methodCallSelector} = require('./selectors/index.js');
5+
const {isMethodCall} = require('./ast/index.js');
66

77
const MESSAGE_ID = 'catch-error-name';
88
const messages = {
99
[MESSAGE_ID]: 'The catch parameter `{{originalName}}` should be named `{{fixedName}}`.',
1010
};
1111

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;
3434

3535
/** @param {import('eslint').Rule.RuleContext} context */
3636
const create = context => {
@@ -50,7 +50,14 @@ const create = context => {
5050
|| name.endsWith(expectedName.charAt(0).toUpperCase() + expectedName.slice(1));
5151

5252
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+
5461
const originalName = node.name;
5562

5663
if (

0 commit comments

Comments
 (0)