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

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle, InsideExampleGroup, 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

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

Instance Method Details

#on_send(node) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rubocop/cop/rspec/variable_definition.rb', line 34

def on_send(node)
  return unless inside_example_group?(node)

  variable_definition?(node) do |variable|
    next unless style_offense?(variable)

    add_offense(
      variable,
      message: format(MSG, style: style)
    ) do |corrector|
      corrector.replace(variable, correct_variable(variable))
    end
  end
end