Skip to content

Commit de1ccfb

Browse files
committed
wip(eslint-plugin): add a new invalid case and a lint against it, also include the rule in general config
1 parent fdad562 commit de1ccfb

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

projects/eslint-plugin/rules/general/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
module.exports = {
77
'array-is-array': require('./lib/rules/array-is-array'),
88
'destructure-requires': require('./lib/rules/destructure-requires'),
9+
'blank-line-declaration-usage': require('./lib/rules/blank-line-declaration-usage'),
910
'empty-line-between-elements': require('./lib/rules/empty-line-between-elements'),
1011
'expect-assert': require('./lib/rules/expect-assert'),
1112
'group-imports': require('./lib/rules/group-imports'),

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,21 @@ module.exports = {
1313
if (nodeId.type === 'ArrayPattern') {
1414
return nodeId.elements[0].name;
1515
}
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+
}
1625
else {
1726
return nodeId.name;
1827
}
1928
};
2029

21-
if (!node.declarations.length) {
30+
if (node.declarations.length < 1) {
2231
return;
2332
}
2433

@@ -61,9 +70,22 @@ module.exports = {
6170
nodeToCheckName = matchingArgument.name;
6271
}
6372
}
73+
else if (node.declarations[0].id.type === 'ObjectPattern') {
74+
const matchingArgument = variableName.find(
75+
(name) =>
76+
name ===
77+
nodeToCheck.declarations[0].init.object.name
78+
);
79+
80+
if (matchingArgument) {
81+
nodeToCheckName = matchingArgument;
82+
}
83+
}
6484
else {
65-
nodeToCheckName =
66-
nodeToCheck.declarations[0].init.object.name;
85+
if (nodeToCheck.declarations[0].init.object) {
86+
nodeToCheckName =
87+
nodeToCheck.declarations[0].init.object.name;
88+
}
6789
}
6890

6991
if (nodeToCheckName === variableName) {

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,42 @@ ruleTester.run('blank-line-declaration-usage', rule, {
6666
},
6767
],
6868
},
69+
{
70+
code: `
71+
const obj = {
72+
foo: 'bar',
73+
bar: 'foo',
74+
hi: 'bryce',
75+
}
76+
77+
const {foo} = obj;
78+
const length = foo.length;
79+
`,
80+
errors: [
81+
{
82+
message: 'error msg',
83+
type: 'VariableDeclaration',
84+
},
85+
],
86+
},
87+
{
88+
code: `
89+
const obj = {
90+
foo: 'bar',
91+
bar: 'foo',
92+
hi: 'bryce',
93+
}
94+
95+
const {foo, bar, hi} = obj;
96+
const length = hi.length;
97+
`,
98+
errors: [
99+
{
100+
message: 'error msg',
101+
type: 'VariableDeclaration',
102+
},
103+
],
104+
},
69105
],
70106
valid: [
71107
{
@@ -86,5 +122,18 @@ ruleTester.run('blank-line-declaration-usage', rule, {
86122
Object.keys(obj)
87123
`,
88124
},
125+
{
126+
code: `
127+
const obj = {
128+
foo: 'bar',
129+
bar: 'foo',
130+
hi: 'bryce',
131+
}
132+
133+
const {foo} = obj;
134+
135+
const length = foo.length;
136+
`,
137+
},
89138
],
90139
});

0 commit comments

Comments
 (0)