Skip to content

Commit

Permalink
Add support for ignoring :nodoc: methods
Browse files Browse the repository at this point in the history
Adds new `NodocMethods` option in rules:

- `Typing/MethodReturnTypeRestriction`
- `Typing/MethodParameterTypeRestriction`
  • Loading branch information
Sija committed Jan 30, 2025
1 parent 6630cd3 commit 28c8b22
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
21 changes: 21 additions & 0 deletions spec/ameba/rule/typing/method_parameter_type_restriction_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ module Ameba::Rule::Typing
CRYSTAL
end

it "passes if a method has a `:nodoc:` annotation" do
expect_no_issues subject, <<-CRYSTAL
# :nodoc:
def foo(bar); end
CRYSTAL
end

it "fails if a public method parameter doesn't have a type restriction" do
expect_issue subject, <<-CRYSTAL
def hello(a)
Expand Down Expand Up @@ -195,6 +202,20 @@ module Ameba::Rule::Typing
CRYSTAL
end
end

context "#nodoc_methods" do
rule = MethodParameterTypeRestriction.new
rule.nodoc_methods = true

it "fails if a public method parameter doesn't have a type restriction" do
expect_issue rule, <<-CRYSTAL
# :nodoc:
def foo(bar)
# ^ error: Method parameter should have a type restriction
end
CRYSTAL
end
end
end
end
end
21 changes: 21 additions & 0 deletions spec/ameba/rule/typing/method_return_type_restriction_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ module Ameba::Rule::Typing
CRYSTAL
end

it "passes if a method has a `:nodoc:` annotation" do
expect_no_issues subject, <<-CRYSTAL
# :nodoc:
def foo; end
CRYSTAL
end

it "fails if a public method doesn't have a return type restriction" do
expect_issue subject, <<-CRYSTAL
def hello
Expand Down Expand Up @@ -119,6 +126,20 @@ module Ameba::Rule::Typing
CRYSTAL
end
end

context "#nodoc_methods" do
rule = MethodReturnTypeRestriction.new
rule.nodoc_methods = true

it "fails if a public method doesn't have a return type restriction" do
expect_issue rule, <<-CRYSTAL
# :nodoc:
def foo
# ^^^^^ error: Method should have a return type restriction
end
CRYSTAL
end
end
end
end
end
7 changes: 6 additions & 1 deletion src/ameba/rule/typing/method_parameter_type_restriction.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ module Ameba::Rule::Typing
# BlockParameters: false
# PrivateMethods: false
# ProtectedMethods: false
# NodocMethods: false
# ```
class MethodParameterTypeRestriction < Base
include AST::Util

properties do
since_version "1.7.0"
description "Recommends that method parameters have type restrictions"
Expand All @@ -59,6 +62,7 @@ module Ameba::Rule::Typing
block_parameters false
private_methods false
protected_methods false
nodoc_methods false
end

MSG = "Method parameter should have a type restriction"
Expand All @@ -80,7 +84,8 @@ module Ameba::Rule::Typing

private def valid_visibility?(node : Crystal::ASTNode) : Bool
(!private_methods? && node.visibility.private?) ||
(!protected_methods? && node.visibility.protected?)
(!protected_methods? && node.visibility.protected?) ||
(!nodoc_methods? && nodoc?(node))
end
end
end
7 changes: 6 additions & 1 deletion src/ameba/rule/typing/method_return_type_restriction.cr
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ module Ameba::Rule::Typing
# Enabled: true
# PrivateMethods: false
# ProtectedMethods: false
# NodocMethods: false
# ```
class MethodReturnTypeRestriction < Base
include AST::Util

properties do
since_version "1.7.0"
description "Recommends that methods have a return type restriction"
enabled false
private_methods false
protected_methods false
nodoc_methods false
end

MSG = "Method should have a return type restriction"
Expand All @@ -46,7 +50,8 @@ module Ameba::Rule::Typing
private def valid_return_type?(node : Crystal::ASTNode) : Bool
!!node.return_type ||
(node.visibility.private? && !private_methods?) ||
(node.visibility.protected? && !protected_methods?)
(node.visibility.protected? && !protected_methods?) ||
(!nodoc_methods? && nodoc?(node))
end
end
end

0 comments on commit 28c8b22

Please sign in to comment.