Skip to content

Commit ec6e9ea

Browse files
Franco Victoriomattiaerre
authored andcommitted
Group and break modifiers (#102)
1 parent eca6cbf commit ec6e9ea

10 files changed

Lines changed: 360 additions & 112 deletions

File tree

src/printer.js

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,41 +99,69 @@ function genericPrint(path, options, print) {
9999
]);
100100
}
101101
return concat(['using ', node.libraryName, ' for *;']);
102-
case 'FunctionDefinition':
102+
case 'FunctionDefinition': {
103+
let parts = [];
104+
103105
if (node.isConstructor) {
104106
if (node.name) {
105-
doc = `function ${node.name}`;
107+
parts.push(`function ${node.name}`);
106108
} else {
107-
doc = 'constructor';
109+
parts.push('constructor');
108110
}
109111
} else if (node.name === '') {
110-
doc = 'function';
112+
parts.push('function');
111113
} else {
112-
doc = concat(['function ', node.name]);
114+
parts = parts.concat(['function ', node.name]);
113115
}
114116

115-
doc = concat([doc, '(', path.call(print, 'parameters'), ')']);
117+
parts = parts.concat(['(', path.call(print, 'parameters'), ')']);
118+
119+
let modifiers = [];
116120
if (node.visibility && node.visibility !== 'default') {
117-
doc = join(' ', [doc, node.visibility]);
121+
modifiers.push(node.visibility);
118122
}
119123
// @TODO: check stateMutability null vs default
120124
if (node.stateMutability && node.stateMutability !== 'default') {
121-
doc = join(' ', [doc, node.stateMutability]);
125+
modifiers.push(node.stateMutability);
122126
}
123127
if (node.modifiers.length > 0) {
124-
doc = join(' ', [doc, join(' ', path.map(print, 'modifiers'))]);
128+
modifiers = modifiers.concat(path.map(print, 'modifiers'));
125129
}
126130
if (node.returnParameters) {
127-
doc = join(' ', [
128-
doc,
131+
modifiers.push(
129132
concat(['returns (', path.call(print, 'returnParameters'), ')'])
130-
]);
133+
);
134+
}
135+
136+
if (modifiers.length > 0) {
137+
parts.push(
138+
group(
139+
concat(
140+
[
141+
indent(line),
142+
join(indent(line), modifiers),
143+
node.body ? line : null
144+
].filter(x => x)
145+
)
146+
)
147+
);
148+
} else if (node.body) {
149+
parts.push(' ');
131150
}
151+
132152
if (node.body) {
133-
return concat([join(' ', [doc, path.call(print, 'body')])]);
153+
parts.push(path.call(print, 'body'));
154+
} else {
155+
parts.push(';');
134156
}
135-
return concat([doc, ';']);
157+
158+
return concat(parts);
159+
}
136160
case 'ParameterList':
161+
// don't insert softlines when there are no parameters
162+
if (node.parameters.length === 0) {
163+
return '';
164+
}
137165
return group(
138166
concat([
139167
indent(

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -566,9 +566,9 @@ library IntegerSet {
566566
function iterate_valid(data storage self, uint index) returns (bool) {
567567
return index < self.items.length;
568568
}
569-
function iterate_advance(data storage self, uint index) returns (
570-
uint r_index
571-
) {
569+
function iterate_advance(data storage self, uint index)
570+
returns (uint r_index)
571+
{
572572
index++;
573573
while (iterate_valid(
574574
self,
@@ -827,16 +827,15 @@ contract Ballot {
827827
return false;
828828
}
829829
830-
function foobar() payable owner(myPrice) returns (
831-
uint[],
832-
address myAdd,
833-
string[] names
834-
) {}
835-
function foobar() payable owner(myPrice) returns (
836-
uint[],
837-
address myAdd,
838-
string[] names
839-
);
830+
function foobar()
831+
payable
832+
owner(myPrice)
833+
returns (uint[], address myAdd, string[] names)
834+
{}
835+
function foobar()
836+
payable
837+
owner(myPrice)
838+
returns (uint[], address myAdd, string[] names);
840839
841840
Voter you = Voter(1, true);
842841

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ contract BasicIterator {
6363
}
6464
}
6565
66-
function getSum() constant returns (uint) { // "constant" just means this function returns something to the caller
66+
function getSum()
67+
constant
68+
returns (uint) // "constant" just means this function returns something to the caller
69+
{
6770
// which is immediately followed by what type gets returned, in this case a full uint256
6871
uint8 sum = 0;
6972
uint8 x = 0;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
contract FunctionDefinitions {
2+
function noParamsNoModifiers() {
3+
a = 1;
4+
}
5+
6+
function oneParam(uint x) {
7+
a = 1;
8+
}
9+
10+
function oneModifier() modifier1 {
11+
a = 1;
12+
}
13+
14+
function manyParams(uint x1, uint x2, uint x3, uint x4, uint x5, uint x6, uint x7, uint x8, uint x9, uint x10) {
15+
a = 1;
16+
}
17+
18+
function manyModifiers() modifier1 modifier2 modifier3 modifier4 modifier5 modifier6 modifier7 modifier8 modifier9 modifier10 {
19+
a = 1;
20+
}
21+
22+
function someParamsSomeModifiers(uint x1, uint x2, uint x3) modifier1 modifier2 modifier3 {
23+
a = 1;
24+
}
25+
26+
function someParamsManyModifiers(uint x1, uint x2, uint x3) modifier1 modifier2 modifier3 modifier4 modifier5 modifier6 modifier7 modifier8 modifier9 modifier10 {
27+
a = 1;
28+
}
29+
30+
function manyParamsSomeModifiers(uint x1, uint x2, uint x3, uint x4, uint x5, uint x6, uint x7, uint x8, uint x9, uint x10) modifier1 modifier2 modifier3 {
31+
a = 1;
32+
}
33+
34+
function manyParamsManyModifiers(uint x1, uint x2, uint x3, uint x4, uint x5, uint x6, uint x7, uint x8, uint x9, uint x10) modifier1 modifier2 modifier3 modifier4 modifier5 modifier6 modifier7 modifier8 modifier9 modifier10 {
35+
a = 1;
36+
}
37+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`FunctionDefinitions.sol 1`] = `
4+
contract FunctionDefinitions {
5+
function noParamsNoModifiers() {
6+
a = 1;
7+
}
8+
9+
function oneParam(uint x) {
10+
a = 1;
11+
}
12+
13+
function oneModifier() modifier1 {
14+
a = 1;
15+
}
16+
17+
function manyParams(uint x1, uint x2, uint x3, uint x4, uint x5, uint x6, uint x7, uint x8, uint x9, uint x10) {
18+
a = 1;
19+
}
20+
21+
function manyModifiers() modifier1 modifier2 modifier3 modifier4 modifier5 modifier6 modifier7 modifier8 modifier9 modifier10 {
22+
a = 1;
23+
}
24+
25+
function someParamsSomeModifiers(uint x1, uint x2, uint x3) modifier1 modifier2 modifier3 {
26+
a = 1;
27+
}
28+
29+
function someParamsManyModifiers(uint x1, uint x2, uint x3) modifier1 modifier2 modifier3 modifier4 modifier5 modifier6 modifier7 modifier8 modifier9 modifier10 {
30+
a = 1;
31+
}
32+
33+
function manyParamsSomeModifiers(uint x1, uint x2, uint x3, uint x4, uint x5, uint x6, uint x7, uint x8, uint x9, uint x10) modifier1 modifier2 modifier3 {
34+
a = 1;
35+
}
36+
37+
function manyParamsManyModifiers(uint x1, uint x2, uint x3, uint x4, uint x5, uint x6, uint x7, uint x8, uint x9, uint x10) modifier1 modifier2 modifier3 modifier4 modifier5 modifier6 modifier7 modifier8 modifier9 modifier10 {
38+
a = 1;
39+
}
40+
}
41+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42+
contract FunctionDefinitions {
43+
function noParamsNoModifiers() {
44+
a = 1;
45+
}
46+
47+
function oneParam(uint x) {
48+
a = 1;
49+
}
50+
51+
function oneModifier() modifier1 {
52+
a = 1;
53+
}
54+
55+
function manyParams(
56+
uint x1,
57+
uint x2,
58+
uint x3,
59+
uint x4,
60+
uint x5,
61+
uint x6,
62+
uint x7,
63+
uint x8,
64+
uint x9,
65+
uint x10
66+
) {
67+
a = 1;
68+
}
69+
70+
function manyModifiers()
71+
modifier1
72+
modifier2
73+
modifier3
74+
modifier4
75+
modifier5
76+
modifier6
77+
modifier7
78+
modifier8
79+
modifier9
80+
modifier10
81+
{
82+
a = 1;
83+
}
84+
85+
function someParamsSomeModifiers(uint x1, uint x2, uint x3)
86+
modifier1
87+
modifier2
88+
modifier3
89+
{
90+
a = 1;
91+
}
92+
93+
function someParamsManyModifiers(uint x1, uint x2, uint x3)
94+
modifier1
95+
modifier2
96+
modifier3
97+
modifier4
98+
modifier5
99+
modifier6
100+
modifier7
101+
modifier8
102+
modifier9
103+
modifier10
104+
{
105+
a = 1;
106+
}
107+
108+
function manyParamsSomeModifiers(
109+
uint x1,
110+
uint x2,
111+
uint x3,
112+
uint x4,
113+
uint x5,
114+
uint x6,
115+
uint x7,
116+
uint x8,
117+
uint x9,
118+
uint x10
119+
) modifier1 modifier2 modifier3 {
120+
a = 1;
121+
}
122+
123+
function manyParamsManyModifiers(
124+
uint x1,
125+
uint x2,
126+
uint x3,
127+
uint x4,
128+
uint x5,
129+
uint x6,
130+
uint x7,
131+
uint x8,
132+
uint x9,
133+
uint x10
134+
)
135+
modifier1
136+
modifier2
137+
modifier3
138+
modifier4
139+
modifier5
140+
modifier6
141+
modifier7
142+
modifier8
143+
modifier9
144+
modifier10
145+
{
146+
a = 1;
147+
}
148+
}
149+
150+
`;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
run_spec(__dirname);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ contract IndexOf {
7878
7979
int whatwastheval = -10; // -2 = not yet tested, as separate from -1, tested but error
8080
81-
function indexOf(string _a, string _b) returns (int) { // _a = string to search, _b = string we want to find
81+
function indexOf(string _a, string _b)
82+
returns (int) // _a = string to search, _b = string we want to find
83+
{
8284
bytes memory a = bytes(_a);
8385
bytes memory b = bytes(_b);
8486

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ contract SampleCrowdsale is CappedCrowdsale, RefundableCrowdsale {
8888
uint256 _goal,
8989
uint256 _cap,
9090
address _wallet
91-
) public CappedCrowdsale(_cap) FinalizableCrowdsale RefundableCrowdsale(_goal) Crowdsale(_startTime, _endTime, _rate, _wallet) {
91+
)
92+
public
93+
CappedCrowdsale(_cap)
94+
FinalizableCrowdsale
95+
RefundableCrowdsale(_goal)
96+
Crowdsale(_startTime, _endTime, _rate, _wallet)
97+
{
9298
//As goal needs to be met for a successful crowdsale
9399
//the value needs to less or equal than a cap which is limit for accepted funds
94100
require(_goal <= _cap);

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,10 @@ contract SplittableCommodity is MintableCommodity {
7676
bytes operatorData
7777
);
7878
79-
function split(
80-
uint _tokenId,
81-
address _to,
82-
uint256 _amount
83-
) public whenNotPaused {
79+
function split(uint _tokenId, address _to, uint256 _amount)
80+
public
81+
whenNotPaused
82+
{
8483
address supplierProxy = IContractRegistry(
8584
contractRegistry
8685
).getLatestProxyAddr("Supplier");

0 commit comments

Comments
 (0)