@@ -359,7 +359,8 @@ class Alias < Node
359359 # Formats an argument to the alias keyword. For symbol literals it uses the
360360 # value of the symbol directly to look like bare words.
361361 class AliasArgumentFormatter
362- # [Backref | DynaSymbol | GVar | SymbolLiteral] the argument being passed to alias
362+ # [Backref | DynaSymbol | GVar | SymbolLiteral] the argument being passed
363+ # to alias
363364 attr_reader :argument
364365
365366 def initialize ( argument )
@@ -2245,14 +2246,26 @@ def format(q)
22452246 when Call
22462247 case ( receiver = child . receiver )
22472248 when Call
2248- children << receiver
2249+ if receiver . receiver . nil?
2250+ break
2251+ else
2252+ children << receiver
2253+ end
22492254 when MethodAddBlock
2250- receiver . call . is_a? ( Call ) ? children << receiver : break
2255+ if receiver . call . is_a? ( Call ) && !receiver . call . receiver . nil?
2256+ children << receiver
2257+ else
2258+ break
2259+ end
22512260 else
22522261 break
22532262 end
22542263 when MethodAddBlock
2255- child . call . is_a? ( Call ) ? children << child . call : break
2264+ if child . call . is_a? ( Call ) && !child . call . receiver . nil?
2265+ children << child . call
2266+ else
2267+ break
2268+ end
22562269 else
22572270 break
22582271 end
@@ -2271,7 +2284,8 @@ def format(q)
22712284 # of just Statements nodes.
22722285 parent = parents [ 3 ] if parent . is_a? ( Block ) && parent . keywords?
22732286
2274- if parent . is_a? ( MethodAddBlock ) && parent . call . is_a? ( Call ) && parent . call . message . value == "sig"
2287+ if parent . is_a? ( MethodAddBlock ) && parent . call . is_a? ( Call ) &&
2288+ parent . call . message . value == "sig"
22752289 threshold = 2
22762290 end
22772291 end
@@ -2300,7 +2314,7 @@ def format_chain(q, children)
23002314 # formatter so it's as if we had descending normally into them. This is
23012315 # necessary so they can check their parents as normal.
23022316 q . stack . concat ( children )
2303- q . format ( children . last . receiver )
2317+ q . format ( children . last . receiver ) if children . last . receiver
23042318
23052319 q . group do
23062320 if attach_directly? ( children . last )
@@ -2343,7 +2357,8 @@ def format_chain(q, children)
23432357 # If the parent call node has a comment on the message then we need
23442358 # to print the operator trailing in order to keep it working.
23452359 last_child = children . last
2346- if last_child . is_a? ( Call ) && last_child . message . comments . any?
2360+ if last_child . is_a? ( Call ) && last_child . message . comments . any? &&
2361+ last_child . operator
23472362 q . format ( CallOperatorFormatter . new ( last_child . operator ) )
23482363 skip_operator = true
23492364 else
@@ -2372,9 +2387,9 @@ def self.chained?(node)
23722387
23732388 case node
23742389 when Call
2375- true
2390+ ! node . receiver . nil?
23762391 when MethodAddBlock
2377- node . call . is_a? ( Call )
2392+ node . call . is_a? ( Call ) && ! node . call . receiver . nil?
23782393 else
23792394 false
23802395 end
@@ -2405,7 +2420,7 @@ def format_child(
24052420 case child
24062421 when Call
24072422 q . group do
2408- unless skip_operator
2423+ if ! skip_operator && child . operator
24092424 q . format ( CallOperatorFormatter . new ( child . operator ) )
24102425 end
24112426 q . format ( child . message ) if child . message != :call
@@ -2495,7 +2510,8 @@ def format(q)
24952510 # If we're at the top of a call chain, then we're going to do some
24962511 # specialized printing in case we can print it nicely. We _only_ do this
24972512 # at the top of the chain to avoid weird recursion issues.
2498- if CallChainFormatter . chained? ( receiver ) && !CallChainFormatter . chained? ( q . parent )
2513+ if CallChainFormatter . chained? ( receiver ) &&
2514+ !CallChainFormatter . chained? ( q . parent )
24992515 q . group do
25002516 q
25012517 . if_break { CallChainFormatter . new ( self ) . format ( q ) }
@@ -2507,11 +2523,13 @@ def format(q)
25072523 else
25082524 q . format ( message )
25092525
2510- if arguments . is_a? ( ArgParen ) && arguments . arguments . nil? && !message . is_a? ( Const )
2511- # If you're using an explicit set of parentheses on something that looks
2512- # like a constant, then we need to match that in order to maintain valid
2513- # Ruby. For example, you could do something like Foo(), on which we
2514- # would need to keep the parentheses to make it look like a method call.
2526+ if arguments . is_a? ( ArgParen ) && arguments . arguments . nil? &&
2527+ !message . is_a? ( Const )
2528+ # If you're using an explicit set of parentheses on something that
2529+ # looks like a constant, then we need to match that in order to
2530+ # maintain valid Ruby. For example, you could do something like Foo(),
2531+ # on which we would need to keep the parentheses to make it look like
2532+ # a method call.
25152533 else
25162534 q . format ( arguments )
25172535 end
@@ -3726,7 +3744,13 @@ def child_nodes
37263744 alias deconstruct child_nodes
37273745
37283746 def deconstruct_keys ( _keys )
3729- { left : left , operator : operator , right : right , location : location , comments : comments }
3747+ {
3748+ left : left ,
3749+ operator : operator ,
3750+ right : right ,
3751+ location : location ,
3752+ comments : comments
3753+ }
37303754 end
37313755
37323756 def format ( q )
@@ -5133,7 +5157,11 @@ def format(q)
51335157 if ContainsAssignment . call ( statement ) || q . parent . is_a? ( In )
51345158 q . group { format_flat ( q ) }
51355159 else
5136- q . group { q . if_break { format_break ( q , force : false ) } . if_flat { format_flat ( q ) } }
5160+ q . group do
5161+ q
5162+ . if_break { format_break ( q , force : false ) }
5163+ . if_flat { format_flat ( q ) }
5164+ end
51375165 end
51385166 else
51395167 # If we can transform this node into a ternary, then we're going to
@@ -9063,7 +9091,8 @@ def format(q)
90639091 #
90649092 # foo = bar while foo
90659093 #
9066- if node . modifier? && ( statement = node . statements . body . first ) && ( statement . is_a? ( Begin ) || ContainsAssignment . call ( statement ) )
9094+ if node . modifier? && ( statement = node . statements . body . first ) &&
9095+ ( statement . is_a? ( Begin ) || ContainsAssignment . call ( statement ) )
90679096 q . format ( statement )
90689097 q . text ( " #{ keyword } " )
90699098 q . format ( node . predicate )
@@ -9466,9 +9495,7 @@ def format(q)
94669495 # last argument to the predicate is and endless range, then you are
94679496 # forced to use the "then" keyword to make it parse properly.
94689497 last = arguments . parts . last
9469- if last . is_a? ( RangeLiteral ) && !last . right
9470- q . text ( " then" )
9471- end
9498+ q . text ( " then" ) if last . is_a? ( RangeLiteral ) && !last . right
94729499 end
94739500 end
94749501
0 commit comments