Skip to content

Parse ruby 2.1 <visibility> def method definition #435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/rdoc/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ class RDoc::Context < RDoc::CodeObject

attr_accessor :visibility

##
# Current visibility of this line

attr_writer :current_line_visibility

##
# Hash of registered methods. Attributes are also registered here,
# twice if they are RW.
Expand Down Expand Up @@ -148,6 +153,7 @@ def initialize_methods_etc
@extends = []
@constants = []
@external_aliases = []
@current_line_visibility = nil

# This Hash maps a method name to a list of unmatched aliases (aliases of
# a method not yet encountered).
Expand Down Expand Up @@ -478,7 +484,11 @@ def add_method method
end
else
@methods_hash[key] = method
method.visibility = @visibility
if @current_line_visibility
method.visibility, @current_line_visibility = @current_line_visibility, nil
else
method.visibility = @visibility
end
add_to @method_list, method
resolve_aliases method
end
Expand Down
3 changes: 3 additions & 0 deletions lib/rdoc/parser/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,7 @@ def parse_statements(container, single = NORMAL, current_method = nil,

unget_tk tk
keep_comment = true
container.current_line_visibility = nil

when TkCLASS then
parse_class container, single, tk, comment
Expand Down Expand Up @@ -1888,6 +1889,8 @@ def parse_visibility(container, single, tk)
#
when TkNL, TkUNLESS_MOD, TkIF_MOD, TkSEMICOLON then
container.ongoing_visibility = vis
when TkDEF
container.current_line_visibility = vis
else
update_visibility container, vis_type, vis, singleton
end
Expand Down
21 changes: 21 additions & 0 deletions test/test_rdoc_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,27 @@ def test_sort_sections_tomdoc_missing
assert_equal [nil, 'Public', 'Internal'], titles
end

def test_visibility_def
assert_equal :private, @c6.find_method_named('priv1').visibility
assert_equal :protected, @c6.find_method_named('prot1').visibility
assert_equal :public, @c6.find_method_named('pub1').visibility
assert_equal :private, @c6.find_method_named('priv2').visibility
assert_equal :protected, @c6.find_method_named('prot2').visibility
assert_equal :public, @c6.find_method_named('pub2').visibility
assert_equal :private, @c6.find_method_named('priv3').visibility
assert_equal :protected, @c6.find_method_named('prot3').visibility
assert_equal :public, @c6.find_method_named('pub3').visibility
assert_equal :private, @c6.find_method_named('priv4').visibility
assert_equal :protected, @c6.find_method_named('prot4').visibility
assert_equal :public, @c6.find_method_named('pub4').visibility
assert_equal :private, @c6.find_method_named('priv5').visibility
assert_equal :protected, @c6.find_method_named('prot5').visibility
assert_equal :public, @c6.find_method_named('pub5').visibility
assert_equal :private, @c6.find_method_named('priv6').visibility
assert_equal :protected, @c6.find_method_named('prot6').visibility
assert_equal :public, @c6.find_method_named('pub6').visibility
end

def util_visibilities
@pub = RDoc::AnyMethod.new nil, 'pub'
@prot = RDoc::AnyMethod.new nil, 'prot'
Expand Down
4 changes: 2 additions & 2 deletions test/test_rdoc_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def test_add_file_relative

def test_all_classes_and_modules
expected = %w[
C1 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1
C1 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1 C6
Child
M1 M1::M2
Parent
Expand Down Expand Up @@ -213,7 +213,7 @@ def test_class_path

def test_classes
expected = %w[
C1 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1
C1 C2 C2::C3 C2::C3::H1 C3 C3::H1 C3::H2 C4 C4::C4 C5 C5::C1 C6
Child
Parent
]
Expand Down
25 changes: 25 additions & 0 deletions test/xref_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@ class C1
end
end

class C6
private def priv1() end
def pub1() end
protected def prot1() end
def pub2() end
public def pub3() end
def pub4() end

private
private def priv2() end
def priv3() end
protected def prot2() end
def priv4() end
public def pub5() end
def priv5() end

protected
private def priv6() end
def prot3() end
protected def prot4() end
def prot5() end
public def pub6() end
def prot6() end
end

module M1
def m
end
Expand Down
1 change: 1 addition & 0 deletions test/xref_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def generator.file_dir() nil end
@c5_c1 = @xref_data.find_module_named 'C5::C1'
@c3_h1 = @xref_data.find_module_named 'C3::H1'
@c3_h2 = @xref_data.find_module_named 'C3::H2'
@c6 = @xref_data.find_module_named 'C6'

@m1 = @xref_data.find_module_named 'M1'
@m1_m = @m1.method_list.first
Expand Down