Skip to content

Commit b48a7ab

Browse files
rails-template-sync[bot]Template Bot
andauthored
Apply template update: Rubocop: add linter to disallow before blocks (#1486)
Source: mockdeep/Rails-Template#1399 Co-authored-by: Template Bot <template-bot@users.noreply.github.com>
1 parent f118560 commit b48a7ab

File tree

6 files changed

+146
-2
lines changed

6 files changed

+146
-2
lines changed

.rubocop.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
require:
2+
- ./linters/rubocop/cop/rspec/no_before_hook
3+
14
inherit_from: .rubocop_todo.yml
25
inherit_mode: { merge: [Exclude] }
36

47
plugins:
58
- rubocop-capybara
69
- rubocop-factory_bot
10+
- rubocop-i18n
711
- rubocop-rails
812
- rubocop-rake
913
- rubocop-rspec
@@ -29,7 +33,11 @@ RSpec/MessageExpectation:
2933
EnforcedStyle: expect
3034
Exclude: [spec/support/matchers/**/*.rb]
3135
RSpec/MessageSpies: { EnforcedStyle: receive }
32-
RSpec/MultipleMemoizedHelpers: { Max: 0, AllowSubject: false }
36+
RSpec/MultipleMemoizedHelpers:
37+
Max: 0
38+
AllowSubject: false
39+
Exclude:
40+
- spec/linters/rubocop/**/*.rb
3341
Style/ClassAndModuleChildren: { EnforcedStyle: compact }
3442
Style/MethodCallWithArgsParentheses:
3543
AllowedMethods:
@@ -49,6 +57,13 @@ Style/WordArray: { EnforcedStyle: brackets }
4957
Style/DocumentationMethod: { Enabled: false }
5058
Style/Documentation: { Enabled: false }
5159

60+
I18n/GetText/DecorateString:
61+
Exclude:
62+
- linters/**/*.rb
63+
I18n/RailsI18n/DecorateString:
64+
Exclude:
65+
- linters/**/*.rb
66+
5267
################################################################################
5368
#
5469
# Rules we don't want to enable

.rubocop_todo.yml

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config --auto-gen-only-exclude --exclude-limit 400`
3-
# on 2026-03-11 01:58:30 UTC using RuboCop version 1.85.1.
3+
# on 2026-04-07 03:24:43 UTC using RuboCop version 1.86.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -12,6 +12,31 @@ Capybara/NegationMatcherAfterVisit:
1212
- 'spec/system/account_setup_spec.rb'
1313
- 'spec/system/stories_index_spec.rb'
1414

15+
# Offense count: 5
16+
# This cop supports safe autocorrection (--autocorrect).
17+
I18n/GetText/DecorateFunctionMessage:
18+
Exclude:
19+
- 'app/commands/cast_boolean.rb'
20+
- 'app/models/user.rb'
21+
- 'spec/support/matchers/change_all_records.rb'
22+
- 'spec/support/matchers/change_record.rb'
23+
- 'spec/support/matchers/invoke.rb'
24+
25+
# Offense count: 4
26+
# This cop supports safe autocorrection (--autocorrect).
27+
I18n/GetText/DecorateString:
28+
Exclude:
29+
- 'Rakefile'
30+
- 'spec/rails_helper.rb'
31+
- 'spec/system/login_spec.rb'
32+
33+
# Offense count: 4
34+
I18n/RailsI18n/DecorateString:
35+
Exclude:
36+
- 'Rakefile'
37+
- 'spec/rails_helper.rb'
38+
- 'spec/system/login_spec.rb'
39+
1540
# Offense count: 6
1641
# This cop supports safe autocorrection (--autocorrect).
1742
# Configuration parameters: AllowMultipleStyles, EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle.
@@ -188,6 +213,13 @@ RSpec/NestedGroups:
188213
Exclude:
189214
- 'spec/integration/feed_importing_spec.rb'
190215

216+
# Offense count: 3
217+
RSpec/NoBeforeHook:
218+
Exclude:
219+
- 'spec/commands/feed/import_from_opml_spec.rb'
220+
- 'spec/system/authentication_spec.rb'
221+
- 'spec/system/profile_spec.rb'
222+
191223
# Offense count: 5
192224
# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames.
193225
RSpec/VerifiedDoubles:
@@ -260,6 +292,16 @@ Rails/Validation:
260292
Exclude:
261293
- 'app/models/story.rb'
262294

295+
# Offense count: 1
296+
# This cop supports unsafe autocorrection (--autocorrect-all).
297+
# Configuration parameters: EnforcedStyle, EnforcedStyleForClasses, EnforcedStyleForModules.
298+
# SupportedStyles: nested, compact
299+
# SupportedStylesForClasses: ~, nested, compact
300+
# SupportedStylesForModules: ~, nested, compact
301+
Style/ClassAndModuleChildren:
302+
Exclude:
303+
- 'linters/rubocop/cop/rspec/no_before_hook.rb'
304+
263305
# Offense count: 23
264306
# This cop supports unsafe autocorrection (--autocorrect-all).
265307
# Configuration parameters: EnforcedStyle.

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ group :development do
3636
gem "rubocop", require: false
3737
gem "rubocop-capybara", require: false
3838
gem "rubocop-factory_bot", require: false
39+
gem "rubocop-i18n", require: false
3940
gem "rubocop-rails", require: false
4041
gem "rubocop-rake", require: false
4142
gem "rubocop-rspec", require: false

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,9 @@ GEM
358358
rubocop-factory_bot (2.28.0)
359359
lint_roller (~> 1.1)
360360
rubocop (~> 1.72, >= 1.72.1)
361+
rubocop-i18n (3.2.3)
362+
lint_roller (~> 1.1)
363+
rubocop (>= 1.72.1)
361364
rubocop-rails (2.34.3)
362365
activesupport (>= 4.2.0)
363366
lint_roller (~> 1.1)
@@ -487,6 +490,7 @@ DEPENDENCIES
487490
rubocop
488491
rubocop-capybara
489492
rubocop-factory_bot
493+
rubocop-i18n
490494
rubocop-rails
491495
rubocop-rake
492496
rubocop-rspec
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module RSpec
6+
# Disallows the use of `before` hooks in specs.
7+
#
8+
# @example
9+
# # bad
10+
# before { do_something }
11+
# before(:each) { do_something }
12+
#
13+
# # good
14+
# # Inline setup directly in the example.
15+
class NoBeforeHook < Base
16+
MSG = "Do not use `before` hooks."
17+
18+
# @!method before_hook?(node)
19+
def_node_matcher(:before_hook?, <<~PATTERN)
20+
(block (send nil? :before ...) ...)
21+
PATTERN
22+
23+
def on_block(node)
24+
return unless before_hook?(node)
25+
26+
add_offense(node)
27+
end
28+
29+
alias on_numblock on_block
30+
end
31+
end
32+
end
33+
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# frozen_string_literal: true
2+
3+
require "rubocop"
4+
require "rubocop-rspec"
5+
require "rubocop/rspec/cop_helper"
6+
require "rubocop/rspec/expect_offense"
7+
require_relative("../../../../../linters/rubocop/cop/rspec/no_before_hook")
8+
9+
RSpec.describe RuboCop::Cop::RSpec::NoBeforeHook do
10+
include CopHelper
11+
include RuboCop::RSpec::ExpectOffense
12+
13+
let(:cop) { described_class.new }
14+
15+
it "registers an offense for a before block" do
16+
expect_offense(<<~RUBY)
17+
before { do_something }
18+
^^^^^^^^^^^^^^^^^^^^^^^ RSpec/NoBeforeHook: Do not use `before` hooks.
19+
RUBY
20+
end
21+
22+
it "registers an offense for before(:each)" do
23+
expect_offense(<<~RUBY)
24+
before(:each) { do_something }
25+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RSpec/NoBeforeHook: Do not use `before` hooks.
26+
RUBY
27+
end
28+
29+
it "registers an offense for before(:all)" do
30+
expect_offense(<<~RUBY)
31+
before(:all) { do_something }
32+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RSpec/NoBeforeHook: Do not use `before` hooks.
33+
RUBY
34+
end
35+
36+
it "does not register an offense for an after block" do
37+
expect_no_offenses(<<~RUBY)
38+
after { do_something }
39+
RUBY
40+
end
41+
42+
it "does not register an offense for a regular method call" do
43+
expect_no_offenses(<<~RUBY)
44+
def setup
45+
do_something
46+
end
47+
RUBY
48+
end
49+
end

0 commit comments

Comments
 (0)