1 | 'use strict';
|
2 | const getDocumentationUrl = require('./utils/get-documentation-url');
|
3 | const {findVariable, isOpeningParenToken, isClosingParenToken} = require('eslint-utils');
|
4 |
|
5 | const ERROR_MESSAGE_ID = 'error';
|
6 |
|
7 | const selector = [
|
8 | 'CatchClause',
|
9 | '>',
|
10 | 'Identifier.param'
|
11 | ].join('');
|
12 |
|
13 | const create = context => {
|
14 | return {
|
15 | [selector]: node => {
|
16 | const scope = context.getScope();
|
17 | const variable = findVariable(scope, node);
|
18 |
|
19 | if (variable.references.length !== 0) {
|
20 | return;
|
21 | }
|
22 |
|
23 | const {name} = node;
|
24 |
|
25 | context.report({
|
26 | node,
|
27 | messageId: ERROR_MESSAGE_ID,
|
28 | data: {name},
|
29 | fix: fixer => {
|
30 | const tokenBefore = context.getTokenBefore(node);
|
31 | const tokenAfter = context.getTokenAfter(node);
|
32 |
|
33 |
|
34 | if (!isOpeningParenToken(tokenBefore) || !isClosingParenToken(tokenAfter)) {
|
35 | throw new Error('Unexpected token.');
|
36 | }
|
37 |
|
38 | return [
|
39 | tokenBefore,
|
40 | node,
|
41 | tokenAfter
|
42 | ].map(nodeOrToken => fixer.remove(nodeOrToken));
|
43 | }
|
44 | });
|
45 | }
|
46 | };
|
47 | };
|
48 |
|
49 | module.exports = {
|
50 | create,
|
51 | meta: {
|
52 | type: 'suggestion',
|
53 | docs: {
|
54 | url: getDocumentationUrl(__filename)
|
55 | },
|
56 | fixable: 'code',
|
57 | messages: {
|
58 | [ERROR_MESSAGE_ID]: 'Remove unused catch binding `{{name}}`.'
|
59 | }
|
60 | }
|
61 | };
|