Skip to content

Commit

Permalink
Reimplement RBS/Style/InstanceWithInstance
Browse files Browse the repository at this point in the history
- Module is not target since it has a different meaning from `instance` and `self`.
- Interface has `interface` as invalid.
- The meaning of `self` and `instance` changes in generic class.
  • Loading branch information
ksss committed Dec 24, 2024
1 parent 50c829d commit 3a79e53
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 18 deletions.
28 changes: 16 additions & 12 deletions lib/rubocop/cop/rbs/style/instance_with_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@ class InstanceWithInstance < RuboCop::RBS::CopBase
extend AutoCorrector
MSG = 'Use `self` instead of `instance`.'

def on_rbs_def(decl)
return unless decl.kind == :instance
# @rbs decl: RBS::AST::Declarations::Class
def on_rbs_class(decl)
# The meaning of `self` and `instance` changes in generic class.
return unless decl.type_params.empty?

decl.overloads.each do |overload|
overload.method_type.each_type do |type|
check_type(type)
end
end
end
decl.members.each do |member|
case member
when ::RBS::AST::Members::MethodDefinition
next unless member.kind == :instance

def on_rbs_var(decl)
case decl
when ::RBS::AST::Members::InstanceVariable
check_type(decl.type)
member.overloads.each do |overload|
overload.method_type.each_type do |type|
check_type(type)
end
end
when ::RBS::AST::Members::InstanceVariable
check_type(member.type)
end
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ module RuboCop

MSG: ::String

def on_rbs_def: (untyped decl) -> untyped

def on_rbs_var: (untyped decl) -> untyped
# @rbs decl: RBS::AST::Declarations::Class
def on_rbs_class: (RBS::AST::Declarations::Class decl) -> untyped

def check_type: (untyped type) -> untyped
end
Expand Down
56 changes: 53 additions & 3 deletions spec/rubocop/cop/rbs/style/instance_with_instance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
it 'registers an offense' do
expect_offense(<<~RBS)
class Foo
def foo: () -> instance
^^^^^^^^ Use `self` instead of `instance`.
def foo: (instance) -> instance
^^^^^^^^ Use `self` instead of `instance`.
^^^^^^^^ Use `self` instead of `instance`.
def bar: [T] () { () [self: instance] -> void } -> T
^^^^^^^^ Use `self` instead of `instance`.
def self.foo: () -> instance
Expand All @@ -24,7 +28,9 @@ def self?.foo: () -> instance

expect_correction(<<~RBS)
class Foo
def foo: () -> self
def foo: (self) -> self
def bar: [T] () { () [self: self] -> void } -> T
def self.foo: () -> instance
Expand All @@ -38,4 +44,48 @@ def self?.foo: () -> instance
end
RBS
end

it 'registers an offense on inherited generic class' do
expect_offense(<<~RBS)
class Bar < Foo[Integer]
def foo: (instance) -> instance
^^^^^^^^ Use `self` instead of `instance`.
^^^^^^^^ Use `self` instead of `instance`.
end
RBS

expect_correction(<<~RBS)
class Bar < Foo[Integer]
def foo: (self) -> self
end
RBS
end

it 'does not register an offense' do
expect_no_offenses(<<~RBS)
class Generic[T]
def a: () -> instance
end
RBS

expect_no_offenses(<<~RBS)
module Foo
def foo: () -> instance
@ivar: instance
end
class Bar
module Foo
def foo: () -> instance
end
end
RBS

expect_no_offenses(<<~RBS)
interface _Foo
def foo: () -> instance
end
RBS
end
end

0 comments on commit 3a79e53

Please sign in to comment.