Skip to content

Commit

Permalink
RDoc::Context#add_module_alias now duplicates aliased classes. Fixes r…
Browse files Browse the repository at this point in the history
…uby#143

RDoc::ClassModule#update_aliases no longer adds a class under the Object
namespace.  This is useless and confusing.

Tested RDoc::Darkfish#setup
  • Loading branch information
drbrain committed Nov 19, 2012
1 parent c5ac18b commit ca02643
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 18 deletions.
2 changes: 2 additions & 0 deletions History.rdoc
Expand Up @@ -99,6 +99,8 @@
Lucy
* Generated RD parser files are now included in the gem. Issue #145 by
Marvin Gülker
* Class and module aliases now create new classes to avoid duplicate names
in the class list. Issue #143 by Richard Schneeman, Rails Issue #2839

=== 3.12 / 2011-12-15

Expand Down
9 changes: 7 additions & 2 deletions lib/rdoc/class_module.rb
Expand Up @@ -711,8 +711,13 @@ def update_aliases
next unless cm = const.is_alias_for
cm_alias = cm.dup
cm_alias.name = const.name
cm_alias.parent = self
cm_alias.full_name = nil # force update for new parent

# Don't move top-level aliases under Object, they look ugly there
unless RDoc::TopLevel === cm_alias.parent then
cm_alias.parent = self
cm_alias.full_name = nil # force update for new parent
end

cm_alias.aliases.clear
cm_alias.is_alias_for = cm

Expand Down
27 changes: 15 additions & 12 deletions lib/rdoc/context.rb
Expand Up @@ -467,31 +467,34 @@ def add_module(class_type, name)
def add_module_alias from, name, file
return from if @done_documenting

to_name = child_name(name)
to_name = child_name name

# if we already know this name, don't register an alias:
# see the metaprogramming in lib/active_support/basic_object.rb,
# where we already know BasicObject as a class when we find
# where we already know BasicObject is a class when we find
# BasicObject = BlankSlate
return from if @store.find_class_or_module to_name

if from.module? then
@store.modules_hash[to_name] = from
@modules[name] = from
to = from.dup
to.name = name
to.full_name = nil

if to.module? then
@store.modules_hash[to_name] = to
@modules[name] = to
else
@store.classes_hash[to_name] = from
@classes[name] = from
@store.classes_hash[to_name] = to
@classes[name] = to
end

# HACK: register a constant for this alias:
# constant value and comment will be updated after,
# when the Ruby parser adds the constant
const = RDoc::Constant.new name, nil, ''
# Registers a constant for this alias. The constant value and comment
# will be updated later, when the Ruby parser adds the constant
const = RDoc::Constant.new name, nil, to.comment
const.record_location file
const.is_alias_for = from
add_constant const

from
to
end

##
Expand Down
21 changes: 21 additions & 0 deletions lib/rdoc/generator/darkfish.rb
Expand Up @@ -85,6 +85,12 @@ class RDoc::Generator::Darkfish

attr_reader :base_dir

##
# Classes and modules to be used by this generator, not necessarily
# displayed. See also #modsort

attr_reader :classes

##
# No files will be written when dry_run is true.

Expand All @@ -96,11 +102,26 @@ class RDoc::Generator::Darkfish

attr_accessor :file_output

##
# Files to be displayed by this generator

attr_reader :files

##
# The JSON index generator for this Darkfish generator

attr_reader :json_index

##
# Methods to be displayed by this generator

attr_reader :methods

##
# Sorted list of classes and modules to be displayed by this generator

attr_reader :modsort

##
# The RDoc::Store that is the source of the generated content

Expand Down
24 changes: 24 additions & 0 deletions test/test_rdoc_class_module.rb
Expand Up @@ -1260,6 +1260,30 @@ def test_update_aliases_reparent
assert_equal 'O1::A1', o1_a1_m.full_name
end

def test_update_aliases_reparent_root
store = RDoc::Store.new

top_level = store.add_file 'file.rb'

klass = top_level.add_class RDoc::NormalClass, 'Klass'
object = top_level.add_class RDoc::NormalClass, 'Object'

const = RDoc::Constant.new 'A', nil, ''
const.record_location top_level
const.is_alias_for = klass

top_level.add_module_alias klass, 'A', top_level

object.add_constant const

object.update_aliases

assert_equal %w[A Klass Object], store.classes_hash.keys.sort

assert_equal 'A', store.classes_hash['A'].full_name
assert_equal 'Klass', store.classes_hash['Klass'].full_name
end

def test_update_includes
a = RDoc::Include.new 'M1', nil
b = RDoc::Include.new 'M2', nil
Expand Down
21 changes: 20 additions & 1 deletion test/test_rdoc_context.rb
Expand Up @@ -229,10 +229,29 @@ def test_add_module_alias

alias_constant = @c2.constants.first

assert_equal c4, c3_c4
assert_equal 'C2::C4', c3_c4.full_name
assert_equal tl, alias_constant.file
end

def test_add_module_alias_top_level
store = RDoc::Store.new

top_level = store.add_file 'file.rb'

klass = top_level.add_class RDoc::NormalClass, 'Klass'
klass.comment = 'klass comment'

object = top_level.add_class RDoc::NormalClass, 'Object'

top_level.add_module_alias klass, 'A', top_level

refute_empty object.constants

constant = object.constants.first

assert_equal 'klass comment', constant.comment
end

def test_add_module_class
k = @c1.add_class RDoc::NormalClass, 'Klass', nil
m = @c1.add_module RDoc::NormalModule, 'Klass'
Expand Down
31 changes: 30 additions & 1 deletion test/test_rdoc_generator_darkfish.rb
Expand Up @@ -31,7 +31,14 @@ def setup

@top_level = @store.add_file 'file.rb'
@top_level.parser = RDoc::Parser::Ruby
@klass = @top_level.add_class RDoc::NormalClass, 'Object'
@klass = @top_level.add_class RDoc::NormalClass, 'Klass'

@alias_constant = RDoc::Constant.new 'A', nil, ''
@alias_constant.record_location @top_level

@top_level.add_constant @alias_constant

@klass.add_module_alias @klass, 'A', @top_level

@meth = RDoc::AnyMethod.new nil, 'method'
@meth_bang = RDoc::AnyMethod.new nil, 'method!'
Expand All @@ -43,6 +50,11 @@ def setup

@ignored = @top_level.add_class RDoc::NormalClass, 'Ignored'
@ignored.ignore

@store.complete :private

@object = @store.find_class_or_module 'Object'
@klass_alias = @store.find_class_or_module 'Klass::A'
end

def teardown
Expand All @@ -61,6 +73,13 @@ def refute_file path
refute File.exist?(path), "#{path} exists"
end

def mu_pp obj
s = ''
s = PP.pp obj, s
s = s.force_encoding Encoding.default_external if defined? Encoding
s.chomp
end

def test_generate
top_level = @store.add_file 'file.rb'
top_level.add_class @klass.class, @klass.name
Expand Down Expand Up @@ -126,6 +145,16 @@ def test_generate_static_dry_run
refute_file 'image.png'
end

def test_setup
@g.setup

assert_equal [@klass_alias, @ignored, @klass, @object],
@g.classes.sort_by { |klass| klass.full_name }
assert_equal [@top_level], @g.files
assert_equal [@meth, @meth, @meth_bang, @meth_bang], @g.methods
assert_equal [@klass_alias, @klass, @object], @g.modsort
end

def test_template_for
classpage = Pathname.new @options.template_dir + 'class.rhtml'

Expand Down
4 changes: 2 additions & 2 deletions test/test_rdoc_parser_ruby.rb
Expand Up @@ -1002,7 +1002,7 @@ def test_parse_constant_alias

@parser.parse_constant klass, tk, @comment

assert_equal cB, klass.find_module_named('A')
assert_equal 'Foo::A', klass.find_module_named('A').full_name
end

def test_parse_constant_alias_same_name
Expand All @@ -1018,7 +1018,7 @@ def test_parse_constant_alias_same_name

@parser.parse_constant foo, tk, @comment

assert_equal top_bar, bar.find_module_named('A')
assert_equal 'A', bar.find_module_named('A').full_name
end

def test_parse_constant_stopdoc
Expand Down

0 comments on commit ca02643

Please sign in to comment.