Skip to content

Commit aa41b10

Browse files
Treat function calls similar to member access when indenting (#562)
1 parent 07351ab commit aa41b10

File tree

5 files changed

+189
-9
lines changed

5 files changed

+189
-9
lines changed

src/nodes/FunctionCall.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const {
22
doc: {
3-
builders: { line, softline }
3+
builders: { group, ifBreak, indent, label, line, softline }
44
}
55
} = require('prettier');
66

@@ -24,9 +24,12 @@ const printArguments = (path, print) =>
2424
lastSeparator: [softline, ')']
2525
});
2626

27+
let groupIndex = 0;
2728
const FunctionCall = {
2829
print: ({ node, path, print, options }) => {
30+
let expressionDoc = path.call(print, 'expression');
2931
let argumentsDoc = ')';
32+
3033
if (node.arguments && node.arguments.length > 0) {
3134
if (node.names && node.names.length > 0) {
3235
argumentsDoc = printObject(node, path, print, options);
@@ -35,7 +38,24 @@ const FunctionCall = {
3538
}
3639
}
3740

38-
return [path.call(print, 'expression'), '(', argumentsDoc];
41+
// If we are at the end of a MemberAccessChain we should indent the
42+
// arguments accordingly.
43+
if (expressionDoc.label === 'MemberAccessChain') {
44+
expressionDoc = group(expressionDoc.contents, {
45+
id: `FunctionCall.expression-${groupIndex}`
46+
});
47+
48+
groupIndex += 1;
49+
50+
argumentsDoc = ifBreak(indent(argumentsDoc), argumentsDoc, {
51+
groupId: expressionDoc.id
52+
});
53+
// We wrap the expression in a label in case there is an IndexAccess or
54+
// a FunctionCall following this IndexAccess.
55+
return label('MemberAccessChain', [expressionDoc, '(', argumentsDoc]);
56+
}
57+
58+
return [expressionDoc, '(', argumentsDoc];
3959
}
4060
};
4161

src/nodes/IndexAccess.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
11
const {
22
doc: {
3-
builders: { group, indent, softline }
3+
builders: { group, ifBreak, indent, label, softline }
44
}
55
} = require('prettier');
66

7+
let groupIndex = 0;
78
const IndexAccess = {
8-
print: ({ path, print }) => [
9-
path.call(print, 'base'),
10-
'[',
11-
group([indent([softline, path.call(print, 'index')]), softline, ']'])
12-
]
9+
print: ({ path, print }) => {
10+
let baseDoc = path.call(print, 'base');
11+
let indexDoc = group([
12+
indent([softline, path.call(print, 'index')]),
13+
softline,
14+
']'
15+
]);
16+
17+
// If we are at the end of a MemberAccessChain we should indent the
18+
// arguments accordingly.
19+
if (baseDoc.label === 'MemberAccessChain') {
20+
baseDoc = group(baseDoc.contents, {
21+
id: `IndexAccess.base-${groupIndex}`
22+
});
23+
24+
groupIndex += 1;
25+
26+
indexDoc = ifBreak(indent(indexDoc), indexDoc, {
27+
groupId: baseDoc.id
28+
});
29+
// We wrap the expression in a label in case there is an IndexAccess or
30+
// a FunctionCall following this IndexAccess.
31+
return label('MemberAccessChain', [baseDoc, '[', indexDoc]);
32+
}
33+
34+
return [baseDoc, '[', indexDoc];
35+
}
1336
};
1437

1538
module.exports = IndexAccess;

src/nodes/MemberAccess.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ const processChain = (chain) => {
102102
// The doc[] containing the rest of the chain
103103
const restOfChain = group(indent(chain.slice(firstSeparatorIndex)));
104104

105-
return group([firstExpression, restOfChain]);
105+
// We wrap the expression in a label in case there is an IndexAccess or
106+
// a FunctionCall following this MemberAccess.
107+
return label('MemberAccessChain', group([firstExpression, restOfChain]));
106108
};
107109

108110
const MemberAccess = {

tests/format/MemberAccess/MemberAccess.sol

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,50 @@ contract MemberAccess {
1313
veryLongVariable.veryLongMember.veryLongCall(veryLongAttribute).veryLongMember.veryLongMember;
1414
veryLongVariable.veryLongMember.veryLongMember.veryLongMember.veryLongCall(veryLongAttribute);
1515
veryLongVariable.veryLongCall(veryLongAttribute).veryLongCall(veryLongAttribute).veryLongCall(veryLongAttribute);
16+
uint256[] memory amounts = IUniswapV2Router(routerAddress)
17+
.swapExactTokensForTokens(
18+
sourceAmount,
19+
minDestination,
20+
path,
21+
address(this, aoeu, aoeueu, aoeu)
22+
)[IUniswapV2Router(routerAddress)
23+
.swapExactTokensForTokens(
24+
sourceAmount,
25+
minDestination,
26+
path,
27+
address(this, aoeu, aoeueu, aoeu)
28+
)];
29+
uint256[] memory amounts = IUniswapV2Router(routerAddress)
30+
.swapExactTokensForTokens(
31+
sourceAmount,
32+
minDestination,
33+
path,
34+
address(this, aoeu, aoeueu, aoeu)
35+
)[IUniswapV2Router(routerAddress)
36+
.swapExactTokensForTokens(
37+
sourceAmount,
38+
minDestination,
39+
path,
40+
address(this, aoeu, aoeueu, aoeu)
41+
)][IUniswapV2Router(routerAddress)
42+
.swapExactTokensForTokens(
43+
sourceAmount,
44+
minDestination,
45+
path,
46+
address(this, aoeu, aoeueu, aoeu)
47+
)];
48+
uint256[] memory amounts = IUniswapV2Router(routerAddress)
49+
.swapExactTokensForTokens(
50+
sourceAmount,
51+
minDestination,
52+
path,
53+
address(this, aoeu, aoeueu, aoeu)
54+
)(
55+
sourceAmount,
56+
minDestination,
57+
path,
58+
address(this, aoeu, aoeueu, aoeu)
59+
);
1660
}
1761
}
1862

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,50 @@ contract MemberAccess {
2121
veryLongVariable.veryLongMember.veryLongCall(veryLongAttribute).veryLongMember.veryLongMember;
2222
veryLongVariable.veryLongMember.veryLongMember.veryLongMember.veryLongCall(veryLongAttribute);
2323
veryLongVariable.veryLongCall(veryLongAttribute).veryLongCall(veryLongAttribute).veryLongCall(veryLongAttribute);
24+
uint256[] memory amounts = IUniswapV2Router(routerAddress)
25+
.swapExactTokensForTokens(
26+
sourceAmount,
27+
minDestination,
28+
path,
29+
address(this, aoeu, aoeueu, aoeu)
30+
)[IUniswapV2Router(routerAddress)
31+
.swapExactTokensForTokens(
32+
sourceAmount,
33+
minDestination,
34+
path,
35+
address(this, aoeu, aoeueu, aoeu)
36+
)];
37+
uint256[] memory amounts = IUniswapV2Router(routerAddress)
38+
.swapExactTokensForTokens(
39+
sourceAmount,
40+
minDestination,
41+
path,
42+
address(this, aoeu, aoeueu, aoeu)
43+
)[IUniswapV2Router(routerAddress)
44+
.swapExactTokensForTokens(
45+
sourceAmount,
46+
minDestination,
47+
path,
48+
address(this, aoeu, aoeueu, aoeu)
49+
)][IUniswapV2Router(routerAddress)
50+
.swapExactTokensForTokens(
51+
sourceAmount,
52+
minDestination,
53+
path,
54+
address(this, aoeu, aoeueu, aoeu)
55+
)];
56+
uint256[] memory amounts = IUniswapV2Router(routerAddress)
57+
.swapExactTokensForTokens(
58+
sourceAmount,
59+
minDestination,
60+
path,
61+
address(this, aoeu, aoeueu, aoeu)
62+
)(
63+
sourceAmount,
64+
minDestination,
65+
path,
66+
address(this, aoeu, aoeueu, aoeu)
67+
);
2468
}
2569
}
2670
@@ -104,6 +148,53 @@ contract MemberAccess {
104148
.veryLongCall(veryLongAttribute)
105149
.veryLongCall(veryLongAttribute)
106150
.veryLongCall(veryLongAttribute);
151+
uint256[] memory amounts = IUniswapV2Router(routerAddress)
152+
.swapExactTokensForTokens(
153+
sourceAmount,
154+
minDestination,
155+
path,
156+
address(this, aoeu, aoeueu, aoeu)
157+
)[
158+
IUniswapV2Router(routerAddress).swapExactTokensForTokens(
159+
sourceAmount,
160+
minDestination,
161+
path,
162+
address(this, aoeu, aoeueu, aoeu)
163+
)
164+
];
165+
uint256[] memory amounts = IUniswapV2Router(routerAddress)
166+
.swapExactTokensForTokens(
167+
sourceAmount,
168+
minDestination,
169+
path,
170+
address(this, aoeu, aoeueu, aoeu)
171+
)[
172+
IUniswapV2Router(routerAddress).swapExactTokensForTokens(
173+
sourceAmount,
174+
minDestination,
175+
path,
176+
address(this, aoeu, aoeueu, aoeu)
177+
)
178+
][
179+
IUniswapV2Router(routerAddress).swapExactTokensForTokens(
180+
sourceAmount,
181+
minDestination,
182+
path,
183+
address(this, aoeu, aoeueu, aoeu)
184+
)
185+
];
186+
uint256[] memory amounts = IUniswapV2Router(routerAddress)
187+
.swapExactTokensForTokens(
188+
sourceAmount,
189+
minDestination,
190+
path,
191+
address(this, aoeu, aoeueu, aoeu)
192+
)(
193+
sourceAmount,
194+
minDestination,
195+
path,
196+
address(this, aoeu, aoeueu, aoeu)
197+
);
107198
}
108199
}
109200

0 commit comments

Comments
 (0)