forge-fmt 0.2.0

solidity formatting
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
pragma solidity ^0.5.2;

// forgefmt: disable-next-line
pragma    solidity     ^0.5.2;

import {
    symbol1 as alias1,
    symbol2 as alias2,
    symbol3 as alias3,
    symbol4
} from "File2.sol";

// forgefmt: disable-next-line
import {symbol1 as alias1, symbol2 as alias2, symbol3 as alias3, symbol4} from 'File2.sol';

enum States {
    State1,
    State2,
    State3,
    State4,
    State5,
    State6,
    State7,
    State8,
    State9
}

// forgefmt: disable-next-line
enum States { State1, State2, State3, State4, State5, State6, State7, State8, State9 }

// forgefmt: disable-next-line
bytes32 constant private BYTES = 0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749;

// forgefmt: disable-start

// comment1


// comment2
/* comment 3 */ /* 
    comment4
     */ // comment 5


/// Doccomment 1
    /// Doccomment 2

/**
     * docccoment 3
  */


// forgefmt: disable-end

// forgefmt: disable-start

function test1() {}

function test2() {}

// forgefmt: disable-end

contract Constructors is Ownable, Changeable {
    //forgefmt: disable-next-item
    function Constructors(variable1) public Changeable(variable1) Ownable() onlyOwner {
    }

    //forgefmt: disable-next-item
    constructor(variable1, variable2, variable3, variable4, variable5, variable6, variable7) public Changeable(variable1, variable2, variable3, variable4, variable5, variable6, variable7) Ownable() onlyOwner {}
}

function test() {
    uint256 pi_approx = 666 / 212;
    uint256 pi_approx = /* forgefmt: disable-start */ 666    /    212; /* forgefmt: disable-end */

    // forgefmt: disable-next-item
    uint256 pi_approx = 666 /
        212;

    uint256 test_postfix = 1; // forgefmt: disable-start
                              // comment1
                              // comment2
                              // comment3
                              // forgefmt: disable-end
}

// forgefmt: disable-next-item
function testFunc(uint256   num, bytes32 data  ,    address receiver)
    public payable    attr1   Cool( "hello"   ) {}

function testAttrs(uint256 num, bytes32 data, address receiver)
    // forgefmt: disable-next-line
    public payable    attr1   Cool( "hello"   )
{}

// forgefmt: disable-next-line
function testParams(uint256   num, bytes32 data  ,    address receiver)
    public
    payable
    attr1
    Cool("hello")
{}

function testDoWhile() external {
    //forgefmt: disable-start
    uint256 i;
    do { "test"; } while (i != 0);

    do 
    {}
    while
    (
i != 0);

    bool someVeryVeryLongCondition;
    do { "test"; } while(
        someVeryVeryLongCondition && !someVeryVeryLongCondition && 
!someVeryVeryLongCondition &&
someVeryVeryLongCondition); 

    do i++; while(i < 10);

    do do i++; while (i < 30); while(i < 20);
    //forgefmt: disable-end
}

function forStatement() {
    //forgefmt: disable-start
        for
    (uint256 i1
        ; i1 < 10;      i1++)
    {
             i1++;
            }

        uint256 i2;
        for(++i2;i2<10;i2++)
        
        {}

        uint256 veryLongVariableName = 1000;
        for ( uint256 i3; i3 < 10
        && veryLongVariableName>999 &&      veryLongVariableName< 1001
        ; i3++)
        { i3 ++ ; }

        for (type(uint256).min;;) {}

        for (;;) { "test" ; }

        for (uint256 i4; i4< 10; i4++) i4++;

        for (uint256 i5; ;)
            for (uint256 i6 = 10; i6 > i5; i6--)
                i5++;
    //forgefmt: disable-end
}

function callArgTest() {
    //forgefmt: disable-start
        target.run{ gas: gasleft(), value: 1 wei };

        target.run{gas:1,value:0x00}();

        target.run{ 
                gas : 1000, 
        value: 1 ether 
        } ();

        target.run{  gas: estimate(),
    value: value(1) }(); 

        target.run { value:
        value(1 ether), gas: veryAndVeryLongNameOfSomeGasEstimateFunction() } ();

        target.run /* comment 1 */ { value: /* comment2 */ 1 }; 

        target.run { /* comment3 */ value: 1, // comment4
        gas: gasleft()};

        target.run {
            // comment5
            value: 1,
            // comment6
            gas: gasleft()};
    //forgefmt: disable-end
}

function ifTest() {
    // forgefmt: disable-start
    if (condition)
            execute();
        else
            executeElse();
    // forgefmt: disable-end

    /* forgefmt: disable-next-line */
    if (condition   &&   anotherLongCondition ) {
        execute();
    }
}

function yulTest() {
    // forgefmt: disable-start
        assembly {
            let payloadSize := sub(calldatasize(), 4)
            calldatacopy(0, 4, payloadSize)
            mstore(payloadSize, shl(96, caller()))

            let result :=
                delegatecall(gas(), moduleImpl, 0, add(payloadSize, 20), 0, 0)

            returndatacopy(0, 0, returndatasize())

            switch result
            case 0 { revert(0, returndatasize()) }
            default { return(0, returndatasize()) }
        }
    // forgefmt: disable-end
}

function literalTest() {
    // forgefmt: disable-start

        true;
            0x123_456;
        .1;
    "foobar";
            hex"001122FF";
        0xc02aaa39b223Fe8D0A0e5C4F27ead9083c756Cc2;
    // forgefmt: disable-end

    // forgefmt: disable-next-line
    bytes memory bytecode = hex"ff";
}

function returnTest() {
    // forgefmt: disable-start
        if (val == 0) {
        return // return single 1
        0x00;
        }

        if (val == 1) { return 
        1; }

        if (val == 2) {
                return 3
                -
                    1;
        }

        if (val == 4) {
            /* return single 2 */ return 2** // return single 3
            3 // return single 4
            ;
        }

        return  value(); // return single 5
            return  ;
            return /* return mul 4 */
            (
                987654321, 1234567890,/* return mul 5 */ false);
    // forgefmt: disable-end
}

function namedFuncCall() {
    // forgefmt: disable-start
        SimpleStruct memory simple = SimpleStruct({ val: 0 });

        ComplexStruct memory complex = ComplexStruct({ val: 1, anotherVal: 2, flag: true, timestamp: block.timestamp });

        StructWithAVeryLongNameThatExceedsMaximumLengthThatIsAllowedForFormatting memory long = StructWithAVeryLongNameThatExceedsMaximumLengthThatIsAllowedForFormatting({ whyNameSoLong: "dunno" });
    
        SimpleStruct memory simple2 = SimpleStruct(
    { // comment1 
        /* comment2 */ val : /* comment3 */ 0
    
    }
        );
    // forgefmt: disable-end
}

function revertTest() {
    // forgefmt: disable-start
        revert ({ });

        revert EmptyError({});

        revert SimpleError({ val: 0 });

        revert ComplexError(
            {
                val: 0,
                    ts: block.timestamp,
                        message: "some reason"
            });
        
        revert SomeVeryVeryVeryLongErrorNameWithNamedArgumentsThatExceedsMaximumLength({ val: 0, ts: 0x00, message: "something unpredictable happened that caused execution to revert"});

        revert // comment1 
        ({});
    // forgefmt: disable-end
}

function testTernary() {
    // forgefmt: disable-start
        bool condition;
        bool someVeryVeryLongConditionUsedInTheTernaryExpression;

        condition ? 0 : 1;

        someVeryVeryLongConditionUsedInTheTernaryExpression ? 1234567890 : 987654321;

        condition /* comment1 */ ? /* comment2 */ 1001 /* comment3 */ : /* comment4 */ 2002;

        // comment5
        someVeryVeryLongConditionUsedInTheTernaryExpression ? 1
        // comment6
        :
        // comment7
        0; // comment8
    // forgefmt: disable-end
}

function thisTest() {
    // forgefmt: disable-start
        this.someFunc();
        this.someVeryVeryVeryLongVariableNameThatWillBeAccessedByThisKeyword();
        this // comment1
            .someVeryVeryVeryLongVariableNameThatWillBeAccessedByThisKeyword();
        address(this).balance;
        
        address thisAddress = address(
            // comment2
             /* comment3 */ this // comment 4
        );
    // forgefmt: disable-end
}

function tryTest() {
    // forgefmt: disable-start
        try unknown.empty() {} catch {}

        try unknown.lookup() returns (uint256) {} catch Error(string memory) {}

        try unknown.lookup() returns (uint256) {} catch Error(string memory) {} catch (bytes memory) {}

    try unknown
        .lookup() returns   (uint256
                ) {
                } catch ( bytes  memory ){}

        try unknown.empty() {
            unknown.doSomething();
        } catch {
            unknown.handleError();
        }

        try unknown.empty() {
            unknown.doSomething();
        } catch Error(string memory) {}
        catch Panic(uint) {}
        catch {
            unknown.handleError();
        }

        try unknown.lookupMultipleValues() returns (uint256, uint256, uint256, uint256, uint256) {} catch Error(string memory) {} catch {}
 
        try unknown.lookupMultipleValues() returns (uint256, uint256, uint256, uint256, uint256) {
            unknown.doSomething();
        } 
        catch Error(string memory) {
             unknown.handleError();
        }
        catch {}
    // forgefmt: disable-end
}

function testArray() {
    // forgefmt: disable-start
        msg.data[
            // comment1
            4:];
        msg.data[
            : /* comment2 */ msg.data.length // comment3
            ];
        msg.data[
        // comment4 
        4 // comment5
        :msg.data.length /* comment6 */];
    // forgefmt: disable-end
}

function testUnit() {
    // forgefmt: disable-start
        uint256 timestamp;
        timestamp = 1 seconds;
        timestamp = 1 minutes;
        timestamp = 1 hours;
        timestamp = 1 days;
        timestamp = 1 weeks;

        uint256 value;
        value = 1 wei;
        value = 1 gwei;
        value = 1 ether;

        uint256 someVeryVeryVeryLongVaribleNameForTheMultiplierForEtherValue;

        value =  someVeryVeryVeryLongVaribleNameForTheMultiplierForEtherValue * 1 /* comment1 */ ether; // comment2

        value = 1 // comment3
        // comment4
        ether; // comment5
    // forgefmt: disable-end
}

contract UsingExampleContract {
    // forgefmt: disable-start
    using  UsingExampleLibrary      for   *  ;
        using UsingExampleLibrary for uint;
    using Example.UsingExampleLibrary  for  uint;
            using { M.g, M.f} for uint;
    using UsingExampleLibrary for   uint  global;
    using { These, Are, MultipleLibraries, ThatNeedToBePut, OnSeparateLines } for uint;
    using { This.isareally.longmember.access.expression.that.needs.to.besplit.into.lines } for uint;
    // forgefmt: disable-end
}

function testAssignment() {
    // forgefmt: disable-start
        (, uint256 second) = (1, 2);
        (uint256 listItem001) = 1;
        (uint256 listItem002, uint256 listItem003) = (10, 20);
        (uint256 listItem004, uint256 listItem005, uint256 listItem006) =
            (10, 20, 30);
    // forgefmt: disable-end
}

function testWhile() {
    // forgefmt: disable-start
        uint256 i1;
            while (  i1 <  10 ) {
            i1++;
        }

        while (i1<10) i1++;

        while (i1<10)
            while (i1<10)
                i1++;

         uint256 i2;
        while ( i2   < 10) { i2++; }

        uint256 i3; while (
            i3 < 10
        ) { i3++; }

        uint256 i4; while (i4 < 10) 

        { i4 ++ ;}

        uint256 someLongVariableName;
        while (
            someLongVariableName < 10 && someLongVariableName < 11 && someLongVariableName < 12
        ) { someLongVariableName ++; } someLongVariableName++;
    // forgefmt: disable-end
}

function testLine() {}

function   /* forgefmt: disable-line */ testLine(   ) { }

function testLine() {}

function   testLine(   ) { }  // forgefmt: disable-line

// forgefmt: disable-start

    type Hello is uint256;

error
  TopLevelCustomError();
  error TopLevelCustomErrorWithArg(uint    x)  ;
error TopLevelCustomErrorArgWithoutName  (string);

    event Event1(uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a, uint256 indexed a);

// forgefmt: disable-stop