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
24 changes: 24 additions & 0 deletions spec/compiler/semantic/enum_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -613,4 +613,28 @@ describe "Semantic: enum" do
a_def = result.program.types["Foo"].lookup_defs("foo").first
a_def.previous.should be_nil
end

it "adds docs to helper methods" do
result = top_level_semantic <<-CRYSTAL, wants_doc: true
enum Foo
# These are the docs for `Bar`
Bar = 1
end
CRYSTAL

a_defs = result.program.types["Foo"].lookup_defs("bar?")
a_defs.first.doc.should eq("Returns `true` if this enum value equals `Bar`")
end

it "marks helper methods with `:nodoc:` if the member is `:nodoc:`" do
result = top_level_semantic <<-CRYSTAL, wants_doc: true
enum Foo
# :nodoc:
Bar = 1
end
CRYSTAL

a_defs = result.program.types["Foo"].lookup_defs("bar?")
a_defs.first.doc.should eq(":nodoc:")
end
end
7 changes: 7 additions & 0 deletions src/compiler/crystal/semantic/top_level_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,13 @@ class Crystal::TopLevelVisitor < Crystal::SemanticVisitor
method_name = is_flags ? "includes?" : "=="
body = Call.new(Var.new("self").at(member), method_name, Path.new(member.name).at(member)).at(member)
a_def = Def.new("#{member.name.underscore}?", body: body).at(member)

a_def.doc = if member.doc.try &.starts_with?(":nodoc:")
":nodoc:"
else
"Returns `true` if this enum value #{is_flags ? "contains" : "equals"} `#{member.name}`"
end

enum_type.add_def a_def
end

Expand Down