forked from sindresorhus/eslint-plugin-unicorn
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefer-optional-catch-binding.js
61 lines (52 loc) · 1.24 KB
/
prefer-optional-catch-binding.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
'use strict';
const getDocumentationUrl = require('./utils/get-documentation-url');
const {findVariable, isOpeningParenToken, isClosingParenToken} = require('eslint-utils');
const ERROR_MESSAGE_ID = 'error';
const selector = [
'CatchClause',
'>',
'Identifier.param'
].join('');
const create = context => {
return {
[selector]: node => {
const scope = context.getScope();
const variable = findVariable(scope, node);
if (variable.references.length !== 0) {
return;
}
const {name} = node;
context.report({
node,
messageId: ERROR_MESSAGE_ID,
data: {name},
fix: fixer => {
const tokenBefore = context.getTokenBefore(node);
const tokenAfter = context.getTokenAfter(node);
/* istanbul ignore next */
if (!isOpeningParenToken(tokenBefore) || !isClosingParenToken(tokenAfter)) {
throw new Error('Unexpected token.');
}
return [
tokenBefore,
node,
tokenAfter
].map(nodeOrToken => fixer.remove(nodeOrToken));
}
});
}
};
};
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
url: getDocumentationUrl(__filename)
},
fixable: 'code',
messages: {
[ERROR_MESSAGE_ID]: 'Remove unused catch binding paramter `{{name}}`.'
}
}
};