Class: RuboCop::Cop::RSpec::VariableDefinition

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle, Variable
Defined in:
lib/rubocop/cop/rspec/variable_definition.rb

Overview

Checks that memoized helpers names are symbols or strings.

Examples:

EnforcedStyle: symbols (default)

# bad
subject('user') { create_user }
let('user_name') { 'Adam' }

# good
subject(:user) { create_user }
let(:user_name) { 'Adam' }

EnforcedStyle: strings

# bad
subject(:user) { create_user }
let(:user_name) { 'Adam' }

# good
subject('user') { create_user }
let('user_name') { 'Adam' }

Constant Summary collapse

MSG =
'Use %<style>s for variable names.'

Constants included from Variable

RuboCop::Cop::RSpec::Variable::Helpers, RuboCop::Cop::RSpec::Variable::Subjects

Instance Method Summary collapse

Methods included from Variable

#variable_definition?

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#on_send(node) ⇒ Object


32
33
34
35
36
37
38
39
40
41
42
43
# 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