Skip to content

Commit

Permalink
Handle spaces in type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
KaanOzkan committed Mar 6, 2024
1 parent 0ef3f07 commit dcf2f1c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/rubocop/cop/sorbet/forbid_t_struct.rb
Expand Up @@ -153,7 +153,8 @@ def nilable?
end

def type
@type.gsub(/[[:space:]]+/, "").strip
copy = @type.gsub(/[[:space:]]+/, "").strip # Remove newlines and spaces
copy.gsub(",", ", ") # Add a space after each comma
end
end

Expand Down
57 changes: 57 additions & 0 deletions spec/rubocop/cop/sorbet/forbid_t_struct_spec.rb
Expand Up @@ -409,6 +409,63 @@ def initialize(baz:, foo: nil, bar: nil)
expect(autocorrect_source(source)).to(eq(corrected))
end

it "handles spaces in types" do
source = <<~RUBY
class Foo < T::Struct
const :foo, T.any(Integer, String)
const :bar, T.nilable(
T.any(Integer, String)
)
const :baz, T::Hash[Integer, String]
const :qux, T::Hash[Symbol, T.any(Integer, String)]
const :quux, T.any(
T::Hash[Integer, String],
String
)
end
RUBY

corrected = <<~RUBY
class Foo
extend T::Sig
sig { returns(T.any(Integer, String)) }
attr_reader :foo
sig { returns(T.nilable(T.any(Integer, String))) }
attr_reader :bar
sig { returns(T::Hash[Integer, String]) }
attr_reader :baz
sig { returns(T::Hash[Symbol, T.any(Integer, String)]) }
attr_reader :qux
sig { returns(T.any(T::Hash[Integer, String], String)) }
attr_reader :quux
sig do
params(
foo: T.any(Integer, String),
baz: T::Hash[Integer, String],
qux: T::Hash[Symbol, T.any(Integer, String)],
quux: T.any(T::Hash[Integer, String], String),
bar: T.nilable(T.any(Integer, String))
).void
end
def initialize(foo:, baz:, qux:, quux:, bar: nil)
@foo = foo
@bar = bar
@baz = baz
@qux = qux
@quux = quux
end
end
RUBY

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

it "strips new lines from type definitions" do
source = <<~RUBY
class Foo < T::Struct
Expand Down

0 comments on commit dcf2f1c

Please sign in to comment.