Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Checks for `T.must(object[key])` and recommends `object.fetch(key)` instead. While usually safe, false positives are possible if `object` does not respond to `fetch`, or if `Hash#default_proc` (or similar) is being used.
- Loading branch information
1 parent
166eb19
commit 7066e9d
Showing
6 changed files
with
135 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,65 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Sorbet | ||
# rubocop:disable Lint/RedundantCopDisableDirective | ||
|
||
# Checks for `T.must(object[key])` and recommends `object.fetch(key)` instead. | ||
# | ||
# @safety | ||
# False positives are possible if `object` does not respond to `fetch`, | ||
# or if `Hash#default_proc` (or similar) is being used. | ||
# | ||
# @example | ||
# # bad | ||
# T.must(object[key]) | ||
# | ||
# # good | ||
# object.fetch(key) | ||
# | ||
# # good | ||
# object[key] | ||
# | ||
# # good | ||
# # If `object` does not `respond_to? :fetch`, or if using `Hash` `default_proc` | ||
# T.must(object[key]) # rubocop:disable Sorbet/FetchWhenMust | ||
# | ||
class FetchWhenMust < RuboCop::Cop::Base | ||
# rubocop:enable Lint/RedundantCopDisableDirective | ||
|
||
extend AutoCorrector | ||
|
||
MSG = "Use `%<expected>s` instead of `%<actual>s` when value must always be found, and receiver supports it." | ||
RESTRICT_ON_SEND = [:must].freeze | ||
|
||
# @!method t_must_on_index_result(node) | ||
def_node_matcher :t_must_on_index_result, <<~PATTERN | ||
(send | ||
(const { nil? cbase } :T) :must | ||
{ | ||
(index $_ $_) | ||
(send $_ :[] $_) | ||
} | ||
) | ||
PATTERN | ||
|
||
def on_send(node) | ||
t_must_on_index_result(node) do |receiver, key| | ||
message = format(MSG, expected: expected(receiver, key), actual: node.source) | ||
|
||
add_offense(node, message: message) do |corrector| | ||
corrector.replace(node, expected(receiver, key)) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def expected(receiver, key) | ||
"#{receiver.source}.fetch(#{key.source})" | ||
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
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,32 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe(RuboCop::Cop::Sorbet::FetchWhenMust, :config) do | ||
it "registers an offense when using `T.must(object[key])`" do | ||
expect_offense(<<~RUBY) | ||
T.must(object[key]) | ||
^^^^^^^^^^^^^^^^^^^ Use `object.fetch(key)` instead of `T.must(object[key])` when value must always be found, and receiver supports it. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
object.fetch(key) | ||
RUBY | ||
end | ||
|
||
it "does not register an offense when using `object[key]` without `T.must`" do | ||
expect_no_offenses(<<~RUBY) | ||
object[key] | ||
RUBY | ||
end | ||
|
||
it "does not register an offense when using `object[key]` without `T.must`" do | ||
expect_no_offenses(<<~RUBY) | ||
T.must(object.fetch(key)) | ||
RUBY | ||
end | ||
|
||
it "does not register an offense when using `T.must(object[key1, key2])`" do | ||
expect_no_offenses(<<~RUBY) | ||
T.must(object[key1, key2]) | ||
RUBY | ||
end | ||
end |