Skip to content

Commit

Permalink
Add docs to enum member helper methods (#15379)
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodywasishere authored Jan 29, 2025
1 parent 80e051d commit 2978cd1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
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

0 comments on commit 2978cd1

Please sign in to comment.