Skip to content

Commit 7e54a31

Browse files
authored
End of chain (#523)
1 parent 5a8518b commit 7e54a31

File tree

3 files changed

+159
-8
lines changed

3 files changed

+159
-8
lines changed

src/nodes/MemberAccess.js

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,44 @@ const {
44
}
55
} = require('prettier/standalone');
66

7-
const isBeginnigOfChain = (path) => {
8-
const parentNodeType = path.getParentNode().type;
7+
const isEndOfChain = (node, path) => {
8+
let i = 0;
9+
let currentNode = node;
10+
let parentNode = path.getParentNode(i);
11+
while (
12+
parentNode &&
13+
[
14+
'FunctionCall',
15+
'IndexAccess',
16+
'NameValueExpression',
17+
'MemberAccess'
18+
].includes(parentNode.type)
19+
) {
20+
switch (parentNode.type) {
21+
case 'MemberAccess':
22+
// If direct ParentNode is a MemberAcces we are not at the end of the chain
23+
return false;
924

10-
if (parentNodeType === 'MemberAccess') return false;
11-
if (parentNodeType === 'FunctionCall') {
12-
const grandParentNodeType = path.getParentNode(1).type;
13-
return grandParentNodeType !== 'MemberAccess';
14-
}
25+
case 'IndexAccess':
26+
// If direct ParentNode is an IndexAccess and currentNode is not the base
27+
// then it must be the index in which case it is the end of the chain.
28+
if (currentNode !== parentNode.base) return true;
29+
break;
1530

31+
case 'FunctionCall':
32+
// If direct ParentNode is a FunctionCall and currentNode is not the expression
33+
// then it must be and argument in which case it is the end of the chain.
34+
if (currentNode !== parentNode.expression) return true;
35+
break;
36+
37+
default:
38+
break;
39+
}
40+
41+
i += 1;
42+
currentNode = parentNode;
43+
parentNode = path.getParentNode(i);
44+
}
1645
return true;
1746
};
1847

@@ -51,7 +80,7 @@ const MemberAccess = {
5180
node.memberName
5281
];
5382

54-
return isBeginnigOfChain(path) ? group(doc) : doc;
83+
return isEndOfChain(node, path) ? group(doc) : doc;
5584
}
5685
};
5786

tests/MemberAccess/MemberAccess.sol

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,44 @@ contract MemberAccess {
1313
veryLongVariable.veryLongCall(veryLongAttribute).veryLongCall(veryLongAttribute).veryLongCall(veryLongAttribute);
1414
}
1515
}
16+
17+
contract MemberAccessIsEndOfChainCases {
18+
// break if is an ReturnStatement
19+
uint a = b.c;
20+
21+
function() modifierCase(b.c) {
22+
// break if is an argument of a FunctionCall
23+
a(b.c);
24+
// break if is an index of an IndexAccess
25+
a[b.c];
26+
// break if is a part of a BinaryOperation
27+
a = b.c;
28+
// break if is a part of a UnaryOperation
29+
!b.c;
30+
// break if is a condition of a IfStatement
31+
if (b.c) {}
32+
// break if is a condition of a WhileStatement
33+
while (b.c) {}
34+
// break if is a part of a ForStatement
35+
// for (b.c;;) {}
36+
for (;b.c;) {}
37+
// for (;;b.c) {}
38+
// break if is a part of a VariableDeclarationStatement
39+
uint a = b.c;
40+
// break if is an ExpressionStatement
41+
b.c;
42+
// break if is an TupleExpression
43+
[a, b.c];
44+
(b.c);
45+
// break if is an Conditional
46+
a.b ? c : d;
47+
a ? b.c : d;
48+
a ? b : c.d;
49+
// break if is an NameValueList
50+
a.b{value: c.d}();
51+
// break if is an TryStatement
52+
try a.b() {} catch {}
53+
// break if is an ReturnStatement
54+
return b.c;
55+
}
56+
}

tests/MemberAccess/__snapshots__/jsfmt.spec.js.snap

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,46 @@ contract MemberAccess {
2222
}
2323
}
2424
25+
contract MemberAccessIsEndOfChainCases {
26+
// break if is an ReturnStatement
27+
uint a = b.c;
28+
29+
function() modifierCase(b.c) {
30+
// break if is an argument of a FunctionCall
31+
a(b.c);
32+
// break if is an index of an IndexAccess
33+
a[b.c];
34+
// break if is a part of a BinaryOperation
35+
a = b.c;
36+
// break if is a part of a UnaryOperation
37+
!b.c;
38+
// break if is a condition of a IfStatement
39+
if (b.c) {}
40+
// break if is a condition of a WhileStatement
41+
while (b.c) {}
42+
// break if is a part of a ForStatement
43+
// for (b.c;;) {}
44+
for (;b.c;) {}
45+
// for (;;b.c) {}
46+
// break if is a part of a VariableDeclarationStatement
47+
uint a = b.c;
48+
// break if is an ExpressionStatement
49+
b.c;
50+
// break if is an TupleExpression
51+
[a, b.c];
52+
(b.c);
53+
// break if is an Conditional
54+
a.b ? c : d;
55+
a ? b.c : d;
56+
a ? b : c.d;
57+
// break if is an NameValueList
58+
a.b{value: c.d}();
59+
// break if is an TryStatement
60+
try a.b() {} catch {}
61+
// break if is an ReturnStatement
62+
return b.c;
63+
}
64+
}
2565
=====================================output=====================================
2666
pragma solidity ^0.5.0;
2767
@@ -59,5 +99,46 @@ contract MemberAccess {
5999
}
60100
}
61101
102+
contract MemberAccessIsEndOfChainCases {
103+
// break if is an ReturnStatement
104+
uint256 a = b.c;
105+
106+
function() modifierCase(b.c) {
107+
// break if is an argument of a FunctionCall
108+
a(b.c);
109+
// break if is an index of an IndexAccess
110+
a[b.c];
111+
// break if is a part of a BinaryOperation
112+
a = b.c;
113+
// break if is a part of a UnaryOperation
114+
!b.c;
115+
// break if is a condition of a IfStatement
116+
if (b.c) {}
117+
// break if is a condition of a WhileStatement
118+
while (b.c) {}
119+
// break if is a part of a ForStatement
120+
// for (b.c;;) {}
121+
for (; b.c; ) {}
122+
// for (;;b.c) {}
123+
// break if is a part of a VariableDeclarationStatement
124+
uint256 a = b.c;
125+
// break if is an ExpressionStatement
126+
b.c;
127+
// break if is an TupleExpression
128+
[a, b.c];
129+
(b.c);
130+
// break if is an Conditional
131+
a.b ? c : d;
132+
a ? b.c : d;
133+
a ? b : c.d;
134+
// break if is an NameValueList
135+
a.b{value: c.d}();
136+
// break if is an TryStatement
137+
try a.b() {} catch {}
138+
// break if is an ReturnStatement
139+
return b.c;
140+
}
141+
}
142+
62143
================================================================================
63144
`;

0 commit comments

Comments
 (0)