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 Typing/MethodParameterTypeRestriction #520

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6ad15dc
Add `Typing/MethodParamTypeRestriction`
nobodywasishere Dec 2, 2024
3b63399
MethodParamTypeRestriction DefaultValue default value should be true
nobodywasishere Dec 2, 2024
cd8690d
Fix method param restriction specs not having params
nobodywasishere Dec 28, 2024
bdd97c9
Refactor specs to put enabled options in contexts
nobodywasishere Dec 28, 2024
fc21650
Remove undocumented option
nobodywasishere Dec 28, 2024
f2b245a
Update docs + spec messages
nobodywasishere Dec 29, 2024
b724ee0
Apply suggestions from code review
nobodywasishere Dec 29, 2024
c62d1e8
Fix specs
nobodywasishere Dec 29, 2024
9235fdd
Remove redundant spec
nobodywasishere Dec 29, 2024
ef2db2a
Fix docs
nobodywasishere Dec 29, 2024
4ea2cdb
Fix docs pt. 2
nobodywasishere Dec 29, 2024
fd95a13
Never apply to double splat params or splat params without names
nobodywasishere Jan 9, 2025
e0d8055
Add underscore type to spec
nobodywasishere Jan 9, 2025
e10568f
Apply suggestions from code review
nobodywasishere Jan 9, 2025
9127da2
MethodParamTypeRestriction -> MethodParameterTypeRestriction
nobodywasishere Jan 9, 2025
957a29a
Apply suggestions from code review
nobodywasishere Jan 10, 2025
8f427f0
Formatting
nobodywasishere Jan 10, 2025
547ea55
Add specs for block param
nobodywasishere Jan 10, 2025
136b3ea
Don't prefer name location, doesn't make a difference
nobodywasishere Jan 10, 2025
5ec4fb6
Apply suggestions from code review
nobodywasishere Jan 12, 2025
1e85ac2
Apply suggestions from code review
nobodywasishere Jan 12, 2025
54a062a
Apply suggestions from code review
nobodywasishere Jan 12, 2025
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
1 change: 1 addition & 0 deletions spec/ameba/base_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Ameba::Rule
Naming
Performance
Style
Typing
]
end
end
Expand Down
161 changes: 161 additions & 0 deletions spec/ameba/rule/typing/method_param_type_restriction_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
require "../../../spec_helper"

module Ameba::Rule::Typing
subject = MethodParamTypeRestriction.new
nobodywasishere marked this conversation as resolved.
Show resolved Hide resolved

it "passes if a method param has a type restriction" do
expect_no_issues subject, <<-CRYSTAL
def hello(a : String, b : _) : String
"hello world" + a + b
end

def hello(*a : String) : String
"hello world" + a.join(", ")
end
CRYSTAL
end

it "fails if a splat method param with a name doesn't have a type restriction" do
expect_issue subject, <<-CRYSTAL
def hello(*a) : String
# ^ error: Method parameter should have a type restriction
"hello world" + a
end
CRYSTAL
end

it "passes if a splat param without a name doesn't have a type restriction" do
expect_no_issues subject, <<-CRYSTAL
def hello(hello : String, *, world : String = "world") : String
hello + world + a
end
CRYSTAL
end

it "passes if a double splat method param doesn't have a type restriction" do
expect_no_issues subject, <<-CRYSTAL
def hello(a : String, **world) : String
"hello world" + a
end
CRYSTAL
end

it "passes if a private method method param doesn't have a type restriction" do
expect_no_issues subject, <<-CRYSTAL
private def hello(a)
"hello world" + a
end
CRYSTAL
end

it "passes if a protected method param doesn't have a type restriction" do
expect_no_issues subject, <<-CRYSTAL
protected def hello(a)
"hello world" + a
end
CRYSTAL
end

it "fails if a public method param doesn't have a type restriction" do
expect_issue subject, <<-CRYSTAL
def hello(a)
# ^ error: Method parameter should have a type restriction
"hello world" + a
end
CRYSTAL
end

it "passes if a method param with a default value doesn't have a type restriction" do
expect_no_issues subject, <<-CRYSTAL
def hello(a = "jim")
"hello there, " + a
end
CRYSTAL
end

context "properties" do
context "#private_methods" do
rule = MethodParamTypeRestriction.new
rule.private_methods = true

it "passes if a method has a return type restriction" do
expect_no_issues rule, <<-CRYSTAL
private def hello(a : String) : String
"hello world" + a
end
CRYSTAL
end

it "passes if a protected method param doesn't have a type restriction" do
expect_no_issues rule, <<-CRYSTAL
protected def hello(a)
"hello world"
end
CRYSTAL
end

it "fails if a public or private method doesn't have a return type restriction" do
expect_issue rule, <<-CRYSTAL
def hello(a)
# ^ error: Method parameter should have a type restriction
"hello world"
end

private def hello(a)
# ^ error: Method parameter should have a type restriction
"hello world"
end
CRYSTAL
end
end

context "#protected_methods" do
rule = MethodParamTypeRestriction.new
rule.protected_methods = true

it "passes if a method has a return type restriction" do
expect_no_issues rule, <<-CRYSTAL
protected def hello(a : String) : String
"hello world" + a
end
CRYSTAL
end

it "passes if a private method param doesn't have a type restriction" do
expect_no_issues rule, <<-CRYSTAL
private def hello(a)
"hello world"
end
CRYSTAL
end

it "fails if a public or protected method doesn't have a return type restriction" do
expect_issue rule, <<-CRYSTAL
def hello(a)
# ^ error: Method parameter should have a type restriction
"hello world"
end

protected def hello(a)
# ^ error: Method parameter should have a type restriction
"hello world"
end
CRYSTAL
end
end

context "#default_value" do
it "fails if a method param with a default value doesn't have a type restriction" do
rule = MethodParamTypeRestriction.new
rule.default_value = true

expect_issue rule, <<-CRYSTAL
def hello(a = "world")
# ^ error: Method parameter should have a type restriction
"hello \#{a}"
end
CRYSTAL
end
end
end
end
89 changes: 89 additions & 0 deletions src/ameba/rule/typing/method_param_type_restriction.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
module Ameba::Rule::Typing
# A rule that enforces method parameters have type restrictions, with optional enforcement of block parameters
#
# For example, this is considered invalid:
#
# ```
# def add(a, b)
# a + b
# end
# ```
#
# And this is considered valid:
#
# ```
# def add(a : String, b : String)
# a + b
# end
# ```
#
# When the config options `PrivateMethods` and `ProtectedMethods`
# are true, this rule is also applied to private and protected methods, respectively.
#
# The `BlockParam` configuration option will extend this to block params, where these are invalid:
#
# ```
# def exec(&)
# end
#
# def exec(&block)
# end
# ```
#
# And this is valid:
#
# ```
# def exec(&block : String -> String)
# yield "cmd"
# end
# ```
#
# The config option `DefaultValue` controls whether this rule applies to parameters that have a default value.
#
# YAML configuration example:
#
# ```
# Typing/MethodParamTypeRestriction:
# Enabled: false
# DefaultValue: false
Sija marked this conversation as resolved.
Show resolved Hide resolved
# PrivateMethods: false
# ProtectedMethods: false
# BlockParam: false
# ```
class MethodParamTypeRestriction < Base
nobodywasishere marked this conversation as resolved.
Show resolved Hide resolved
properties do
since_version "1.7.0"
description "Recommends that method parameters have type restrictions"
enabled false
default_value false
private_methods false
protected_methods false
block_param false
end

MSG = "Method parameter should have a type restriction"

def test(source, node : Crystal::Def)
return if valid?(node)

node.args.each do |arg|
next if arg.restriction || arg.name.empty? || (!default_value? && arg.default_value)

issue_for arg, MSG, prefer_name_location: true
end

if block_param?
node.block_arg.try do |block_arg|
next if block_arg.restriction

issue_for block_arg, MSG, prefer_name_location: true
end
end
end

private def valid?(node : Crystal::ASTNode) : Bool
(!private_methods? && node.visibility.private?) ||
(!protected_methods? && node.visibility.protected?)
end
end
end
Loading