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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7756b10a94b5df5b45c50b6cdaa6577146dbbf15
4
- data.tar.gz: d1e67defd358f1ce5f60affaa2c33e45aa09d744
3
+ metadata.gz: c782c9f7772d236e5608adea55c1dbef8ab620b8
4
+ data.tar.gz: 7851ccc5dd286f7488b4460a021179e35698429d
5
5
  SHA512:
6
- metadata.gz: f2cbb1aacb409e57504ab7206b917648ad9e5db97a98302179a5902ab8a8b83335fcc674580f380f2314ec055f9c8be35156c1bab5d7b5ba6384429a8b42fdf9
7
- data.tar.gz: 7476f5a7ce8dc2a0ab1f4791c1d5d0a4688b2462761f50775dd8404928faced642dfeedbbacbb2d3525e673003889fe3e7c643305bb92658ea397ad5fcdc4368
6
+ metadata.gz: 6147cba71db38cd92ca1c99ad7cfb9e002c7287bc114d8d318f99650d6ca4f3f06765f07d2f291dfedddfb394a3d84433a3f37b8bbea25c701e0d0bcaae7304b
7
+ data.tar.gz: 3f26e46e871f05bebe920624c33d12154fc7b773b426d263c2a6f2d14f247bc93d4d35683a150e8878636b5f729cc1fc0a96cecd7de2adaa99787652434f7946
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Change log
2
2
 
3
+ ## 4.2.5 (2016-08-23)
4
+
5
+ * (mvz) Detect safe navigation operator and report on it.
6
+
3
7
  ## 4.2.4 (2016-08-15)
4
8
 
5
9
  * (troessner) Make Reek more resilient on incomprehensible source.
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
- call_node_finder = NodeFinder.new(ctx, :send, NilCallNodeDetector)
14
- case_node_finder = NodeFinder.new(ctx, :when, NilWhenNodeDetector)
15
- smelly_nodes = call_node_finder.smelly_nodes + case_node_finder.smelly_nodes
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: [node.line],
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
@@ -7,6 +7,6 @@ module Reek
7
7
  # @public
8
8
  module Version
9
9
  # @public
10
- STRING = '4.2.4'.freeze
10
+ STRING = '4.2.5'.freeze
11
11
  end
12
12
  end
@@ -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 nilcheck foo
10
- foo.nil?
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 multiple nil? methods' do
22
+ it 'reports when scope uses #nil?' do
23
23
  src = <<-EOS
24
- def chk_multi_nil(para)
25
- para.nil?
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 twice when scope uses == nil and === nil' do
31
+ it 'reports when scope uses == nil' do
34
32
  src = <<-EOS
35
- def chk_eq_nil(para)
36
- para == nil
37
- para === nil
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
- expect('def chk_eq_nil_rev(para); nil == para; end').to reek_of(:NilCheck)
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 multiple case-clauses checking nil' do
58
+ it 'reports when scope uses a case-clause checking nil' do
48
59
  src = <<-EOS
49
60
  def case_nil
50
- case @inst_var
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 a when clause that checks nil and other values' do
69
+ it 'reports when scope uses &.' do
64
70
  src = <<-EOS
65
- def case_nil
66
- case @inst_var
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
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-17 00:00:00.000000000 Z
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: