Skip to content

Commit 57c60c6

Browse files
authored
Removing forced separation of functions and contracts (#960)
1 parent 2984bcc commit 57c60c6

16 files changed

Lines changed: 67 additions & 137 deletions

File tree

src/common/backward-compatibility.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,8 @@ export function getNextNonSpaceNonCommentCharacter(text, node, locEnd) {
2727
: util.getNextNonSpaceNonCommentCharacter(text, locEnd(node)); // V3 exposes this function directly
2828
}
2929

30-
export function isFirst(path, _, index) {
31-
return isPrettier2 ? index === 0 : path.isFirst;
32-
}
33-
3430
export function isLast(path, key, index) {
3531
return isPrettier2
3632
? index === path.getParentNode()[key].length - 1
3733
: path.isLast;
3834
}
39-
40-
export function previous(path, key, index) {
41-
return isPrettier2 ? path.getParentNode()[key][index - 1] : path.previous;
42-
}
43-
44-
export function next(path, key, index) {
45-
return isPrettier2 ? path.getParentNode()[key][index + 1] : path.next;
46-
}

src/common/printer-helpers.js

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import { doc } from 'prettier';
22
import {
3-
isFirst,
43
isLast,
54
isNextLineEmpty,
6-
isPrettier2,
7-
next,
8-
previous
5+
isPrettier2
96
} from './backward-compatibility.js';
107

118
const { group, indent, join, line, softline, hardline } = doc.builders;
@@ -24,7 +21,7 @@ export const printComments = (node, path, options, filter = () => true) => {
2421
return null;
2522
}
2623
comment.printed = true;
27-
return options.printer.printComment(commentPath);
24+
return options.printer.printComment(commentPath, options);
2825
}, 'comments')
2926
.filter(Boolean)
3027
);
@@ -39,22 +36,6 @@ export const printComments = (node, path, options, filter = () => true) => {
3936
/* c8 ignore stop */
4037
};
4138

42-
const shouldHaveEmptyLine = (node, checkForLeading) =>
43-
Boolean(
44-
// if node is not FunctionDefinition, it should have an empty line
45-
node.type !== 'FunctionDefinition' ||
46-
// if FunctionDefinition is not abstract, it should have an empty line
47-
node.body ||
48-
// if FunctionDefinition has the comment we are looking for (trailing or
49-
// leading), it should have an empty line
50-
node.comments?.some((comment) => checkForLeading && comment.leading)
51-
);
52-
53-
const separatingLine = (firstNode, secondNode) =>
54-
shouldHaveEmptyLine(firstNode, false) || shouldHaveEmptyLine(secondNode, true)
55-
? hardline
56-
: '';
57-
5839
export function printPreservingEmptyLines(path, key, options, print) {
5940
const parts = [];
6041
path.each((childPath, index) => {
@@ -71,38 +52,16 @@ export function printPreservingEmptyLines(path, key, options, print) {
7152
parts.push(hardline);
7253
}
7354

74-
// Only attempt to prepend an empty line if `node` is not the first item
75-
// and an empty line hasn't already been appended after the previous `node`
76-
if (
77-
!isFirst(childPath, key, index) &&
78-
parts[parts.length - 2] !== hardline
79-
) {
80-
if (nodeType === 'FunctionDefinition') {
81-
// Prepend FunctionDefinition with an empty line if there should be a
82-
// separation with the previous `node`
83-
parts.push(separatingLine(previous(childPath, key, index), node));
84-
} else if (nodeType === 'ContractDefinition') {
85-
// Prepend ContractDefinition with an empty line
86-
parts.push(hardline);
87-
}
88-
}
89-
9055
parts.push(print(childPath));
9156

9257
// Only attempt to append an empty line if `node` is not the last item
93-
if (!isLast(childPath, key, index)) {
94-
if (isNextLineEmpty(options.originalText, options.locEnd(node) + 1)) {
95-
// Append an empty line if the original text already had an one after
96-
// the current `node`
97-
parts.push(hardline);
98-
} else if (nodeType === 'FunctionDefinition') {
99-
// Append FunctionDefinition with an empty line if there should be a
100-
// separation with the next `node`
101-
parts.push(separatingLine(node, next(childPath, key, index)));
102-
} else if (nodeType === 'ContractDefinition') {
103-
// Append ContractDefinition with an empty line
104-
parts.push(hardline);
105-
}
58+
if (
59+
!isLast(childPath, key, index) &&
60+
isNextLineEmpty(options.originalText, options.locEnd(node) + 1)
61+
) {
62+
// Append an empty line if the original text already had an one after
63+
// the current `node`
64+
parts.push(hardline);
10665
}
10766
}, key);
10867

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ pragma solidity ^0.5.2;
2323
contract AddressPayable {
2424
using Address for address payable;
2525
address payable[] hello;
26-
2726
function sendSomeEth(
2827
address payable to,
2928
address payable[] memory world

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,6 @@ import {symbol1 as alias, symbol2} from "File.sol";
568568
569569
interface i {
570570
event ForeignEvent();
571-
572571
function f();
573572
}
574573
@@ -582,7 +581,6 @@ contract c {
582581
val3 = 1 finney; // 1 * 10 ** 15
583582
val4 = 1 ether; // 1 * 10 ** 18
584583
}
585-
586584
uint256 val1;
587585
uint256 val2;
588586
uint256 val3;
@@ -600,22 +598,18 @@ contract test {
600598
function test() {
601599
choices = ActionChoices.GoStraight;
602600
}
603-
604601
function getChoice() returns (uint d) {
605602
d = uint256(choices);
606603
}
607-
608604
ActionChoices choices;
609605
}
610606
611607
contract Base {
612608
function Base(uint i) {
613609
m_i = i;
614610
}
615-
616611
uint public m_i;
617612
}
618-
619613
contract Derived is Base(0) {
620614
function Derived(uint i) Base(i) {}
621615
}
@@ -653,20 +647,16 @@ contract c {
653647
function() returns (uint) {
654648
return g(8);
655649
}
656-
657650
function g(uint pos) internal returns (uint) {
658651
setData(pos, 8);
659652
return getData(pos);
660653
}
661-
662654
function setData(uint pos, uint value) internal {
663655
data[pos] = value;
664656
}
665-
666657
function getData(uint pos) internal {
667658
return data[pos];
668659
}
669-
670660
mapping(uint => uint) data;
671661
mapping(uint id => uint) data;
672662
mapping(address owner => mapping(address spender => uint amount)) allowances;
@@ -689,7 +679,6 @@ library IntegerSet {
689679
/// Number of stored items.
690680
uint size;
691681
}
692-
693682
function insert(
694683
data storage self,
695684
uint value
@@ -705,27 +694,22 @@ library IntegerSet {
705694
return false;
706695
}
707696
}
708-
709697
function remove(data storage self, uint value) returns (bool success) {
710698
uint index = self.index[value];
711699
if (index == 0) return false;
712700
delete self.index[value];
713701
delete self.items[index];
714702
self.size--;
715703
}
716-
717704
function contains(data storage self, uint value) returns (bool) {
718705
return self.index[value] > 0;
719706
}
720-
721707
function iterate_start(data storage self) returns (uint index) {
722708
return iterate_advance(self, 0);
723709
}
724-
725710
function iterate_valid(data storage self, uint index) returns (bool) {
726711
return index < self.items.length;
727712
}
728-
729713
function iterate_advance(
730714
data storage self,
731715
uint index
@@ -736,7 +720,6 @@ library IntegerSet {
736720
) index++;
737721
return index;
738722
}
739-
740723
function iterate_get(data storage self, uint index) returns (uint value) {
741724
return self.items[index];
742725
}
@@ -746,15 +729,13 @@ library IntegerSet {
746729
contract User {
747730
/// Just a struct holding our data.
748731
IntegerSet.data data;
749-
750732
/// Insert something
751733
function insert(uint v) returns (uint size) {
752734
/// Sends \`data\` via reference, so IntegerSet can modify it.
753735
IntegerSet.insert(data, v);
754736
/// We can access members of the struct - but we should take care not to mess with them.
755737
return data.size;
756738
}
757-
758739
/// Computes the sum of all stored data.
759740
function sum() returns (uint s) {
760741
for (
@@ -974,7 +955,6 @@ contract Ballot {
974955
owner(myPrice)
975956
returns (uint[], address myAdd, string[] names)
976957
{}
977-
978958
function foobar()
979959
payable
980960
owner(myPrice)
@@ -1071,13 +1051,9 @@ contract Overrides {
10711051
require(msg.sender == owner(), "Ownable: caller is not the owner");
10721052
_;
10731053
}
1074-
10751054
function foo() public override {}
1076-
10771055
function bar() public override(Foo) {}
1078-
10791056
function baz() public override(Foo, Bar) {}
1080-
10811057
function long()
10821058
public
10831059
override(

tests/format/Comments/Comments.sol

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ pragma solidity ^0.4.24;
22

33

44
contract Comments1 {
5+
/* solhint-disable var-name-mixedcase */
6+
IEIP712DomainSeparator private EIP712domainSeparator;
7+
bytes32 private _CACHED_DOMAIN_SEPARATOR;
8+
9+
10+
/* solhint-enable var-name-mixedcase */
11+
12+
513
function() {
614
// solhint-disable-previous-line no-empty-blocks
715
}
@@ -39,6 +47,18 @@ contract Comments6 /*why the name `Comments6`*/ is Interface1/*why we used Inter
3947
}
4048

4149
contract Comments7 {
50+
51+
52+
// 1 comment before first function
53+
54+
55+
56+
// 2 comment before first function
57+
58+
59+
60+
// 3 comment before first function
61+
4262
function someFunction(
4363
uint a, // the first value
4464
uint b, // the second value

tests/format/Comments/__snapshots__/jsfmt.spec.js.snap

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ pragma solidity ^0.4.24;
1010
1111
1212
contract Comments1 {
13+
/* solhint-disable var-name-mixedcase */
14+
IEIP712DomainSeparator private EIP712domainSeparator;
15+
bytes32 private _CACHED_DOMAIN_SEPARATOR;
16+
17+
18+
/* solhint-enable var-name-mixedcase */
19+
20+
1321
function() {
1422
// solhint-disable-previous-line no-empty-blocks
1523
}
@@ -47,6 +55,18 @@ contract Comments6 /*why the name \`Comments6\`*/ is Interface1/*why we used Int
4755
}
4856
4957
contract Comments7 {
58+
59+
60+
// 1 comment before first function
61+
62+
63+
64+
// 2 comment before first function
65+
66+
67+
68+
// 3 comment before first function
69+
5070
function someFunction(
5171
uint a, // the first value
5272
uint b, // the second value
@@ -149,6 +169,12 @@ contract Comments13 {
149169
pragma solidity ^0.4.24;
150170
151171
contract Comments1 {
172+
/* solhint-disable var-name-mixedcase */
173+
IEIP712DomainSeparator private EIP712domainSeparator;
174+
bytes32 private _CACHED_DOMAIN_SEPARATOR;
175+
176+
/* solhint-enable var-name-mixedcase */
177+
152178
function() {
153179
// solhint-disable-previous-line no-empty-blocks
154180
}
@@ -212,6 +238,12 @@ contract Comments4 is
212238
}
213239
214240
contract Comments7 {
241+
// 1 comment before first function
242+
243+
// 2 comment before first function
244+
245+
// 3 comment before first function
246+
215247
function someFunction(
216248
uint a, // the first value
217249
uint b, // the second value

tests/format/FunctionCalls/__snapshots__/jsfmt.spec.js.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ contract FunctionCalls {
6262
6363
Voter airbnb = Voter({ weight: 2, voted: true });
6464
}
65-
6665
function verify(
6766
ForwardRequest calldata req,
6867
bytes calldata signature
@@ -215,7 +214,6 @@ contract FunctionCalls {
215214
216215
Voter airbnb = Voter({weight: 2, voted: true});
217216
}
218-
219217
function verify(
220218
ForwardRequest calldata req,
221219
bytes calldata signature

0 commit comments

Comments
 (0)