-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
catch-error-name.js
126 lines (112 loc) · 2.71 KB
/
catch-error-name.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
'use strict';
const {findVariable} = require('eslint-utils');
const avoidCapture = require('./utils/avoid-capture.js');
const {renameVariable} = require('./fix/index.js');
const {matches, methodCallSelector} = require('./selectors/index.js');
const MESSAGE_ID = 'catch-error-name';
const messages = {
[MESSAGE_ID]: 'The catch parameter `{{originalName}}` should be named `{{fixedName}}`.'
};
const selector = matches([
// `try {} catch (foo) {}`
[
'CatchClause',
' > ',
'Identifier.param'
].join(''),
// - `promise.then(…, foo => {})`
// - `promise.then(…, function(foo) {})`
// - `promise.catch(foo => {})`
// - `promise.catch(function(foo) {})`
[
matches([
methodCallSelector({name: 'then', length: 2}),
methodCallSelector({name: 'catch', length: 1})
]),
' > ',
':matches(FunctionExpression, ArrowFunctionExpression).arguments:last-child',
' > ',
'Identifier.params:first-child'
].join('')
]);
const create = context => {
const options = {
name: 'error',
ignore: [],
...context.options[0]
};
const {name: expectedName} = options;
const ignore = options.ignore.map(
pattern => pattern instanceof RegExp ? pattern : new RegExp(pattern, 'u')
);
const isNameAllowed = name =>
name === expectedName ||
ignore.some(regexp => regexp.test(name)) ||
name.endsWith(expectedName) ||
name.endsWith(expectedName.charAt(0).toUpperCase() + expectedName.slice(1));
return {
[selector]: node => {
const originalName = node.name;
if (
isNameAllowed(originalName) ||
isNameAllowed(originalName.replace(/_+$/g, ''))
) {
return;
}
const scope = context.getScope();
const variable = findVariable(scope, node);
// This was reported https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1075#issuecomment-768072967
// But can't reproduce, just ignore this case
/* istanbul ignore next */
if (!variable) {
return;
}
if (originalName === '_' && variable.references.length === 0) {
return;
}
const scopes = [
variable.scope,
...variable.references.map(({from}) => from)
];
const fixedName = avoidCapture(expectedName, scopes);
const problem = {
node,
messageId: MESSAGE_ID,
data: {
originalName,
fixedName: fixedName || expectedName
}
};
if (fixedName) {
problem.fix = fixer => renameVariable(variable, fixedName, fixer);
}
return problem;
}
};
};
const schema = [
{
type: 'object',
properties: {
name: {
type: 'string'
},
ignore: {
type: 'array',
uniqueItems: true
}
}
}
];
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Enforce a specific parameter name in catch clauses.'
},
fixable: 'code',
schema,
messages
}
};