From 8f8b47aeb7d205fecffafabe869cecde43f2b799 Mon Sep 17 00:00:00 2001 From: Wei Zhe Heng Date: Tue, 25 Oct 2022 11:01:29 +0800 Subject: [PATCH] Break and return false in forced_brace_bounds? when Parent is a Binary --- lib/syntax_tree/node.rb | 2 +- lib/syntax_tree/parser.rb | 2 +- test/fixtures/binary.rb | 14 ++++++++++++++ test/fixtures/if.rb | 21 +++++++++++++++++++++ test/fixtures/if_mod.rb | 21 +++++++++++++++++++++ test/fixtures/ifop.rb | 21 +++++++++++++++++++++ test/fixtures/unless.rb | 21 +++++++++++++++++++++ test/fixtures/unless_mod.rb | 21 +++++++++++++++++++++ test/fixtures/until.rb | 21 +++++++++++++++++++++ test/fixtures/until_mod.rb | 21 +++++++++++++++++++++ test/fixtures/while.rb | 21 +++++++++++++++++++++ test/fixtures/while_mod.rb | 21 +++++++++++++++++++++ 12 files changed, 205 insertions(+), 2 deletions(-) diff --git a/lib/syntax_tree/node.rb b/lib/syntax_tree/node.rb index aa133b7f..e2d135b4 100644 --- a/lib/syntax_tree/node.rb +++ b/lib/syntax_tree/node.rb @@ -2068,7 +2068,7 @@ def forced_brace_bounds?(q) previous = nil q.parents.any? do |parent| case parent - when Paren, Statements + when Paren, Statements, Binary # If we hit certain breakpoints then we know we're safe. return false when If, IfMod, IfOp, Unless, UnlessMod, While, WhileMod, Until, diff --git a/lib/syntax_tree/parser.rb b/lib/syntax_tree/parser.rb index 61a7ca57..0744fb63 100644 --- a/lib/syntax_tree/parser.rb +++ b/lib/syntax_tree/parser.rb @@ -2625,7 +2625,7 @@ def on_paren(contents) # If we encounter a parse error, just immediately bail out so that our # runner can catch it. - def on_parse_error(error, *) + def on_parse_error(error) raise ParseError.new(error, lineno, column) end alias on_alias_error on_parse_error diff --git a/test/fixtures/binary.rb b/test/fixtures/binary.rb index f8833cdc..8d1ff483 100644 --- a/test/fixtures/binary.rb +++ b/test/fixtures/binary.rb @@ -9,3 +9,17 @@ - foo * barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr +% +foooooooooooooooooooooo || barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } +- +foooooooooooooooooooooo || + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? do |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + end +% +foooooooooooooooooooooo && barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } +- +foooooooooooooooooooooo && + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? do |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + end diff --git a/test/fixtures/if.rb b/test/fixtures/if.rb index cfd6a882..bf615969 100644 --- a/test/fixtures/if.rb +++ b/test/fixtures/if.rb @@ -67,3 +67,24 @@ if true # comment1 # comment2 end +% +if foooooooooooooooooooooo || barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } + something +end +- +if foooooooooooooooooooooo || + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? do |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + end + something +end +% +if barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } + something +end +- +if barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + } + something +end diff --git a/test/fixtures/if_mod.rb b/test/fixtures/if_mod.rb index ea80e623..5aff7a3c 100644 --- a/test/fixtures/if_mod.rb +++ b/test/fixtures/if_mod.rb @@ -13,3 +13,24 @@ - foo = barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr if foo +% +foo = barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr if foooooooooooooooooooooo || barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } +- +foo = + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr if foooooooooooooooooooooo || + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? do |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + end +% +foo = bar if foooooooooooooooooooooo || barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } +- +foo = bar if foooooooooooooooooooooo || + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? do |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + end +% +foo = bar if barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } +- +foo = bar if barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz +} diff --git a/test/fixtures/ifop.rb b/test/fixtures/ifop.rb index e56eb987..20ff42a1 100644 --- a/test/fixtures/ifop.rb +++ b/test/fixtures/ifop.rb @@ -16,3 +16,24 @@ foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo ? break : baz +% +barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } ? bar : baz +- +if barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + } + bar +else + baz +end +% +fooooooooooooooo || barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } ? bar : baz +- +if fooooooooooooooo || + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? do |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + end + bar +else + baz +end diff --git a/test/fixtures/unless.rb b/test/fixtures/unless.rb index 2d5038c1..7687b813 100644 --- a/test/fixtures/unless.rb +++ b/test/fixtures/unless.rb @@ -36,3 +36,24 @@ unless true # comment1 # comment2 end +% +unless foooooooooooooooooooooo || barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } + something +end +- +unless foooooooooooooooooooooo || + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? do |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + end + something +end +% +unless barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } + something +end +- +unless barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + } + something +end diff --git a/test/fixtures/unless_mod.rb b/test/fixtures/unless_mod.rb index e2dbb764..97e57866 100644 --- a/test/fixtures/unless_mod.rb +++ b/test/fixtures/unless_mod.rb @@ -13,3 +13,24 @@ - foo = barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr unless foo +% +foo = barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr unless foooooooooooooooooooooo || barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } +- +foo = + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr unless foooooooooooooooooooooo || + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? do |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + end +% +foo = bar unless foooooooooooooooooooooo || barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } +- +foo = bar unless foooooooooooooooooooooo || + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? do |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + end +% +foo = bar unless barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } +- +foo = bar unless barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz +} diff --git a/test/fixtures/until.rb b/test/fixtures/until.rb index f3ef5202..55160919 100644 --- a/test/fixtures/until.rb +++ b/test/fixtures/until.rb @@ -27,3 +27,24 @@ until true # comment1 # comment2 end +% +until foooooooooooooooooooooo || barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } + something +end +- +until foooooooooooooooooooooo || + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? do |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + end + something +end +% +until barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } + something +end +- +until barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + } + something +end diff --git a/test/fixtures/until_mod.rb b/test/fixtures/until_mod.rb index 869cc1e7..537faf46 100644 --- a/test/fixtures/until_mod.rb +++ b/test/fixtures/until_mod.rb @@ -13,3 +13,24 @@ - foo = barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr until foo +% +foo = barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr until foooooooooooooooooooooo || barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } +- +foo = + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr until foooooooooooooooooooooo || + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? do |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + end +% +foo = bar until foooooooooooooooooooooo || barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } +- +foo = bar until foooooooooooooooooooooo || + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? do |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + end +% +foo = bar until barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } +- +foo = bar until barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz +} diff --git a/test/fixtures/while.rb b/test/fixtures/while.rb index 9415135a..64b89c5d 100644 --- a/test/fixtures/while.rb +++ b/test/fixtures/while.rb @@ -27,3 +27,24 @@ while true # comment1 # comment2 end +% +while foooooooooooooooooooooo || barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } + something +end +- +while foooooooooooooooooooooo || + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? do |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + end + something +end +% +while barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } + something +end +- +while barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + } + something +end diff --git a/test/fixtures/while_mod.rb b/test/fixtures/while_mod.rb index 18a07b4c..5eba915c 100644 --- a/test/fixtures/while_mod.rb +++ b/test/fixtures/while_mod.rb @@ -13,3 +13,24 @@ - foo = barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr while foo +% +foo = barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr while foooooooooooooooooooooo || barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } +- +foo = + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr while foooooooooooooooooooooo || + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? do |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + end +% +foo = bar while foooooooooooooooooooooo || barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } +- +foo = bar while foooooooooooooooooooooo || + barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? do |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz + end +% +foo = bar while barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz } +- +foo = bar while barrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr.any? { |bar| + bazzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz +}