Skip to content

Commit 7450b5a

Browse files
committed
wip(eslint-plugin): add new blank-line-declaration-usage rule and tests for it
1 parent c406ccc commit 7450b5a

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

projects/eslint-plugin/configs/general.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ const config = {
9393
'@liferay/aui/no-node': 'error',
9494
'@liferay/aui/no-object': 'error',
9595
'@liferay/aui/no-one': 'error',
96+
'@liferay/blank-line-declaration-usage': 'error',
9697
'@liferay/destructure-requires': 'error',
9798
'@liferay/empty-line-between-elements': 'error',
9899
'@liferay/expect-assert': 'error',
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* SPDX-FileCopyrightText: © 2017 Liferay, Inc. <https://liferay.com>
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
6+
module.exports = {
7+
create(context) {
8+
return {
9+
VariableDeclaration(node) {
10+
if (!node.declarations.length) {
11+
return;
12+
}
13+
14+
const variableName = node.declarations[0].id.name;
15+
16+
const variableDeclarationIndex = node.parent.body.indexOf(node);
17+
18+
const nodeToCheck =
19+
node.parent.body[variableDeclarationIndex + 1];
20+
21+
if (!nodeToCheck) {
22+
return;
23+
}
24+
25+
const linesBetween =
26+
nodeToCheck.loc.start.line - node.loc.end.line;
27+
28+
if (linesBetween > 1) {
29+
return;
30+
}
31+
32+
let nodeToCheckName;
33+
34+
if (nodeToCheck.type === 'ExpressionStatement') {
35+
const matchingArgument = nodeToCheck.expression.arguments.find(
36+
(argument) => argument.name === variableName
37+
);
38+
39+
if (matchingArgument) {
40+
nodeToCheckName = matchingArgument.name;
41+
}
42+
}
43+
else {
44+
nodeToCheckName =
45+
nodeToCheck.declarations[0].init.object.name;
46+
}
47+
48+
if (nodeToCheckName === variableName) {
49+
context.report({
50+
message: 'error msg',
51+
node,
52+
});
53+
}
54+
},
55+
};
56+
},
57+
58+
meta: {
59+
docs: {
60+
category: 'Best Practices',
61+
description: 'error msg',
62+
recommended: false,
63+
url: 'https://github.com/liferay/eslint-config-liferay/issues/139',
64+
},
65+
fixable: 'code', // or "code" or "whitespace"
66+
schema: [],
67+
type: 'problem',
68+
},
69+
};
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* SPDX-FileCopyrightText: © 2017 Liferay, Inc. <https://liferay.com>
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
6+
const MultiTester = require('../../../../../scripts/MultiTester');
7+
const rule = require('../../../lib/rules/blank-line-declaration-usage');
8+
9+
const parserOptions = {
10+
parserOptions: {
11+
ecmaVersion: 6,
12+
},
13+
};
14+
15+
const ruleTester = new MultiTester(parserOptions);
16+
17+
ruleTester.run('blank-line-declaration-usage', rule, {
18+
invalid: [
19+
{
20+
code: `
21+
const test = 'test';
22+
const testLength = test.length;
23+
`,
24+
errors: [
25+
{
26+
message: 'error msg',
27+
type: 'VariableDeclaration',
28+
},
29+
],
30+
},
31+
{
32+
code: `
33+
const obj = {
34+
foo: 'bar',
35+
bar: 'foo',
36+
hi: 'bryce',
37+
}
38+
Object.keys(obj)
39+
`,
40+
errors: [
41+
{
42+
message: 'error msg',
43+
type: 'VariableDeclaration',
44+
},
45+
],
46+
},
47+
],
48+
valid: [
49+
{
50+
code: `
51+
const test = 'test';
52+
53+
const testLength = test.length;
54+
`,
55+
},
56+
{
57+
code: `
58+
const obj = {
59+
foo: 'bar',
60+
bar: 'foo',
61+
hi: 'bryce',
62+
}
63+
64+
Object.keys(obj)
65+
`,
66+
},
67+
],
68+
});

0 commit comments

Comments
 (0)