Skip to content

Commit 46e5034

Browse files
Janthermattiaerre
authored andcommitted
Adding spaces between functions and contracts (#217)
* Removing extra empty lines at the end of a block * Adding spaces between functions and contracts * adding scenario where function is followed by a non-function node
1 parent 3e56653 commit 46e5034

File tree

25 files changed

+140
-25
lines changed

25 files changed

+140
-25
lines changed

src/nodes/print-preserving-empty-lines.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,46 @@ const {
88
function printPreservingEmptyLines(path, key, options, print) {
99
const parts = [];
1010
path.each(childPath => {
11+
const node = childPath.getValue();
12+
const nodeType = node.type;
13+
1114
if (parts.length !== 0) {
1215
parts.push(hardline);
1316
}
1417

18+
if (childPath.getName() > 0) {
19+
if (nodeType === 'ContractDefinition') {
20+
if (parts[parts.length - 2] !== hardline) {
21+
parts.push(hardline);
22+
}
23+
parts.push(hardline);
24+
}
25+
26+
if (
27+
nodeType === 'FunctionDefinition' &&
28+
parts[parts.length - 2] !== hardline
29+
) {
30+
parts.push(hardline);
31+
}
32+
}
33+
1534
parts.push(print(childPath));
35+
1636
if (
1737
isNextLineEmptyAfterIndex(
1838
options.originalText,
19-
options.locEnd(childPath.getValue()) + 1
20-
)
39+
options.locEnd(node) + 1
40+
) ||
41+
nodeType === 'FunctionDefinition'
2142
) {
2243
parts.push(hardline);
2344
}
2445
}, key);
2546

47+
if (parts.length > 1 && parts[parts.length - 1] === hardline) {
48+
parts.pop();
49+
}
50+
2651
return concat(parts);
2752
}
2853

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ contract AddressPayable {
1313
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1414
pragma solidity ^0.5.2;
1515
16+
1617
contract AddressPayable {
1718
address payable[] hello;
19+
1820
function sendSomeEth(address payable to, address payable[] memory world)
1921
public
2022
payable

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,45 +426,56 @@ import "SomeFile.sol" as SomeOtherFile;
426426
import "AnotherFile.sol" as SomeSymbol;
427427
import {symbol1 as alias, symbol2} from "File.sol";
428428
429+
429430
interface i {
430431
function f();
431432
}
432433
434+
433435
contract c {
434436
function c() {
435437
val1 = 1 wei; // 1
436438
val2 = 1 szabo; // 1 * 10 ** 12
437439
val3 = 1 finney; // 1 * 10 ** 15
438440
val4 = 1 ether; // 1 * 10 ** 18
439441
}
442+
440443
uint256 val1;
441444
uint256 val2;
442445
uint256 val3;
443446
uint256 val4;
444447
}
445448
449+
446450
contract test {
447451
enum ActionChoices {GoLeft, GoRight, GoStraight, SitStill}
448452
449453
function test() {
450454
choices = ActionChoices.GoStraight;
451455
}
456+
452457
function getChoice() returns (uint256 d) {
453458
d = uint256(choices);
454459
}
460+
455461
ActionChoices choices;
456462
}
457463
464+
458465
contract Base {
459466
function Base(uint256 i) {
460467
m_i = i;
461468
}
469+
462470
uint256 public m_i;
463471
}
472+
473+
464474
contract Derived is Base(0) {
465475
function Derived(uint256 i) Base(i) {}
466476
}
467477
478+
468479
contract C {
469480
uint248 x; // 31 bytes: slot 0, offset 0
470481
uint16 y; // 2 bytes: slot 1, offset 0 (does not fit in slot 0)
@@ -480,6 +491,7 @@ contract C {
480491
uint8 gamma; // 1 byte, slot 7 (start new slot after array)
481492
}
482493
494+
483495
contract test {
484496
function f(uint256 x, uint256 y) returns (uint256 z) {
485497
var c = x + 3;
@@ -488,36 +500,44 @@ contract test {
488500
}
489501
}
490502
503+
491504
contract test {
492505
function f(uint256 x, uint256 y) returns (uint256 z) {
493506
return 10;
494507
}
495508
}
496509
510+
497511
contract c {
498512
function() returns (uint256) {
499513
return g(8);
500514
}
515+
501516
function g(uint256 pos) internal returns (uint256) {
502517
setData(pos, 8);
503518
return getData(pos);
504519
}
520+
505521
function setData(uint256 pos, uint256 value) internal {
506522
data[pos] = value;
507523
}
524+
508525
function getData(uint256 pos) internal {
509526
return data[pos];
510527
}
528+
511529
mapping(uint256 => uint256) data;
512530
}
513531
532+
514533
contract Sharer {
515534
function sendHalf(address addr) returns (uint256 balance) {
516535
if (!addr.send(msg.value / 2)) throw; // also reverts the transfer to Sharer
517536
return address(this).balance;
518537
}
519538
}
520539
540+
521541
/// @dev Models a modifiable and iterable set of uint values.
522542
library IntegerSet {
523543
struct data {
@@ -528,6 +548,7 @@ library IntegerSet {
528548
/// Number of stored items.
529549
uint256 size;
530550
}
551+
531552
function insert(data storage self, uint256 value)
532553
returns (bool alreadyPresent)
533554
{
@@ -542,22 +563,27 @@ library IntegerSet {
542563
return false;
543564
}
544565
}
566+
545567
function remove(data storage self, uint256 value) returns (bool success) {
546568
uint256 index = self.index[value];
547569
if (index == 0) return false;
548570
delete self.index[value];
549571
delete self.items[index];
550572
self.size--;
551573
}
574+
552575
function contains(data storage self, uint256 value) returns (bool) {
553576
return self.index[value] > 0;
554577
}
578+
555579
function iterate_start(data storage self) returns (uint256 index) {
556580
return iterate_advance(self, 0);
557581
}
582+
558583
function iterate_valid(data storage self, uint256 index) returns (bool) {
559584
return index < self.items.length;
560585
}
586+
561587
function iterate_advance(data storage self, uint256 index)
562588
returns (uint256 r_index)
563589
{
@@ -567,24 +593,28 @@ library IntegerSet {
567593
) index++;
568594
return index;
569595
}
596+
570597
function iterate_get(data storage self, uint256 index)
571598
returns (uint256 value)
572599
{
573600
return self.items[index];
574601
}
575602
}
576603
604+
577605
/// How to use it:
578606
contract User {
579607
/// Just a struct holding our data.
580608
IntegerSet.data data;
609+
581610
/// Insert something
582611
function insert(uint256 v) returns (uint256 size) {
583612
/// Sends \`data\` via reference, so IntegerSet can modify it.
584613
IntegerSet.insert(data, v);
585614
/// We can access members of the struct - but we should take care not to mess with them.
586615
return data.size;
587616
}
617+
588618
/// Computes the sum of all stored data.
589619
function sum() returns (uint256 s) {
590620
for (
@@ -595,6 +625,7 @@ contract User {
595625
}
596626
}
597627
628+
598629
// This broke it at one point (namely the modifiers).
599630
contract DualIndex {
600631
mapping(uint256 => mapping(uint256 => uint256)) data;
@@ -622,20 +653,26 @@ contract DualIndex {
622653
}
623654
}
624655
656+
625657
contract A {}
626658
659+
627660
contract B {}
628661
662+
629663
contract C is A, B {}
630664
665+
631666
contract TestPrivate {
632667
uint256 private value;
633668
}
634669
670+
635671
contract TestInternal {
636672
uint256 internal value;
637673
}
638674
675+
639676
contract FromSolparse is A, B, TestPrivate, TestInternal {
640677
function() {
641678
uint256 a = 6**9;
@@ -644,6 +681,7 @@ contract FromSolparse is A, B, TestPrivate, TestInternal {
644681
}
645682
}
646683
684+
647685
contract CommentedOutFunction {
648686
// FYI: This empty function, as well as the commented
649687
// out function below (bad code) is important to this test.
@@ -652,24 +690,27 @@ contract CommentedOutFunction {
652690
// function something()
653691
// uint x = 10;
654692
// }
655-
656693
}
657694
695+
658696
library VarHasBrackets {
659697
string constant specialRight = "}";
660698
//string storage specialLeft = "{";
661699
}
662700
701+
663702
library UsingExampleLibrary {
664703
function sum(uint256[] storage self) returns (uint256 s) {
665704
for (uint256 i = 0; i < self.length; i++) s += self[i];
666705
}
667706
}
668707
708+
669709
contract UsingExampleContract {
670710
using UsingExampleLibrary for uint256[];
671711
}
672712
713+
673714
contract NewStuff {
674715
uint256[] b;
675716
@@ -679,11 +720,13 @@ contract NewStuff {
679720
}
680721
}
681722
723+
682724
// modifier with expression
683725
contract MyContract {
684726
function fun() mymodifier(foo.bar()) {}
685727
}
686728
729+
687730
library GetCode {
688731
function at(address _addr) returns (bytes o_code) {
689732
assembly {
@@ -705,6 +748,7 @@ library GetCode {
705748
}
706749
}
707750
751+
708752
contract assemblyLocalBinding {
709753
function test() {
710754
assembly {
@@ -716,6 +760,7 @@ contract assemblyLocalBinding {
716760
}
717761
}
718762
763+
719764
contract assemblyReturn {
720765
uint256 a = 10;
721766
@@ -729,10 +774,12 @@ contract assemblyReturn {
729774
}
730775
}
731776
777+
732778
contract usesConst {
733779
uint256 const = 0;
734780
}
735781
782+
736783
contract memoryArrays {
737784
uint256 seven = 7;
738785
@@ -746,6 +793,7 @@ contract memoryArrays {
746793
}
747794
}
748795
796+
749797
contract DeclarativeExpressions {
750798
uint256 a;
751799
uint256 b = 7;
@@ -767,6 +815,7 @@ contract DeclarativeExpressions {
767815
}
768816
}
769817
818+
770819
contract VariableDeclarationTuple {
771820
function getMyTuple() returns (bool, bool) {
772821
return (true, false);
@@ -783,11 +832,13 @@ contract VariableDeclarationTuple {
783832
}
784833
}
785834
835+
786836
contract TypeIndexSpacing {
787837
uint256[7] x;
788838
uint256[] y;
789839
}
790840
841+
791842
contract Ballot {
792843
struct Voter {
793844
uint256 weight;
@@ -803,6 +854,7 @@ contract Ballot {
803854
owner(myPrice)
804855
returns (uint256[], address myAdd, string[] names)
805856
{}
857+
806858
function foobar()
807859
payable
808860
owner(myPrice)
@@ -815,6 +867,7 @@ contract Ballot {
815867
Voter airbnb = Voter({weight: 2, voted: true});
816868
}
817869
870+
818871
contract multilineReturn {
819872
function a() returns (uint256 x) {
820873
return 5;

0 commit comments

Comments
 (0)