Skip to content

Commit fdad562

Browse files
committed
wip(eslint-plugin): add a new invalid case and a lint against it
1 parent 7450b5a commit fdad562

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@ module.exports = {
77
create(context) {
88
return {
99
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 {
17+
return nodeId.name;
18+
}
19+
};
20+
1021
if (!node.declarations.length) {
1122
return;
1223
}
1324

14-
const variableName = node.declarations[0].id.name;
15-
25+
const variableName = getNodeName(node);
1626
const variableDeclarationIndex = node.parent.body.indexOf(node);
1727

1828
const nodeToCheck =
@@ -40,6 +50,17 @@ module.exports = {
4050
nodeToCheckName = matchingArgument.name;
4151
}
4252
}
53+
else if (
54+
nodeToCheck.declarations[0].init.type === 'TemplateLiteral'
55+
) {
56+
const matchingArgument = nodeToCheck.declarations[0].init.expressions.find(
57+
(expression) => expression.name === variableName
58+
);
59+
60+
if (matchingArgument) {
61+
nodeToCheckName = matchingArgument.name;
62+
}
63+
}
4364
else {
4465
nodeToCheckName =
4566
nodeToCheck.declarations[0].init.object.name;
@@ -62,7 +83,7 @@ module.exports = {
6283
recommended: false,
6384
url: 'https://github.com/liferay/eslint-config-liferay/issues/139',
6485
},
65-
fixable: 'code', // or "code" or "whitespace"
86+
fixable: 'code',
6687
schema: [],
6788
type: 'problem',
6889
},

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,28 @@ ruleTester.run('blank-line-declaration-usage', rule, {
4444
},
4545
],
4646
},
47+
{
48+
code: `
49+
function updateData() {
50+
const obj = {
51+
foo: 'bar',
52+
bar: 'foo',
53+
hi: 'bryce',
54+
}
55+
56+
const formattedCategoryIds = Object.keys(obj).length
57+
? 'superLongStringSoThatItForcesTheLineToBeWrapped'
58+
: 'justASimpleString';
59+
const APIUrl = \`${formattedCategoryIds}\`;
60+
}
61+
`,
62+
errors: [
63+
{
64+
message: 'error msg',
65+
type: 'VariableDeclaration',
66+
},
67+
],
68+
},
4769
],
4870
valid: [
4971
{

0 commit comments

Comments
 (0)