rubocop-elegant 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/config/default.yml +4 -0
- data/lib/rubocop/cop/elegant/no_comments.rb +2 -3
- data/lib/rubocop/cop/elegant/no_nil_return.rb +49 -0
- data/lib/rubocop/cop/elegant_cops.rb +1 -0
- data/rubocop-elegant.gemspec +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d208aee894da49bf76dac6b393ecd907d137ce125f445903078f34def9435955
|
|
4
|
+
data.tar.gz: 2bb699562cc741372c3ab5f708a815cc2e046476d3a7646b267c72eb4c49ca98
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2c74ed34a71c7fc3b89799f80dc97a77afecf8c2d3d1220b27454b829b27ffd452937a9779a0b17bb562308ef023e9a93ecda0fb6a87c6d10413ef78abf69ca5
|
|
7
|
+
data.tar.gz: 472da807a9a7530dcdf5cb5147115b060a379a149d05adf53762134b07314d51d70fe57b855fdf8355a1c0190855a8f1ce59b663a7073ca7f0765aa24f716450
|
data/Gemfile
CHANGED
|
@@ -9,7 +9,7 @@ gemspec
|
|
|
9
9
|
gem 'minitest', '~>6.0', require: false
|
|
10
10
|
gem 'minitest-reporters', '~>1.7', require: false
|
|
11
11
|
gem 'rake', '~>13.2', require: false
|
|
12
|
-
gem 'rdoc', '7.
|
|
12
|
+
gem 'rdoc', '7.2.0', require: false
|
|
13
13
|
gem 'rubocop', '~>1.74', require: false
|
|
14
14
|
gem 'rubocop-minitest', '~>0.38', require: false
|
|
15
15
|
gem 'rubocop-performance', '~>1.25', require: false
|
data/config/default.yml
CHANGED
|
@@ -17,6 +17,10 @@ Elegant/NoEmptyLinesInBlocks:
|
|
|
17
17
|
Description: 'Disallows empty lines inside blocks (do/end, if/end, while, case, begin, etc.)'
|
|
18
18
|
Enabled: true
|
|
19
19
|
VersionAdded: '0.0.19'
|
|
20
|
+
Elegant/NoNilReturn:
|
|
21
|
+
Description: 'Forbids a method or function from returning nil on any code path'
|
|
22
|
+
Enabled: true
|
|
23
|
+
VersionAdded: '0.1.0'
|
|
20
24
|
Elegant/PairedBrackets:
|
|
21
25
|
Description: 'Enforces the paired brackets notation: a bracket must pair on the same line, or start/end its line'
|
|
22
26
|
Enabled: true
|
|
@@ -50,8 +50,7 @@ class RuboCop::Cop::Elegant::NoComments < RuboCop::Cop::Base
|
|
|
50
50
|
def docblock?(comment)
|
|
51
51
|
line = comment.location.line
|
|
52
52
|
successor = codeline(line)
|
|
53
|
-
|
|
54
|
-
definition?(successor)
|
|
53
|
+
successor.positive? && definition?(successor)
|
|
55
54
|
end
|
|
56
55
|
|
|
57
56
|
def codeline(start)
|
|
@@ -63,7 +62,7 @@ class RuboCop::Cop::Elegant::NoComments < RuboCop::Cop::Base
|
|
|
63
62
|
next if stripped.empty? || stripped.start_with?('#')
|
|
64
63
|
return idx + 1
|
|
65
64
|
end
|
|
66
|
-
|
|
65
|
+
0
|
|
67
66
|
end
|
|
68
67
|
|
|
69
68
|
def definition?(line)
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SPDX-FileCopyrightText: Copyright (c) 2019-2026 Yegor Bugayenko
|
|
4
|
+
# SPDX-License-Identifier: MIT
|
|
5
|
+
|
|
6
|
+
# Forbids a method from returning the literal +nil+ explicitly. The
|
|
7
|
+
# cop flags two patterns: a +return nil+ statement anywhere inside
|
|
8
|
+
# the method body, and a method whose tail expression is the literal
|
|
9
|
+
# +nil+ (whether it stands alone or appears as the last statement of
|
|
10
|
+
# a multi-statement body). Methods that omit +return+, use a bare
|
|
11
|
+
# +return+ keyword, or merely happen to evaluate to +nil+ through
|
|
12
|
+
# control flow are not flagged.
|
|
13
|
+
class RuboCop::Cop::Elegant::NoNilReturn < RuboCop::Cop::Base
|
|
14
|
+
MSG = 'Method must not return nil explicitly'
|
|
15
|
+
public_constant :MSG
|
|
16
|
+
|
|
17
|
+
def on_def(node)
|
|
18
|
+
check(node)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def on_defs(node)
|
|
22
|
+
check(node)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def check(node)
|
|
28
|
+
return if node.body.nil?
|
|
29
|
+
explicit(node)
|
|
30
|
+
tail(node.body)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def explicit(node)
|
|
34
|
+
node.each_descendant(:return) do |ret|
|
|
35
|
+
next unless ret.each_ancestor(:def, :defs).first.equal?(node)
|
|
36
|
+
add_offense(ret) if void?(ret)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def void?(ret)
|
|
41
|
+
return false if ret.children.empty?
|
|
42
|
+
ret.children.all?(&:nil_type?)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def tail(node)
|
|
46
|
+
return add_offense(node) if node.nil_type?
|
|
47
|
+
tail(node.children.last) if node.begin_type? || node.kwbegin_type?
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -13,4 +13,5 @@ require_relative 'elegant/no_class_in_module'
|
|
|
13
13
|
require_relative 'elegant/no_comments'
|
|
14
14
|
require_relative 'elegant/no_empty_lines_in_blocks'
|
|
15
15
|
require_relative 'elegant/no_empty_lines_in_methods'
|
|
16
|
+
require_relative 'elegant/no_nil_return'
|
|
16
17
|
require_relative 'elegant/paired_brackets'
|
data/rubocop-elegant.gemspec
CHANGED
|
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
|
|
9
9
|
s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to?(:required_rubygems_version=)
|
|
10
10
|
s.required_ruby_version = '>=2.2'
|
|
11
11
|
s.name = 'rubocop-elegant'
|
|
12
|
-
s.version = '0.
|
|
12
|
+
s.version = '0.2.0'
|
|
13
13
|
s.license = 'MIT'
|
|
14
14
|
s.summary = 'Set of custom RuboCop cops for elegant Ruby coding'
|
|
15
15
|
s.description =
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rubocop-elegant
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Yegor Bugayenko
|
|
@@ -61,6 +61,7 @@ files:
|
|
|
61
61
|
- lib/rubocop/cop/elegant/no_comments.rb
|
|
62
62
|
- lib/rubocop/cop/elegant/no_empty_lines_in_blocks.rb
|
|
63
63
|
- lib/rubocop/cop/elegant/no_empty_lines_in_methods.rb
|
|
64
|
+
- lib/rubocop/cop/elegant/no_nil_return.rb
|
|
64
65
|
- lib/rubocop/cop/elegant/paired_brackets.rb
|
|
65
66
|
- lib/rubocop/cop/elegant_cops.rb
|
|
66
67
|
- lib/rubocop/elegant/plugin.rb
|