Skip to content

CSS: Improve arithmetic operators #1811

Open

Description

@lydell

Goal: Print them more like JS. Flatten indentation and group operators. Also, space the operators (as mentioned in #2793): 1+1 → 1 + 1.

.Image {
  @media (max-width: $image-width-responsive) {
    padding-bottom:
      (
        100% *
        $image-height /
        ($image-width-responsive + $image-margin-responsive * 2)
      );
  }
}
.Image {
  @media (max-width: $image-width-responsive) {
    padding-bottom: (
      100% *
        $image-height / ($image-width-responsive + $image-margin-responsive * 2)
    );
  }
}

Compare to this JS:

{
  {
    padding_bottom = (
      100 *
        $image_height /($image_width_responsive + $image_margin_responsive * 2)
    );
  }
}
{
  {
    padding_bottom =
      100 *
      $image_height /
      ($image_width_responsive + $image_margin_responsive * 2);
  }
}

a {
  b {
    img {
      max-height:
        calc(
          100vh -
          #{var(--margin)} * 2 - #{var(--padding)} * 2 -
          #{$extra-space-for-scrollbar}
        );
    }
  }
}
a {
  b {
    img {
      max-height: calc(
        100vh -
          #{var(--margin)} *
          2 -
          #{var(--padding)} *
          2 -
          #{$extra-space-for-scrollbar}
      );
    }
  }
}

Compare to this JS:

{
  {
      max_height = calc(
        100 -
          $(car(__margin)) *
          2 -
          $(car(__padding)) *
          2 -
          $($extra_space_for_scrollbar)
        );
  }
}
{
  {
    max_height = calc(
      100 -
        $(car(__margin)) * 2 -
        $(car(__padding)) * 2 -
        $($extra_space_for_scrollbar)
    );
  }
}

Extracted from: #1805

Important comment by @vjeux:

Right now we only get a stream of tokens. We'd need to parse it to be able to reprint it properly. For this, we need to implement a small arithmetic parser and print it correctly. It shouldn't be too hard

Activity

  1. added
    lang:css/scss/less
    on Aug 17, 2017
  2. j-f1 commented on Jan 29, 2018

    @j-f1
    Member

    Here’s the current status of this:

    Prettier 1.10.2
    Playground link

    --parser scss
    --print-width 20

    Input:

    .Image {
      @media (max-width: $image-width-responsive) {
        padding-bottom:
          (
            100% *
            $image-height /
            ($image-width-responsive + $image-margin-responsive * 2)
          );
      }
    }
    

    Output:

    .Image {
      @media (max-width: $image-width-responsive) {
        padding-bottom: (
          100% *
            $image-height /
            (
              $image-width-responsive +
                $image-margin-responsive *
                2
            )
        );
      }
    }
    

    Is this good enough to close the issue?

  3. lydell commented on Jan 29, 2018

    @lydell
    MemberAuthor

    The other example does not look very good though. I exceeds the print width.

    Prettier 1.10.2
    Playground link

    --parser css

    Input:

    a {
      b {
        img {
          max-height:
            calc(
              100vh -
              #{var(--margin)} * 2 - #{var(--padding)} * 2 -
              #{$extra-space-for-scrollbar}
            );
        }
      }
    }

    Output:

    a {
      b {
        img {
          max-height: calc(
            100vh - #{var(--margin)} * 2 - #{var(--padding)} * 2 - #{$extra-space-for-scrollbar}
          );
        }
      }
    }
  4. lydell commented on Jan 29, 2018

    @lydell
    MemberAuthor

    And if you use a sane print width, the first example is not so pretty either:

    Prettier 1.10.2
    Playground link

    --parser css

    Input:

    .Image {
      @media (max-width: $image-width-responsive) {
        padding-bottom:
          (
            100% *
            $image-height /
            ($image-width-responsive + $image-margin-responsive * 2)
          );
      }
    }

    Output:

    .Image {
      @media (max-width: $image-width-responsive) {
        padding-bottom: (
          100% * $image-height / ($image-width-responsive + $image-margin-responsive *
                2)
        );
      }
    }
  5. j-f1 commented on Jan 29, 2018

    @j-f1
    Member

    if you use a sane print width

    Looks like I forgot to change this option after adding a playground example to another issue.

  6. added theissue type on Mar 3, 2025
  7. TomFryersMidsummer commented on Oct 9, 2025

    @TomFryersMidsummer

    Also, when experimentalOperatorPosition: start is set, if the lines do need breaking, then the operators ought to go at the beginnings of the lines, matching JS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    lang:css/scss/less

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions