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

Wrong formatting of object literal inside ternary operator when using tabs #12517

Open
FlameWolf opened this issue Mar 23, 2022 · 6 comments
Open

Comments

@FlameWolf
Copy link

Prettier 2.6.0
Playground link

# Options (if any):
--use-tabs

Input:

const flag = true;
const response = flag ? {
    name: "Jane Doe",
    age: 32
  } : {
    name: "John Doe",
    age: 23
  };

Output:

const flag = true;
const response = flag
	? {
			name: "Jane Doe",
			age: 32
	  }
	: {
			name: "John Doe",
			age: 23
	  };

Expected behavior:

  • The closing curly braces should not have leading spaces
  • The code inside the curly braces should have one fewer tabs of indentation
const flag = true;
const response = flag
	? {
		name: "Jane Doe",
		age: 32
	}
	: {
		name: "John Doe",
		age: 23
	};
@importantaffairs
Copy link

Thanks for opening this. I'm also having this issue on version 2.6.1

@aradalvand
Copy link

Same issue here on v2.7.1. No updates still?!

@james-lawrence
Copy link

piling on with another example:

	maybe<T>(key: string, lookup: () => Promise<T>, ...options: option[]): Promise<T> {
		const found = this.get<T>(key)
		return found ? Promise.resolve(found) : lookup().then((v: T) => {
			this.set(key, v, ...options)
			return v
		})
	}

@dylemma
Copy link

dylemma commented Dec 21, 2023

I'm seeing similar behavior in 3.1.1, but but my closing } are not aligned to the opening { like in OP's example; they're just indented by an extra tab. I think I'd prefer aligning the } with spaces over this:

playground

Input

someCondition ? 
	{ 
		a: 1, 
		b: 2
	} : { 
		c: 3
	}

Output

someCondition
	? {
			a: 1,
			b: 2,
		}
	: {
			c: 3,
		};

It also seems to over-indent the closing syntax for functions when they appear in a ternary: playground

@dylemma
Copy link

dylemma commented Dec 21, 2023

I think the correct behavior here would be to align the { and } with each other via spaces after the indentation tabs. And for the "middle" lines I think you would probably want three spaces followed by a tab, e.g.

someCondition
	? {
	  	a: 1,
	  	b: 1,
	  }
	: {
	  	c: 3,
	  }

Admittedly it feels weird for a line to contain \t \t but this approach results in something the should look "right" no matter what the viewer's tab-width is configured to, even for small widths like 1 or 2 spaces.

@thisjt
Copy link

thisjt commented May 17, 2024

I think the correct behavior here would be to align the { and } with each other via spaces after the indentation tabs. And for the "middle" lines I think you would probably want three spaces followed by a tab, e.g.

someCondition
	? {
	  	a: 1,
	  	b: 1,
	  }
	: {
	  	c: 3,
	  }

Admittedly it feels weird for a line to contain \t \t but this approach results in something the should look "right" no matter what the viewer's tab-width is configured to, even for small widths like 1 or 2 spaces.

Sadly this does not work if useTabs are set to true as prettier removes the spaces and reapplies the incorrect tabbing format.

I'm surprised that this has been an issue since 2018 yet there has not been any updates or attempts with fixing this bug.

#4203

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

No branches or pull requests

6 participants