Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Avoid converting if/else into long ternary #243

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

Closed
gschlager opened this issue Jan 3, 2023 · 4 comments
Closed

Avoid converting if/else into long ternary #243

gschlager opened this issue Jan 3, 2023 · 4 comments

Comments

@gschlager
Copy link

gschlager commented Jan 3, 2023

The formatter should avoid converting if/else into ternary if the the resulting line would be too long. I think stree should disregard the configured print-width and use regular if/else when the line length of the ternary exceeds ~60 chars.

Original (easy to read):

if fits_into_octal?(value, length)
  to_octal(value, length)
else
  to_base256(value, length)
end

Formatted with stree (harder to read):

fits_into_octal?(value, length) ? to_octal(value, length) : to_base256(value, length)

(Tested with print-width of 100)

@kddnewton
Copy link
Member

Sorry to be closing issues of yours left and right here, but I'm closing it for the same reason as here: #245 (comment).

@davidtaylorhq
Copy link
Contributor

For anyone that comes across this issue in future - for Discourse we've created this simple plugin which disables the automatic conversion of if...else to ternary: https://github.com/discourse/syntax_tree-disable_ternary

@kddnewton
Copy link
Member

@davidtaylorhq I'd be happy to maintain that in Syntax Tree if you wanted to open a PR. That way I wouldn't accidentally break that if I move a module around.

@davidtaylorhq
Copy link
Contributor

That would be great, thanks @kddnewton. I refactored the plugin to work similarly to the bundled quote/comma plugins and opened #250

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

3 participants