Skip to content

Commit b1ece9b

Browse files
authored
Merge pull request #105 from tycooon/parse-methods-in-calls
Generate tags for methods defined in blocks
2 parents dbf65b1 + 169996f commit b1ece9b

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

lib/ripper-tags/parser.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def on_tstring_content(str)
136136
end
137137

138138
def on_string_add(*args)
139-
[args[1], lineno]
139+
[args[1], lineno] unless args[0].is_a?(Array) && args[0].include?(:string_embexpr)
140140
end
141141

142142
def on_string_embexpr(*)
@@ -291,8 +291,9 @@ def on_do_block(*args)
291291
end
292292

293293
def on_method_add_block(method, body)
294-
return unless method
295-
if %w[class_eval module_eval].include?(method[2]) && body
294+
if method.nil?
295+
body ? body.last : nil
296+
elsif (method[2] == "class_eval" || method[2] == "module_eval") && body
296297
[:class_eval, [
297298
method[1].is_a?(Array) ? method[1][0] : method[1],
298299
method[3]
@@ -302,6 +303,8 @@ def on_method_add_block(method, body)
302303
call = method.dup
303304
call[4] = body.last
304305
call
306+
elsif :fcall == method[0] && body
307+
body.last
305308
else
306309
super
307310
end
@@ -544,12 +547,17 @@ def on_class_eval(name, body)
544547
end
545548

546549
def on_call(*args)
550+
process(args)
547551
end
548-
alias on_aref_field on_call
549-
alias on_field on_call
550-
alias on_fcall on_call
551-
alias on_args on_call
552-
alias on_assoc on_call
553-
alias on_! on_call
552+
553+
def ignore(*args)
554+
end
555+
556+
alias on_aref_field ignore
557+
alias on_field ignore
558+
alias on_fcall ignore
559+
alias on_args ignore
560+
alias on_assoc ignore
561+
alias on_! ignore
554562
end
555563
end

test/test_ripper_tags.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,26 @@ def test_nested_constant_definitions
8787
OPEN = 'open',
8888
]
8989
90+
FROZEN_ARRAY = [ARRAY_ENTRY = 1].freeze
91+
9092
DISPLAY_MAPPING = {
9193
CANCELLED = 'cancelled' => 'Cancelled by user',
9294
STARTED = 'started' => 'Started by user',
9395
}
96+
97+
FROZEN_HASH = { HASH_ENTRY = 2 => 3 }.freeze
9498
EOC
9599

96100
assert_equal %w[
97101
OPEN
98102
STATUSES
103+
ARRAY_ENTRY
104+
FROZEN_ARRAY
99105
CANCELLED
100106
STARTED
101107
DISPLAY_MAPPING
108+
HASH_ENTRY
109+
FROZEN_HASH
102110
], tags.map { |t| t[:name] }
103111

104112
tags.each do |t|
@@ -815,4 +823,27 @@ class Foo
815823
assert_equal 'Foo', tags[0][:name]
816824
assert_equal 'calculate', tags[1][:name]
817825
end
826+
827+
def test_method_defined_in_block
828+
tags = extract(<<-EOC)
829+
RSpec.describe do
830+
def test_method; end
831+
end
832+
833+
module M
834+
included do
835+
def included_method; end
836+
attr_accessor :cache
837+
end
838+
839+
%w[sub gsub].each do |m|
840+
define_method("\#{m}!") {}
841+
attr_reader m
842+
end
843+
end
844+
EOC
845+
846+
expected = ["Object#test_method", "M", "M#included_method", "M#cache", "M#cache="]
847+
assert_equal expected, tags.map { |t| t[:full_name] }
848+
end
818849
end

0 commit comments

Comments
 (0)