Skip to content

Commit bb171f7

Browse files
authored
Update Layout cops to be more consistent with Standard (#55)
In an effort to more closely adhere to https://github.com/standardrb/standard, I've added a spec to detect the introduction of new rules that don't comply with Standard. I've updated the layout cops to more closely match the rules in standard.
1 parent 4a773b0 commit bb171f7

File tree

9 files changed

+158
-35
lines changed

9 files changed

+158
-35
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ group :development do
77
gem 'bundler'
88
gem 'rspec-rails'
99
gem 'rake', '>= 12.3.3'
10+
gem 'standard'
1011
end

Gemfile.lock

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
betterlint (1.13.0)
4+
betterlint (1.14.0)
55
rubocop (~> 1.62.0)
66
rubocop-graphql (~> 1.5.0)
77
rubocop-performance (~> 1.21.0)
@@ -57,6 +57,7 @@ GEM
5757
reline (>= 0.3.8)
5858
json (2.7.1)
5959
language_server-protocol (3.17.0.3)
60+
lint_roller (1.1.0)
6061
loofah (2.22.0)
6162
crass (~> 1.0.2)
6263
nokogiri (>= 1.12.0)
@@ -165,6 +166,18 @@ GEM
165166
rubocop-rspec_rails (2.28.2)
166167
rubocop (~> 1.40)
167168
ruby-progressbar (1.13.0)
169+
standard (1.35.1)
170+
language_server-protocol (~> 3.17.0.2)
171+
lint_roller (~> 1.0)
172+
rubocop (~> 1.62.0)
173+
standard-custom (~> 1.0.0)
174+
standard-performance (~> 1.3)
175+
standard-custom (1.0.2)
176+
lint_roller (~> 1.0)
177+
rubocop (~> 1.50)
178+
standard-performance (1.4.0)
179+
lint_roller (~> 1.1)
180+
rubocop-performance (~> 1.21.0)
168181
stringio (3.1.0)
169182
strscan (3.1.0)
170183
thor (1.3.0)
@@ -186,6 +199,7 @@ DEPENDENCIES
186199
pry
187200
rake (>= 12.3.3)
188201
rspec-rails
202+
standard
189203

190204
BUNDLED WITH
191205
2.4.22

betterlint.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
55

66
Gem::Specification.new do |s|
77
s.name = "betterlint"
8-
s.version = "1.13.0"
8+
s.version = "1.14.0"
99
s.authors = ["Development"]
1010
s.email = ["[email protected]"]
1111
s.summary = "Betterment rubocop configuration"

config/default.yml

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,43 @@ FactoryBot/ConsistentParenthesesStyle:
101101
FactoryBot/SyntaxMethods:
102102
Enabled: false
103103

104+
Layout/ArgumentAlignment:
105+
EnforcedStyle: with_fixed_indentation
106+
107+
Layout/ArrayAlignment:
108+
EnforcedStyle: with_fixed_indentation
109+
104110
Layout/CaseIndentation:
105-
IndentOneStep: true
111+
EnforcedStyle: end
112+
IndentOneStep: false
106113

107114
Layout/ClosingParenthesisIndentation:
108115
Enabled: true
109116

117+
Layout/EndAlignment:
118+
EnforcedStyleAlignWith: variable
119+
120+
Layout/FirstArgumentIndentation:
121+
EnforcedStyle: consistent
122+
110123
Layout/FirstArrayElementIndentation:
111124
EnforcedStyle: consistent
112125

126+
Layout/FirstHashElementIndentation:
127+
EnforcedStyle: consistent
128+
129+
Layout/FirstParameterIndentation:
130+
Enabled: false
131+
132+
Layout/LineContinuationLeadingSpace:
133+
Enabled: false
134+
135+
Layout/LineContinuationSpacing:
136+
Enabled: true
137+
138+
Layout/LineEndStringConcatenationIndentation:
139+
Enabled: false
140+
113141
Layout/LineLength:
114142
Max: 140
115143

@@ -120,11 +148,11 @@ Layout/MultilineOperationIndentation:
120148
EnforcedStyle: indented
121149

122150
Layout/ParameterAlignment:
123-
Enabled: false
151+
Enabled: true
152+
EnforcedStyle: with_fixed_indentation
124153

125-
# Disabling because of a bug in rubocop: https://github.com/rubocop-hq/rubocop/issues/6918
126154
Layout/RescueEnsureAlignment:
127-
Enabled: false
155+
Enabled: true
128156

129157
Lint/AmbiguousBlockAssociation:
130158
Exclude:

lib/rubocop/cop/betterment/non_standard_actions.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ def check_raw_route(node)
5353
if route
5454
(path, param, value) = route
5555
action = case param
56-
when :to then value.first.split('#').last
57-
when :action then value.first
58-
else path
59-
end
56+
when :to then value.first.split('#').last
57+
when :action then value.first
58+
else path
59+
end
6060
add_offense(node, message: MSG_ROUTE_TO) unless allowed_action?(action)
6161
true
6262
end

lib/rubocop/cop/betterment/render_status.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ def on_def(node)
2626

2727
def infer_status(responder)
2828
case extract_template(responder).to_s
29-
when 'new', 'edit'
30-
:unprocessable_entity
31-
else
32-
:ok
29+
when 'new', 'edit'
30+
:unprocessable_entity
31+
else
32+
:ok
3333
end
3434
end
3535

lib/rubocop/cop/betterment/utils/parser.rb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,25 @@ def self.get_return_values(node)
3939
return [node] if node.literal? || node.variable?
4040

4141
case node.type
42-
when :begin
43-
get_return_values(node.children.last)
44-
when :block
45-
get_return_values(node.body)
46-
when :if
47-
if_rets = get_return_values(node.if_branch)
48-
else_rets = get_return_values(node.else_branch)
49-
if_rets + else_rets
50-
when :case
51-
cases = []
52-
node.each_when do |block|
53-
cases += get_return_values(block.body)
54-
end
42+
when :begin
43+
get_return_values(node.children.last)
44+
when :block
45+
get_return_values(node.body)
46+
when :if
47+
if_rets = get_return_values(node.if_branch)
48+
else_rets = get_return_values(node.else_branch)
49+
if_rets + else_rets
50+
when :case
51+
cases = []
52+
node.each_when do |block|
53+
cases += get_return_values(block.body)
54+
end
5555

56-
cases + get_return_values(node.else_branch)
57-
when :send
58-
[node]
59-
else
60-
[]
56+
cases + get_return_values(node.else_branch)
57+
when :send
58+
[node]
59+
else
60+
[]
6161
end
6262
end
6363

spec/standard_compliance_spec.rb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe 'standard compliance' do
6+
let(:exceptions) do
7+
%w(
8+
Layout/LineLength
9+
Lint/AmbiguousBlockAssociation
10+
Lint/AmbiguousOperator
11+
Lint/AmbiguousRegexpLiteral
12+
Lint/BooleanSymbol
13+
Metrics/AbcSize
14+
Metrics/ClassLength
15+
Metrics/CyclomaticComplexity
16+
Metrics/ModuleLength
17+
Metrics/ParameterLists
18+
Metrics/PerceivedComplexity
19+
Naming/PredicateName
20+
Naming/VariableNumber
21+
Style/BlockDelimiters
22+
Style/FrozenStringLiteralComment
23+
Style/LambdaCall
24+
Style/MissingElse
25+
Style/NumberedParameters
26+
Style/PercentLiteralDelimiters
27+
Style/SafeNavigation
28+
Style/StringLiterals
29+
Style/TrailingCommaInArguments
30+
Style/TrailingCommaInArrayLiteral
31+
Style/TrailingCommaInHashLiteral
32+
Style/YodaCondition
33+
)
34+
end
35+
36+
let(:default_config) do
37+
root = Gem.loaded_specs['rubocop'].full_gem_path
38+
path = File.expand_path('config/default.yml', root)
39+
config = YAML.load_file(path, permitted_classes: [Regexp, Symbol])
40+
config.delete('AllCops')
41+
config
42+
end
43+
44+
let(:standard_config) do
45+
root = Gem.loaded_specs['standard'].full_gem_path
46+
path = File.expand_path('config/base.yml', root)
47+
YAML.load_file(path)
48+
end
49+
50+
let(:betterlint_config) do
51+
YAML.load_file('config/default.yml')
52+
end
53+
54+
it 'complies with standardrb with notable exceptions' do
55+
violations = betterlint_config.filter_map do |name, betterlint|
56+
default = default_config[name] or next
57+
standard = standard_config.fetch(name, {})
58+
59+
betterlint = default.merge(betterlint)
60+
standard = default.merge(standard)
61+
62+
name if betterlint != standard
63+
end
64+
65+
new_violations = (violations - exceptions).sort
66+
old_exceptions = (exceptions - violations).sort
67+
68+
expect(new_violations).to be_empty, <<~MSG
69+
All new rules should match Standard. These following rules are invalid:
70+
71+
#{new_violations.join("\n")}
72+
MSG
73+
74+
expect(old_exceptions).to be_empty, <<~MSG
75+
The following rules are now compliant with Standard and can be removed from the exceptions:
76+
77+
#{old_exceptions.join("\n")}
78+
MSG
79+
end
80+
end

spec/support/betterlint_config.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
.default_configuration.for_cop(cop_class)
1515
.merge(betterlint_config.for_cop(cop_class))
1616
.merge({
17-
'Enabled' => true, # in case it is 'pending'
18-
'AutoCorrect' => true, # in case defaults set it to false
19-
})
17+
'Enabled' => true, # in case it is 'pending'
18+
'AutoCorrect' => true, # in case defaults set it to false
19+
})
2020
.merge(cop_config)
2121
end
2222
end

0 commit comments

Comments
 (0)