@@ -426,5 +426,85 @@ def test_variables_in_the_top_level
426426 assert_equal ( 1 , variable . definitions [ 0 ] . start_line )
427427 assert_equal ( 2 , variable . usages [ 0 ] . start_line )
428428 end
429+
430+ def test_aref_field
431+ tree = SyntaxTree . parse ( <<~RUBY )
432+ object = {}
433+ object["name"] = "something"
434+ RUBY
435+
436+ visitor = Collector . new
437+ visitor . visit ( tree )
438+
439+ assert_equal ( 0 , visitor . arguments . length )
440+ assert_equal ( 1 , visitor . variables . length )
441+
442+ variable = visitor . variables [ "object" ]
443+ assert_equal ( 1 , variable . definitions . length )
444+ assert_equal ( 1 , variable . usages . length )
445+
446+ assert_equal ( 1 , variable . definitions [ 0 ] . start_line )
447+ assert_equal ( 2 , variable . usages [ 0 ] . start_line )
448+ end
449+
450+ def test_aref_on_a_method_call
451+ tree = SyntaxTree . parse ( <<~RUBY )
452+ object = MyObject.new
453+ object.attributes["name"] = "something"
454+ RUBY
455+
456+ visitor = Collector . new
457+ visitor . visit ( tree )
458+
459+ assert_equal ( 0 , visitor . arguments . length )
460+ assert_equal ( 1 , visitor . variables . length )
461+
462+ variable = visitor . variables [ "object" ]
463+ assert_equal ( 1 , variable . definitions . length )
464+ assert_equal ( 1 , variable . usages . length )
465+
466+ assert_equal ( 1 , variable . definitions [ 0 ] . start_line )
467+ assert_equal ( 2 , variable . usages [ 0 ] . start_line )
468+ end
469+
470+ def test_aref_with_two_accesses
471+ tree = SyntaxTree . parse ( <<~RUBY )
472+ object = MyObject.new
473+ object["first"]["second"] ||= []
474+ RUBY
475+
476+ visitor = Collector . new
477+ visitor . visit ( tree )
478+
479+ assert_equal ( 0 , visitor . arguments . length )
480+ assert_equal ( 1 , visitor . variables . length )
481+
482+ variable = visitor . variables [ "object" ]
483+ assert_equal ( 1 , variable . definitions . length )
484+ assert_equal ( 1 , variable . usages . length )
485+
486+ assert_equal ( 1 , variable . definitions [ 0 ] . start_line )
487+ assert_equal ( 2 , variable . usages [ 0 ] . start_line )
488+ end
489+
490+ def test_aref_on_a_method_call_with_arguments
491+ tree = SyntaxTree . parse ( <<~RUBY )
492+ object = MyObject.new
493+ object.instance_variable_get(:@attributes)[:something] = :other_thing
494+ RUBY
495+
496+ visitor = Collector . new
497+ visitor . visit ( tree )
498+
499+ assert_equal ( 0 , visitor . arguments . length )
500+ assert_equal ( 1 , visitor . variables . length )
501+
502+ variable = visitor . variables [ "object" ]
503+ assert_equal ( 1 , variable . definitions . length )
504+ assert_equal ( 1 , variable . usages . length )
505+
506+ assert_equal ( 1 , variable . definitions [ 0 ] . start_line )
507+ assert_equal ( 2 , variable . usages [ 0 ] . start_line )
508+ end
429509 end
430510end
0 commit comments