gitlab-styles 2.1.0 → 2.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 +6 -0
- data/lib/gitlab/styles/rubocop.rb +1 -0
- data/lib/gitlab/styles/rubocop/cop/line_break_after_guard_clauses.rb +102 -0
- data/lib/gitlab/styles/version.rb +1 -1
- data/rubocop-layout.yml +8 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5c5dbefc4cc4e9fce7794623c0688cb4bfb145e186a0c12376e8cf973b09c1b
|
4
|
+
data.tar.gz: ce47876a9ac193036759b85aea6ef23d1c2caf3037e057d640849fe2cce8b197
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f2713efa71f126be8e9bf9b44ca4caef0c09f69bdbec2f2abc143184356891341b4b423606f742ea7d517feabec0dadd259553846adcf8c068c80dcc8e588dd
|
7
|
+
data.tar.gz: 4c83cc34cebefb7b7d0bc53adeec8b733f9adefbe446c7e46cd81f60fa136de48445de38b71533edf76f32789c3fa4caf46b27860dd23177a3607377f81a6397
|
data/Gemfile
CHANGED
@@ -5,6 +5,7 @@ require 'gitlab/styles/rubocop/cop/redirect_with_status'
|
|
5
5
|
require 'gitlab/styles/rubocop/cop/polymorphic_associations'
|
6
6
|
require 'gitlab/styles/rubocop/cop/active_record_dependent'
|
7
7
|
require 'gitlab/styles/rubocop/cop/in_batches'
|
8
|
+
require 'gitlab/styles/rubocop/cop/line_break_after_guard_clauses'
|
8
9
|
require 'gitlab/styles/rubocop/cop/rspec/single_line_hook'
|
9
10
|
|
10
11
|
module Gitlab
|
@@ -0,0 +1,102 @@
|
|
1
|
+
module Gitlab
|
2
|
+
module Styles
|
3
|
+
module Rubocop
|
4
|
+
module Cop
|
5
|
+
# Ensures a line break after guard clauses.
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# # bad
|
9
|
+
# return unless condition
|
10
|
+
# do_stuff
|
11
|
+
#
|
12
|
+
# # good
|
13
|
+
# return unless condition
|
14
|
+
#
|
15
|
+
# do_stuff
|
16
|
+
#
|
17
|
+
# # bad
|
18
|
+
# raise if condition
|
19
|
+
# do_stuff
|
20
|
+
#
|
21
|
+
# # good
|
22
|
+
# raise if condition
|
23
|
+
#
|
24
|
+
# do_stuff
|
25
|
+
#
|
26
|
+
# Multiple guard clauses are allowed without
|
27
|
+
# line break.
|
28
|
+
#
|
29
|
+
# # good
|
30
|
+
# return unless condition_a
|
31
|
+
# return unless condition_b
|
32
|
+
#
|
33
|
+
# do_stuff
|
34
|
+
#
|
35
|
+
# Guard clauses in case statement are allowed without
|
36
|
+
# line break.
|
37
|
+
#
|
38
|
+
# # good
|
39
|
+
# case model
|
40
|
+
# when condition_a
|
41
|
+
# return true unless condition_b
|
42
|
+
# when
|
43
|
+
# ...
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# Guard clauses before end are allowed without
|
47
|
+
# line break.
|
48
|
+
#
|
49
|
+
# # good
|
50
|
+
# if condition_a
|
51
|
+
# do_something
|
52
|
+
# else
|
53
|
+
# do_something_else
|
54
|
+
# return unless condition
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# do_something_more
|
58
|
+
class LineBreakAfterGuardClauses < RuboCop::Cop::Cop
|
59
|
+
MSG = 'Add a line break after guard clauses'.freeze
|
60
|
+
|
61
|
+
def_node_matcher :guard_clause_node?, <<-PATTERN
|
62
|
+
[{(send nil? {:raise :fail :throw} ...) return break next} single_line?]
|
63
|
+
PATTERN
|
64
|
+
|
65
|
+
def on_if(node)
|
66
|
+
return unless node.single_line?
|
67
|
+
return unless guard_clause?(node)
|
68
|
+
return if next_line(node).blank? || clause_last_line?(next_line(node)) || guard_clause?(next_sibling(node))
|
69
|
+
|
70
|
+
add_offense(node, location: :expression, message: MSG)
|
71
|
+
end
|
72
|
+
|
73
|
+
def autocorrect(node)
|
74
|
+
lambda do |corrector|
|
75
|
+
corrector.insert_after(node.loc.expression, "\n")
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def guard_clause?(node)
|
82
|
+
return false unless node.if_type?
|
83
|
+
|
84
|
+
guard_clause_node?(node.if_branch)
|
85
|
+
end
|
86
|
+
|
87
|
+
def next_sibling(node)
|
88
|
+
node.parent.children[node.sibling_index + 1]
|
89
|
+
end
|
90
|
+
|
91
|
+
def next_line(node)
|
92
|
+
processed_source[node.loc.line]
|
93
|
+
end
|
94
|
+
|
95
|
+
def clause_last_line?(line)
|
96
|
+
line =~ /^\s*(?:end|elsif|else|when|rescue|ensure)/
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
data/rubocop-layout.yml
CHANGED
@@ -191,14 +191,20 @@ Layout/SpaceBeforeComment:
|
|
191
191
|
Layout/SpaceBeforeSemicolon:
|
192
192
|
Enabled: true
|
193
193
|
|
194
|
-
# Checks for
|
195
|
-
|
194
|
+
# Checks that brackets used for array literals have or don't have
|
195
|
+
# surrounding space depending on configuration..
|
196
|
+
Layout/SpaceInsideArrayLiteralBrackets:
|
196
197
|
Enabled: true
|
197
198
|
|
198
199
|
# Use spaces inside hash literal braces - or don't.
|
199
200
|
Layout/SpaceInsideHashLiteralBraces:
|
200
201
|
Enabled: true
|
201
202
|
|
203
|
+
# Checks that reference brackets have or don't have
|
204
|
+
# surrounding space depending on configuration.
|
205
|
+
Layout/SpaceInsideReferenceBrackets:
|
206
|
+
Enabled: true
|
207
|
+
|
202
208
|
# No spaces inside range literals.
|
203
209
|
Layout/SpaceInsideRangeLiteral:
|
204
210
|
Enabled: true
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gitlab-styles
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- GitLab
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-12-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|
@@ -121,6 +121,7 @@ files:
|
|
121
121
|
- lib/gitlab/styles/rubocop/cop/custom_error_class.rb
|
122
122
|
- lib/gitlab/styles/rubocop/cop/gem_fetcher.rb
|
123
123
|
- lib/gitlab/styles/rubocop/cop/in_batches.rb
|
124
|
+
- lib/gitlab/styles/rubocop/cop/line_break_after_guard_clauses.rb
|
124
125
|
- lib/gitlab/styles/rubocop/cop/polymorphic_associations.rb
|
125
126
|
- lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
|
126
127
|
- lib/gitlab/styles/rubocop/cop/rspec/single_line_hook.rb
|
@@ -160,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
160
161
|
version: '0'
|
161
162
|
requirements: []
|
162
163
|
rubyforge_project:
|
163
|
-
rubygems_version: 2.7.
|
164
|
+
rubygems_version: 2.7.3
|
164
165
|
signing_key:
|
165
166
|
specification_version: 4
|
166
167
|
summary: GitLab style guides and shared style configs.
|