File tree Expand file tree Collapse file tree 3 files changed +138
-0
lines changed
Expand file tree Collapse file tree 3 files changed +138
-0
lines changed Original file line number Diff line number Diff 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' ,
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 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+ } ) ;
You can’t perform that action at this time.
0 commit comments