Skip to content
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
52 changes: 52 additions & 0 deletions spec/compiler/crystal/tools/doc/method_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,30 @@ describe Doc::Method do
method.doc_copied_from.should be_nil
end

it "inherits doc from ancestor's previous def (no extra comment)" do
program = top_level_semantic(<<-CRYSTAL, wants_doc: true).program
class Foo
# Some docs
def foo
end

def foo
previous_def
end
end

class Bar < Foo
def foo
super
end
end
CRYSTAL
generator = Doc::Generator.new program, [""]
method = generator.type(program.types["Bar"]).lookup_method("foo").not_nil!
method.doc.should eq("Some docs")
method.doc_copied_from.should eq(generator.type(program.types["Foo"]))
end

it "inherits doc from ancestor (use :inherit:)" do
program = top_level_semantic(<<-CRYSTAL, wants_doc: true).program
class Foo
Expand Down Expand Up @@ -263,5 +287,33 @@ describe Doc::Method do
method.doc.should eq("Before\n\nSome docs\n\nAfter")
method.doc_copied_from.should be_nil
end

it "inherits doc from ancestor through chained :inherit:" do
program = top_level_semantic(<<-CRYSTAL, wants_doc: true).program
class Foo
# Some docs
def foo
end
end

class Bar < Foo
# :inherit:
def foo
super
end
end

class Baz < Bar
# :inherit:
def foo
super
end
end
CRYSTAL
generator = Doc::Generator.new program, [""]
method = generator.type(program.types["Baz"]).lookup_method("foo").not_nil!
method.doc.should eq("Some docs")
method.doc_copied_from.should be_nil
end
end
end
11 changes: 5 additions & 6 deletions src/compiler/crystal/tools/doc/method.cr
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Crystal::Doc::Method

previous_docs = previous_def_docs(@def)
if previous_docs
return DocInfo.new(def_doc, nil)
return DocInfo.new(previous_docs, nil)
end

ancestor_info = self.ancestor_doc_info
Expand Down Expand Up @@ -108,11 +108,10 @@ class Crystal::Doc::Method
# If we find an ancestor method with the same signature
if def_with_metadata.compare_strictness(other_def_with_metadata, self_owner: type.type, other_owner: ancestor) == 0
other_def = other_def_with_metadata.def
doc = other_def.doc
return DocInfo.new(doc, @generator.type(ancestor)) if doc

doc = previous_def_docs(other_def)
return DocInfo.new(doc, nil) if doc
ancestor_type = @generator.type(ancestor)
ancestor_method = Doc::Method.new(@generator, ancestor_type, other_def, @class_method)
doc = ancestor_method.doc
return DocInfo.new(doc, ancestor_type) if doc
end
end
end
Expand Down
Loading