@@ -670,18 +670,22 @@ def self.visit(node, tokens)
670670 # (nil | Array[untyped]) posts
671671 # ) -> AryPtn
672672 def on_aryptn ( constant , requireds , rest , posts )
673- parts = [ constant , *requireds , rest , *posts ] . compact
673+ lbracket = find_token ( LBracket )
674+ lbracket ||= find_token ( LParen ) if constant
674675
675- # If there aren't any parts (no constant, no positional arguments), then
676- # we're matching an empty array. In this case, we're going to look for the
677- # left and right brackets explicitly. Otherwise, we'll just use the bounds
678- # of the various parts.
679- location =
680- if parts . empty?
681- consume_token ( LBracket ) . location . to ( consume_token ( RBracket ) . location )
682- else
683- parts [ 0 ] . location . to ( parts [ -1 ] . location )
684- end
676+ rbracket = find_token ( RBracket )
677+ rbracket ||= find_token ( RParen ) if constant
678+
679+ parts = [ constant , lbracket , *requireds , rest , *posts , rbracket ] . compact
680+
681+ # The location is going to be determined by the first part to the last
682+ # part. This includes potential brackets.
683+ location = parts [ 0 ] . location . to ( parts [ -1 ] . location )
684+
685+ # Now that we have the location calculated, we can remove the brackets
686+ # from the list of tokens.
687+ tokens . delete ( lbracket ) if lbracket
688+ tokens . delete ( rbracket ) if rbracket
685689
686690 # If there is a plain *, then we're going to fix up the location of it
687691 # here because it currently doesn't have anything to use for its precise
@@ -2353,23 +2357,30 @@ def on_method_add_arg(call, arguments)
23532357
23542358 # :call-seq:
23552359 # on_method_add_block: (
2356- # (Call | Command | CommandCall) call,
2360+ # (Break | Call | Command | CommandCall) call,
23572361 # Block block
2358- # ) -> MethodAddBlock
2362+ # ) -> Break | MethodAddBlock
23592363 def on_method_add_block ( call , block )
23602364 location = call . location . to ( block . location )
23612365
23622366 case call
2367+ when Break
2368+ parts = call . arguments . parts
2369+
2370+ node = parts . pop
2371+ copied =
2372+ node . copy ( block : block , location : node . location . to ( block . location ) )
2373+
2374+ copied . comments . concat ( call . comments )
2375+ parts << copied
2376+
2377+ call . copy ( location : location )
23632378 when Command , CommandCall
23642379 node = call . copy ( block : block , location : location )
23652380 node . comments . concat ( call . comments )
23662381 node
23672382 else
2368- MethodAddBlock . new (
2369- call : call ,
2370- block : block ,
2371- location : call . location . to ( block . location )
2372- )
2383+ MethodAddBlock . new ( call : call , block : block , location : location )
23732384 end
23742385 end
23752386
@@ -2592,19 +2603,40 @@ def on_params(
25922603 # have a `nil` for the value instead of a `false`.
25932604 keywords &.map! { |( key , value ) | [ key , value || nil ] }
25942605
2595- parts = [
2596- *requireds ,
2597- *optionals &.flatten ( 1 ) ,
2598- rest ,
2599- *posts ,
2600- *keywords &.flatten ( 1 ) ,
2601- ( keyword_rest if keyword_rest != :nil ) ,
2602- ( block if block != :& )
2603- ] . compact
2606+ # Here we're going to build up a list of all of the params so that we can
2607+ # determine our location information.
2608+ parts = [ ]
2609+
2610+ requireds &.each { |required | parts << required . location }
2611+ optionals &.each do |( key , value ) |
2612+ parts << key . location
2613+ parts << value . location if value
2614+ end
2615+
2616+ parts << rest . location if rest
2617+ posts &.each { |post | parts << post . location }
2618+
2619+ keywords &.each do |( key , value ) |
2620+ parts << key . location
2621+ parts << value . location if value
2622+ end
2623+
2624+ if keyword_rest == :nil
2625+ # When we get a :nil here, it means that we have **nil syntax, which
2626+ # means this set of parameters accepts no more keyword arguments. In
2627+ # this case we need to go and find the location of these two tokens.
2628+ operator = consume_operator ( :** )
2629+ parts << operator . location . to ( consume_keyword ( :nil ) . location )
2630+ elsif keyword_rest
2631+ parts << keyword_rest . location
2632+ end
2633+
2634+ parts << block . location if block && block != :&
2635+ parts = parts . compact
26042636
26052637 location =
26062638 if parts . any?
2607- parts [ 0 ] . location . to ( parts [ -1 ] . location )
2639+ parts [ 0 ] . to ( parts [ -1 ] )
26082640 else
26092641 Location . fixed ( line : lineno , char : char_pos , column : current_column )
26102642 end
0 commit comments