reek 4.2.4 → 4.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/docs/Nil-Check.md +1 -0
- data/lib/reek/smells/nil_check.rb +32 -8
- data/lib/reek/version.rb +1 -1
- data/spec/reek/smells/nil_check_spec.rb +39 -24
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c782c9f7772d236e5608adea55c1dbef8ab620b8
|
4
|
+
data.tar.gz: 7851ccc5dd286f7488b4460a021179e35698429d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6147cba71db38cd92ca1c99ad7cfb9e002c7287bc114d8d318f99650d6ca4f3f06765f07d2f291dfedddfb394a3d84433a3f37b8bbea25c701e0d0bcaae7304b
|
7
|
+
data.tar.gz: 3f26e46e871f05bebe920624c33d12154fc7b773b426d263c2a6f2d14f247bc93d4d35683a150e8878636b5f729cc1fc0a96cecd7de2adaa99787652434f7946
|
data/CHANGELOG.md
CHANGED
data/docs/Nil-Check.md
CHANGED
@@ -37,6 +37,7 @@ _Nil Check_ reports use of
|
|
37
37
|
* <code>.nil?</code> method
|
38
38
|
* <code>==</code> and <code>===</code> operators when checking vs. <code>nil</code>
|
39
39
|
* case statements that use syntax like <code>when nil</code>
|
40
|
+
* use of the safe navigation operator like `foo&.bar`
|
40
41
|
|
41
42
|
## Configuration
|
42
43
|
|
@@ -10,15 +10,29 @@ module Reek
|
|
10
10
|
# See {file:docs/Nil-Check.md} for details.
|
11
11
|
class NilCheck < SmellDetector
|
12
12
|
def sniff(ctx)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
smelly_nodes.map do |node|
|
18
|
-
smell_warning(
|
13
|
+
lines = NodeDetector.new(ctx).detect.map(&:line)
|
14
|
+
if lines.any?
|
15
|
+
[smell_warning(
|
19
16
|
context: ctx,
|
20
|
-
lines:
|
21
|
-
message: 'performs a nil-check')
|
17
|
+
lines: lines,
|
18
|
+
message: 'performs a nil-check')]
|
19
|
+
else
|
20
|
+
[]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Detect all nodes that smell of NilCheck
|
25
|
+
class NodeDetector
|
26
|
+
attr_reader :ctx
|
27
|
+
def initialize(ctx)
|
28
|
+
@ctx = ctx
|
29
|
+
end
|
30
|
+
|
31
|
+
def detect
|
32
|
+
finders = [NodeFinder.new(ctx, :send, NilCallNodeDetector),
|
33
|
+
NodeFinder.new(ctx, :when, NilWhenNodeDetector),
|
34
|
+
NodeFinder.new(ctx, :csend, SafeNavigationNodeDetector)]
|
35
|
+
finders.flat_map(&:smelly_nodes)
|
22
36
|
end
|
23
37
|
end
|
24
38
|
|
@@ -81,6 +95,16 @@ module Reek
|
|
81
95
|
node.condition_list.any? { |it| it.type == :nil }
|
82
96
|
end
|
83
97
|
end
|
98
|
+
|
99
|
+
# Detect safe navigation. Returns true for all nodes, since all :csend
|
100
|
+
# nodes are considered smelly.
|
101
|
+
module SafeNavigationNodeDetector
|
102
|
+
module_function
|
103
|
+
|
104
|
+
def detect(_node)
|
105
|
+
true
|
106
|
+
end
|
107
|
+
end
|
84
108
|
end
|
85
109
|
end
|
86
110
|
end
|
data/lib/reek/version.rb
CHANGED
@@ -6,8 +6,8 @@ require_relative 'smell_detector_shared'
|
|
6
6
|
RSpec.describe Reek::Smells::NilCheck do
|
7
7
|
it 'reports correctly the basic use case' do
|
8
8
|
src = <<-EOS
|
9
|
-
def
|
10
|
-
|
9
|
+
def foo(bar)
|
10
|
+
bar.nil?
|
11
11
|
end
|
12
12
|
EOS
|
13
13
|
expect(src).to reek_of :NilCheck,
|
@@ -19,55 +19,70 @@ RSpec.describe Reek::Smells::NilCheck do
|
|
19
19
|
expect('def no_nils; end').not_to reek_of(:NilCheck)
|
20
20
|
end
|
21
21
|
|
22
|
-
it 'reports when scope uses
|
22
|
+
it 'reports when scope uses #nil?' do
|
23
23
|
src = <<-EOS
|
24
|
-
def
|
25
|
-
|
26
|
-
puts "Hello"
|
27
|
-
\"\".nil?
|
24
|
+
def foo(bar)
|
25
|
+
bar.nil?
|
28
26
|
end
|
29
27
|
EOS
|
30
28
|
expect(src).to reek_of(:NilCheck)
|
31
29
|
end
|
32
30
|
|
33
|
-
it 'reports
|
31
|
+
it 'reports when scope uses == nil' do
|
34
32
|
src = <<-EOS
|
35
|
-
def
|
36
|
-
|
37
|
-
|
33
|
+
def foo(bar)
|
34
|
+
bar == nil
|
35
|
+
end
|
36
|
+
EOS
|
37
|
+
expect(src).to reek_of(:NilCheck)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'reports when scope uses === nil' do
|
41
|
+
src = <<-EOS
|
42
|
+
def foo(bar)
|
43
|
+
bar === nil
|
38
44
|
end
|
39
45
|
EOS
|
40
46
|
expect(src).to reek_of(:NilCheck)
|
41
47
|
end
|
42
48
|
|
43
49
|
it 'reports when scope uses nil ==' do
|
44
|
-
|
50
|
+
src = <<-EOS
|
51
|
+
def foo(bar)
|
52
|
+
nil == bar
|
53
|
+
end
|
54
|
+
EOS
|
55
|
+
expect(src).to reek_of(:NilCheck)
|
45
56
|
end
|
46
57
|
|
47
|
-
it 'reports when scope uses
|
58
|
+
it 'reports when scope uses a case-clause checking nil' do
|
48
59
|
src = <<-EOS
|
49
60
|
def case_nil
|
50
|
-
case @
|
61
|
+
case @foo
|
51
62
|
when nil then puts "Nil"
|
52
63
|
end
|
53
|
-
puts "Hello"
|
54
|
-
case @inst_var2
|
55
|
-
when 1 then puts 1
|
56
|
-
when nil then puts nil.inspect
|
57
|
-
end
|
58
64
|
end
|
59
65
|
EOS
|
60
66
|
expect(src).to reek_of(:NilCheck)
|
61
67
|
end
|
62
68
|
|
63
|
-
it 'reports
|
69
|
+
it 'reports when scope uses &.' do
|
64
70
|
src = <<-EOS
|
65
|
-
def
|
66
|
-
|
67
|
-
when nil, false then puts "Hello"
|
68
|
-
end
|
71
|
+
def foo(bar)
|
72
|
+
bar&.baz
|
69
73
|
end
|
70
74
|
EOS
|
71
75
|
expect(src).to reek_of(:NilCheck)
|
72
76
|
end
|
77
|
+
|
78
|
+
it 'reports all lines when scope uses multiple nilchecks' do
|
79
|
+
src = <<-EOS
|
80
|
+
def foo(bar)
|
81
|
+
bar.nil?
|
82
|
+
bar === nil
|
83
|
+
bar&.baz
|
84
|
+
end
|
85
|
+
EOS
|
86
|
+
expect(src).to reek_of(:NilCheck, lines: [2, 3, 4])
|
87
|
+
end
|
73
88
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reek
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.2.
|
4
|
+
version: 4.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Rutherford
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2016-08-
|
14
|
+
date: 2016-08-23 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: codeclimate-engine-rb
|
@@ -412,3 +412,4 @@ signing_key:
|
|
412
412
|
specification_version: 4
|
413
413
|
summary: Code smell detector for Ruby
|
414
414
|
test_files: []
|
415
|
+
has_rdoc:
|