Skip to content

Commit 78f5dc1

Browse files
authored
space-after-if-condition (#39)
* add line before ContractDefinition and space after condition for IfStatement * add BasicIterator to test `while` * s/function BasicIterator/constructor * fix constructor
1 parent 39cc037 commit 78f5dc1

9 files changed

Lines changed: 437 additions & 3 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "prettier-plugin-solidity",
3-
"version": "1.0.0-alpha.2",
3+
"version": "1.0.0-alpha.3",
44
"description": "prettier plugin for solidity",
55
"main": "src",
66
"scripts": {

src/printer.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function genericPrint(path, options, print) {
4444
]);
4545
}
4646
return concat([
47+
line,
4748
doc,
4849
' {',
4950
indent(line),
@@ -67,7 +68,11 @@ function genericPrint(path, options, print) {
6768
return concat(['using ', node.libraryName, ' for *;']);
6869
case 'FunctionDefinition':
6970
if (node.isConstructor) {
70-
doc = 'constructor';
71+
if (node.name) {
72+
doc = `function ${node.name}`;
73+
} else {
74+
doc = 'constructor';
75+
}
7176
} else if (node.name === '') {
7277
doc = 'function';
7378
} else {
@@ -281,7 +286,7 @@ function genericPrint(path, options, print) {
281286
doc = concat([
282287
'if (',
283288
path.call(print, 'condition'),
284-
')',
289+
') ',
285290
path.call(print, 'trueBody')
286291
]);
287292
if (node.falseBody) {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
This is a very simple demonstration of a while loops. Same as JS/c.
3+
*/
4+
5+
contract BasicIterator {
6+
7+
address creator; // reserve one "address"-type spot
8+
uint8[10] integers; // reserve a chunk of storage for 10 8-bit unsigned integers in an array
9+
10+
function BasicIterator()
11+
{
12+
creator = msg.sender; // set the creator address
13+
uint8 x = 0; // initialize an 8-bit, unsigned integer to zero
14+
while(x < integers.length) // the variable integers was initialized to length 10
15+
{
16+
integers[x] = x; // set integers to [0,1,2,3,4,5,6,7,8,9] over ten iterations
17+
x++;
18+
}
19+
}
20+
21+
function getSum() constant returns (uint) // "constant" just means this function returns something to the caller
22+
{ // which is immediately followed by what type gets returned, in this case a full uint256
23+
uint8 sum = 0;
24+
uint8 x = 0;
25+
while(x < integers.length)
26+
{
27+
sum = sum + integers[x];
28+
x++;
29+
}
30+
return sum;
31+
}
32+
33+
/**********
34+
Standard kill() function to recover funds
35+
**********/
36+
37+
function kill()
38+
{
39+
if (msg.sender == creator)
40+
{
41+
suicide(creator); // kills this contract and sends remaining funds back to creator
42+
}
43+
}
44+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`BasicIterator.sol 1`] = `
4+
/*
5+
This is a very simple demonstration of a while loops. Same as JS/c.
6+
*/
7+
8+
contract BasicIterator {
9+
10+
address creator; // reserve one "address"-type spot
11+
uint8[10] integers; // reserve a chunk of storage for 10 8-bit unsigned integers in an array
12+
13+
function BasicIterator()
14+
{
15+
creator = msg.sender; // set the creator address
16+
uint8 x = 0; // initialize an 8-bit, unsigned integer to zero
17+
while(x < integers.length) // the variable integers was initialized to length 10
18+
{
19+
integers[x] = x; // set integers to [0,1,2,3,4,5,6,7,8,9] over ten iterations
20+
x++;
21+
}
22+
}
23+
24+
function getSum() constant returns (uint) // "constant" just means this function returns something to the caller
25+
{ // which is immediately followed by what type gets returned, in this case a full uint256
26+
uint8 sum = 0;
27+
uint8 x = 0;
28+
while(x < integers.length)
29+
{
30+
sum = sum + integers[x];
31+
x++;
32+
}
33+
return sum;
34+
}
35+
36+
/**********
37+
Standard kill() function to recover funds
38+
**********/
39+
40+
function kill()
41+
{
42+
if (msg.sender == creator)
43+
{
44+
suicide(creator); // kills this contract and sends remaining funds back to creator
45+
}
46+
}
47+
}
48+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49+
50+
contract BasicIterator {
51+
address creator;
52+
uint8[10] integers;
53+
54+
function BasicIterator() {
55+
creator = msg.sender;
56+
uint8 x = 0;
57+
while (x < integers.length) {
58+
integers[x] = x;
59+
x++;
60+
}
61+
}
62+
63+
function getSum() constant returns(uint) {
64+
uint8 sum = 0;
65+
uint8 x = 0;
66+
while (x < integers.length) {
67+
sum = sum + integers[x];
68+
x++;
69+
}
70+
return sum;
71+
}
72+
73+
function kill() {
74+
if (msg.sender == creator) {
75+
suicide(creator);
76+
}
77+
}
78+
}
79+
80+
`;

tests/BasicIterator/jsfmt.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
run_spec(__dirname);
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
pragma solidity ^0.4.22;
2+
3+
contract SimpleAuction {
4+
// Parameters of the auction. Times are either
5+
// absolute unix timestamps (seconds since 1970-01-01)
6+
// or time periods in seconds.
7+
address public beneficiary;
8+
uint public auctionEnd;
9+
10+
// Current state of the auction.
11+
address public highestBidder;
12+
uint public highestBid;
13+
14+
// Allowed withdrawals of previous bids
15+
mapping(address => uint) pendingReturns;
16+
17+
// Set to true at the end, disallows any change
18+
bool ended;
19+
20+
// Events that will be fired on changes.
21+
event HighestBidIncreased(address bidder, uint amount);
22+
event AuctionEnded(address winner, uint amount);
23+
24+
// The following is a so-called natspec comment,
25+
// recognizable by the three slashes.
26+
// It will be shown when the user is asked to
27+
// confirm a transaction.
28+
29+
/// Create a simple auction with `_biddingTime`
30+
/// seconds bidding time on behalf of the
31+
/// beneficiary address `_beneficiary`.
32+
constructor(
33+
uint _biddingTime,
34+
address _beneficiary
35+
) public {
36+
beneficiary = _beneficiary;
37+
auctionEnd = now + _biddingTime;
38+
}
39+
40+
/// Bid on the auction with the value sent
41+
/// together with this transaction.
42+
/// The value will only be refunded if the
43+
/// auction is not won.
44+
function bid() public payable {
45+
// No arguments are necessary, all
46+
// information is already part of
47+
// the transaction. The keyword payable
48+
// is required for the function to
49+
// be able to receive Ether.
50+
51+
// Revert the call if the bidding
52+
// period is over.
53+
require(
54+
now <= auctionEnd,
55+
"Auction already ended."
56+
);
57+
58+
// If the bid is not higher, send the
59+
// money back.
60+
require(
61+
msg.value > highestBid,
62+
"There already is a higher bid."
63+
);
64+
65+
if (highestBid != 0) {
66+
// Sending back the money by simply using
67+
// highestBidder.send(highestBid) is a security risk
68+
// because it could execute an untrusted contract.
69+
// It is always safer to let the recipients
70+
// withdraw their money themselves.
71+
pendingReturns[highestBidder] += highestBid;
72+
}
73+
highestBidder = msg.sender;
74+
highestBid = msg.value;
75+
emit HighestBidIncreased(msg.sender, msg.value);
76+
}
77+
78+
/// Withdraw a bid that was overbid.
79+
function withdraw() public returns (bool) {
80+
uint amount = pendingReturns[msg.sender];
81+
if (amount > 0) {
82+
// It is important to set this to zero because the recipient
83+
// can call this function again as part of the receiving call
84+
// before `send` returns.
85+
pendingReturns[msg.sender] = 0;
86+
87+
if (!msg.sender.send(amount)) {
88+
// No need to call throw here, just reset the amount owing
89+
pendingReturns[msg.sender] = amount;
90+
return false;
91+
}
92+
}
93+
return true;
94+
}
95+
96+
/// End the auction and send the highest bid
97+
/// to the beneficiary.
98+
function auctionEnd() public {
99+
// It is a good guideline to structure functions that interact
100+
// with other contracts (i.e. they call functions or send Ether)
101+
// into three phases:
102+
// 1. checking conditions
103+
// 2. performing actions (potentially changing conditions)
104+
// 3. interacting with other contracts
105+
// If these phases are mixed up, the other contract could call
106+
// back into the current contract and modify the state or cause
107+
// effects (ether payout) to be performed multiple times.
108+
// If functions called internally include interaction with external
109+
// contracts, they also have to be considered interaction with
110+
// external contracts.
111+
112+
// 1. Conditions
113+
require(now >= auctionEnd, "Auction not yet ended.");
114+
require(!ended, "auctionEnd has already been called.");
115+
116+
// 2. Effects
117+
ended = true;
118+
emit AuctionEnded(highestBidder, highestBid);
119+
120+
// 3. Interaction
121+
beneficiary.transfer(highestBid);
122+
}
123+
}

0 commit comments

Comments
 (0)