Skip to content
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

Add docs to enum member helper methods #15379

Merged
Show file tree
Hide file tree
Changes from 1 commit
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_not be_nil
nobodywasishere marked this conversation as resolved.
Show resolved Hide resolved
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.try &.starts_with?(":nodoc:").should be_true
end
end
4 changes: 4 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,10 @@ 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 = <<-DOC
#{":nodoc:" if member.doc.try &.starts_with?(":nodoc:")}
Returns `true` if this enum value #{is_flags ? "contains" : "equals"} `#{member.name}`
DOC
enum_type.add_def a_def
end

Expand Down
Loading