Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental operator position #1097

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

Conversation

Janther
Copy link
Contributor

@Janther Janther commented Feb 17, 2025

Prettier v3.5.0 added a new experimental option.

Experimental options follow this policy.

These options are designed to test if the users adopt the new formatting over the old one.

So far we have been follow closely prettier's decisions regarding formatting and apply when it makes sense in a Solidity context.

This PR contains 2 changes.

  1. Tweaking binary operation's indenting and grouping to align better with prettier's

    • This mainly creates a clear hierarchy where the higher an operation is in the hierarchy, when paired with a lower operation, it will be grouped and indented (visually matching the behaviour of adding parentheses) the hierarchy is as follows:
      1. Exponentiation **
      2. Multiplication *, /, %
      3. Addition +, -
      4. Shift Operation <<, >>
      5. Bit Operation |, &, ^
      6. Inequality <, >, <=, >=
      7. Equality ==, !=
      8. logical &&, ||

    Many of these cases were already taken care of by adding parentheses in unclear cases or when everything would fit in a single line.

    // Original
    x = a * b + c;
    x = a + b * c;
    x = veryLongNameA * veryLongNameB + veryLongNameC;
    x = veryLongNameA + veryLongNameB * veryLongNameC;
    x = veryVeryLongNameA * veryVeryLongNameB + veryVeryLongNameC;
    x = veryVeryLongNameA + veryVeryLongNameB * veryVeryLongNameC;
    // just to compare with the parentheses behaviour
    x = a * b / c;
    x = veryLongNameA * veryLongNameB / veryLongNameC;
    x = veryVeryLongNameA * veryVeryLongNameB / veryVeryLongNameC;
    
    // Current Format
    x = a * b + c;
    x = a + b * c;
    x =
        veryLongNameA *
        veryLongNameB +
        veryLongNameC;
    x =
        veryLongNameA +
        veryLongNameB *
        veryLongNameC;
    x =
        veryVeryLongNameA *
        veryVeryLongNameB +
        veryVeryLongNameC;
    x =
        veryVeryLongNameA +
        veryVeryLongNameB *
        veryVeryLongNameC;
    // just to compare with the parentheses behaviour
    x = (a * b) / c;
    x =
        (veryLongNameA * veryLongNameB) /
        veryLongNameC;
    x =
        (veryVeryLongNameA *
            veryVeryLongNameB) /
        veryVeryLongNameC;
    
    // New Format
    x = a * b + c;
    x = a + b * c;
    x =
        veryLongNameA * veryLongNameB +
        veryLongNameC;
    x =
        veryLongNameA +
        veryLongNameB * veryLongNameC;
    x =
        veryVeryLongNameA *
            veryVeryLongNameB +
        veryVeryLongNameC;
    x =
        veryVeryLongNameA +
        veryVeryLongNameB *
            veryVeryLongNameC;
    // just to compare with the parentheses behaviour
    x = (a * b) / c;
    x =
        (veryLongNameA * veryLongNameB) /
        veryLongNameC;
    x =
        (veryVeryLongNameA *
            veryVeryLongNameB) /
        veryVeryLongNameC;

    Since all of these behaviours follow the same rule there was a big work in standardising the printers in such a way that we can view what are the levels bellow at each level.

    • there is a new parentheses case added.

      // Original
      a == b == c;
      a == b != c;
      a != b == c;
      a != b != c;
      
      // Formatted
      (a == b) == c;
      (a == b) != c;
      (a != b) == c;
      (a != b) != c;

    Tis case was added to the tests that compile and compare the byte code.

    • A simple binary operation will be grouped when is the only operation in an assignment
      // Original
      a = veryLongCall(parameter) + b;
      
      // Current Format
      a =
          veryLongCall(
              parameter
          ) +
          b;
      
      // New Format
      a =
          veryLongCall(
              parameter
          ) + b;
  2. adding support for experimentalOperatorPosition

⚠️ Since there is a huge amount of new test scenarios, I added them in 1 commit to the main branch in which we already trust the way we where formatting, and we can review the format changes directly in this PR

@Janther Janther requested a review from fvictorio February 17, 2025 03:27
@Janther Janther force-pushed the experimentalOperatorPosition branch from 1069c36 to c7d8568 Compare February 17, 2025 19:28
@Janther Janther force-pushed the experimentalOperatorPosition branch 2 times, most recently from 4c7910c to e74e728 Compare March 4, 2025 23:09
@Janther Janther force-pushed the experimentalOperatorPosition branch 11 times, most recently from 2873d58 to d834976 Compare March 11, 2025 03:59
@Janther Janther force-pushed the experimentalOperatorPosition branch from d834976 to 99e0d88 Compare March 14, 2025 02:15
Janther added 2 commits March 15, 2025 13:21
…equality` when it's a child of `IfStatement`, `ForStatement`, or `WhileStatement` so there's no need for separate behaviours
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant