Class: Reek::Smells::UncommunicativeVariableName Private
- Inherits:
-
SmellDetector
- Object
- SmellDetector
- Reek::Smells::UncommunicativeVariableName
- Defined in:
- lib/reek/smells/uncommunicative_variable_name.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
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 UncommunicativeName
checks for
-
1-character names
-
names ending with a number
See Uncommunicative-Variable-Name for details.
Constant Summary collapse
- REJECT_KEY =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The name of the config field that lists the regexps of smelly names to be reported.
'reject'
- DEFAULT_REJECT_SET =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
[/^.$/, /[0-9]$/, /[A-Z]/]
- ACCEPT_KEY =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
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_SET =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
['_']
Constants inherited from SmellDetector
SmellDetector::DEFAULT_EXCLUDE_SET, SmellDetector::EXCLUDE_KEY
Instance Attribute Summary
Attributes inherited from SmellDetector
Class Method Summary collapse
-
.contexts ⇒ Object
private
:nodoc:.
- .default_config ⇒ Object private
- .smell_category ⇒ Object private
Instance Method Summary collapse
- #bad_name?(name, _ctx) ⇒ Boolean private
-
#examine_context(ctx) ⇒ Array<SmellWarning>
private
Checks the given
context
for uncommunicative names. - #find_assignment_variable_names(exp, accumulator) ⇒ Object private
- #find_block_argument_variable_names(exp, accumulator) ⇒ Object private
- #record_variable_name(exp, symbol, accumulator) ⇒ Object private
- #recursively_record_variable_names(accumulator, exp) ⇒ Object private
- #variable_names(exp) ⇒ Object private
Methods inherited from SmellDetector
#config_for, #configure_with, default_smell_category, descendants, #enabled?, #enabled_for?, #examine, #exception?, #initialize, #register, #report_on, #smell_category, smell_type, #smell_type, #value
Constructor Details
This class inherits a constructor from Reek::Smells::SmellDetector
Class Method Details
.contexts ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
:nodoc:
44 45 46 |
# File 'lib/reek/smells/uncommunicative_variable_name.rb', line 44 def self.contexts # :nodoc: [:module, :class, :def, :defs] end |
.default_config ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
37 38 39 40 41 42 |
# File 'lib/reek/smells/uncommunicative_variable_name.rb', line 37 def self.default_config super.merge( REJECT_KEY => DEFAULT_REJECT_SET, ACCEPT_KEY => DEFAULT_ACCEPT_SET ) end |
.smell_category ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
33 34 35 |
# File 'lib/reek/smells/uncommunicative_variable_name.rb', line 33 def self.smell_category 'UncommunicativeName' end |
Instance Method Details
#bad_name?(name, _ctx) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
67 68 69 70 71 |
# File 'lib/reek/smells/uncommunicative_variable_name.rb', line 67 def bad_name?(name, _ctx) var = name.to_s.gsub(/^[@\*\&]*/, '') return false if @accept_names.include?(var) @reject_names.find { |patt| patt =~ var } end |
#examine_context(ctx) ⇒ Array<SmellWarning>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Checks the given context
for uncommunicative names.
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/reek/smells/uncommunicative_variable_name.rb', line 53 def examine_context(ctx) @reject_names = value(REJECT_KEY, ctx, DEFAULT_REJECT_SET) @accept_names = value(ACCEPT_KEY, ctx, DEFAULT_ACCEPT_SET) variable_names(ctx.exp).select do |name, _lines| bad_name?(name, ctx) end.map do |name, lines| SmellWarning.new(self, context: ctx.full_name, lines: lines, message: "has the variable name '#{name}'", parameters: { name: name.to_s }) end end |
#find_assignment_variable_names(exp, accumulator) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/reek/smells/uncommunicative_variable_name.rb', line 80 def find_assignment_variable_names(exp, accumulator) assignment_nodes = exp.each_node(:lvasgn, [:class, :module, :defs, :def]) case exp.first when :class, :module assignment_nodes += exp.each_node(:ivasgn, [:class, :module]) end assignment_nodes.each { |asgn| accumulator[asgn[1]].push(asgn.line) } end |
#find_block_argument_variable_names(exp, accumulator) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/reek/smells/uncommunicative_variable_name.rb', line 91 def find_block_argument_variable_names(exp, accumulator) arg_search_exp = case exp.first when :class, :module exp when :defs, :def exp.body end return unless arg_search_exp args_nodes = arg_search_exp.each_node(:args, [:class, :module, :defs, :def]) args_nodes.each do |args_node| recursively_record_variable_names(accumulator, args_node) end end |
#record_variable_name(exp, symbol, accumulator) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
118 119 120 121 122 123 |
# File 'lib/reek/smells/uncommunicative_variable_name.rb', line 118 def record_variable_name(exp, symbol, accumulator) varname = symbol.to_s.sub(/^\*/, '') return if varname == '' var = varname.to_sym accumulator[var].push(exp.line) end |
#recursively_record_variable_names(accumulator, exp) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/reek/smells/uncommunicative_variable_name.rb', line 107 def recursively_record_variable_names(accumulator, exp) exp.children.each do |subexp| case subexp.type when :mlhs recursively_record_variable_names(accumulator, subexp) else record_variable_name(exp, subexp.name, accumulator) end end end |
#variable_names(exp) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
73 74 75 76 77 78 |
# File 'lib/reek/smells/uncommunicative_variable_name.rb', line 73 def variable_names(exp) result = Hash.new { |hash, key| hash[key] = [] } find_assignment_variable_names(exp, result) find_block_argument_variable_names(exp, result) result.to_a.sort_by { |name, _| name.to_s } end |