diff --git a/lib/rubocop/cop/sorbet/forbid_t_struct.rb b/lib/rubocop/cop/sorbet/forbid_t_struct.rb index b8de9bf..72ba824 100644 --- a/lib/rubocop/cop/sorbet/forbid_t_struct.rb +++ b/lib/rubocop/cop/sorbet/forbid_t_struct.rb @@ -219,8 +219,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 diff --git a/spec/rubocop/cop/sorbet/forbid_t_struct_spec.rb b/spec/rubocop/cop/sorbet/forbid_t_struct_spec.rb index e113c55..ddc4a76 100644 --- a/spec/rubocop/cop/sorbet/forbid_t_struct_spec.rb +++ b/spec/rubocop/cop/sorbet/forbid_t_struct_spec.rb @@ -434,5 +434,56 @@ def initialize(foo: nil) 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