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

Misaligned tranformation for logical operators and and or #174

Closed
dhruvmanila opened this issue Jun 4, 2021 · 2 comments
Closed

Misaligned tranformation for logical operators and and or #174

dhruvmanila opened this issue Jun 4, 2021 · 2 comments

Comments

@dhruvmanila
Copy link

For the following config:

column_width = 80
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 2
quote_style = "AutoPreferDouble"

The output for the following line:

local long_variable_name = some_condition("testing") and condition_was_true(true) or condition_was_false(false)

is:

local long_variable_name = some_condition("testing")
    and condition_was_true(true)
  or condition_was_false(false)

The and and or is not lined up. Is this the expected way or is it a bug?

@JohnnyMorganz
Copy link
Owner

This is actually expected. The and operator takes a higher precedence than the or operator, which is why it is indented one further to display this. [Similar example using prettier]

Lua has no real ternary operator here, so they don't have the same precedence. This actually leads to another thing I wanted to implement that prettier does, but haven't yet: adding parentheses to disambiguate associative. The expression is actually evaluated as (foo and bar) or baz, and the parentheses could help to show this. For example if you wrote foo and false or true to replicate a ternary, and foo was truths, this would actually evaluated to true no matter what happens!
I haven't decided to add these parentheses yet due to how common the "ternary pattern" is, so it may be quite invasive

@dhruvmanila
Copy link
Author

Ah, right understood. Thanks for the explanation.

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

2 participants