Skip to content

Commit

Permalink
Add AST::Util#nodoc?(node) helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Jan 30, 2025
1 parent f11565c commit 6630cd3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
44 changes: 44 additions & 0 deletions spec/ameba/ast/util_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,50 @@ module Ameba::AST
end
end

describe "#nodoc?" do
it "returns true if a node has a single `:nodoc:` annotation" do
node = as_node <<-CRYSTAL, wants_doc: true
# :nodoc:
def foo; end
CRYSTAL

subject.nodoc?(node).should be_true
end

it "returns true if a node has a `:nodoc:` annotation in the first line" do
node = as_node <<-CRYSTAL, wants_doc: true
# :nodoc:
#
# foo
def foo; end
CRYSTAL

subject.nodoc?(node).should be_true
end

it "returns true if a node has a `:nodoc:` annotation in the last line" do
node = as_node <<-CRYSTAL, wants_doc: true
# foo
#
# :nodoc:
def foo; end
CRYSTAL

subject.nodoc?(node).should be_true
end

it "returns false if a node has a `:nodoc:` annotation in the middle" do
node = as_node <<-CRYSTAL, wants_doc: true
# foo
# :nodoc:
# bar
def foo; end
CRYSTAL

subject.nodoc?(node).should be_false
end
end

describe "#control_exp_code" do
it "returns the exp code of a control expression" do
s = "return 1"
Expand Down
12 changes: 12 additions & 0 deletions src/ameba/ast/util.cr
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ module Ameba::AST::Util
end
end

# Returns `true` if node has a `:nodoc:` annotation as
# the first or the last line.
def nodoc?(node)
return false unless node.responds_to?(:doc)
return false unless doc = node.doc.presence

lines = doc.lines.map!(&.strip)

lines.first? == ":nodoc:" ||
lines.last? == ":nodoc:"
end

# Returns the exp code of a control expression.
# Wraps implicit tuple literal with curly brackets (e.g. multi-return).
def control_exp_code(node : Crystal::ControlExpression, code_lines)
Expand Down

0 comments on commit 6630cd3

Please sign in to comment.