File tree Expand file tree Collapse file tree 4 files changed +50
-2
lines changed
Expand file tree Collapse file tree 4 files changed +50
-2
lines changed Original file line number Diff line number Diff line change 1+ * [ #14716 ] ( https://github.com/rubocop/rubocop/issues/14716 ) : Fix an infinite loop error for ` Layout/LineLength ` when ` SplitStrings ` option is enabled and strings span multiple lines. ([ @HariprasanthMSH ] [ ] )
Original file line number Diff line number Diff line change @@ -418,8 +418,11 @@ def largest_possible_string(node)
418418 # The maximum allowed length of a string value is:
419419 # `Max` - end delimiter (quote) - continuation characters (space and slash)
420420 max_length = max - 3
421- # If the string doesn't start at the beginning of the line, the max length is offset
422- max_length -= column_offset_between ( node . loc , node . parent . loc ) if node . parent
421+ # If the string is on the same line as its parent, offset by the column difference
422+ # (Only apply when on same line to avoid negative offsets for multi-line dstr)
423+ if same_line? ( node , node . parent )
424+ max_length -= column_offset_between ( node . loc , node . parent . loc )
425+ end
423426 node . source [ 0 ...( max_length ) ]
424427 end
425428 end
Original file line number Diff line number Diff line change @@ -4178,4 +4178,30 @@ class MyClass
41784178 end
41794179 RUBY
41804180 end
4181+
4182+ it 'does not cause an infinite loop for Layout/LineLength with SplitStrings' do
4183+ create_file ( '.rubocop.yml' , <<~YAML )
4184+ Layout/LineLength:
4185+ Enabled: true
4186+ Max: 30
4187+ SplitStrings: true
4188+ YAML
4189+
4190+ source_file = Pathname ( 'example.rb' )
4191+ create_file ( source_file , <<~RUBY )
4192+ _const = '000000000000000 0000000000000000000000000000 000000000000000000000000000'
4193+ RUBY
4194+
4195+ status = cli . run ( [ '--autocorrect-all' ] )
4196+ expect ( status ) . to eq ( 0 )
4197+ expect ( $stderr. string ) . to eq ( '' )
4198+ expect ( source_file . read ) . to eq ( <<~RUBY )
4199+ # frozen_string_literal: true
4200+
4201+ _const = '000000000000000 ' \\
4202+ '00000000000000000000000000' \\
4203+ '00 ' \\
4204+ '000000000000000000000000000'
4205+ RUBY
4206+ end
41814207end
Original file line number Diff line number Diff line change @@ -1058,6 +1058,24 @@ def method_definition_that_is_just_under_the_line_length_limit(foo) # rubocop:di
10581058 end
10591059 end
10601060 end
1061+
1062+ context 'when the string has no spaces and spans multiple words with `Max` set to 30' do
1063+ let ( :cop_config ) { super ( ) . merge ( 'Max' => 30 ) }
1064+
1065+ it 'registers an offense and corrects by splitting the string' do
1066+ expect_offense ( <<~RUBY )
1067+ _const = '000000000000000 0000000000000000000000000000 000000000000000000000000000'
1068+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [83/30]
1069+ RUBY
1070+
1071+ expect_correction ( <<~'RUBY' )
1072+ _const = '000000000000000 ' \
1073+ '00000000000000000000000000' \
1074+ '00 ' \
1075+ '000000000000000000000000000'
1076+ RUBY
1077+ end
1078+ end
10611079 end
10621080
10631081 context 'when SplitStrings: false' do
You can’t perform that action at this time.
0 commit comments