-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cop against
.void.checked(:tests)
- Loading branch information
Showing
6 changed files
with
191 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
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,77 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Sorbet | ||
# Disallows the usage of `.void.checked(:tests)`. | ||
# | ||
# Using `.void` changes the value returned from the method, but only if | ||
# runtime type checking is enabled for the method. Methods marked `.void` | ||
# will return different values in tests compared with non-test | ||
# environments. This is particularly troublesome if branching on the | ||
# result of a `.void` method, because the returned value in test code | ||
# will be the truthy `VOID` value, while the non-test return value may be | ||
# falsy depending on the method's implementation. | ||
# | ||
# - Use `.returns(T.anything).checked(:tests)` to keep the runtime type | ||
# checking for the rest of the parameters. | ||
# - Use `.void.checked(:never)` if you are on an older version of Sorbet | ||
# which does not have `T.anything` (meaning versions 0.5.10781 or | ||
# earlier. Versions released after 2023-04-14 include `T.anything`.) | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# sig { void.checked(:tests) } | ||
# | ||
# # good | ||
# sig { void } | ||
# sig { returns(T.anything).checked(:tests) } | ||
# sig { void.checked(:never) } | ||
class VoidCheckedTests < ::RuboCop::Cop::Base | ||
include(RuboCop::Cop::RangeHelp) | ||
include(RuboCop::Cop::Sorbet::SignatureHelp) | ||
extend AutoCorrector | ||
|
||
# @!method checked_tests(node) | ||
def_node_search(:checked_tests, <<~PATTERN) | ||
(send _ :checked (sym :tests)) | ||
PATTERN | ||
|
||
MESSAGE = | ||
"The return value in a `.void.checked(:tests)` makes test behavior " \ | ||
"diffferent from non-test behavior. Either use " \ | ||
"`.returns(T.anything).checked(:tests)` to keep checking in tests, " \ | ||
"or `.void.checked(:never)` to leave the return completely untouched." | ||
private_constant(:MESSAGE) | ||
|
||
private def top_level_void(node) | ||
return nil unless node.is_a?(RuboCop::AST::SendNode) | ||
|
||
if node.method_name == :void | ||
node | ||
else | ||
top_level_void(node.receiver) | ||
end | ||
end | ||
|
||
def on_signature(node) | ||
checked_send = checked_tests(node).first | ||
return unless checked_send | ||
|
||
_recv, _block_args, body = *node | ||
void_send = top_level_void(body) | ||
|
||
return unless void_send | ||
|
||
add_offense( | ||
void_send.selector, | ||
message: MESSAGE, | ||
) do |corrector| | ||
corrector.replace(void_send.selector, "returns(T.anything)") | ||
end | ||
end | ||
end | ||
end | ||
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
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
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
73 changes: 73 additions & 0 deletions
73
spec/rubocop/cop/sorbet/signatures/void_checked_tests_spec.rb
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,73 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe(RuboCop::Cop::Sorbet::VoidCheckedTests, :config) do | ||
def message | ||
"The return value in a `.void.checked(:tests)` makes test behavior " \ | ||
"diffferent from non-test behavior. Either use " \ | ||
"`.returns(T.anything).checked(:tests)` to keep checking in tests, " \ | ||
"or `.void.checked(:never)` to leave the return completely untouched." | ||
end | ||
|
||
describe("offenses") do | ||
it("disallows using .void.checked(:tests)") do | ||
expect_offense(<<~RUBY) | ||
sig { void.checked(:tests) } | ||
^^^^ #{message} | ||
def foo; end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
sig { returns(T.anything).checked(:tests) } | ||
def foo; end | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
sig { void.params(x: Integer).override.checked(:tests) } | ||
^^^^ #{message} | ||
def foo(x); end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
sig { returns(T.anything).params(x: Integer).override.checked(:tests) } | ||
def foo(x); end | ||
RUBY | ||
|
||
expect_offense(<<~RUBY) | ||
sig { params(x: Integer).void.checked(:tests) } | ||
^^^^ #{message} | ||
def foo(x); end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
sig { params(x: Integer).returns(T.anything).checked(:tests) } | ||
def foo(x); end | ||
RUBY | ||
end | ||
|
||
it("allows using .returns(T.anything).checked(:tests)") do | ||
expect_no_offenses(<<~RUBY) | ||
sig { returns(T.anything).checked(:tests) } | ||
def foo; end | ||
RUBY | ||
|
||
expect_no_offenses(<<~RUBY) | ||
sig { returns(T.anything).params(x: Integer).override.checked(:tests) } | ||
def foo(x); end | ||
RUBY | ||
|
||
expect_no_offenses(<<~RUBY) | ||
sig { params(x: Integer).returns(T.anything).checked(:tests) } | ||
def foo(x); end | ||
RUBY | ||
end | ||
|
||
it("is not tripped up by the `.void` in `T.proc.void`") do | ||
expect_no_offenses(<<~RUBY) | ||
sig { params(blk: T.proc.void).returns(T.anything).checked(:tests) } | ||
def foo(&blk); end | ||
RUBY | ||
end | ||
end | ||
end |