Skip to content

Commit bc6b7bd

Browse files
Franco Victoriomattiaerre
authored andcommitted
Preserve empty lines (#44)
* Preserve empty lines at the source and contract level * Remove wrong empty lines from snapshots * Better handling of empty lines * Fix linter errors * Preserve empty lines in code blocks * Use destructuring to extract helpers from prettier * Use length instead of counter * Run prettier
1 parent e080450 commit bc6b7bd

File tree

5 files changed

+55
-27
lines changed

5 files changed

+55
-27
lines changed

src/printer.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
1+
const prettier = require('prettier');
2+
13
const {
2-
concat,
3-
group,
4-
hardline,
5-
indent,
6-
join,
7-
line,
8-
softline
9-
} = require('prettier').doc.builders;
4+
doc: {
5+
builders: { concat, group, hardline, indent, join, line, softline }
6+
},
7+
util: { isNextLineEmptyAfterIndex }
8+
} = prettier;
9+
10+
function printPreservingEmptyLines(path, key, options, print) {
11+
const parts = [];
12+
path.each(childPath => {
13+
if (parts.length !== 0) {
14+
parts.push(hardline);
15+
}
16+
17+
parts.push(print(childPath));
18+
if (
19+
isNextLineEmptyAfterIndex(
20+
options.originalText,
21+
options.locEnd(childPath.getValue()) + 1
22+
)
23+
) {
24+
parts.push(hardline);
25+
}
26+
}, key);
27+
28+
return concat(parts);
29+
}
1030

1131
function genericPrint(path, options, print) {
1232
const node = path.getValue();
1333
let doc;
1434
switch (node.type) {
1535
case 'SourceUnit':
16-
return join(hardline, path.map(print, 'children'));
36+
return printPreservingEmptyLines(path, 'children', options, print);
1737
case 'PragmaDirective':
1838
return concat(['pragma ', node.name, ' ', node.value, ';']);
1939
case 'ImportDirective':
@@ -44,11 +64,10 @@ function genericPrint(path, options, print) {
4464
]);
4565
}
4666
return concat([
47-
line,
4867
doc,
4968
' {',
5069
indent(line),
51-
indent(join(hardline, path.map(print, 'subNodes'))),
70+
indent(printPreservingEmptyLines(path, 'subNodes', options, print)),
5271
line,
5372
'}',
5473
line
@@ -97,7 +116,7 @@ function genericPrint(path, options, print) {
97116
]);
98117
}
99118
if (node.body) {
100-
return concat([line, join(' ', [doc, path.call(print, 'body')])]);
119+
return concat([join(' ', [doc, path.call(print, 'body')])]);
101120
}
102121
return concat([doc, ';']);
103122
case 'ParameterList':
@@ -128,7 +147,7 @@ function genericPrint(path, options, print) {
128147
return concat([
129148
'{',
130149
indent(line),
131-
indent(join(line, path.map(print, 'statements'))),
150+
indent(printPreservingEmptyLines(path, 'statements', options, print)),
132151
line,
133152
'}'
134153
]);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ contract BasicIterator {
5050
This is a very simple demonstration of a while loops. Same as JS/c.
5151
*/
5252
53-
5453
contract BasicIterator {
5554
address creator; // reserve one "address"-type spot
5655
uint8[10] integers; // reserve a chunk of storage for 10 8-bit unsigned integers in an array
@@ -74,11 +73,11 @@ contract BasicIterator {
7473
}
7574
return sum;
7675
}
76+
7777
/**********
7878
Standard kill() function to recover funds
7979
**********/
8080
81-
8281
function kill() {
8382
if (msg.sender == creator) {
8483
suicide(creator); // kills this contract and sends remaining funds back to creator

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ lines.
3636
*/
3737
3838
pragma solidity ^0.4.23;
39-
4039
contract Inbox {
4140
string public message;
4241

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,72 +78,74 @@ contract Ownable {
7878
}
7979
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8080
pragma solidity ^0.4.24;
81+
8182
/**
8283
* @title Ownable
8384
* @dev The Ownable contract has an owner address, and provides basic authorization control
8485
* functions, this simplifies the implementation of "user permissions".
8586
*/
86-
8787
contract Ownable {
8888
address private _owner;
89+
8990
event OwnershipRenounced(address indexed previousOwner);
9091
event OwnershipTransferred(
9192
address indexed previousOwner,
9293
address indexed newOwner
9394
);
95+
9496
/**
9597
* @dev The Ownable constructor sets the original \`owner\` of the contract to the sender
9698
* account.
9799
*/
98-
99100
constructor() public {
100101
_owner = msg.sender;
101102
}
103+
102104
/**
103105
* @return the address of the owner.
104106
*/
105-
106107
function owner() public view returns(address) {
107108
return _owner;
108109
}
110+
109111
/**
110112
* @dev Throws if called by any account other than the owner.
111113
*/
112114
modifier onlyOwner() {
113115
require(isOwner());
114116
_;
115117
}
118+
116119
/**
117120
* @return true if \`msg.sender\` is the owner of the contract.
118121
*/
119-
120122
function isOwner() public view returns(bool) {
121123
return msg.sender == _owner;
122124
}
125+
123126
/**
124127
* @dev Allows the current owner to relinquish control of the contract.
125128
* @notice Renouncing to ownership will leave the contract without an owner.
126129
* It will not be possible to call the functions with the \`onlyOwner\`
127130
* modifier anymore.
128131
*/
129-
130132
function renounceOwnership() public onlyOwner {
131133
emit OwnershipRenounced(_owner);
132134
_owner = address(0);
133135
}
136+
134137
/**
135138
* @dev Allows the current owner to transfer control of the contract to a newOwner.
136139
* @param newOwner The address to transfer ownership to.
137140
*/
138-
139141
function transferOwnership(address newOwner) public onlyOwner {
140142
_transferOwnership(newOwner);
141143
}
144+
142145
/**
143146
* @dev Transfers control of the contract to a newOwner.
144147
* @param newOwner The address to transfer ownership to.
145148
*/
146-
147149
function _transferOwnership(address newOwner) internal {
148150
require(newOwner != address(0));
149151
emit OwnershipTransferred(_owner, newOwner);

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,21 @@ contract SimpleAuction {
133133
// or time periods in seconds.
134134
address public beneficiary;
135135
uint public auctionEnd;
136+
136137
// Current state of the auction.
137138
address public highestBidder;
138139
uint public highestBid;
140+
139141
// Allowed withdrawals of previous bids
140142
mapping(address => uint) pendingReturns;
143+
141144
// Set to true at the end, disallows any change
142145
bool ended;
146+
143147
// Events that will be fired on changes.
144148
event HighestBidIncreased(address bidder, uint amount);
145149
event AuctionEnded(address winner, uint amount);
150+
146151
// The following is a so-called natspec comment,
147152
// recognizable by the three slashes.
148153
// It will be shown when the user is asked to
@@ -151,16 +156,15 @@ contract SimpleAuction {
151156
/// Create a simple auction with \`_biddingTime\`
152157
/// seconds bidding time on behalf of the
153158
/// beneficiary address \`_beneficiary\`.
154-
155159
constructor(uint _biddingTime, address _beneficiary) public {
156160
beneficiary = _beneficiary;
157161
auctionEnd = now + _biddingTime;
158162
}
163+
159164
/// Bid on the auction with the value sent
160165
/// together with this transaction.
161166
/// The value will only be refunded if the
162167
/// auction is not won.
163-
164168
function bid() public payable {
165169
// No arguments are necessary, all
166170
// information is already part of
@@ -171,9 +175,11 @@ contract SimpleAuction {
171175
// Revert the call if the bidding
172176
// period is over.
173177
require(now <= auctionEnd, "Auction already ended.");
178+
174179
// If the bid is not higher, send the
175180
// money back.
176181
require(msg.value > highestBid, "There already is a higher bid.");
182+
177183
if (highestBid != 0) {
178184
// Sending back the money by simply using
179185
// highestBidder.send(highestBid) is a security risk
@@ -186,15 +192,16 @@ contract SimpleAuction {
186192
highestBid = msg.value;
187193
emit HighestBidIncreased(msg.sender, msg.value);
188194
}
189-
/// Withdraw a bid that was overbid.
190195
196+
/// Withdraw a bid that was overbid.
191197
function withdraw() public returns(bool) {
192198
uint amount = pendingReturns[msg.sender];
193199
if (amount > 0) {
194200
// It is important to set this to zero because the recipient
195201
// can call this function again as part of the receiving call
196202
// before \`send\` returns.
197203
pendingReturns[msg.sender] = 0;
204+
198205
if (!msg.sender.send(amount)) {
199206
// No need to call throw here, just reset the amount owing
200207
pendingReturns[msg.sender] = amount;
@@ -203,9 +210,9 @@ contract SimpleAuction {
203210
}
204211
return true;
205212
}
213+
206214
/// End the auction and send the highest bid
207215
/// to the beneficiary.
208-
209216
function auctionEnd() public {
210217
// It is a good guideline to structure functions that interact
211218
// with other contracts (i.e. they call functions or send Ether)
@@ -223,9 +230,11 @@ contract SimpleAuction {
223230
// 1. Conditions
224231
require(now >= auctionEnd, "Auction not yet ended.");
225232
require(!ended, "auctionEnd has already been called.");
233+
226234
// 2. Effects
227235
ended = true;
228236
emit AuctionEnded(highestBidder, highestBid);
237+
229238
// 3. Interaction
230239
beneficiary.transfer(highestBid);
231240
}

0 commit comments

Comments
 (0)