Skip to content

Commit eca6cbf

Browse files
Franco Victoriomattiaerre
authored andcommitted
Improve empty definitions of contracts and functions (#100)
1 parent 5b23ca8 commit eca6cbf

2 files changed

Lines changed: 30 additions & 34 deletions

File tree

src/printer.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,29 @@ function genericPrint(path, options, print) {
5454
]);
5555
}
5656
return concat(['import ', doc, ';']);
57-
case 'ContractDefinition':
58-
doc = concat([node.kind, ' ', node.name]);
57+
case 'ContractDefinition': {
58+
let parts = [node.kind, ' ', node.name];
59+
5960
if (node.baseContracts.length > 0) {
60-
doc = concat([
61-
doc,
61+
parts = parts.concat([
6262
' is ',
6363
join(', ', path.map(print, 'baseContracts'))
6464
]);
6565
}
66-
return concat([
67-
doc,
68-
' {',
69-
indent(line),
70-
indent(printPreservingEmptyLines(path, 'subNodes', options, print)),
71-
line,
72-
'}',
73-
line
74-
]);
66+
67+
parts.push(' {');
68+
if (node.subNodes.length > 0) {
69+
parts = parts.concat([
70+
indent(line),
71+
indent(printPreservingEmptyLines(path, 'subNodes', options, print)),
72+
line
73+
]);
74+
}
75+
parts.push('}');
76+
parts.push(line);
77+
78+
return concat(parts);
79+
}
7580
case 'InheritanceSpecifier': {
7681
let parts = [path.call(print, 'baseName')];
7782

@@ -159,6 +164,11 @@ function genericPrint(path, options, print) {
159164
}
160165
return doc;
161166
case 'Block': {
167+
// if block is empty, just return the pair of braces
168+
if (node.statements.length === 0 && !node.comments) {
169+
return '{}';
170+
}
171+
162172
const parts = [
163173
'{',
164174
indent(line),

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,7 @@ contract Base {
466466
}
467467
468468
contract Derived is Base(0) {
469-
function Derived(uint i) Base(i) {
470-
471-
}
469+
function Derived(uint i) Base(i) {}
472470
}
473471
474472
@@ -636,19 +634,13 @@ contract DualIndex {
636634
}
637635
638636
639-
contract A {
640-
641-
}
642-
643-
644-
contract B {
637+
contract A {}
645638
646-
}
647639
640+
contract B {}
648641
649-
contract C is A, B {
650642
651-
}
643+
contract C is A, B {}
652644
653645
654646
contract TestPrivate {
@@ -673,9 +665,7 @@ contract FromSolparse is A, B, TestPrivate, TestInternal {
673665
contract CommentedOutFunction {
674666
// FYI: This empty function, as well as the commented
675667
// out function below (bad code) is important to this test.
676-
function() {
677-
678-
}
668+
function() {}
679669
680670
// function something()
681671
// uint x = 10;
@@ -714,9 +704,7 @@ contract NewStuff {
714704
715705
// modifier with expression
716706
contract MyContract {
717-
function fun() mymodifier(foo.bar()) {
718-
719-
}
707+
function fun() mymodifier(foo.bar()) {}
720708
}
721709
722710
@@ -843,9 +831,7 @@ contract Ballot {
843831
uint[],
844832
address myAdd,
845833
string[] names
846-
) {
847-
848-
}
834+
) {}
849835
function foobar() payable owner(myPrice) returns (
850836
uint[],
851837
address myAdd,

0 commit comments

Comments
 (0)