-
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.
Do not use Ruby Refinements library as it is not supported by Sorbet.
- Loading branch information
1 parent
f4ca5d4
commit dc7a6d7
Showing
6 changed files
with
129 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,43 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Sorbet | ||
# This cop checks for the use of Ruby Refinements library. | ||
# Good examples below are examples of unrelated object methods that | ||
# incidentally have the same name as the module methods we check for | ||
# | ||
# @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 = %i[refine using].freeze | ||
|
||
def on_send(node) | ||
add_offense(node) if node.receiver.nil? | ||
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,57 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Sorbet::Refinement, :config do | ||
it 'registers an offense for using refine' do | ||
expect_offense(<<-RUBY) | ||
module Foo | ||
refine(Date) do | ||
^^^^^^^^^^^^ Do not use Ruby Refinements library as it is not supported by Sorbet. | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for using a refinement' do | ||
expect_offense(<<-RUBY) | ||
module Foo | ||
using(Date) | ||
^^^^^^^^^^^ Do not use Ruby Refinements library as it is not supported by Sorbet. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for using refine in a class' do | ||
expect_offense(<<-RUBY) | ||
class Foo | ||
refine(Date) do | ||
^^^^^^^^^^^^ Do not use Ruby Refinements library as it is not supported by Sorbet. | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for using a refinement in a class' do | ||
expect_offense(<<-RUBY) | ||
class Foo | ||
using(Date) | ||
^^^^^^^^^^^ Do not use Ruby Refinements library as it is not supported by Sorbet. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers no offense for using non-refinement method #refine' do | ||
expect_no_offenses(<<-RUBY) | ||
module Foo | ||
bar.refine(Date) | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers no offense for using non-refinement method #using' do | ||
expect_no_offenses(<<-RUBY) | ||
module Foo | ||
bar.using(Date) | ||
end | ||
RUBY | ||
end | ||
end |