Skip to content

Commit

Permalink
Add Typing/ProcLiteralReturnTypeRestriction (#522)
Browse files Browse the repository at this point in the history
* Add `Typing/ProcLiteralReturnTypeRestriction`

* Typing/ProcLiteralReturnTypeRestriction disabled by default

* Update src/ameba/rule/typing/proc_literal_return_type_restriction.cr

Co-authored-by: Sijawusz Pur Rahnama <[email protected]>

* Apply suggestions from code review

Co-authored-by: Sijawusz Pur Rahnama <[email protected]>

* Fix specs

* Move ProcLiteral in node list

* Apply suggestions from code review

Co-authored-by: Vitalii Elenhaupt <[email protected]>

* Apply suggestions from code review

* Update src/ameba/rule/typing/proc_literal_return_type_restriction.cr

* Update src/ameba/rule/typing/proc_literal_return_type_restriction.cr

---------

Co-authored-by: Sijawusz Pur Rahnama <[email protected]>
Co-authored-by: Vitalii Elenhaupt <[email protected]>
  • Loading branch information
3 people authored Dec 28, 2024
1 parent 915f064 commit 870a60f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require "../../../spec_helper"

module Ameba::Rule::Typing
subject = ProcLiteralReturnTypeRestriction.new

it "passes if a proc literal has a return type restriction" do
expect_no_issues subject, <<-CRYSTAL
my_proc = ->(var : String) : Nil { puts var }
CRYSTAL
end

it "fails if a proc literal doesn't have a return type restriction" do
expect_issue subject, <<-CRYSTAL
my_proc = ->(var : String) { puts var }
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Proc literal should have a return type restriction
CRYSTAL
end
end
1 change: 1 addition & 0 deletions src/ameba/ast/visitors/node_visitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module Ameba::AST
ModuleDef,
MultiAssign,
NilLiteral,
ProcLiteral,
StringInterpolation,
Unless,
Until,
Expand Down
45 changes: 45 additions & 0 deletions src/ameba/rule/typing/proc_literal_return_type_restriction.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Ameba::Rule::Typing
# A rule that enforces that `Proc` literals have a return type.
#
# For example, these are considered valid:
#
# ```
# greeter = ->(name : String) : String { "Hello #{name}" }
# ```
#
# ```
# task = -> : Task { Task.new("execute this command") }
# ```
#
# And these are invalid:
#
# ```
# greeter = ->(name : String) { "Hello #{name}" }
# ```
#
# ```
# task = -> { Task.new("execute this command") }
# ```
#
# YAML configuration example:
#
# ```
# Typing/ProcLiteralReturnTypeRestriction:
# Enabled: false
# ```
class ProcLiteralReturnTypeRestriction < Base
properties do
since_version "1.7.0"
description "Disallows Proc literals without return type restrictions"
enabled false
end

MSG = "Proc literal should have a return type restriction"

def test(source, node : Crystal::ProcLiteral)
return if node.def.return_type

issue_for node, MSG
end
end
end

0 comments on commit 870a60f

Please sign in to comment.