reek 3.7.1 → 3.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +8 -1
  3. data/CHANGELOG.md +5 -0
  4. data/README.md +1 -1
  5. data/defaults.reek +3 -0
  6. data/docs/Code-Smells.md +1 -0
  7. data/docs/How-reek-works-internally.md +3 -2
  8. data/docs/Unused-Private-Method.md +47 -0
  9. data/features/samples.feature +22 -2
  10. data/features/step_definitions/sample_file_steps.rb +3 -0
  11. data/lib/reek/ast/node.rb +1 -2
  12. data/lib/reek/ast/object_refs.rb +30 -7
  13. data/lib/reek/code_comment.rb +25 -19
  14. data/lib/reek/context/class_context.rb +11 -0
  15. data/lib/reek/context/code_context.rb +58 -78
  16. data/lib/reek/context/module_context.rb +18 -0
  17. data/lib/reek/context/send_context.rb +17 -0
  18. data/lib/reek/context/singleton_method_context.rb +0 -3
  19. data/lib/reek/context/statement_counter.rb +32 -0
  20. data/lib/reek/context/visibility_tracker.rb +54 -0
  21. data/lib/reek/context_builder.rb +473 -0
  22. data/lib/reek/examiner.rb +14 -14
  23. data/lib/reek/smells/feature_envy.rb +3 -3
  24. data/lib/reek/smells/smell_detector.rb +1 -0
  25. data/lib/reek/smells/smell_repository.rb +11 -0
  26. data/lib/reek/smells/too_many_statements.rb +1 -1
  27. data/lib/reek/smells/unused_private_method.rb +82 -0
  28. data/lib/reek/smells/utility_function.rb +1 -1
  29. data/lib/reek/smells.rb +1 -0
  30. data/lib/reek/version.rb +1 -1
  31. data/spec/reek/ast/object_refs_spec.rb +20 -20
  32. data/spec/reek/cli/input_spec.rb +55 -0
  33. data/spec/reek/code_comment_spec.rb +10 -0
  34. data/spec/reek/context/code_context_spec.rb +8 -0
  35. data/spec/reek/context/module_context_spec.rb +10 -8
  36. data/spec/reek/context_builder_spec.rb +221 -0
  37. data/spec/reek/examiner_spec.rb +13 -0
  38. data/spec/reek/smells/boolean_parameter_spec.rb +2 -0
  39. data/spec/reek/smells/duplicate_method_call_spec.rb +1 -1
  40. data/spec/reek/smells/feature_envy_spec.rb +1 -1
  41. data/spec/reek/smells/too_many_statements_spec.rb +3 -3
  42. data/spec/reek/smells/unused_private_method_spec.rb +110 -0
  43. data/spec/spec_helper.rb +7 -0
  44. data/tasks/console.rake +5 -0
  45. metadata +13 -5
  46. data/lib/reek/tree_walker.rb +0 -237
  47. data/spec/reek/context/singleton_method_context_spec.rb +0 -16
  48. data/spec/reek/tree_walker_spec.rb +0 -237
@@ -1,237 +0,0 @@
1
- require_relative '../spec_helper'
2
- require_lib 'reek/tree_walker'
3
- require_lib 'reek/source/source_code'
4
-
5
- # Dummy repository to inject into TreeWalker in order to count statements in
6
- # all contexts.
7
- class TestSmellRepository
8
- attr_accessor :num_statements
9
- def examine(context)
10
- self.num_statements = context.num_statements
11
- end
12
- end
13
-
14
- def process_method(source)
15
- exp = Reek::Source::SourceCode.from(source).syntax_tree
16
- repository = TestSmellRepository.new
17
- Reek::TreeWalker.new(repository, exp).walk
18
- repository
19
- end
20
-
21
- def process_singleton_method(source)
22
- exp = Reek::Source::SourceCode.from(source).syntax_tree
23
- repository = TestSmellRepository.new
24
- Reek::TreeWalker.new(repository, exp).walk
25
- repository
26
- end
27
-
28
- RSpec.describe Reek::TreeWalker, 'statement counting' do
29
- it 'counts 1 assignment' do
30
- method = process_method('def one() val = 4; end')
31
- expect(method.num_statements).to eq(1)
32
- end
33
-
34
- it 'counts 3 assignments' do
35
- method = process_method('def one() val = 4; val = 4; val = 4; end')
36
- expect(method.num_statements).to eq(3)
37
- end
38
-
39
- it 'counts 1 attr assignment' do
40
- method = process_method('def one() val[0] = 4; end')
41
- expect(method.num_statements).to eq(1)
42
- end
43
-
44
- it 'counts 1 increment assignment' do
45
- method = process_method('def one() val += 4; end')
46
- expect(method.num_statements).to eq(1)
47
- end
48
-
49
- it 'counts 1 increment attr assignment' do
50
- method = process_method('def one() val[0] += 4; end')
51
- expect(method.num_statements).to eq(1)
52
- end
53
-
54
- it 'counts 1 nested assignment' do
55
- method = process_method('def one() val = fred = 4; end')
56
- expect(method.num_statements).to eq(1)
57
- end
58
-
59
- it 'counts returns' do
60
- method = process_method('def one() val = 4; true; end')
61
- expect(method.num_statements).to eq(2)
62
- end
63
-
64
- it 'counts nil returns' do
65
- method = process_method('def one() val = 4; nil; end')
66
- expect(method.num_statements).to eq(2)
67
- end
68
-
69
- context 'with control statements' do
70
- it 'counts 1 statement in a conditional expression' do
71
- method = process_method('def one() if val == 4; callee(); end; end')
72
- expect(method.num_statements).to eq(1)
73
- end
74
-
75
- it 'counts 3 statements in a conditional expression' do
76
- method = process_method('def one() if val == 4; callee(); callee(); callee(); end; end')
77
- expect(method.num_statements).to eq(3)
78
- end
79
-
80
- it 'counts 1 statements in an else' do
81
- method = process_method('def one() if val == 4; callee(); else; callee(); end; end')
82
- expect(method.num_statements).to eq(2)
83
- end
84
-
85
- it 'counts 3 statements in an else' do
86
- method = process_method('
87
- def one()
88
- if val == 4
89
- callee(); callee(); callee()
90
- else
91
- callee(); callee(); callee()
92
- end
93
- end
94
- ')
95
- expect(method.num_statements).to eq(6)
96
- end
97
-
98
- it 'does not count constant assignment with or equals' do
99
- source = 'class Hi; CONST ||= 1; end'
100
- klass = process_method(source)
101
- expect(klass.num_statements).to eq(0)
102
- end
103
-
104
- it 'does not count multi constant assignment' do
105
- source = 'class Hi; CONST, OTHER_CONST = 1, 2; end'
106
- klass = process_method(source)
107
- expect(klass.num_statements).to eq(0)
108
- end
109
-
110
- it 'does not count empty conditional expression' do
111
- method = process_method('def one() if val == 4; ; end; end')
112
- expect(method.num_statements).to eq(0)
113
- end
114
-
115
- it 'does not count empty else' do
116
- method = process_method('def one() if val == 4; ; else; ; end; end')
117
- expect(method.num_statements).to eq(0)
118
- end
119
-
120
- it 'counts extra statements in an if condition' do
121
- method = process_method('def one() if begin val = callee(); val < 4 end; end; end')
122
- expect(method.num_statements).to eq(1)
123
- end
124
-
125
- it 'counts 1 statement in a while loop' do
126
- method = process_method('def one() while val < 4; callee(); end; end')
127
- expect(method.num_statements).to eq(1)
128
- end
129
-
130
- it 'counts 3 statements in a while loop' do
131
- source = 'def one() while val < 4; callee(); callee(); callee(); end; end'
132
- expect(process_method(source).num_statements).to eq(3)
133
- end
134
-
135
- it 'counts extra statements in a while condition' do
136
- method = process_method('def one() while begin val = callee(); val < 4 end; end; end')
137
- expect(method.num_statements).to eq(1)
138
- end
139
-
140
- it 'counts 1 statement in a until loop' do
141
- method = process_method('def one() until val < 4; callee(); end; end')
142
- expect(method.num_statements).to eq(1)
143
- end
144
-
145
- it 'counts 3 statements in a until loop' do
146
- source = 'def one() until val < 4; callee(); callee(); callee(); end; end'
147
- expect(process_method(source).num_statements).to eq(3)
148
- end
149
-
150
- it 'counts 1 statement in a for loop' do
151
- method = process_method('def one() for i in 0..4; callee(); end; end')
152
- expect(method.num_statements).to eq(1)
153
- end
154
-
155
- it 'counts 3 statements in a for loop' do
156
- source = 'def one() for i in 0..4; callee(); callee(); callee(); end; end'
157
- expect(process_method(source).num_statements).to eq(3)
158
- end
159
-
160
- it 'counts 1 statement in a rescue' do
161
- method = process_method('def one() begin; callee(); rescue; callee(); end; end')
162
- expect(method.num_statements).to eq(2)
163
- end
164
-
165
- it 'counts 3 statements in a rescue' do
166
- method = process_method('
167
- def one()
168
- begin
169
- callee(); callee(); callee()
170
- rescue
171
- callee(); callee(); callee()
172
- end
173
- end
174
- ')
175
- expect(method.num_statements).to eq(6)
176
- end
177
-
178
- it 'counts 1 statement in a when' do
179
- method = process_method('def one() case fred; when "hi"; callee(); end; end')
180
- expect(method.num_statements).to eq(1)
181
- end
182
-
183
- it 'counts 3 statements in a when' do
184
- method = process_method('
185
- def one()
186
- case fred
187
- when "hi" then callee(); callee()
188
- when "lo" then callee()
189
- end
190
- end
191
- ')
192
- expect(method.num_statements).to eq(3)
193
- end
194
-
195
- it 'counts 1 statement in a case else' do
196
- source = 'def one() case fred; when "hi"; callee(); else; callee(); end; end'
197
- expect(process_method(source).num_statements).to eq(2)
198
- end
199
-
200
- it 'counts 3 statements in a case else' do
201
- method = process_method('
202
- def one()
203
- case fred
204
- when "hi" then callee(); callee(); callee()
205
- else callee(); callee(); callee()
206
- end
207
- end
208
- ')
209
- expect(method.num_statements).to eq(6)
210
- end
211
-
212
- it 'does not count empty case' do
213
- method = process_method('def one() case fred; when "hi"; ; when "lo"; ; end; end')
214
- expect(method.num_statements).to eq(0)
215
- end
216
-
217
- it 'does not count empty case else' do
218
- method = process_method('def one() case fred; when "hi"; ; else; ; end; end')
219
- expect(method.num_statements).to eq(0)
220
- end
221
-
222
- it 'counts 2 statement in an iterator' do
223
- method = process_method('def one() fred.each do; callee(); end; end')
224
- expect(method.num_statements).to eq(2)
225
- end
226
-
227
- it 'counts 4 statements in an iterator' do
228
- source = 'def one() fred.each do; callee(); callee(); callee(); end; end'
229
- expect(process_method(source).num_statements).to eq(4)
230
- end
231
-
232
- it 'counts 1 statement in a singleton method' do
233
- method = process_singleton_method('def self.foo; callee(); end')
234
- expect(method.num_statements).to eq(1)
235
- end
236
- end
237
- end