@@ -2222,6 +2222,7 @@ def initialize(node)
2222
2222
2223
2223
def format ( q )
2224
2224
children = [ node ]
2225
+ threshold = 3
2225
2226
2226
2227
# First, walk down the chain until we get to the point where we're not
2227
2228
# longer at a chainable node.
@@ -2238,7 +2239,27 @@ def format(q)
2238
2239
end
2239
2240
end
2240
2241
2241
- if children . length > 2
2242
+ # Here, we have very specialized behavior where if we're within a sig
2243
+ # block, then we're going to assume we're creating a Sorbet type
2244
+ # signature. In that case, we really want the threshold to be lowered so
2245
+ # that we create method chains off of any two method calls within the
2246
+ # block. For more details, see
2247
+ # https://github.com/prettier/plugin-ruby/issues/863.
2248
+ parents = q . parents . take ( 4 )
2249
+ if parent = parents [ 2 ]
2250
+ # If we're at a do_block, then we want to go one more level up. This is
2251
+ # because do blocks have BodyStmt nodes instead of just Statements
2252
+ # nodes.
2253
+ parent = parents [ 3 ] if parent . is_a? ( DoBlock )
2254
+
2255
+ case parent
2256
+ in MethodAddBlock [ call : FCall [ value : { value : "sig" } ] ]
2257
+ threshold = 2
2258
+ else
2259
+ end
2260
+ end
2261
+
2262
+ if children . length >= threshold
2242
2263
q . group { q . if_break { format_chain ( q , children ) } . if_flat { node . format_contents ( q ) } }
2243
2264
else
2244
2265
node . format_contents ( q )
@@ -2329,18 +2350,20 @@ def self.chained?(node)
2329
2350
# For certain nodes, we want to attach directly to the end and don't
2330
2351
# want to indent the first call. So we'll pop off the first children and
2331
2352
# format it separately here.
2332
- def attach_directly? ( child )
2353
+ def attach_directly? ( node )
2333
2354
[ ArrayLiteral , HashLiteral , Heredoc , If , Unless , XStringLiteral ]
2334
- . include? ( child . receiver . class )
2355
+ . include? ( node . receiver . class )
2335
2356
end
2336
2357
2337
2358
def format_child ( q , child , skip_operator : false , skip_attached : false )
2338
2359
# First, format the actual contents of the child.
2339
2360
case child
2340
2361
in Call
2341
- q . format ( CallOperatorFormatter . new ( child . operator ) ) unless skip_operator
2342
- q . format ( child . message ) if child . message != :call
2343
- child . format_arguments ( q ) unless skip_attached
2362
+ q . group do
2363
+ q . format ( CallOperatorFormatter . new ( child . operator ) ) unless skip_operator
2364
+ q . format ( child . message ) if child . message != :call
2365
+ child . format_arguments ( q ) unless skip_attached
2366
+ end
2344
2367
in MethodAddBlock
2345
2368
q . format ( child . block ) unless skip_attached
2346
2369
end
0 commit comments