biome icon indicating copy to clipboard operation
biome copied to clipboard

📝 Multiple ternary operators are double indented after the second one

Open In3luki opened this issue 1 year ago • 12 comments

Environment information

CLI:
  Version:                      1.3.3-nightly.38797b7
  Color support:                true

Platform:
  CPU Architecture:             x86_64
  OS:                           windows

Environment:
  BIOME_LOG_DIR:                unset
  NO_COLOR:                     unset
  TERM:                         "xterm-256color"
  JS_RUNTIME_VERSION:           "v18.16.1"
  JS_RUNTIME_NAME:              "node"
  NODE_PACKAGE_MANAGER:         "npm/9.8.1"

Biome Configuration:
  Status:                       Loaded successfully
  Formatter disabled:           false
  Linter disabled:              false
  Organize imports disabled:    false
  VCS disabled:                 true

Workspace:
  Open Documents:               0

Configuration

No response

Playground link

https://biomejs.dev/playground?lineWidth=120&indentStyle=space&indentWidth=4&code=SABhAG4AZABsAGUAYgBhAHIAcwAuAHIAZQBnAGkAcwB0AGUAcgBIAGUAbABwAGUAcgAoACIAaQBuAGMAbAB1AGQAZQBzACIALAAgACgAYQByAHIAOgAgAHUAbgBrAG4AbwB3AG4ALAAgAGUAbABlAG0AZQBuAHQAOgAgAHUAbgBrAG4AbwB3AG4AKQA6ACAAYgBvAG8AbABlAGEAbgAgAD0APgAgAHsACgAgACAAIAAgAHIAZQB0AHUAcgBuACAAQQByAHIAYQB5AC4AaQBzAEEAcgByAGEAeQAoAGEAcgByACkACgAgACAAIAAgACAAIAAgACAAPwAgAGEAcgByAC4AaQBuAGMAbAB1AGQAZQBzACgAZQBsAGUAbQBlAG4AdAApAAoAIAAgACAAIAAgACAAIAAgADoAIABhAHIAcgAgAGkAbgBzAHQAYQBuAGMAZQBvAGYAIABTAGUAdAAKACAAIAAgACAAIAAgACAAIAAgACAAPwAgAGEAcgByAC4AaABhAHMAKABlAGwAZQBtAGUAbgB0ACkACgAgACAAIAAgACAAIAAgACAAIAAgADoAIABhAHIAcgAgACYAJgAgAHQAeQBwAGUAbwBmACAAYQByAHIAIAA9AD0APQAgACIAbwBiAGoAZQBjAHQAIgAKACAAIAAgACAAIAAgACAAIAAgACAAIAAgAD8AIAAoAHQAeQBwAGUAbwBmACAAZQBsAGUAbQBlAG4AdAAgAD0APQA9ACAAIgBuAHUAbQBiAGUAcgAiACAAfAB8ACAAdAB5AHAAZQBvAGYAIABlAGwAZQBtAGUAbgB0ACAAPQA9AD0AIAAiAHMAdAByAGkAbgBnACIAKQAgACYAJgAgAGUAbABlAG0AZQBuAHQAIABpAG4AIABhAHIAcgAKACAAIAAgACAAIAAgACAAIAAgACAAIAAgADoAIABmAGEAbABzAGUACgB9ACkAOwAKAA%3D%3D

Code of Conduct

  • [X] I agree to follow Biome's Code of Conduct

In3luki avatar Nov 22 '23 18:11 In3luki

The original playground input was formatted with prettier 3.1.0

In3luki avatar Nov 22 '23 18:11 In3luki

Simplifying this down to the base case: Playground Link

longConditionExpression1
  ? consequent1
  : longConditionExpression2
    ? consequent2
    : longConditionExpression3
      ? consequent3
      : consequent4

// Prettier 3.1 output
longConditionExpression1
	? consequent1
	: longConditionExpression2
	  ? consequent2
	  : longConditionExpression3
	    ? consequent3
	    : consequent4;

// Biome output
longConditionExpression1
	? consequent1
	: longConditionExpression2
	  ? consequent2
	  : longConditionExpression3
		  ? consequent3
		  : consequent4;

This happens regardless of indentation settings (spaces or tabs, or width).

It seems like the reason is that the formatter adds both a tab-equivalent indent along with the alignment spaces on the nested ternaries.

faultyserver avatar Dec 02 '23 23:12 faultyserver

It breaks even worse with some content, in both Prettier and Biome:

Playground Link

DASPRiD avatar Apr 12 '24 12:04 DASPRiD

It seems that the issue was recently fixed.

Conaclos avatar Apr 29 '24 15:04 Conaclos

@Conaclos It still looks broken, looking at my posted Playground link.

DASPRiD avatar Apr 29 '24 15:04 DASPRiD

I don't understand where the problem is? We match Prettier here? Do you complain about the new way of formatting ternaries since Prettier 3.1?

Conaclos avatar Apr 29 '24 16:04 Conaclos

To me it looks like a bug in Prettier. In the given example, the property is indented twice, and the closing bracket is indendet once. I would have expected the closing bracket to be on the same level as the opening bracket, and the property indented once with the opening bracket line.

DASPRiD avatar Apr 29 '24 16:04 DASPRiD

To me it looks like a bug in Prettier. In the given example, the property is indented twice, and the closing bracket is indendet once. I would have expected the closing bracket to be on the same level as the opening bracket, and the property indented once with the opening bracket line.

Are you referring to this example? Which property? The closing bracket is the closing curly brace? I really don't get what you talk about...

Conaclos avatar Apr 29 '24 16:04 Conaclos

No, I was referring to this example.

DASPRiD avatar Apr 29 '24 16:04 DASPRiD

Ok I see. I am reopening the issue. Thanks for your help :)

Conaclos avatar Apr 29 '24 17:04 Conaclos

Is there a Prettier issue we can track @DASPRiD? Otherwise, we match Prettier, which is NOT a bug

ematipico avatar May 04 '24 12:05 ematipico

@ematipico A quick search yielded a few related ones:

  • https://github.com/prettier/prettier/issues/4203
  • https://github.com/prettier/prettier/issues/12517
  • https://github.com/prettier/prettier/issues/5811

DASPRiD avatar May 04 '24 13:05 DASPRiD