Skip to content

Commit

Permalink
Merge pull request #203 from Shopify/at-tmp
Browse files Browse the repository at this point in the history
`ForbidTStruct` cop's autocorrect should support new lines and adhere to max line length
  • Loading branch information
KaanOzkan committed Mar 6, 2024
2 parents 5cb3256 + dcf2f1c commit bd50ee1
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 3 deletions.
38 changes: 35 additions & 3 deletions 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,11 @@ def initialize_assign
def nilable?
type.start_with?("T.nilable(")
end

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

# @!method t_struct?(node)
Expand Down Expand Up @@ -215,8 +220,35 @@ def initialize_method(indent, props)
sorted_props = props.sort_by { |prop| prop.default || prop.factory || prop.nilable? ? 1 : 0 }

string = +"\n"
string << "#{indent}sig { params(#{sorted_props.map(&:initialize_sig_param).join(", ")}).void }\n"
string << "#{indent}def initialize(#{sorted_props.map(&:initialize_param).join(", ")})\n"

line = "#{indent}sig { params(#{sorted_props.map(&:initialize_sig_param).join(", ")}).void }\n"
if line.length <= max_line_length
string << line
else
string << "#{indent}sig do\n"
string << "#{indent} params(\n"
sorted_props.each do |prop|
string << "#{indent} #{prop.initialize_sig_param}"
string << "," if prop != sorted_props.last
string << "\n"
end
string << "#{indent} ).void\n"
string << "#{indent}end\n"
end

line = "#{indent}def initialize(#{sorted_props.map(&:initialize_param).join(", ")})\n"
if line.length <= max_line_length
string << line
else
string << "#{indent}def initialize(\n"
sorted_props.each do |prop|
string << "#{indent} #{prop.initialize_param}"
string << "," if prop != sorted_props.last
string << "\n"
end
string << "#{indent})\n"
end

props.each do |prop|
string << "#{indent} #{prop.initialize_assign}\n"
end
Expand Down
134 changes: 134 additions & 0 deletions spec/rubocop/cop/sorbet/forbid_t_struct_spec.rb
Expand Up @@ -408,5 +408,139 @@ 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
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

it "splits long lines" do
source = <<~RUBY
class Foo < T::Struct
const :foo, LONG_CONSTANT_NAME_WITH_MANY_CHARS, default: LONG_CONSTANT_NAME_WITH_MANY_CHARS
const :bar, LONG_CONSTANT_NAME_WITH_MANY_CHARS, default: LONG_CONSTANT_NAME_WITH_MANY_CHARS
const :baz, LONG_CONSTANT_NAME_WITH_MANY_CHARS, default: LONG_CONSTANT_NAME_WITH_MANY_CHARS
const :qux, LONG_CONSTANT_NAME_WITH_MANY_CHARS, default: LONG_CONSTANT_NAME_WITH_MANY_CHARS
end
RUBY

corrected = <<~RUBY
class Foo
extend T::Sig
sig { returns(LONG_CONSTANT_NAME_WITH_MANY_CHARS) }
attr_reader :foo
sig { returns(LONG_CONSTANT_NAME_WITH_MANY_CHARS) }
attr_reader :bar
sig { returns(LONG_CONSTANT_NAME_WITH_MANY_CHARS) }
attr_reader :baz
sig { returns(LONG_CONSTANT_NAME_WITH_MANY_CHARS) }
attr_reader :qux
sig do
params(
foo: LONG_CONSTANT_NAME_WITH_MANY_CHARS,
bar: LONG_CONSTANT_NAME_WITH_MANY_CHARS,
baz: LONG_CONSTANT_NAME_WITH_MANY_CHARS,
qux: LONG_CONSTANT_NAME_WITH_MANY_CHARS
).void
end
def initialize(
foo: LONG_CONSTANT_NAME_WITH_MANY_CHARS,
bar: LONG_CONSTANT_NAME_WITH_MANY_CHARS,
baz: LONG_CONSTANT_NAME_WITH_MANY_CHARS,
qux: LONG_CONSTANT_NAME_WITH_MANY_CHARS
)
@foo = foo
@bar = bar
@baz = baz
@qux = qux
end
end
RUBY

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

0 comments on commit bd50ee1

Please sign in to comment.