@@ -670,18 +670,22 @@ def self.visit(node, tokens)
670
670
# (nil | Array[untyped]) posts
671
671
# ) -> AryPtn
672
672
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
674
675
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
685
689
686
690
# If there is a plain *, then we're going to fix up the location of it
687
691
# here because it currently doesn't have anything to use for its precise
@@ -2353,23 +2357,30 @@ def on_method_add_arg(call, arguments)
2353
2357
2354
2358
# :call-seq:
2355
2359
# on_method_add_block: (
2356
- # (Call | Command | CommandCall) call,
2360
+ # (Break | Call | Command | CommandCall) call,
2357
2361
# Block block
2358
- # ) -> MethodAddBlock
2362
+ # ) -> Break | MethodAddBlock
2359
2363
def on_method_add_block ( call , block )
2360
2364
location = call . location . to ( block . location )
2361
2365
2362
2366
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 )
2363
2378
when Command , CommandCall
2364
2379
node = call . copy ( block : block , location : location )
2365
2380
node . comments . concat ( call . comments )
2366
2381
node
2367
2382
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 )
2373
2384
end
2374
2385
end
2375
2386
@@ -2592,19 +2603,40 @@ def on_params(
2592
2603
# have a `nil` for the value instead of a `false`.
2593
2604
keywords &.map! { |( key , value ) | [ key , value || nil ] }
2594
2605
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
2604
2636
2605
2637
location =
2606
2638
if parts . any?
2607
- parts [ 0 ] . location . to ( parts [ -1 ] . location )
2639
+ parts [ 0 ] . to ( parts [ -1 ] )
2608
2640
else
2609
2641
Location . fixed ( line : lineno , char : char_pos , column : current_column )
2610
2642
end
0 commit comments