Class: ImproveYourCode::SmellDetectors::UncommunicativeVariableName

Inherits:
BaseDetector
  • Object
show all
Defined in:
lib/improve_your_code/smell_detectors/uncommunicative_variable_name.rb

Constant Summary collapse

REJECT_KEY =
'reject'
DEFAULT_REJECT_SET =
[
  /^.$/, # single-character names
  /[0-9]$/,  # any name ending with a number
  /[A-Z]/    # camelCaseVariableNames
].freeze
ACCEPT_KEY =
'accept'
DEFAULT_ACCEPT_SET =
[/^_$/].freeze

Constants inherited from BaseDetector

BaseDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from BaseDetector

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseDetector

configuration_keys, descendants, inherited, #initialize, #run, #smell_type, smell_type, to_detector, todo_configuration_for, valid_detector?

Constructor Details

This class inherits a constructor from ImproveYourCode::SmellDetectors::BaseDetector

Class Method Details

.contextsObject



24
25
26
# File 'lib/improve_your_code/smell_detectors/uncommunicative_variable_name.rb', line 24

def self.contexts
  %i[module class def defs]
end

.default_configObject



17
18
19
20
21
22
# File 'lib/improve_your_code/smell_detectors/uncommunicative_variable_name.rb', line 17

def self.default_config
  super.merge(
    REJECT_KEY => DEFAULT_REJECT_SET,
    ACCEPT_KEY => DEFAULT_ACCEPT_SET
  )
end

Instance Method Details

#sniffObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/improve_your_code/smell_detectors/uncommunicative_variable_name.rb', line 28

def sniff
  variable_names.select do |name, _lines|
    uncommunicative_variable_name?(name)
  end.map do |name, lines|
    smell_warning(
      context: context,
      lines: lines,
      message: "has the variable name '#{name}'",
      parameters: { name: name.to_s }
    )
  end
end