|
5 | 5 |
|
6 | 6 | module.exports = {
|
7 | 7 | create(context) {
|
| 8 | + const sourceCode = context.getSourceCode(); |
| 9 | + |
8 | 10 | return {
|
9 | 11 | VariableDeclaration(node) {
|
10 |
| - const getNodeName = (node) => { |
11 |
| - const nodeId = node.declarations[0].id; |
12 |
| - |
13 |
| - if (nodeId.type === 'ArrayPattern') { |
14 |
| - return nodeId.elements[0].name; |
15 |
| - } |
16 |
| - else if (nodeId.type === 'ObjectPattern') { |
17 |
| - let nameArray = []; |
18 |
| - |
19 |
| - nodeId.properties.map((property) => |
20 |
| - nameArray.push(property.key.name) |
21 |
| - ); |
22 |
| - |
23 |
| - return nameArray; |
24 |
| - } |
25 |
| - else { |
26 |
| - return nodeId.name; |
27 |
| - } |
28 |
| - }; |
29 |
| - |
30 |
| - if (node.declarations.length < 1 || node.parent.body.body) { |
31 |
| - return; |
32 |
| - } |
33 |
| - |
34 |
| - const variableName = getNodeName(node); |
35 |
| - const variableDeclarationIndex = node.parent.body.indexOf(node); |
36 |
| - |
37 |
| - const nodeToCheck = |
38 |
| - node.parent.body[variableDeclarationIndex + 1]; |
39 |
| - |
40 |
| - if (!nodeToCheck) { |
| 12 | + if ( |
| 13 | + node.parent.type === 'ForOfStatement' || |
| 14 | + node.parent.type === 'ForStatement' |
| 15 | + ) { |
41 | 16 | return;
|
42 | 17 | }
|
43 | 18 |
|
44 |
| - const linesBetween = |
45 |
| - nodeToCheck.loc.start.line - node.loc.end.line; |
| 19 | + const {references} = context.getDeclaredVariables(node)[0]; |
46 | 20 |
|
47 |
| - if (linesBetween > 1) { |
48 |
| - return; |
49 |
| - } |
| 21 | + const declarationLine = references[0].identifier.loc.start.line; |
50 | 22 |
|
51 |
| - let nodeToCheckName; |
52 |
| - |
53 |
| - if (nodeToCheck.type === 'ExpressionStatement') { |
54 |
| - let matchingArgument; |
| 23 | + references.slice(1).forEach((reference) => { |
| 24 | + const referenceLine = reference.identifier.loc.start.line; |
55 | 25 |
|
56 | 26 | if (
|
57 |
| - nodeToCheck.expression.type === 'AssignmentExpression' |
| 27 | + sourceCode.lines |
| 28 | + .slice(declarationLine, referenceLine - 1) |
| 29 | + .indexOf('') === -1 |
58 | 30 | ) {
|
59 |
| - matchingArgument = nodeToCheck.expression.right.elements.find( |
60 |
| - (el) => el.name === variableName |
61 |
| - ); |
62 |
| - } |
63 |
| - else { |
64 |
| - matchingArgument = nodeToCheck.expression.arguments.find( |
65 |
| - (argument) => argument.name === variableName |
66 |
| - ); |
| 31 | + context.report({ |
| 32 | + fix: (fixer) => { |
| 33 | + const referenceStartLine = |
| 34 | + reference.identifier.loc.start.line; |
| 35 | + const declarationEndLine = node.loc.end.line; |
| 36 | + |
| 37 | + if ( |
| 38 | + referenceStartLine === |
| 39 | + declarationEndLine + 1 |
| 40 | + ) { |
| 41 | + return fixer.insertTextAfter(node, '\n'); |
| 42 | + } |
| 43 | + }, |
| 44 | + message: 'error msg', |
| 45 | + node: reference.identifier, |
| 46 | + }); |
67 | 47 | }
|
68 |
| - |
69 |
| - if (matchingArgument) { |
70 |
| - nodeToCheckName = matchingArgument.name; |
71 |
| - } |
72 |
| - } |
73 |
| - else if ( |
74 |
| - nodeToCheck.declarations[0].init.type === 'TemplateLiteral' |
75 |
| - ) { |
76 |
| - const matchingArgument = nodeToCheck.declarations[0].init.expressions.find( |
77 |
| - (expression) => expression.name === variableName |
78 |
| - ); |
79 |
| - |
80 |
| - if (matchingArgument) { |
81 |
| - nodeToCheckName = matchingArgument.name; |
82 |
| - } |
83 |
| - } |
84 |
| - else if (node.declarations[0].id.type === 'ObjectPattern') { |
85 |
| - const matchingArgument = variableName.find( |
86 |
| - (name) => |
87 |
| - name === |
88 |
| - nodeToCheck.declarations[0].init.object.name |
89 |
| - ); |
90 |
| - |
91 |
| - if (matchingArgument) { |
92 |
| - nodeToCheckName = matchingArgument; |
93 |
| - } |
94 |
| - } |
95 |
| - else if (nodeToCheck.declarations[0].init.arguments) { |
96 |
| - const matchingArgument = nodeToCheck.declarations[0].init.arguments.find( |
97 |
| - (arg) => arg.name === variableName |
98 |
| - ); |
99 |
| - |
100 |
| - if (matchingArgument) { |
101 |
| - nodeToCheckName = matchingArgument.name; |
102 |
| - } |
103 |
| - } |
104 |
| - else { |
105 |
| - if (nodeToCheck.declarations[0].init.object) { |
106 |
| - nodeToCheckName = |
107 |
| - nodeToCheck.declarations[0].init.object.name; |
108 |
| - } |
109 |
| - } |
110 |
| - |
111 |
| - if (nodeToCheckName === variableName) { |
112 |
| - context.report({ |
113 |
| - message: 'error msg', |
114 |
| - node, |
115 |
| - }); |
116 |
| - } |
| 48 | + }); |
117 | 49 | },
|
118 | 50 | };
|
119 | 51 | },
|
|
0 commit comments