-
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.
Merge pull request #263 from corsonknowles/main
Add Sorbet/Refinement
- Loading branch information
Showing
6 changed files
with
162 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,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Sorbet | ||
# Checks for the use of Ruby Refinements library. Refinements add | ||
# complexity and incur a performance penalty that can be significant | ||
# for large code bases. Good examples are cases of unrelated | ||
# methods that happen to have the same name as these module methods. | ||
# | ||
# @example | ||
# # bad | ||
# module Foo | ||
# refine(Date) do | ||
# end | ||
# end | ||
# | ||
# # bad | ||
# module Foo | ||
# using(Date) do | ||
# end | ||
# end | ||
# | ||
# # good | ||
# module Foo | ||
# bar.refine(Date) | ||
# end | ||
# | ||
# # good | ||
# module Foo | ||
# bar.using(Date) | ||
# end | ||
|
||
class Refinement < Base | ||
MSG = "Do not use Ruby Refinements library as it is not supported by Sorbet." | ||
RESTRICT_ON_SEND = [:refine, :using].freeze | ||
|
||
def on_send(node) | ||
return unless node.receiver.nil? | ||
return unless node.first_argument&.const_type? | ||
|
||
if node.method?(:refine) | ||
return unless node.block_node | ||
return unless node.parent.parent.module_type? | ||
end | ||
|
||
add_offense(node) | ||
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,63 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe(RuboCop::Cop::Sorbet::Refinement, :config) do | ||
it "reports an offense for use of using" do | ||
expect_offense(<<~RUBY, "my_class.rb") | ||
using MyRefinement | ||
^^^^^^^^^^^^^^^^^^ Do not use Ruby Refinements library as it is not supported by Sorbet. | ||
RUBY | ||
end | ||
|
||
it "reports an offense for use of refine" do | ||
expect_offense(<<~RUBY, "my_refinement.rb") | ||
module MyRefinement | ||
refine(String) do | ||
^^^^^^^^^^^^^^ Do not use Ruby Refinements library as it is not supported by Sorbet. | ||
def to_s | ||
"foo" | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it "reports no offense for use of using with non-const argument" do | ||
expect_no_offenses(<<~RUBY, "my_class.rb") | ||
using "foo" | ||
RUBY | ||
end | ||
|
||
it "reports no offense for use of refine with non-const argument" do | ||
expect_no_offenses(<<~RUBY, "my_refinement.rb") | ||
module MyRefinement | ||
refine "foo" do | ||
def to_s | ||
"foo" | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it "reports no offense for use of refine with no block argument" do | ||
expect_no_offenses(<<~RUBY, "my_refinement.rb") | ||
module MyRefinement | ||
refine(String) | ||
end | ||
RUBY | ||
end | ||
|
||
it "reports no offense for use of refine outside of module" do | ||
expect_no_offenses(<<~RUBY, "my_refinement.rb") | ||
module MyNamespace | ||
class MyClass | ||
refine(String) do | ||
def to_s | ||
"foo" | ||
end | ||
end | ||
end | ||
end | ||
RUBY | ||
end | ||
end |