Skip to content

Commit 82f5e69

Browse files
committed
wip(eslint-plugin): apply Bryce's suggestion and add a fixer method
1 parent e3e7a33 commit 82f5e69

File tree

1 file changed

+30
-98
lines changed

1 file changed

+30
-98
lines changed

projects/eslint-plugin/rules/general/lib/rules/blank-line-declaration-usage.js

Lines changed: 30 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -5,115 +5,47 @@
55

66
module.exports = {
77
create(context) {
8+
const sourceCode = context.getSourceCode();
9+
810
return {
911
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+
) {
4116
return;
4217
}
4318

44-
const linesBetween =
45-
nodeToCheck.loc.start.line - node.loc.end.line;
19+
const {references} = context.getDeclaredVariables(node)[0];
4620

47-
if (linesBetween > 1) {
48-
return;
49-
}
21+
const declarationLine = references[0].identifier.loc.start.line;
5022

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

5626
if (
57-
nodeToCheck.expression.type === 'AssignmentExpression'
27+
sourceCode.lines
28+
.slice(declarationLine, referenceLine - 1)
29+
.indexOf('') === -1
5830
) {
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+
});
6747
}
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+
});
11749
},
11850
};
11951
},

0 commit comments

Comments
 (0)