Skip to content

Commit 3356e35

Browse files
committed
wip: fix message, remove obsolete variables, and add output to invalid examples
1 parent e5b604d commit 3356e35

File tree

2 files changed

+113
-42
lines changed

2 files changed

+113
-42
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@
33
* SPDX-License-Identifier: MIT
44
*/
55

6+
const message =
7+
'Expected an empty line between variable declaration and usage.';
8+
69
module.exports = {
710
create(context) {
811
const sourceCode = context.getSourceCode();
912

1013
return {
1114
VariableDeclaration(node) {
1215
if (
16+
node.parent.type === 'ForStatement' ||
17+
node.parent.type === 'ForInStatement' ||
1318
node.parent.type === 'ForOfStatement' ||
14-
node.parent.type === 'ForStatement'
19+
node.parent.type === 'WhileStatement'
1520
) {
1621
return;
1722
}
@@ -30,18 +35,13 @@ module.exports = {
3035
) {
3136
context.report({
3237
fix: (fixer) => {
33-
const referenceStartLine =
34-
reference.identifier.loc.start.line;
3538
const declarationEndLine = node.loc.end.line;
3639

37-
if (
38-
referenceStartLine ===
39-
declarationEndLine + 1
40-
) {
40+
if (referenceLine === declarationEndLine + 1) {
4141
return fixer.insertTextAfter(node, '\n');
4242
}
4343
},
44-
message: 'error msg',
44+
message,
4545
node: reference.identifier,
4646
});
4747
}
@@ -53,7 +53,7 @@ module.exports = {
5353
meta: {
5454
docs: {
5555
category: 'Best Practices',
56-
description: 'error msg',
56+
description: message,
5757
recommended: false,
5858
url: 'https://github.com/liferay/eslint-config-liferay/issues/139',
5959
},

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

Lines changed: 104 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const parserOptions = {
1212
},
1313
};
1414

15+
const message =
16+
'Expected an empty line between variable declaration and usage.';
17+
1518
const ruleTester = new MultiTester(parserOptions);
1619

1720
ruleTester.run('blank-line-declaration-usage', rule, {
@@ -23,84 +26,135 @@ ruleTester.run('blank-line-declaration-usage', rule, {
2326
`,
2427
errors: [
2528
{
26-
message: 'error msg',
27-
type: 'VariableDeclaration',
29+
message,
30+
type: 'Identifier',
2831
},
2932
],
33+
output: `
34+
const test = 'test';
35+
36+
const testLength = test.length;
37+
`,
3038
},
3139
{
3240
code: `
3341
const obj = {
3442
foo: 'bar',
3543
bar: 'foo',
36-
hi: 'bryce',
44+
life: 'ray',
3745
}
38-
Object.keys(obj)
46+
Object.keys(obj)
3947
`,
4048
errors: [
4149
{
42-
message: 'error msg',
43-
type: 'VariableDeclaration',
50+
message,
51+
type: 'Identifier',
4452
},
4553
],
54+
output: `
55+
const obj = {
56+
foo: 'bar',
57+
bar: 'foo',
58+
life: 'ray',
59+
}
60+
61+
Object.keys(obj)
62+
`,
4663
},
4764
{
4865
code: `
4966
function updateData() {
5067
const obj = {
5168
foo: 'bar',
5269
bar: 'foo',
53-
hi: 'bryce',
70+
life: 'ray',
5471
}
5572
56-
const formattedCategoryIds = Object.keys(obj).length
73+
const ids = Object.keys(obj).length
5774
? 'superLongStringSoThatItForcesTheLineToBeWrapped'
5875
: 'justASimpleString';
59-
const APIUrl = \`${formattedCategoryIds}\`;
76+
ids.length;
6077
}
6178
`,
6279
errors: [
6380
{
64-
message: 'error msg',
65-
type: 'VariableDeclaration',
81+
message,
82+
type: 'Identifier',
6683
},
6784
],
85+
output: `
86+
function updateData() {
87+
const obj = {
88+
foo: 'bar',
89+
bar: 'foo',
90+
life: 'ray',
91+
}
92+
93+
const ids = Object.keys(obj).length
94+
? 'superLongStringSoThatItForcesTheLineToBeWrapped'
95+
: 'justASimpleString';
96+
97+
ids.length;
98+
}
99+
`,
68100
},
69101
{
70102
code: `
71103
const obj = {
72104
foo: 'bar',
73105
bar: 'foo',
74-
hi: 'bryce',
106+
life: 'ray',
75107
}
76-
108+
77109
const {foo} = obj;
78110
const length = foo.length;
79111
`,
80112
errors: [
81113
{
82-
message: 'error msg',
83-
type: 'VariableDeclaration',
114+
message,
115+
type: 'Identifier',
84116
},
85117
],
118+
output: `
119+
const obj = {
120+
foo: 'bar',
121+
bar: 'foo',
122+
life: 'ray',
123+
}
124+
125+
const {foo} = obj;
126+
127+
const length = foo.length;
128+
`,
86129
},
87130
{
88131
code: `
89132
const obj = {
90133
foo: 'bar',
91134
bar: 'foo',
92-
hi: 'bryce',
135+
life: 'ray',
93136
}
94-
95-
const {foo, bar, hi} = obj;
96-
const length = hi.length;
137+
138+
const {foo, bar, life} = obj;
139+
const length = life.length;
97140
`,
98141
errors: [
99142
{
100-
message: 'error msg',
101-
type: 'VariableDeclaration',
143+
message,
144+
type: 'Identifier',
102145
},
103146
],
147+
output: `
148+
const obj = {
149+
foo: 'bar',
150+
bar: 'foo',
151+
life: 'ray',
152+
}
153+
154+
const {foo, bar, life} = obj;
155+
156+
const length = life.length;
157+
`,
104158
},
105159
{
106160
code: `
@@ -109,36 +163,53 @@ ruleTester.run('blank-line-declaration-usage', rule, {
109163
`,
110164
errors: [
111165
{
112-
message: 'error msg',
113-
type: 'VariableDeclaration',
166+
message,
167+
type: 'Identifier',
114168
},
115169
],
170+
output: `
171+
const filePath = context.getFilename();
172+
173+
const filename = path.basename(something, filePath);
174+
`,
116175
},
117176
{
118177
code: `
119178
for (const file of files) {
120179
const contents = readFileAsync(file, 'utf8');
121180
const length = contents.length;
122-
}
181+
}
123182
`,
124183
errors: [
125184
{
126-
message: 'error msg',
127-
type: 'VariableDeclaration',
185+
message,
186+
type: 'Identifier',
128187
},
129188
],
189+
output: `
190+
for (const file of files) {
191+
const contents = readFileAsync(file, 'utf8');
192+
193+
const length = contents.length;
194+
}
195+
`,
130196
},
131197
{
132198
code: `
133-
var cbSpy = sinon.spy();
134-
prototype.rows = [cbSpy];
199+
const cbSpy = sinon.spy();
200+
prototype.rows = [cbSpy];
135201
`,
136202
errors: [
137203
{
138-
message: 'error msg',
139-
type: 'VariableDeclaration',
204+
message,
205+
type: 'Identifier',
140206
},
141207
],
208+
output: `
209+
const cbSpy = sinon.spy();
210+
211+
prototype.rows = [cbSpy];
212+
`,
142213
},
143214
],
144215
valid: [
@@ -154,7 +225,7 @@ ruleTester.run('blank-line-declaration-usage', rule, {
154225
const obj = {
155226
foo: 'bar',
156227
bar: 'foo',
157-
hi: 'bryce',
228+
life: 'ray',
158229
}
159230
160231
Object.keys(obj)
@@ -165,9 +236,9 @@ ruleTester.run('blank-line-declaration-usage', rule, {
165236
const obj = {
166237
foo: 'bar',
167238
bar: 'foo',
168-
hi: 'bryce',
239+
life: 'ray',
169240
}
170-
241+
171242
const {foo} = obj;
172243
173244
const length = foo.length;

0 commit comments

Comments
 (0)