Class: Reek::SmellDetectors::UncommunicativeModuleName

Inherits:
BaseDetector
  • Object
show all
Defined in:
lib/reek/smell_detectors/uncommunicative_module_name.rb

Overview

An Uncommunicative Name is a name that doesn’t communicate its intent well enough.

Poor names make it hard for the reader to build a mental picture of what’s going on in the code. They can also be mis-interpreted; and they hurt the flow of reading, because the reader must slow down to interpret the names.

Currently UncommunicativeModuleName checks for

  • 1-character names

  • names ending with a number

See Uncommunicative-Module-Name for details.

Constant Summary collapse

REJECT_KEY =

The name of the config field that lists the regexps of smelly names to be reported.

'reject'
DEFAULT_REJECT_PATTERNS =
[/^.$/, /[0-9]$/].freeze
ACCEPT_KEY =

The name of the config field that lists the specific names that are to be treated as exceptions; these names will not be reported as uncommunicative.

'accept'
DEFAULT_ACCEPT_PATTERNS =
[].freeze

Constants inherited from BaseDetector

BaseDetector::DEFAULT_EXCLUDE_SET, BaseDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from BaseDetector

#config, #context

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseDetector

#config_for, configuration_keys, descendants, #enabled?, #exception?, #expression, inherited, #initialize, #run, smell_type, #smell_type, #smell_warning, #source_line, to_detector, todo_configuration_for, #value

Constructor Details

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

Class Method Details

.contextsObject



39
40
41
# File 'lib/reek/smell_detectors/uncommunicative_module_name.rb', line 39

def self.contexts
  [:module, :class]
end

.default_configObject



33
34
35
36
37
# File 'lib/reek/smell_detectors/uncommunicative_module_name.rb', line 33

def self.default_config
  super.merge(
    REJECT_KEY => DEFAULT_REJECT_PATTERNS,
    ACCEPT_KEY => DEFAULT_ACCEPT_PATTERNS)
end

Instance Method Details

#accept_patternsObject (private)



72
73
74
# File 'lib/reek/smell_detectors/uncommunicative_module_name.rb', line 72

def accept_patterns
  @accept_patterns ||= Array value(ACCEPT_KEY, context)
end

#acceptable_name?(module_name:, fully_qualified_name:) ⇒ Boolean (private)

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/reek/smell_detectors/uncommunicative_module_name.rb', line 63

def acceptable_name?(module_name:, fully_qualified_name:)
  accept_patterns.any? { |accept_pattern| fully_qualified_name.match accept_pattern } ||
    reject_patterns.none? { |reject_pattern| module_name.match reject_pattern }
end

#reject_patternsObject (private)



68
69
70
# File 'lib/reek/smell_detectors/uncommunicative_module_name.rb', line 68

def reject_patterns
  @reject_patterns ||= Array value(REJECT_KEY, context)
end

#sniffArray<SmellWarning>

Checks the detector’s context for uncommunicative names.

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/reek/smell_detectors/uncommunicative_module_name.rb', line 48

def sniff
  fully_qualified_name = context.full_name
  module_name          = expression.simple_name

  return [] if acceptable_name?(module_name: module_name,
                                fully_qualified_name: fully_qualified_name)

  [smell_warning(
    lines: [source_line],
    message: "has the name '#{module_name}'",
    parameters: { name: module_name })]
end