Skip to content

Commit

Permalink
Add Sorbet/Refinement
Browse files Browse the repository at this point in the history
Do not use Ruby Refinements library as it is not supported by Sorbet.
  • Loading branch information
corsonknowles committed Sep 18, 2024
1 parent f4ca5d4 commit dc7a6d7
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Sorbet/Refinement:
Description: Do not use Ruby Refinements library as it is not supported by Sorbet.
Enabled: pending
VersionAdded: '<<next>>'

inherit_mode:
merge:
- Exclude
Expand Down
43 changes: 43 additions & 0 deletions lib/rubocop/cop/sorbet/refinement.rb
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
1 change: 1 addition & 0 deletions lib/rubocop/cop/sorbet_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
require_relative "sorbet/forbid_t_unsafe"
require_relative "sorbet/forbid_t_untyped"
require_relative "sorbet/redundant_extend_t_sig"
require_relative 'sorbet/refinement'
require_relative "sorbet/type_alias_name"
require_relative "sorbet/obsolete_strict_memoization"
require_relative "sorbet/buggy_obsolete_strict_memoization"
Expand Down
1 change: 1 addition & 0 deletions manual/cops.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ In the following section you find all available cops:
* [Sorbet/MultipleTEnumValues](cops_sorbet.md#sorbetmultipletenumvalues)
* [Sorbet/ObsoleteStrictMemoization](cops_sorbet.md#sorbetobsoletestrictmemoization)
* [Sorbet/RedundantExtendTSig](cops_sorbet.md#sorbetredundantextendtsig)
* [Sorbet/Refinement](cops_sorbet.md#sorbetrefinement)
* [Sorbet/SignatureBuildOrder](cops_sorbet.md#sorbetsignaturebuildorder)
* [Sorbet/SingleLineRbiClassModuleDefinitions](cops_sorbet.md#sorbetsinglelinerbiclassmoduledefinitions)
* [Sorbet/StrictSigil](cops_sorbet.md#sorbetstrictsigil)
Expand Down
22 changes: 22 additions & 0 deletions manual/cops_sorbet.md
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,28 @@ class Example
end
```
## Sorbet/Refinement
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
--- | --- | --- | --- | ---
Enabled | Yes | No | <<next>> | -
# bad
module Foo
using(Date) do
end
end
# good
module Foo
bar.refine(Date)
end
# good
module Foo
bar.using(Date)
end
## Sorbet/SignatureBuildOrder
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
Expand Down
57 changes: 57 additions & 0 deletions spec/rubocop/cop/sorbet/refinement_spec.rb
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

0 comments on commit dc7a6d7

Please sign in to comment.