Skip to content

Commit ee84287

Browse files
Janthermattiaerre
authored andcommitted
FunctionTypeName (#133)
1 parent 588bffc commit ee84287

File tree

3 files changed

+73
-26
lines changed

3 files changed

+73
-26
lines changed

src/nodes/FunctionTypeName.js

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,65 @@
1+
/* eslint-disable implicit-arrow-linebreak */
12
const {
23
doc: {
3-
builders: { concat, join }
4+
builders: { concat, group, indent, join, line, softline }
45
}
56
} = require('prettier');
67

7-
const FunctionTypeName = {
8-
print: ({ node, path, print }) => {
9-
const returns = returnTypes => {
10-
if (returnTypes.length > 0) {
11-
return concat([
12-
'returns (',
13-
join(', ', path.map(print, 'returnTypes')),
14-
')'
15-
]);
16-
}
17-
return null;
18-
};
19-
20-
return join(
21-
' ',
22-
[
8+
const parameterTypes = (node, path, print) =>
9+
group(
10+
concat([
11+
indent(
2312
concat([
24-
'function(',
25-
join(', ', path.map(print, 'parameterTypes')),
26-
')'
27-
]),
28-
returns(node.returnTypes),
29-
node.visibility === 'default' ? null : node.visibility,
30-
node.stateMutability
31-
].filter(element => element)
32-
);
13+
softline,
14+
join(concat([',', line]), path.map(print, 'parameterTypes'))
15+
])
16+
),
17+
softline
18+
])
19+
);
20+
21+
const returnTypes = (node, path, print) => {
22+
if (node.returnTypes.length > 0) {
23+
return concat([
24+
line,
25+
'returns (',
26+
join(', ', path.map(print, 'returnTypes')),
27+
')'
28+
]);
29+
}
30+
return '';
31+
};
32+
33+
const visibility = node => {
34+
if (node.visibility && node.visibility !== 'default') {
35+
return concat([line, node.visibility]);
3336
}
37+
return '';
38+
};
39+
40+
const stateMutability = node => {
41+
if (node.stateMutability && node.stateMutability !== 'default') {
42+
return concat([line, node.stateMutability]);
43+
}
44+
return '';
45+
};
46+
47+
const FunctionTypeName = {
48+
print: ({ node, path, print }) =>
49+
concat([
50+
'function(',
51+
parameterTypes(node, path, print),
52+
')',
53+
indent(
54+
group(
55+
concat([
56+
returnTypes(node, path, print),
57+
visibility(node),
58+
stateMutability(node)
59+
])
60+
)
61+
)
62+
])
3463
};
3564

3665
module.exports = FunctionTypeName;

tests/Etc/Etc.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ contract Contract {
1111
function (uint, uint) returns(bool)a;
1212
function(bytes32, bytes32) internal view[] b;
1313
function (bytes32, bytes32)internal[] c;
14+
function(bytes32, bytes32, bytes32, bytes32, bytes32, bytes32) internal view[] d;
15+
function(bytes32, bytes32, bytes32, bytes32, bytes32, bytes32, bytes32, bytes32, bytes32) internal view[] e;
1416
}
1517

1618
modifier modifierWithoutParams() {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ contract Contract {
1414
function (uint, uint) returns(bool)a;
1515
function(bytes32, bytes32) internal view[] b;
1616
function (bytes32, bytes32)internal[] c;
17+
function(bytes32, bytes32, bytes32, bytes32, bytes32, bytes32) internal view[] d;
18+
function(bytes32, bytes32, bytes32, bytes32, bytes32, bytes32, bytes32, bytes32, bytes32) internal view[] e;
1719
}
1820
1921
modifier modifierWithoutParams() {
@@ -57,6 +59,20 @@ contract Contract {
5759
function(uint, uint) returns (bool) a;
5860
function(bytes32, bytes32) internal view[] b;
5961
function(bytes32, bytes32) internal[] c;
62+
function(bytes32, bytes32, bytes32, bytes32, bytes32, bytes32)
63+
internal
64+
view[] d;
65+
function(
66+
bytes32,
67+
bytes32,
68+
bytes32,
69+
bytes32,
70+
bytes32,
71+
bytes32,
72+
bytes32,
73+
bytes32,
74+
bytes32
75+
) internal view[] e;
6076
}
6177
6278
modifier modifierWithoutParams() {

0 commit comments

Comments
 (0)