-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Typing/ProcLiteralReturnTypeRestriction
(#522)
* 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
1 parent
915f064
commit 870a60f
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ module Ameba::Rule | |
Naming | ||
Performance | ||
Style | ||
Typing | ||
] | ||
end | ||
end | ||
|
18 changes: 18 additions & 0 deletions
18
spec/ameba/rule/typing/proc_literal_return_type_restriction_spec.cr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/ameba/rule/typing/proc_literal_return_type_restriction.cr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |