forked from troessner/reek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uncommunicative_name.rb
78 lines (67 loc) · 2.26 KB
/
uncommunicative_name.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require 'reek/smells/smell_detector'
require 'reek/smell_warning'
module Reek
module Smells
#
# 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 consisting of a single character followed by a number
#
class UncommunicativeName < SmellDetector
# The name of the config field that lists the regexps of
# smelly names to be rejected.
REJECT_KEY = 'reject'
# 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_KEY = 'accept'
def self.default_config
super.adopt(
REJECT_KEY => [/^.[0-9]*$/],
ACCEPT_KEY => ['Inline::C']
)
end
def self.contexts # :nodoc:
[:module, :class, :defn, :defs, :iter]
end
def initialize(config = UncommunicativeName.default_config)
super
@reject = config[REJECT_KEY]
@accept = config[ACCEPT_KEY]
end
#
# Checks the given +context+ for uncommunicative names.
# Remembers any smells found.
#
def examine_context(context)
consider_name(context)
consider_variables(context)
end
def consider_variables(context) # :nodoc:
context.variable_names.each do |name|
next unless is_bad_name?(name)
found(context, "has the variable name '#{name}'")
end
end
def consider_name(context) # :nodoc:
name = context.name
return false if @accept.include?(context.to_s) # TODO: fq_name() ?
return false unless is_bad_name?(name)
found(context, "has the name '#{name}'")
end
def is_bad_name?(name) # :nodoc:
var = name.effective_name
return false if var == '*' or @accept.include?(var)
@reject.detect {|patt| patt === var}
end
end
end
end