Skip to content

Disallow turning a conditional into a ternary if there's a Not node without parentheses in the predicate #72

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/syntax_tree/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5192,7 +5192,7 @@ def call(q, node)
else
# Otherwise, we're going to check the conditional for certain cases.
case node
in predicate: Assign | Command | CommandCall | MAssign | OpAssign
in predicate: Assign | Command | CommandCall | MAssign | Not | OpAssign
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this isn't quite everything, as we want to allow:

if not(a)
  b
else
  c
end

-->

not(a) ? b : c

false
in {
statements: { body: [truthy] },
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/if.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@
%
if foo {}
end
%
Copy link
Contributor Author

@bbuchalter bbuchalter May 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surprised this new fixture doesn't result in a test failure in CI. I get one locally:

  1) Failure:
SyntaxTree::FormattingTest#test_formatted_if_9 [/Users/brian.buchalter/workspace/syntax_tree/test/formatting_test.rb:9]:
--- expected
+++ actual
@@ -1,6 +1,2 @@
-"if not a
-  b
-else
-  c
-end
+"not a ? b : c
 "

if not a
b
else
c
end