Skip to content

Commit 594c158

Browse files
authored
Function calls set group (#524)
* including the last ')' in the group so if the call reaches the charLimit with the ')' it will break * function calls now set up a groupId and expose it via labels * MemberAccess reacts accordingly to the function calls groupId
1 parent 562d00f commit 594c158

File tree

4 files changed

+80
-58
lines changed

4 files changed

+80
-58
lines changed

src/nodes/FunctionCall.js

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,59 @@
11
const {
22
doc: {
3-
builders: { group, line, softline }
3+
builders: { label, line, softline }
44
}
55
} = require('prettier/standalone');
66

77
const printSeparatedList = require('./print-separated-list');
88

9-
const printObject = (node, path, print, options) =>
10-
group([
11-
'{',
12-
printSeparatedList(
13-
path
14-
.map(print, 'arguments')
15-
.map((arg, index) => [node.names[index], ': ', arg]),
16-
{ firstSeparator: options.bracketSpacing ? line : softline }
17-
),
18-
'}'
19-
]);
9+
let functionCallId = 0;
2010

21-
const printArguments = (node, path, print, options) => {
22-
if (node.names && node.names.length > 0) {
23-
return printObject(node, path, print, options);
24-
}
25-
if (node.arguments && node.arguments.length > 0) {
26-
return printSeparatedList(path.map(print, 'arguments'));
27-
}
28-
return '';
29-
};
11+
const printObject = (node, path, print, options, groupId) => [
12+
'{',
13+
printSeparatedList(
14+
path
15+
.map(print, 'arguments')
16+
.map((arg, index) => [node.names[index], ': ', arg]),
17+
{
18+
groupId,
19+
firstSeparator: options.bracketSpacing ? line : softline,
20+
lastSeparator: [options.bracketSpacing ? line : softline, '})']
21+
}
22+
)
23+
];
24+
25+
const printArguments = (path, print, groupId) =>
26+
printSeparatedList(path.map(print, 'arguments'), {
27+
groupId,
28+
lastSeparator: [softline, ')']
29+
});
3030

3131
const FunctionCall = {
32-
print: ({ node, path, print, options }) => [
33-
path.call(print, 'expression'),
34-
'(',
35-
printArguments(node, path, print, options),
36-
')'
37-
]
32+
print: ({ node, path, print, options }) => {
33+
let argumentsDoc = ')';
34+
const functionCallLabel = { type: 'FunctionCall', groupId: null };
35+
if (node.arguments && node.arguments.length > 0) {
36+
functionCallLabel.groupId = `FunctionCall-${functionCallId}`;
37+
functionCallId += 1;
38+
if (node.names && node.names.length > 0) {
39+
argumentsDoc = printObject(
40+
node,
41+
path,
42+
print,
43+
options,
44+
functionCallLabel.groupId
45+
);
46+
} else {
47+
argumentsDoc = printArguments(path, print, functionCallLabel.groupId);
48+
}
49+
}
50+
51+
return label(JSON.stringify(functionCallLabel), [
52+
path.call(print, 'expression'),
53+
'(',
54+
argumentsDoc
55+
]);
56+
}
3857
};
3958

4059
module.exports = FunctionCall;

src/nodes/MemberAccess.js

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

@@ -33,11 +33,22 @@ const shallIndent = (path) => {
3333

3434
const MemberAccess = {
3535
print: ({ node, path, print }) => {
36+
const expressionDoc = path.call(print, 'expression');
37+
let separator = [softline, '.'];
38+
let labelData;
39+
if (expressionDoc.label) {
40+
labelData = JSON.parse(expressionDoc.label);
41+
}
42+
if (labelData && labelData.groupId) {
43+
separator = ifBreak('.', [softline, '.'], {
44+
groupId: labelData.groupId
45+
});
46+
}
47+
3648
const doc = [
37-
path.call(print, 'expression'),
38-
shallIndent(path)
39-
? indent([softline, '.', node.memberName])
40-
: [softline, '.', node.memberName]
49+
expressionDoc,
50+
shallIndent(path) ? indent(separator) : separator,
51+
node.memberName
4152
];
4253

4354
return isBeginnigOfChain(path) ? group(doc) : doc;

src/nodes/print-separated-list.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ const {
1414
const printSeparatedList = (
1515
list,
1616
{
17+
groupId = undefined,
1718
firstSeparator = softline,
1819
separator = [',', line],
1920
lastSeparator = firstSeparator
2021
} = {}
21-
) => group([indent([firstSeparator, join(separator, list)]), lastSeparator]);
22-
22+
) =>
23+
group(
24+
[indent([firstSeparator, join(separator, list)]), lastSeparator],
25+
groupId ? { id: groupId } : {}
26+
);
2327
module.exports = printSeparatedList;

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

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ contract FunctionCalls {
8181
keccak256(req.data)
8282
)
8383
)
84-
)
85-
._hashTypedDataV2(
84+
)._hashTypedDataV2(
8685
keccak256(
8786
abi.encode(
8887
TYPEHASH,
@@ -94,8 +93,7 @@ contract FunctionCalls {
9493
keccak256(req.data)
9594
)
9695
)
97-
)
98-
._hashTypedDataV3(
96+
)._hashTypedDataV3(
9997
keccak256(
10098
abi.encode(
10199
TYPEHASH,
@@ -107,8 +105,7 @@ contract FunctionCalls {
107105
keccak256(req.data)
108106
)
109107
)
110-
)
111-
.recover(signature);
108+
).recover(signature);
112109
signer = _hashTypedDataV1(
113110
keccak256(
114111
abi.encode(
@@ -121,8 +118,7 @@ contract FunctionCalls {
121118
keccak256(req.data)
122119
)
123120
)
124-
)
125-
._hashTypedDataV2(
121+
)._hashTypedDataV2(
126122
keccak256(
127123
abi.encode(
128124
TYPEHASH,
@@ -134,8 +130,7 @@ contract FunctionCalls {
134130
keccak256(req.data)
135131
)
136132
)
137-
)
138-
._hashTypedDataV3(
133+
)._hashTypedDataV3(
139134
keccak256(
140135
abi.encode(
141136
TYPEHASH,
@@ -147,8 +142,7 @@ contract FunctionCalls {
147142
keccak256(req.data)
148143
)
149144
)
150-
)
151-
.recover(signature);
145+
).recover(signature);
152146
return _nonces[req.from] == req.nonce && signer == req.from;
153147
}
154148
}
@@ -236,8 +230,7 @@ contract FunctionCalls {
236230
keccak256(req.data)
237231
)
238232
)
239-
)
240-
._hashTypedDataV2(
233+
)._hashTypedDataV2(
241234
keccak256(
242235
abi.encode(
243236
TYPEHASH,
@@ -249,8 +242,7 @@ contract FunctionCalls {
249242
keccak256(req.data)
250243
)
251244
)
252-
)
253-
._hashTypedDataV3(
245+
)._hashTypedDataV3(
254246
keccak256(
255247
abi.encode(
256248
TYPEHASH,
@@ -262,8 +254,7 @@ contract FunctionCalls {
262254
keccak256(req.data)
263255
)
264256
)
265-
)
266-
.recover(signature);
257+
).recover(signature);
267258
signer = _hashTypedDataV1(
268259
keccak256(
269260
abi.encode(
@@ -276,8 +267,7 @@ contract FunctionCalls {
276267
keccak256(req.data)
277268
)
278269
)
279-
)
280-
._hashTypedDataV2(
270+
)._hashTypedDataV2(
281271
keccak256(
282272
abi.encode(
283273
TYPEHASH,
@@ -289,8 +279,7 @@ contract FunctionCalls {
289279
keccak256(req.data)
290280
)
291281
)
292-
)
293-
._hashTypedDataV3(
282+
)._hashTypedDataV3(
294283
keccak256(
295284
abi.encode(
296285
TYPEHASH,
@@ -302,8 +291,7 @@ contract FunctionCalls {
302291
keccak256(req.data)
303292
)
304293
)
305-
)
306-
.recover(signature);
294+
).recover(signature);
307295
return _nonces[req.from] == req.nonce && signer == req.from;
308296
}
309297
}

0 commit comments

Comments
 (0)