Skip to content

Commit 882519b

Browse files
[Fix #14716] Fix infinite loop for Layout/LineLength with SplitStrings
1 parent 66e3214 commit 882519b

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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][])

lib/rubocop/cop/layout/line_length.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff 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

spec/rubocop/cli/autocorrect_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff 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
41814207
end

spec/rubocop/cop/layout/line_length_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)