Skip to content

Commit ba2a7e3

Browse files
authored
Merge pull request #621 from nvasilevski/improve-read-write-attribute-message
[ReadWriteAttribute] Improve cop message to be more dynamic
2 parents f271b4e + 3d3fbdc commit ba2a7e3

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

lib/rubocop/cop/rails/read_write_attribute.rb

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module Rails
3737
class ReadWriteAttribute < Base
3838
extend AutoCorrector
3939

40-
MSG = 'Prefer `%<prefer>s` over `%<current>s`.'
40+
MSG = 'Prefer `%<prefer>s`.'
4141
RESTRICT_ON_SEND = %i[read_attribute write_attribute].freeze
4242

4343
def_node_matcher :read_write_attribute?, <<~PATTERN
@@ -51,15 +51,8 @@ def on_send(node)
5151
return unless read_write_attribute?(node)
5252
return if within_shadowing_method?(node)
5353

54-
add_offense(node.loc.selector, message: message(node)) do |corrector|
55-
case node.method_name
56-
when :read_attribute
57-
replacement = read_attribute_replacement(node)
58-
when :write_attribute
59-
replacement = write_attribute_replacement(node)
60-
end
61-
62-
corrector.replace(node.source_range, replacement)
54+
add_offense(node, message: build_message(node)) do |corrector|
55+
corrector.replace(node.source_range, node_replacement(node))
6356
end
6457
end
6558

@@ -74,12 +67,32 @@ def within_shadowing_method?(node)
7467
end
7568
end
7669

77-
def message(node)
70+
def build_message(node)
71+
if node.single_line?
72+
single_line_message(node)
73+
else
74+
multi_line_message(node)
75+
end
76+
end
77+
78+
def single_line_message(node)
79+
format(MSG, prefer: node_replacement(node))
80+
end
81+
82+
def multi_line_message(node)
7883
if node.method?(:read_attribute)
79-
format(MSG, prefer: 'self[:attr]', current: 'read_attribute(:attr)')
84+
format(MSG, prefer: 'self[:attr]')
8085
else
81-
format(MSG, prefer: 'self[:attr] = val',
82-
current: 'write_attribute(:attr, val)')
86+
format(MSG, prefer: 'self[:attr] = val')
87+
end
88+
end
89+
90+
def node_replacement(node)
91+
case node.method_name
92+
when :read_attribute
93+
read_attribute_replacement(node)
94+
when :write_attribute
95+
write_attribute_replacement(node)
8396
end
8497
end
8598

spec/rubocop/cop/rails/read_write_attribute_spec.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
it 'registers an offense and corrects a symbol' do
66
expect_offense(<<~RUBY)
77
res = read_attribute(:test)
8-
^^^^^^^^^^^^^^ Prefer `self[:attr]` over `read_attribute(:attr)`.
8+
^^^^^^^^^^^^^^^^^^^^^ Prefer `self[:test]`.
99
RUBY
1010

1111
expect_correction(<<~RUBY)
@@ -16,7 +16,7 @@
1616
it 'register an offense and corrects a string' do
1717
expect_offense(<<~RUBY)
1818
res = read_attribute('test')
19-
^^^^^^^^^^^^^^ Prefer `self[:attr]` over `read_attribute(:attr)`.
19+
^^^^^^^^^^^^^^^^^^^^^^ Prefer `self['test']`.
2020
RUBY
2121

2222
expect_correction(<<~RUBY)
@@ -36,7 +36,7 @@ def foo
3636
expect_offense(<<~RUBY)
3737
def foo
3838
bar || read_attribute(:baz)
39-
^^^^^^^^^^^^^^ Prefer `self[:attr]` over `read_attribute(:attr)`.
39+
^^^^^^^^^^^^^^^^^^^^ Prefer `self[:baz]`.
4040
end
4141
RUBY
4242

@@ -50,7 +50,7 @@ def foo
5050
it 'autocorrects without parentheses' do
5151
expect_offense(<<~RUBY)
5252
res = read_attribute 'test'
53-
^^^^^^^^^^^^^^ Prefer `self[:attr]` over `read_attribute(:attr)`.
53+
^^^^^^^^^^^^^^^^^^^^^ Prefer `self['test']`.
5454
RUBY
5555

5656
expect_correction(<<~RUBY)
@@ -61,7 +61,7 @@ def foo
6161
it 'corrects an expression' do
6262
expect_offense(<<~RUBY)
6363
res = read_attribute('test_' + postfix)
64-
^^^^^^^^^^^^^^ Prefer `self[:attr]` over `read_attribute(:attr)`.
64+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `self['test_' + postfix]`.
6565
RUBY
6666

6767
expect_correction(<<~RUBY)
@@ -72,7 +72,7 @@ def foo
7272
it 'corrects multiline' do
7373
expect_offense(<<~RUBY)
7474
res = read_attribute(
75-
^^^^^^^^^^^^^^ Prefer `self[:attr]` over `read_attribute(:attr)`.
75+
^^^^^^^^^^^^^^^ Prefer `self[:attr]`.
7676
(
7777
'test_' + postfix
7878
).to_sym
@@ -96,7 +96,7 @@ def foo
9696
it 'registers an offense and corrects' do
9797
expect_offense(<<~RUBY)
9898
write_attribute(:test, val)
99-
^^^^^^^^^^^^^^^ Prefer `self[:attr] = val` over `write_attribute(:attr, val)`.
99+
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `self[:test] = val`.
100100
RUBY
101101

102102
expect_correction(<<~RUBY)
@@ -109,7 +109,7 @@ def foo
109109
it 'registers an offense and corrects' do
110110
expect_offense(<<~RUBY)
111111
write_attribute('attr', 'test')
112-
^^^^^^^^^^^^^^^ Prefer `self[:attr] = val` over `write_attribute(:attr, val)`.
112+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `self['attr'] = 'test'`.
113113
RUBY
114114

115115
expect_correction(<<~RUBY)
@@ -121,7 +121,7 @@ def foo
121121
it 'registers an offense and corrects without parentheses' do
122122
expect_offense(<<~RUBY)
123123
write_attribute 'attr', 'test'
124-
^^^^^^^^^^^^^^^ Prefer `self[:attr] = val` over `write_attribute(:attr, val)`.
124+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `self['attr'] = 'test'`.
125125
RUBY
126126

127127
expect_correction(<<~RUBY)
@@ -141,7 +141,7 @@ def foo=(value)
141141
expect_offense(<<~RUBY)
142142
def foo=(value)
143143
bar(value) || write_attribute(:baz, "baz")
144-
^^^^^^^^^^^^^^^ Prefer `self[:attr] = val` over `write_attribute(:attr, val)`.
144+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `self[:baz] = "baz"`.
145145
end
146146
RUBY
147147

@@ -155,7 +155,7 @@ def foo=(value)
155155
it 'corrects assignment with chained methods' do
156156
expect_offense(<<~RUBY)
157157
write_attribute(:attr, 'test_' + postfix)
158-
^^^^^^^^^^^^^^^ Prefer `self[:attr] = val` over `write_attribute(:attr, val)`.
158+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `self[:attr] = 'test_' + postfix`.
159159
RUBY
160160

161161
expect_correction(<<~RUBY)
@@ -166,7 +166,7 @@ def foo=(value)
166166
it 'autocorrects multiline' do
167167
expect_offense(<<~RUBY)
168168
write_attribute(
169-
^^^^^^^^^^^^^^^ Prefer `self[:attr] = val` over `write_attribute(:attr, val)`.
169+
^^^^^^^^^^^^^^^^ Prefer `self[:attr] = val`.
170170
:attr,
171171
(
172172
'test_' + postfix

0 commit comments

Comments
 (0)