Skip to content

Commit

Permalink
Merge pull request #105 from tycooon/parse-methods-in-calls
Browse files Browse the repository at this point in the history
Generate tags for methods defined in blocks
  • Loading branch information
mislav authored May 18, 2023
2 parents dbf65b1 + 169996f commit b1ece9b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/ripper-tags/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def on_tstring_content(str)
end

def on_string_add(*args)
[args[1], lineno]
[args[1], lineno] unless args[0].is_a?(Array) && args[0].include?(:string_embexpr)
end

def on_string_embexpr(*)
Expand Down Expand Up @@ -291,8 +291,9 @@ def on_do_block(*args)
end

def on_method_add_block(method, body)
return unless method
if %w[class_eval module_eval].include?(method[2]) && body
if method.nil?
body ? body.last : nil
elsif (method[2] == "class_eval" || method[2] == "module_eval") && body
[:class_eval, [
method[1].is_a?(Array) ? method[1][0] : method[1],
method[3]
Expand All @@ -302,6 +303,8 @@ def on_method_add_block(method, body)
call = method.dup
call[4] = body.last
call
elsif :fcall == method[0] && body
body.last
else
super
end
Expand Down Expand Up @@ -544,12 +547,17 @@ def on_class_eval(name, body)
end

def on_call(*args)
process(args)
end
alias on_aref_field on_call
alias on_field on_call
alias on_fcall on_call
alias on_args on_call
alias on_assoc on_call
alias on_! on_call

def ignore(*args)
end

alias on_aref_field ignore
alias on_field ignore
alias on_fcall ignore
alias on_args ignore
alias on_assoc ignore
alias on_! ignore
end
end
31 changes: 31 additions & 0 deletions test/test_ripper_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,26 @@ def test_nested_constant_definitions
OPEN = 'open',
]
FROZEN_ARRAY = [ARRAY_ENTRY = 1].freeze
DISPLAY_MAPPING = {
CANCELLED = 'cancelled' => 'Cancelled by user',
STARTED = 'started' => 'Started by user',
}
FROZEN_HASH = { HASH_ENTRY = 2 => 3 }.freeze
EOC

assert_equal %w[
OPEN
STATUSES
ARRAY_ENTRY
FROZEN_ARRAY
CANCELLED
STARTED
DISPLAY_MAPPING
HASH_ENTRY
FROZEN_HASH
], tags.map { |t| t[:name] }

tags.each do |t|
Expand Down Expand Up @@ -815,4 +823,27 @@ class Foo
assert_equal 'Foo', tags[0][:name]
assert_equal 'calculate', tags[1][:name]
end

def test_method_defined_in_block
tags = extract(<<-EOC)
RSpec.describe do
def test_method; end
end
module M
included do
def included_method; end
attr_accessor :cache
end
%w[sub gsub].each do |m|
define_method("\#{m}!") {}
attr_reader m
end
end
EOC

expected = ["Object#test_method", "M", "M#included_method", "M#cache", "M#cache="]
assert_equal expected, tags.map { |t| t[:full_name] }
end
end

0 comments on commit b1ece9b

Please sign in to comment.