Skip to content

Commit 1386f77

Browse files
dvandersluismergify[bot]
authored andcommitted
Updated Layout/LineLength, Style/CharacterLiteral, Style/IpAddresses, Style/StringLiterals, Style/StringLiteralsInInterpolation to use Base.
Updated `StringHelp` and `StringLiteralCorrector` for use with `Base` instead of `Cop`.
1 parent 8f2fdbe commit 1386f77

File tree

7 files changed

+45
-48
lines changed

7 files changed

+45
-48
lines changed

lib/rubocop/cop/correctors/string_literal_corrector.rb

+6-8
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@ class StringLiteralCorrector
77
extend Util
88

99
class << self
10-
def correct(node, style)
10+
def correct(corrector, node, style)
1111
return if node.dstr_type?
1212

13-
lambda do |corrector|
14-
str = node.str_content
15-
if style == :single_quotes
16-
corrector.replace(node, to_string_literal(str))
17-
else
18-
corrector.replace(node, str.inspect)
19-
end
13+
str = node.str_content
14+
if style == :single_quotes
15+
corrector.replace(node, to_string_literal(str))
16+
else
17+
corrector.replace(node, str.inspect)
2018
end
2119
end
2220
end

lib/rubocop/cop/layout/line_length.rb

+6-16
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ module Layout
5858
# bar: "0000000000",
5959
# baz: "0000000000",
6060
# }
61-
class LineLength < Cop
61+
class LineLength < Base
6262
include CheckLineBreakable
6363
include ConfigurableMax
6464
include IgnoredPattern
6565
include RangeHelp
6666
include LineLengthHelp
67+
extend AutoCorrector
6768

6869
MSG = 'Line is too long. [%<length>d/%<max>d]'
6970

@@ -78,28 +79,16 @@ def on_potential_breakable_node(node)
7879
alias on_hash on_potential_breakable_node
7980
alias on_send on_potential_breakable_node
8081

81-
def investigate(processed_source)
82+
def on_new_investigation
8283
check_for_breakable_semicolons(processed_source)
8384
end
8485

85-
def investigate_post_walk(processed_source)
86+
def on_investigation_end
8687
processed_source.lines.each_with_index do |line, line_index|
8788
check_line(line, line_index)
8889
end
8990
end
9091

91-
def correctable?
92-
super && !breakable_range.nil?
93-
end
94-
95-
def autocorrect(range)
96-
return if range.nil?
97-
98-
lambda do |corrector|
99-
corrector.insert_before(range, "\n")
100-
end
101-
end
102-
10392
private
10493

10594
attr_accessor :breakable_range
@@ -203,8 +192,9 @@ def register_offense(loc, line, line_index)
203192

204193
self.breakable_range = breakable_range_by_line_index[line_index]
205194

206-
add_offense(breakable_range, location: loc, message: message) do
195+
add_offense(loc, message: message) do |corrector|
207196
self.max = line_length(line)
197+
corrector.insert_before(breakable_range, "\n") unless breakable_range.nil?
208198
end
209199
end
210200

lib/rubocop/cop/mixin/string_help.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ def on_str(node)
1414
return if part_of_ignored_node?(node)
1515

1616
if offense?(node)
17-
add_offense(node) { opposite_style_detected }
17+
add_offense(node) do |corrector|
18+
opposite_style_detected
19+
autocorrect(corrector, node) if respond_to?(:autocorrect, true)
20+
end
1821
else
1922
correct_style_detected
2023
end

lib/rubocop/cop/style/character_literal.rb

+10-11
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ module Style
1414
#
1515
# # good
1616
# ?\C-\M-d
17-
class CharacterLiteral < Cop
17+
class CharacterLiteral < Base
1818
include StringHelp
19+
extend AutoCorrector
1920

2021
MSG = 'Do not use the character literal - ' \
2122
'use string literal instead.'
@@ -26,17 +27,15 @@ def offense?(node)
2627
node.source.size.between?(2, 3)
2728
end
2829

29-
def autocorrect(node)
30-
lambda do |corrector|
31-
string = node.source[1..-1]
30+
def autocorrect(corrector, node)
31+
string = node.source[1..-1]
3232

33-
# special character like \n
34-
# or ' which needs to use "" or be escaped.
35-
if string.length == 2 || string == "'"
36-
corrector.replace(node, %("#{string}"))
37-
elsif string.length == 1 # normal character
38-
corrector.replace(node, "'#{string}'")
39-
end
33+
# special character like \n
34+
# or ' which needs to use "" or be escaped.
35+
if string.length == 2 || string == "'"
36+
corrector.replace(node, %("#{string}"))
37+
elsif string.length == 1 # normal character
38+
corrector.replace(node, "'#{string}'")
4039
end
4140
end
4241

lib/rubocop/cop/style/ip_addresses.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Style
1818
#
1919
# # good
2020
# ip_address = ENV['DEPLOYMENT_IP_ADDRESS']
21-
class IpAddresses < Cop
21+
class IpAddresses < Base
2222
include StringHelp
2323

2424
IPV6_MAX_SIZE = 45 # IPv4-mapped IPv6 is the longest

lib/rubocop/cop/style/string_literals.rb

+14-8
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ module Style
2626
# "Just some text"
2727
# "No special chars or interpolation"
2828
# "Every string in #{project} uses double_quotes"
29-
class StringLiterals < Cop
29+
class StringLiterals < Base
3030
include ConfigurableEnforcedStyle
3131
include StringLiteralsHelp
32+
extend AutoCorrector
3233

3334
MSG_INCONSISTENT = 'Inconsistent quote style.'
3435

@@ -46,19 +47,25 @@ def on_dstr(node)
4647
quote_styles = detect_quote_styles(node)
4748

4849
if quote_styles.size > 1
49-
add_offense(node, message: MSG_INCONSISTENT)
50+
register_offense(node, message: MSG_INCONSISTENT)
5051
else
5152
check_multiline_quote_style(node, quote_styles[0])
5253
end
5354

5455
ignore_node(node)
5556
end
5657

57-
def autocorrect(node)
58-
StringLiteralCorrector.correct(node, style)
58+
private
59+
60+
def autocorrect(corrector, node)
61+
StringLiteralCorrector.correct(corrector, node, style)
5962
end
6063

61-
private
64+
def register_offense(node, message: nil)
65+
add_offense(node, message: message || message(node)) do |corrector|
66+
autocorrect(corrector, node)
67+
end
68+
end
6269

6370
def all_string_literals?(nodes)
6471
nodes.all? { |n| n.str_type? || n.dstr_type? }
@@ -99,14 +106,13 @@ def consistent_multiline?
99106
end
100107

101108
def check_multiline_quote_style(node, quote)
102-
range = node.source_range
103109
children = node.children
104110
if unexpected_single_quotes?(quote)
105111
all_children_with_quotes = children.all? { |c| wrong_quotes?(c) }
106-
add_offense(node, location: range) if all_children_with_quotes
112+
register_offense(node) if all_children_with_quotes
107113
elsif unexpected_double_quotes?(quote) &&
108114
!accept_child_double_quotes?(children)
109-
add_offense(node, location: range)
115+
register_offense(node)
110116
end
111117
end
112118

lib/rubocop/cop/style/string_literals_in_interpolation.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ module Style
1919
#
2020
# # good
2121
# result = "Tests #{success ? "PASS" : "FAIL"}"
22-
class StringLiteralsInInterpolation < Cop
22+
class StringLiteralsInInterpolation < Base
2323
include ConfigurableEnforcedStyle
2424
include StringLiteralsHelp
25+
extend AutoCorrector
2526

26-
def autocorrect(node)
27-
StringLiteralCorrector.correct(node, style)
27+
def autocorrect(corrector, node)
28+
StringLiteralCorrector.correct(corrector, node, style)
2829
end
2930

3031
private

0 commit comments

Comments
 (0)