Skip to content

[SCSS] Put unary boolean operator in front of expression when wrapping #16232

Description

@apfelbox

Prettier 3.1.1
Playground link

--parser scss

Input:

@if ($verylonglonglonglonglonglonglonglonglonglonglonglonglonglongvariabletoforceabreak and $a and not $b) {
    color: red;
}

Output:

@if (
  $verylonglonglonglonglonglonglonglonglonglonglonglonglonglongvariabletoforceabreak and
    $a and not
    $b
) {
  color: red;
}

Expected behavior:

@if (
  $verylonglonglonglonglonglonglonglonglonglonglonglonglonglongvariabletoforceabreak and
    $a and
    not $b
) {
  color: red;
}

Hi,

It is pretty confusing, that the unary not is placed in the line above instead of in front of the expression it is affecting.
That makes it really hard to read long if statements with multiple terms correctly.

Activity

  1. apfelbox commented on Apr 25, 2024

    @apfelbox
    Author

    Actually, looking deeper into it, the whole segmentation of the CNF seems pretty chopped up:

    Prettier 3.2.5
    Playground link

    --parser scss

    Input:

    @mixin fluid-prop($property, $min-vw, $max-vw, $min-value, $max-value) {
    	@if $property != null {
    		@if type-of($min-value) == 'number' and not unitless($min-value) {
    			#{$property}: $min-value; // Fallback for older browsers
    		}
    		@if type-of($min-vw) == 'number' and not unitless($min-value) and type-of($max-vw) == 'number' and not unitless($min-value) and type-of($min-value) == 'number' and not unitless($min-value) and type-of($max-value) == 'number' and not unitless($min-value) and unit($min-vw) == unit($max-vw) and unit($min-vw) == unit($min-value) and unit($min-vw) == unit($max-value) {
    			$factor: math.div(1, $max-vw - $min-vw) * ($max-value - $min-value);
    			$calc-value: unquote("#{ $min-value - ($min-vw * $factor) } + #{ 100vw * $factor }");
    
    			#{$property}: clamp(#{ if($min-value > $max-value, $max-value, $min-value) }, #{ $calc-value }, #{ if($min-value > $max-value, $min-value, $max-value) });
    		}
    	}
    }

    Output:

    @mixin fluid-prop($property, $min-vw, $max-vw, $min-value, $max-value) {
      @if $property != null {
        @if type-of($min-value) == "number" and not unitless($min-value) {
          #{$property}: $min-value; // Fallback for older browsers
        }
        @if type-of($min-vw) ==
          "number" and not
          unitless($min-value) and
          type-of($max-vw) ==
          "number" and not
          unitless($min-value) and
          type-of($min-value) ==
          "number" and not
          unitless($min-value) and
          type-of($max-value) ==
          "number" and not
          unitless($min-value) and
          unit($min-vw) ==
          unit($max-vw) and
          unit($min-vw) ==
          unit($min-value) and
          unit($min-vw) ==
          unit($max-value)
        {
          $factor: math.div(1, $max-vw - $min-vw) * ($max-value - $min-value);
          $calc-value: unquote(
            "#{ $min-value - ($min-vw * $factor) } + #{ 100vw * $factor }"
          );
    
          #{$property}: clamp(
            #{if($min-value > $max-value, $max-value, $min-value)},
            #{$calc-value},
            #{if($min-value > $max-value, $min-value, $max-value)}
          );
        }
      }
    }
    

    Expected output:

    /* ... */
    @if type-of($min-vw) == "number" and 
          not unitless($min-value) and
          type-of($max-vw) == "number" and 
          not unitless($min-value) and
          type-of($min-value) == "number" and 
          not unitless($min-value) and
          type-of($max-value) == "number" and 
          not unitless($min-value) and
          unit($min-vw) == unit($max-vw) and
          unit($min-vw) == unit($min-value) and
          unit($min-vw) == unit($max-value)
        {
    /* ... */

    Why?

    This seems like a canonical way to write a conjunctive normal form, as every boolean segment is in one line, including their identity check or unary operators.

  2. guilhermetod commented on Apr 29, 2024

    @guilhermetod

    Given #3806 is also highly requested, i suggest going for another strategy:

    @if (
      $verylonglonglonglonglonglonglonglonglonglonglonglonglonglongvariabletoforceabreak
        and $a
        and not $b
    ) {
      color: red;
    }
  3. apfelbox commented on Apr 29, 2024

    @apfelbox
    Author

    @guilhermetod I would like your proposal even more. 👍

    Edit: one of the big advantages of your proposal is that you only have on changed line when adding a new term to the @if. Same argument as trailing commas, basically.

  4. added
    lang:css/scss/less
    on Apr 30, 2024
  5. g-plane commented on May 18, 2024

    @g-plane

    Would you like to give a try to Malva (another CSS formatter)? It supports both @apfelbox 's and @guilhermetod 's suggestions with configuration.

    Input

    @if ($verylonglonglonglonglonglonglonglonglonglonglonglonglonglongvariabletoforceabreak and $a and not $b) {
        color: red;
    }

    When operatorLinebreak is "after"

    It formats as @apfelbox suggests.

    @if (
      $verylonglonglonglonglonglonglonglonglonglonglonglonglonglongvariabletoforceabreak and
        $a and
        not $b
    ) {
      color: red;
    }

    playground

    When operatorLinebreak is "before"

    It formats as @guilhermetod suggests.

    @if (
      $verylonglonglonglonglonglonglonglonglonglonglonglonglonglongvariabletoforceabreak
        and $a
        and not $b
    ) {
      color: red;
    }

    playground

    Complex input

    It also works with #16232 (comment) . Result:

    @mixin fluid-prop($property, $min-vw, $max-vw, $min-value, $max-value) {
      @if $property != null {
        @if type-of($min-value) == "number" and not unitless($min-value) {
          #{$property}: $min-value; // Fallback for older browsers
        }
        @if type-of($min-vw) == "number" and not unitless($min-value) and
          type-of($max-vw) == "number" and
          not unitless($min-value) and
          type-of($min-value) == "number" and
          not unitless($min-value) and
          type-of($max-value) == "number" and
          not unitless($min-value) and
          unit($min-vw) == unit($max-vw) and
          unit($min-vw) == unit($min-value) and
          unit($min-vw) == unit($max-value) {
          $factor: math.div(1, $max-vw - $min-vw) * ($max-value - $min-value);
          $calc-value: unquote(
            "#{$min-value - ($min-vw * $factor)} + #{100vw * $factor}"
          );
    
          #{$property}: clamp(
            #{if($min-value > $max-value, $max-value, $min-value)},
            #{$calc-value},
            #{if($min-value > $max-value, $min-value, $max-value)}
          );
        }
      }
    }

    playground

  6. added theissue type on Mar 3, 2025
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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions