Skip to content

Commit

Permalink
Strip spaces from type definitions
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
  • Loading branch information
Morriar authored and KaanOzkan committed Mar 5, 2024
1 parent 217960f commit cdfa443
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/rubocop/cop/sorbet/forbid_t_struct.rb
Expand Up @@ -102,7 +102,7 @@ def on_send(node)
end

class Property
attr_reader :node, :kind, :name, :type, :default, :factory
attr_reader :node, :kind, :name, :default, :factory

def initialize(node, kind, name, type, default:, factory:)
@node = node
Expand Down Expand Up @@ -151,6 +151,10 @@ def initialize_assign
def nilable?
type.start_with?("T.nilable(")
end

def type
@type.gsub(/[[:space:]]+/, "").strip
end
end

# @!method t_struct?(node)
Expand Down
26 changes: 26 additions & 0 deletions spec/rubocop/cop/sorbet/forbid_t_struct_spec.rb
Expand Up @@ -408,5 +408,31 @@ def initialize(baz:, foo: nil, bar: nil)

expect(autocorrect_source(source)).to(eq(corrected))
end

it "strips new lines from type definitions" do
source = <<~RUBY
class Foo < T::Struct
const :foo, T.nilable(
Integer
)
end
RUBY

corrected = <<~RUBY
class Foo
extend T::Sig
sig { returns(T.nilable(Integer)) }
attr_reader :foo
sig { params(foo: T.nilable(Integer)).void }
def initialize(foo: nil)
@foo = foo
end
end
RUBY

expect(autocorrect_source(source)).to(eq(corrected))
end
end
end

0 comments on commit cdfa443

Please sign in to comment.