class RuboCop::Cop::RSpec::VariableDefinition
Checks that memoized helpers names are symbols or strings.
@example EnforcedStyle: symbols (default)
# bad subject('user') { create_user } let('user_name') { 'Adam' } # good subject(:user) { create_user } let(:user_name) { 'Adam' }
@example EnforcedStyle: strings
# bad subject(:user) { create_user } let(:user_name) { 'Adam' } # good subject('user') { create_user } let('user_name') { 'Adam' }
Constants
- MSG
Public Instance Methods
on_send(node)
click to toggle source
# File lib/rubocop/cop/rspec/variable_definition.rb, line 32 def on_send(node) variable_definition?(node) do |variable| 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 Instance Methods
correct_variable(variable)
click to toggle source
# File lib/rubocop/cop/rspec/variable_definition.rb, line 47 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
string?(node)
click to toggle source
# File lib/rubocop/cop/rspec/variable_definition.rb, line 63 def string?(node) node.str_type? end
style_violation?(variable)
click to toggle source
# File lib/rubocop/cop/rspec/variable_definition.rb, line 58 def style_violation?(variable) style == :symbols && string?(variable) || style == :strings && symbol?(variable) end
symbol?(node)
click to toggle source
# File lib/rubocop/cop/rspec/variable_definition.rb, line 67 def symbol?(node) node.sym_type? || node.dsym_type? end