Skip to content

Commit b413db6

Browse files
authored
Merge pull request #224 from koic/lazy_load_cops
Load cops lazily
2 parents 7643698 + 0effb4b commit b413db6

11 files changed

Lines changed: 111 additions & 28 deletions

File tree

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Layout/LineLength:
2828
Max: 80 # default: 120
2929
AllowedPatterns:
3030
- '^\s*# .*https?:\/\/.+\[.+\]\.?$' # Allow long asciidoc links
31+
- '^\s*register_cop :' # Cop registrations in the department modules
3132

3233
Layout/MultilineMethodCallIndentation:
3334
EnforcedStyle: indented

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Edge (Unreleased)
44

5+
- Speed up loading rubocop-capybara by lazily loading only the cops needed for a run. This requires RuboCop 1.89.0+. ([@koic])
56
- Fix an incorrect autocorrect for `Capybara/RedundantWithinFind` when `find_by_id` uses a dynamic id. ([@ydah])
67
- Fix a false positive for `Capybara/RSpec/HaveSelector` when `DefaultSelector` is unsupported. ([@ydah])
78
- Fix an incorrect autocorrect for `Capybara/SpecificFinders` when a selector class is empty or contains an escaped dot. ([@ydah])

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ task :new_cop, [:cop] do |_task, args|
9494
generator = RuboCop::Capybara::Cop::Generator.new(cop_name)
9595
generator.write_source
9696
generator.write_spec
97-
generator.inject_require(root_file_path: 'lib/rubocop/cop/capybara_cops.rb')
97+
generator.inject_registration
9898
generator.inject_config
9999

100100
puts generator.todo

docs/modules/ROOT/pages/development.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Use the bundled rake task `new_cop` to generate a cop template:
1414
$ bundle exec rake 'new_cop[Capybara/CopName]'
1515
[create] lib/rubocop/cop/capybara/cop_name.rb
1616
[create] spec/rubocop/cop/capybara/cop_name_spec.rb
17-
[modify] lib/rubocop/cop/capybara_cops.rb - `require_relative 'capybara/cop_name'` was injected.
17+
[modify] lib/rubocop/cop/capybara.rb - `register_cop :CopName, "#{__dir__}/capybara/cop_name"` was injected.
1818
[modify] A configuration for the cop is added into config/default.yml.
1919
Do 4 steps:
2020
1. Modify the description of Capybara/CopName in config/default.yml

lib/rubocop-capybara.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
require_relative 'rubocop/cop/capybara/mixin/css_attributes_parser'
1010
require_relative 'rubocop/cop/capybara/mixin/css_selector'
1111

12-
require_relative 'rubocop/cop/capybara_cops'
12+
require_relative 'rubocop/cop/capybara'
1313

1414
RuboCop::Cop::Style::TrailingCommaInArguments.singleton_class.prepend(
1515
Module.new do

lib/rubocop/cop/capybara.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'capybara/rspec'
4+
5+
module RuboCop
6+
module Cop
7+
# Cops for the `Capybara` department. The department's cops are
8+
# registered for lazy loading and their files are loaded on demand.
9+
module Capybara
10+
extend LazyLoader
11+
12+
register_cop :AmbiguousClick, "#{__dir__}/capybara/ambiguous_click"
13+
register_cop :AssertStyle, "#{__dir__}/capybara/assert_style"
14+
register_cop :FindAllFirst, "#{__dir__}/capybara/find_all_first"
15+
register_cop :RedundantWithinFind, "#{__dir__}/capybara/redundant_within_find"
16+
register_cop :SpecificActions, "#{__dir__}/capybara/specific_actions"
17+
register_cop :SpecificFinders, "#{__dir__}/capybara/specific_finders"
18+
end
19+
end
20+
end

lib/rubocop/cop/capybara/rspec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
module RuboCop
4+
module Cop
5+
module Capybara
6+
# Cops for the `Capybara/RSpec` department. The department's cops are
7+
# registered for lazy loading and their files are loaded on demand.
8+
module RSpec
9+
extend LazyLoader
10+
11+
register_cop :CurrentPathExpectation, "#{__dir__}/rspec/current_path_expectation"
12+
register_cop :HaveContent, "#{__dir__}/rspec/have_content"
13+
register_cop :HaveSelector, "#{__dir__}/rspec/have_selector"
14+
register_cop :MatchStyle, "#{__dir__}/rspec/match_style"
15+
register_cop :NegationMatcher, "#{__dir__}/rspec/negation_matcher"
16+
register_cop :NegationMatcherAfterVisit, "#{__dir__}/rspec/negation_matcher_after_visit"
17+
register_cop :PredicateMatcher, "#{__dir__}/rspec/predicate_matcher"
18+
register_cop :SpecificMatcher, "#{__dir__}/rspec/specific_matcher"
19+
register_cop :VisibilityMatcher, "#{__dir__}/rspec/visibility_matcher"
20+
end
21+
end
22+
end
23+
end

lib/rubocop/cop/capybara_cops.rb

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
# frozen_string_literal: true
22

3-
require_relative 'capybara/rspec/current_path_expectation'
4-
require_relative 'capybara/rspec/have_content'
5-
require_relative 'capybara/rspec/have_selector'
6-
require_relative 'capybara/rspec/match_style'
7-
require_relative 'capybara/rspec/negation_matcher'
8-
require_relative 'capybara/rspec/negation_matcher_after_visit'
9-
require_relative 'capybara/rspec/predicate_matcher'
10-
require_relative 'capybara/rspec/specific_matcher'
11-
require_relative 'capybara/rspec/visibility_matcher'
12-
13-
require_relative 'capybara/ambiguous_click'
14-
require_relative 'capybara/assert_style'
15-
require_relative 'capybara/find_all_first'
16-
require_relative 'capybara/redundant_within_find'
17-
require_relative 'capybara/specific_actions'
18-
require_relative 'capybara/specific_finders'
3+
# @deprecated This file is deprecated. Cops are registered for lazy loading in
4+
# `rubocop/cop/capybara`; this file is kept for compatibility with code that
5+
# requires it directly.
6+
require_relative 'capybara'

rubocop-capybara.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ Gem::Specification.new do |spec|
3434
}
3535

3636
spec.add_dependency 'lint_roller', '~> 1.1'
37-
spec.add_dependency 'rubocop', '~> 1.81'
37+
spec.add_dependency 'rubocop', '~> 1.89'
3838
end

spec/project/default_config_spec.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,7 @@
1313
end
1414

1515
let(:cop_names) do
16-
glob = SpecHelper::ROOT.join('lib', 'rubocop', 'cop', 'capybara',
17-
'{,rspec}', '*.rb')
18-
Pathname.glob(glob).map do |file|
19-
file_name = file.basename('.rb').to_s
20-
cop_name = file_name.gsub(/(^|_)(.)/) { Regexp.last_match(2).upcase }
21-
namespace = namespaces[file.dirname.basename.to_s]
22-
"#{namespace}/#{cop_name}"
23-
end
16+
RuboCop::Cop::Registry.global.names.grep(%r{\ACapybara/})
2417
end
2518

2619
let(:config_keys) do

0 commit comments

Comments
 (0)