lib/rubocop/cop/rails/read_write_attribute.rb in rubocop-0.35.1 vs lib/rubocop/cop/rails/read_write_attribute.rb in rubocop-0.36.0
- old
+ new
@@ -1,6 +1,7 @@
# encoding: utf-8
+# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# This cop checks for the use of the read_attribute or
@@ -14,11 +15,11 @@
#
# # good
# x = self[:attr]
# self[:attr] = val
class ReadWriteAttribute < Cop
- MSG = 'Prefer `%s` over `%s`.'
+ MSG = 'Prefer `%s` over `%s`.'.freeze
def on_send(node)
receiver, method_name, *_args = *node
return if receiver
return unless [:read_attribute,
@@ -45,28 +46,25 @@
replacement = read_attribute_replacement(node)
when :write_attribute
replacement = write_attribute_replacement(node)
end
- ->(corrector) { corrector.replace(node.loc.expression, replacement) }
+ ->(corrector) { corrector.replace(node.source_range, replacement) }
end
private
def read_attribute_replacement(node)
_receiver, _method_name, body = *node
- "self[#{body.loc.expression.source}]"
+ "self[#{body.source}]"
end
def write_attribute_replacement(node)
_receiver, _method_name, *args = *node
name, value = *args
- name_source = name.loc.expression.source
- value_source = value.loc.expression.source
-
- "self[#{name_source}] = #{value_source}"
+ "self[#{name.source}] = #{value.source}"
end
end
end
end
end