rubocop-canon 0.4.0 → 0.5.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
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f8b4a8966824947c873a90109b21e96a5d2c3fa1d01c71c3423ffe45afa7864e
|
|
4
|
+
data.tar.gz: eea9755c6e9ceab22ab0a316c5ec3eb52e3f290baa9cfe1a546adaaa25dc1bb3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fd398ceccc79ca4d5d8f575dcf9141e84bd0648e7ca673edb166955c56898b65f61efcd4988435cb21831a83485c2972b7e12c97defeb9f5ab7610eead5ad805
|
|
7
|
+
data.tar.gz: 65d340eb6eec5fba2b6d95c592abc238d869dd047135059089583596558191633f227a7a0e9aaa125383c241fd433214a435a252f03ca511e808f067a432adca
|
|
@@ -12,6 +12,7 @@ module RuboCop
|
|
|
12
12
|
include RangeHelp
|
|
13
13
|
|
|
14
14
|
BLOCK_TYPES = %i[block numblock].freeze
|
|
15
|
+
MSG_MULTILINE = 'Add a blank line around the multiline statement.'
|
|
15
16
|
|
|
16
17
|
private
|
|
17
18
|
|
|
@@ -31,6 +32,26 @@ module RuboCop
|
|
|
31
32
|
node.first_line != node.last_line
|
|
32
33
|
end
|
|
33
34
|
|
|
35
|
+
def multiline_pair?(previous, following)
|
|
36
|
+
return true if multiline?(previous)
|
|
37
|
+
|
|
38
|
+
multiline?(following)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def require_blank_line(previous, following, message)
|
|
42
|
+
return if comments_between?(previous, following)
|
|
43
|
+
return unless blank_lines_between(previous, following).zero?
|
|
44
|
+
|
|
45
|
+
register_missing_blank_line(previous, following, message)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def forbid_blank_line(previous, following, message)
|
|
49
|
+
return if comments_between?(previous, following)
|
|
50
|
+
return unless blank_lines_between(previous, following) == 1
|
|
51
|
+
|
|
52
|
+
register_extra_blank_line(previous, message)
|
|
53
|
+
end
|
|
54
|
+
|
|
34
55
|
def comments_between?(previous, following)
|
|
35
56
|
processed_source.comments.any? do |comment|
|
|
36
57
|
comment.loc.line >= previous.last_line && comment.loc.line < following.first_line
|
|
@@ -53,7 +53,6 @@ module RuboCop
|
|
|
53
53
|
MSG_EXTRA = 'Remove the blank line inside the trailing phase.'
|
|
54
54
|
MSG_MISSING = 'Add a blank line before the trailing phase.'
|
|
55
55
|
MSG_SETUP_SPLIT = 'Remove the blank line inside the setup.'
|
|
56
|
-
MSG_MULTILINE = 'Add a blank line around the multiline statement.'
|
|
57
56
|
|
|
58
57
|
def on_block(node)
|
|
59
58
|
return unless configured_block?(node)
|
|
@@ -99,26 +98,6 @@ module RuboCop
|
|
|
99
98
|
end
|
|
100
99
|
end
|
|
101
100
|
|
|
102
|
-
def require_blank_line(previous, following, message)
|
|
103
|
-
return if comments_between?(previous, following)
|
|
104
|
-
return unless blank_lines_between(previous, following).zero?
|
|
105
|
-
|
|
106
|
-
register_missing_blank_line(previous, following, message)
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
def forbid_blank_line(previous, following, message)
|
|
110
|
-
return if comments_between?(previous, following)
|
|
111
|
-
return unless blank_lines_between(previous, following) == 1
|
|
112
|
-
|
|
113
|
-
register_extra_blank_line(previous, message)
|
|
114
|
-
end
|
|
115
|
-
|
|
116
|
-
def multiline_pair?(previous, following)
|
|
117
|
-
return true if multiline?(previous)
|
|
118
|
-
|
|
119
|
-
multiline?(following)
|
|
120
|
-
end
|
|
121
|
-
|
|
122
101
|
def detect_trailing_start(statements)
|
|
123
102
|
statements.index { |statement| trailing?(statement) }
|
|
124
103
|
end
|
|
@@ -4,7 +4,7 @@ module RuboCop
|
|
|
4
4
|
module Cop
|
|
5
5
|
module Canon
|
|
6
6
|
# Enforces blank lines around a method body's standalone calls and
|
|
7
|
-
# nowhere else.
|
|
7
|
+
# multiline statements, and nowhere else.
|
|
8
8
|
#
|
|
9
9
|
# `SeparatedMethods` names the calls that stand on their own —
|
|
10
10
|
# `authorize!`, `expose`, `mail`. Each takes exactly one blank line
|
|
@@ -15,8 +15,15 @@ module RuboCop
|
|
|
15
15
|
# The list is empty by default, so the cop only removes until it is
|
|
16
16
|
# configured.
|
|
17
17
|
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
18
|
+
# Guard clauses group: two in a row take no blank line between them.
|
|
19
|
+
# The position after the last guard belongs to
|
|
20
|
+
# `Layout/EmptyLineAfterGuardClause`, so this cop leaves it alone — and
|
|
21
|
+
# recognises a guard by that cop's own definition: an `if` whose branch
|
|
22
|
+
# is a single-line `return`, `break`, `next`, `raise` or `fail`. A bare
|
|
23
|
+
# `raise` on its own line is a statement, not a guard.
|
|
24
|
+
#
|
|
25
|
+
# A statement spanning several lines is always set apart by a blank
|
|
26
|
+
# line on each side, guards included.
|
|
20
27
|
#
|
|
21
28
|
# An entry matches a method name anywhere in the call chain. Write it
|
|
22
29
|
# dotted — `errors.add` — to match a receiver and method together, so
|
|
@@ -47,12 +54,12 @@ module RuboCop
|
|
|
47
54
|
# Current.account = nil
|
|
48
55
|
# end
|
|
49
56
|
#
|
|
50
|
-
# # good — the
|
|
57
|
+
# # good — guards group, and the last one owns the blank line after it
|
|
51
58
|
# def total
|
|
52
|
-
#
|
|
53
|
-
# return 0 if items.empty?
|
|
59
|
+
# return 0 if order.nil?
|
|
60
|
+
# return 0 if order.items.empty?
|
|
54
61
|
#
|
|
55
|
-
# items.sum(&:amount)
|
|
62
|
+
# order.items.sum(&:amount)
|
|
56
63
|
# end
|
|
57
64
|
class MethodBodyBlankLines < Base
|
|
58
65
|
extend AutoCorrector
|
|
@@ -60,7 +67,7 @@ module RuboCop
|
|
|
60
67
|
|
|
61
68
|
MSG_EXTRA = 'Remove the blank line inside the method body.'
|
|
62
69
|
MSG_MISSING = 'Add a blank line around the standalone call.'
|
|
63
|
-
JUMP_METHODS = %i[raise fail
|
|
70
|
+
JUMP_METHODS = %i[raise fail].freeze
|
|
64
71
|
|
|
65
72
|
def on_def(node)
|
|
66
73
|
check_method_body(node)
|
|
@@ -77,29 +84,32 @@ module RuboCop
|
|
|
77
84
|
return if statements.size < 2
|
|
78
85
|
|
|
79
86
|
statements.each_cons(2) do |previous, following|
|
|
80
|
-
next if
|
|
87
|
+
next if guard_boundary?(previous, following)
|
|
81
88
|
|
|
82
|
-
check_gap(previous, following
|
|
89
|
+
check_gap(previous, following)
|
|
83
90
|
end
|
|
84
91
|
end
|
|
85
92
|
|
|
86
|
-
def
|
|
87
|
-
return
|
|
88
|
-
|
|
89
|
-
blank_lines = blank_lines_between(previous, following)
|
|
90
|
-
return if blank_lines == expected
|
|
91
|
-
return if blank_lines > 1
|
|
93
|
+
def guard_boundary?(previous, following)
|
|
94
|
+
return false unless guard?(previous)
|
|
92
95
|
|
|
93
|
-
|
|
96
|
+
!guard?(following)
|
|
97
|
+
end
|
|
94
98
|
|
|
95
|
-
|
|
99
|
+
def check_gap(previous, following)
|
|
100
|
+
if multiline_pair?(previous, following)
|
|
101
|
+
require_blank_line(previous, following, MSG_MULTILINE)
|
|
102
|
+
elsif separated_pair?(previous, following)
|
|
103
|
+
require_blank_line(previous, following, MSG_MISSING)
|
|
104
|
+
else
|
|
105
|
+
forbid_blank_line(previous, following, MSG_EXTRA)
|
|
106
|
+
end
|
|
96
107
|
end
|
|
97
108
|
|
|
98
|
-
def
|
|
99
|
-
return
|
|
100
|
-
return 1 if separated?(following)
|
|
109
|
+
def separated_pair?(previous, following)
|
|
110
|
+
return true if separated?(previous)
|
|
101
111
|
|
|
102
|
-
|
|
112
|
+
separated?(following)
|
|
103
113
|
end
|
|
104
114
|
|
|
105
115
|
def separated?(node)
|
|
@@ -116,21 +126,23 @@ module RuboCop
|
|
|
116
126
|
end
|
|
117
127
|
|
|
118
128
|
def guard?(node)
|
|
119
|
-
return true if jump?(node)
|
|
120
129
|
return false unless node.if_type?
|
|
121
|
-
return false unless node.modifier_form?
|
|
122
|
-
return true if jump?(node.if_branch)
|
|
123
130
|
|
|
124
|
-
|
|
131
|
+
branch = node.if_branch
|
|
132
|
+
return false if branch.nil?
|
|
133
|
+
return true if branch.guard_clause?
|
|
134
|
+
|
|
135
|
+
jump?(branch)
|
|
125
136
|
end
|
|
126
137
|
|
|
127
138
|
def jump?(node)
|
|
128
|
-
return false if node.nil?
|
|
129
139
|
return true if node.return_type?
|
|
130
140
|
return true if node.break_type?
|
|
131
141
|
return true if node.next_type?
|
|
142
|
+
return false unless node.send_type?
|
|
143
|
+
return false unless node.receiver.nil?
|
|
132
144
|
|
|
133
|
-
|
|
145
|
+
JUMP_METHODS.include?(node.method_name)
|
|
134
146
|
end
|
|
135
147
|
|
|
136
148
|
def separated_methods
|