Class: RuboCop::Cop::RSpec::VariableName

Inherits:
Base
  • Object
show all
Includes:
ConfigurableNaming, IgnoredPattern, Variable
Defined in:
lib/rubocop/cop/rspec/variable_name.rb

Overview

Checks that memoized helper names use the configured style.

Variables can be excluded from checking using the `IgnoredPatterns` option.

Examples:

EnforcedStyle: snake_case (default)

# bad
subject(:userName1) { 'Adam' }
let(:userName2) { 'Adam' }

# good
subject(:user_name_1) { 'Adam' }
let(:user_name_2) { 'Adam' }

EnforcedStyle: camelCase

# bad
subject(:user_name_1) { 'Adam' }
let(:user_name_2) { 'Adam' }

# good
subject(:userName1) { 'Adam' }
let(:userName2) { 'Adam' }

IgnoredPatterns configuration


# rubocop.yml
# RSpec/VariableName:
#   EnforcedStyle: snake_case
#   IgnoredPatterns:
#     - ^userFood
# okay because it matches the `^userFood` regex in `IgnoredPatterns`
subject(:userFood_1) { 'spaghetti' }
let(:userFood_2) { 'fettuccine' }

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 inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Instance Method Details

#on_send(node) ⇒ Object


49
50
51
52
53
54
55
56
# File 'lib/rubocop/cop/rspec/variable_name.rb', line 49

def on_send(node)
  variable_definition?(node) do |variable|
    return if variable.dstr_type? || variable.dsym_type?
    return if matches_ignored_pattern?(variable.value)

    check_name(node, variable.value, variable.loc.expression)
  end
end