Skip to content

Commit 45f1b00

Browse files
niranjan-patilbbatsov
authored andcommitted
[Fix #14128] Allow long fully-qualified namespace strings to exceed max length
1 parent bd1690c commit 45f1b00

File tree

13 files changed

+218
-33
lines changed

13 files changed

+218
-33
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#14128](https://github.com/rubocop/rubocop/issues/14128): Allow long fully-qualified namespace strings to exceed max length. ([@niranjan-patil][])

config/default.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,7 @@ Layout/LineLength:
11021102
# To make it possible to copy or click on URIs in the code, we allow lines
11031103
# containing a URI to be longer than Max.
11041104
AllowURI: true
1105+
AllowQualifiedName: true
11051106
URISchemes:
11061107
- http
11071108
- https

docs/modules/ROOT/pages/cops_layout.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4935,6 +4935,10 @@ bar: "0000000000", baz: "0000000000"}
49354935
| `[]`
49364936
| Array
49374937
4938+
| AllowQualifiedName
4939+
| `true`
4940+
| Boolean
4941+
49384942
| SplitStrings
49394943
| `false`
49404944
| Boolean

lib/rubocop/cop/layout/line_length.rb

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def check_line(line, line_index)
258258
if ignore_cop_directives? && directive_on_source_line?(line_index)
259259
return check_directive_line(line, line_index)
260260
end
261-
return check_uri_line(line, line_index) if allow_uri?
261+
return check_line_for_exemptions(line, line_index) if allow_uri? || allow_qualified_name?
262262

263263
register_offense(excess_range(nil, line, line_index), line, line_index)
264264
end
@@ -358,11 +358,32 @@ def check_directive_line(line, line_index)
358358
)
359359
end
360360

361-
def check_uri_line(line, line_index)
362-
uri_range = find_excessive_uri_range(line)
363-
return if uri_range && allowed_uri_position?(line, uri_range)
361+
def check_line_for_exemptions(line, line_index)
362+
uri_range = range_if_applicable(line, :uri)
363+
qualified_name_range = range_if_applicable(line, :qualified_name)
364364

365-
register_offense(excess_range(uri_range, line, line_index), line, line_index)
365+
return if allowed_combination?(line, uri_range, qualified_name_range)
366+
367+
range = uri_range || qualified_name_range
368+
register_offense(excess_range(range, line, line_index), line, line_index)
369+
end
370+
371+
def range_if_applicable(line, type)
372+
return unless type == :uri ? allow_uri? : allow_qualified_name?
373+
374+
find_excessive_range(line, type)
375+
end
376+
377+
def allowed_combination?(line, uri_range, qualified_name_range)
378+
if uri_range && qualified_name_range
379+
allowed_position?(line, uri_range) && allowed_position?(line, qualified_name_range)
380+
elsif uri_range
381+
allowed_position?(line, uri_range)
382+
elsif qualified_name_range
383+
allowed_position?(line, qualified_name_range)
384+
else
385+
false
386+
end
366387
end
367388

368389
def breakable_dstr?(node)

lib/rubocop/cop/mixin/line_length_help.rb

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,24 @@ def allow_uri?
2525
config.for_cop('Layout/LineLength')['AllowURI']
2626
end
2727

28-
def allowed_uri_position?(line, uri_range)
29-
uri_range.begin < max_line_length && uri_range.end == line_length(line)
28+
def allow_qualified_name?
29+
config.for_cop('Layout/LineLength')['AllowQualifiedName']
30+
end
31+
32+
def allowed_position?(line, range)
33+
range.begin < max_line_length && range.end == line_length(line)
3034
end
3135

3236
def line_length(line)
3337
line.length + indentation_difference(line)
3438
end
3539

36-
def find_excessive_uri_range(line)
37-
last_uri_match = match_uris(line).last
38-
return nil unless last_uri_match
40+
def find_excessive_range(line, type)
41+
last_match = (type == :uri ? match_uris(line) : match_qualified_names(line)).last
42+
return nil unless last_match
3943

40-
begin_position, end_position = last_uri_match.offset(0)
41-
end_position = extend_uri_end_position(line, end_position)
44+
begin_position, end_position = last_match.offset(0)
45+
end_position = extend_end_position(line, end_position)
4246

4347
line_indentation_difference = indentation_difference(line)
4448
begin_position += line_indentation_difference
@@ -57,6 +61,14 @@ def match_uris(string)
5761
matches
5862
end
5963

64+
def match_qualified_names(string)
65+
matches = []
66+
string.scan(qualified_name_regexp) do
67+
matches << $LAST_MATCH_INFO
68+
end
69+
matches
70+
end
71+
6072
def indentation_difference(line)
6173
return 0 unless tab_indentation_width
6274

@@ -70,7 +82,7 @@ def indentation_difference(line)
7082
index * (tab_indentation_width - 1)
7183
end
7284

73-
def extend_uri_end_position(line, end_position)
85+
def extend_end_position(line, end_position)
7486
# Extend the end position YARD comments with linked URLs of the form {<uri> <title>}
7587
if line&.match(/{(\s|\S)*}$/)
7688
match = line[end_position..line_length(line)]&.match(/(\s|\S)*}/)
@@ -101,6 +113,10 @@ def uri_regexp
101113
end
102114
end
103115

116+
def qualified_name_regexp
117+
/\b(?:[A-Z][A-Za-z0-9_]*::)+[A-Za-z_][A-Za-z0-9_]*\b/
118+
end
119+
104120
def valid_uri?(uri_ish_string)
105121
URI.parse(uri_ish_string)
106122
true

lib/rubocop/cop/style/if_unless_modifier.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,17 @@ def too_long_line_based_on_ignore_cop_directives?(range, line)
223223

224224
def too_long_line_based_on_allow_uri?(line)
225225
if allow_uri?
226-
uri_range = find_excessive_uri_range(line)
227-
return false if uri_range && allowed_uri_position?(line, uri_range)
226+
uri_range = find_excessive_range(line, :uri)
227+
return false if uri_range && allowed_position?(line, uri_range)
228+
end
229+
230+
true
231+
end
232+
233+
def too_long_line_based_on_allow_qualified_name?(line)
234+
if allow_qualified_name?
235+
namespace_range = find_excessive_range(line, :namespace)
236+
return false if namespace_range && allowed_position?(line, namespace_range)
228237
end
229238

230239
true

lib/rubocop/lsp/diagnostic.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def code_description(config)
7979
LanguageServer::Protocol::Interface::CodeDescription.new(href: doc_url)
8080
end
8181

82-
# rubocop:disable Layout/LineLength, Metrics/MethodLength
82+
# rubocop:disable Metrics/MethodLength
8383
def autocorrect_action
8484
LanguageServer::Protocol::Interface::CodeAction.new(
8585
title: "Autocorrect #{@offense.cop_name}",
@@ -98,7 +98,7 @@ def autocorrect_action
9898
is_preferred: true
9999
)
100100
end
101-
# rubocop:enable Layout/LineLength, Metrics/MethodLength
101+
# rubocop:enable Metrics/MethodLength
102102

103103
# rubocop:disable Metrics/MethodLength
104104
def offense_replacements
@@ -120,7 +120,7 @@ def offense_replacements
120120
end
121121
# rubocop:enable Metrics/MethodLength
122122

123-
# rubocop:disable Layout/LineLength, Metrics/MethodLength
123+
# rubocop:disable Metrics/MethodLength
124124
def disable_line_action
125125
LanguageServer::Protocol::Interface::CodeAction.new(
126126
title: "Disable #{@offense.cop_name} for this line",
@@ -138,7 +138,7 @@ def disable_line_action
138138
)
139139
)
140140
end
141-
# rubocop:enable Layout/LineLength, Metrics/MethodLength
141+
# rubocop:enable Metrics/MethodLength
142142

143143
def line_disable_comment
144144
new_text = if @offense.source_line.include?(' # rubocop:disable ')

lib/ruby_lsp/rubocop/addon.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def deactivate
3434
@runtime_adapter = nil
3535
end
3636

37-
# rubocop:disable Layout/LineLength, Metrics/MethodLength
37+
# rubocop:disable Metrics/MethodLength
3838
def register_additional_file_watchers(global_state, message_queue)
3939
return unless global_state.supports_watching_files
4040

@@ -59,7 +59,7 @@ def register_additional_file_watchers(global_state, message_queue)
5959
)
6060
)
6161
end
62-
# rubocop:enable Layout/LineLength, Metrics/MethodLength
62+
# rubocop:enable Metrics/MethodLength
6363

6464
def workspace_did_change_watched_files(changes)
6565
return unless changes.any? { |change| change[:uri].end_with?('.rubocop.yml') }

spec/rubocop/cli/auto_gen_config_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def f
5151
5252
# Offense count: 2
5353
# This cop supports safe autocorrection (--autocorrect).
54-
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
54+
# Configuration parameters: AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
5555
# URISchemes: http, https
5656
Layout/LineLength:
5757
Max: 138
@@ -163,7 +163,7 @@ def f
163163
164164
# Offense count: 1
165165
# This cop supports safe autocorrection (--autocorrect).
166-
# Configuration parameters: Max, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
166+
# Configuration parameters: Max, AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
167167
# URISchemes: http, https
168168
Layout/LineLength:
169169
Exclude:
@@ -350,7 +350,7 @@ def m
350350
'# Offense count: 1',
351351
'# This cop supports safe autocorrection (--autocorrect).',
352352
'# Configuration parameters: AllowHeredoc, ' \
353-
'AllowURI, URISchemes, IgnoreCopDirectives, ' \
353+
'AllowURI, AllowQualifiedName, URISchemes, IgnoreCopDirectives, ' \
354354
'AllowedPatterns, SplitStrings.',
355355
'# URISchemes: http, https',
356356
'Layout/LineLength:',
@@ -428,7 +428,7 @@ def m
428428
'# Offense count: 1',
429429
'# This cop supports safe autocorrection (--autocorrect).',
430430
'# Configuration parameters: AllowHeredoc, ' \
431-
'AllowURI, URISchemes, IgnoreCopDirectives, ' \
431+
'AllowURI, AllowQualifiedName, URISchemes, IgnoreCopDirectives, ' \
432432
'AllowedPatterns, SplitStrings.',
433433
'# URISchemes: http, https',
434434
'Layout/LineLength:',
@@ -499,7 +499,7 @@ def f
499499
500500
# Offense count: 2
501501
# This cop supports safe autocorrection (--autocorrect).
502-
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
502+
# Configuration parameters: AllowHeredoc, AllowURI, AllowQualifiedName, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
503503
# URISchemes: http, https
504504
Layout/LineLength:
505505
Max: 138
@@ -930,7 +930,7 @@ def a; end
930930
'# Offense count: 2',
931931
'# This cop supports safe autocorrection (--autocorrect).',
932932
'# Configuration parameters: AllowHeredoc, ' \
933-
'AllowURI, URISchemes, IgnoreCopDirectives, ' \
933+
'AllowURI, AllowQualifiedName, URISchemes, IgnoreCopDirectives, ' \
934934
'AllowedPatterns, SplitStrings.',
935935
'# URISchemes: http, https',
936936
'Layout/LineLength:',
@@ -1024,7 +1024,7 @@ def a; end
10241024
'# Offense count: 3',
10251025
'# This cop supports safe autocorrection (--autocorrect).',
10261026
'# Configuration parameters: AllowHeredoc, ' \
1027-
'AllowURI, URISchemes, IgnoreCopDirectives, ' \
1027+
'AllowURI, AllowQualifiedName, URISchemes, IgnoreCopDirectives, ' \
10281028
'AllowedPatterns, SplitStrings.',
10291029
'# URISchemes: http, https',
10301030
'Layout/LineLength:',
@@ -1328,7 +1328,7 @@ def a; end
13281328
'',
13291329
'# This cop supports safe autocorrection (--autocorrect).',
13301330
'# Configuration parameters: AllowHeredoc, ' \
1331-
'AllowURI, URISchemes, IgnoreCopDirectives, ' \
1331+
'AllowURI, AllowQualifiedName, URISchemes, IgnoreCopDirectives, ' \
13321332
'AllowedPatterns, SplitStrings.',
13331333
'# URISchemes: http, https',
13341334
'Layout/LineLength:',

spec/rubocop/cli/options_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1891,7 +1891,7 @@ def expect_offense_detected
18911891
context 'when the cop has the "info" severity' do
18921892
before do
18931893
create_file(target_file, <<~RUBY)
1894-
Long::Line::Not::Autocorrectable
1894+
some_object.some_method.another_method.yet_another_method
18951895
RUBY
18961896

18971897
create_file('.rubocop.yml', <<~YAML)

0 commit comments

Comments
 (0)