Skip to content

Commit e7b81dd

Browse files
Franco Victoriomattiaerre
authored andcommitted
Print else on new line when consequent is not a block (#68)
* Print else on new line when consequent is not a block * bump version
1 parent dc768cc commit e7b81dd

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

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.8",
3+
"version": "1.0.0-alpha.9",
44
"description": "prettier plugin for solidity",
55
"main": "src",
66
"scripts": {

src/printer.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,13 @@ function genericPrint(path, options, print) {
325325
path.call(print, 'trueBody')
326326
]);
327327
if (node.falseBody) {
328-
doc = concat([doc, ' else ', path.call(print, 'falseBody')]);
328+
const elseOnSameLine = node.trueBody.type === 'Block';
329+
doc = concat([
330+
doc,
331+
elseOnSameLine ? ' ' : hardline,
332+
'else ',
333+
path.call(print, 'falseBody')
334+
]);
329335
}
330336
return doc;
331337
case 'EnumDefinition':

tests/Etc/Etc.sol

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ contract Contract {
2222
require(msg.sender != x);
2323
_;
2424
}
25+
26+
function ifBlockInOneLine(uint a) returns (uint b) {
27+
if (a < 0) b = 0x67; else if (a == 0) b = 0x12; else b = 0x78;
28+
}
2529
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ contract Contract {
2525
require(msg.sender != x);
2626
_;
2727
}
28+
29+
function ifBlockInOneLine(uint a) returns (uint b) {
30+
if (a < 0) b = 0x67; else if (a == 0) b = 0x12; else b = 0x78;
31+
}
2832
}
2933
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3034
pragma solidity ^0.4.24;
@@ -51,6 +55,12 @@ contract Contract {
5155
require(msg.sender != x);
5256
_;
5357
}
58+
59+
function ifBlockInOneLine(uint a) returns(uint b) {
60+
if (a < 0) b = 0x67;
61+
else if (a == 0) b = 0x12;
62+
else b = 0x78;
63+
}
5464
}
5565
5666
`;

0 commit comments

Comments
 (0)