lib/rubocop/cop/rspec/variable_definition.rb in rubocop-rspec-2.5.0 vs lib/rubocop/cop/rspec/variable_definition.rb in rubocop-rspec-2.6.0
- old
+ new
@@ -21,23 +21,40 @@
#
# # good
# subject('user') { create_user }
# let('user_name') { 'Adam' }
class VariableDefinition < Base
+ extend AutoCorrector
include ConfigurableEnforcedStyle
include Variable
MSG = 'Use %<style>s for variable names.'
def on_send(node)
variable_definition?(node) do |variable|
- if style_violation?(variable)
- add_offense(variable, message: format(MSG, style: style))
+ next unless style_violation?(variable)
+
+ add_offense(
+ variable,
+ message: format(MSG, style: style)
+ ) do |corrector|
+ corrector.replace(variable, correct_variable(variable))
end
end
end
private
+
+ def correct_variable(variable)
+ case variable.type
+ when :dsym
+ variable.source[1..-1]
+ when :sym
+ variable.value.to_s.inspect
+ else
+ variable.value.to_sym.inspect
+ end
+ end
def style_violation?(variable)
style == :symbols && string?(variable) ||
style == :strings && symbol?(variable)
end