ubiquity / ubiquity-dollar

Ubiquity Dollar (UUSD) smart contracts and user interface.
https://uad.ubq.fi
Apache License 2.0
34 stars 91 forks source link

Enforce same line bracket opens in Solidity #839

Closed 0x4007 closed 12 months ago

0x4007 commented 12 months ago

A custom lint-staged script would need to run to enforce this bracket style on commits. I was unable to find a tool out-of-the-box for this but it is possible that they exist.

@rndquu we are having styling issues, we should decide whether to allow

function setRndnuu() private {
    // code
}

or

function setRndnuu() private
{

}

this can be seen here as well as many other parts of the code

I guess the linter decides how to format code

I looked into this and we would have to make a custom tool to enforce this with lint-staged I believe.

Originally posted by @pavlovcik in https://github.com/ubiquity/ubiquity-dollar/issues/832#issuecomment-1818047062

rndquu commented 12 months ago

Let's start with utilizing our existing linter. Perhaps there is a rule which enforces line brackets to a particular format. If there is no such rule (or some other linter with such rule) then we could increase time estimate to < 1 Day to create our own custom solution.

gitcoindev commented 12 months ago

Let's start with utilizing our existing linter. Perhaps there is a rule which enforces line brackets to a particular format. If there is no such rule (or some other linter with such rule) then we could increase time estimate to < 1 Day to create our own custom solution.

Good point. I started looking into this as well, the existing linter should be able to enforce this. I will check and should have a solution today.

gitcoindev commented 12 months ago

I already found the root cause, we should use prettier-plugin-solidity in lint-staged settings. I will test and submit a pr soon.

gitcoindev commented 12 months ago

/start

ubiquibot[bot] commented 12 months ago

Deadline Mon, 20 Nov 2023 13:37:26 UTC
Registered Wallet 0x7e92476D69Ff1377a8b45176b1829C4A5566653a

Tips:

gitcoindev commented 12 months ago

I added the 'yarn format' to packages/contracts , made sure prettier-plugin-solidity is explicitly used and all in all the current formatting of development branch contracts matches Prettier way of formatting.

The full explanation is available at https://github.com/prettier-solidity/prettier-plugin-solidity/blob/main/STYLEGUIDE.md#function-declaration

I will add a mention to the style guide in contracts README.md , pasting here below the content for full clarity.

Function Declaration

function increment(uint x) public pure returns (uint) {
    return x + 1;
}

function increment(uint x) public pure onlyowner returns (uint) {
    return x + 1;
}
function kill() public onlyowner {
    selfdestruct(owner);
}
function thisFunctionHasLotsOfArguments(
    address a,
    address b,
    address c,
    address d,
    address e,
    address f
)
    public
{
    doSomething();
}
function thisFunctionNameIsReallyLong(address x, address y, address z)
    public
    onlyowner
    priced
    returns (address)
{
    doSomething();
}

function thisFunctionNameIsReallyLong(
    address x,
    address y,
    address z,
)
    public
    onlyowner
    priced
    returns (address)
{
    doSomething();
}
function thisFunctionNameIsReallyLong(
    address a,
    address b,
    address c
)
    public
    returns (
        address someAddressName,
        uint256 LongArgument,
        uint256 Argument
    )
{
    doSomething()

    return (
        veryLongReturnArg1,
        veryLongReturnArg2,
        veryLongReturnArg3
    );
}
pragma solidity >=0.4.0 <0.7.0;

// Base contracts just to make this compile
contract B {
    constructor(uint) public {
    }
}

contract C {
    constructor(uint, uint) public {
    }
}

contract D {
    constructor(uint) public {
    }
}

contract A is B, C, D {
    uint x;

    constructor(uint param1, uint param2, uint param3, uint param4, uint param5)
        B(param1)
        C(param2, param3)
        D(param4)
        public
    {
        // do something with param5
        x = param5;
    }
}

These guidelines for function declarations are intended to improve readability. Authors should use their best judgement as this guide does not try to cover all possible permutations for function declarations.

Therefore, there are certain cases where the opening brace of a solidity function is formatted in the same line and some on the new line, depending on the rules above.

gitcoindev commented 12 months ago

Pull request opened https://github.com/ubiquity/ubiquity-dollar/pull/840

molecula451 commented 12 months ago

the idea (my idea) is to enforce the tool to have the same pattern, this is a per best solidity practices, what do you think are the nuances on this due to long range functions with params? @gitcoindev

unless rndqnuu has different in how he wants to format.

best practice for solidity is


function getPavlovcik() private {

}

struct PavlovcikInfo {
   address info;
}

struct OpenPavlovick {
   mapping(address => PavlovcikInfo) data; 
}
rndquu commented 12 months ago

I guess we can use any approach towards the brackets formatting unless it is consistent across the whole project

gitcoindev commented 12 months ago

I guess we can use any approach towards the brackets formatting unless it is consistent across the whole project

I checked a few big projects

They are also fine with mixed brackets formatting, see example here: https://github.com/Uniswap/v4-core/blob/main/src/test/MockHooks.sol vs https://github.com/Uniswap/v4-core/blob/main/src/test/MockContract.sol

I would stick then to whatever prettier output formats, too.

gitcoindev commented 12 months ago

@molecula451 another point to use prettier output without changes is: https://prettier.io/docs/en/why-prettier

And from the main website https://prettier.io/

Why?

And Ubiquity would use the same formatting tool as big code-bases, which makes it easier to onboard new partners / projects.

0x4007 commented 12 months ago
getPavlovcik()

"Hello, World!"

ubiquibot[bot] commented 12 months ago

Permit generation skipped because this issue has been closed by an external contributor.

If you've enjoyed your experience in the DevPool, we'd appreciate your support. Follow Ubiquity on GitHub and star this repo. Your endorsement means the world to us and helps us grow!
We are excited to announce that the DevPool and UbiquiBot are now available to partners! Our ideal collaborators are globally distributed crypto-native organizations, who actively work on open source on GitHub, and excel in research & development. If you can introduce us to the repository maintainers in these types of companies, we have a special bonus in store for you!

ubiquibot[bot] commented 12 months ago

Task Creator Reward

pavlovcik: [ CLAIM 26.7 WXDAI ]

ubiquibot[bot] commented 12 months ago

Conversation Rewards

rndquu: [ CLAIM 23.4 WXDAI ]

gitcoindev: [ CLAIM 272.1 WXDAI ]

molecula451: [ CLAIM 24.9 WXDAI ]

pavlovcik: [ CLAIM 0.6 WXDAI ]