diff --git a/CHANGELOG.md b/CHANGELOG.md index c8094c424..a367b7ade 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Add new `SpecSuffixOnly` option to `RSpec/FilePath` cop. ([@zdennis][]) * Allow `RSpec/RepeatedExampleGroupBody` to differ only by described_class. ([@robotdana][]) * Fix `RSpec/FilePath` detection across sibling directories. ([@rolfschmidt][]) +* Improve the performance of `RSpec/SubjectStub` by an order of magnitude. ([@andrykonchin][]) ## 1.39.0 (2020-05-01) @@ -514,3 +515,4 @@ Compatibility release so users can upgrade RuboCop to 0.51.0. No new features. [@zdennis]: https://github.com/zdennis [@robotdana]: https://github.com/robotdana [@rolfschmidt]: https://github.com/rolfschmidt +[@andrykonchin]: https://github.com/andrykonchin diff --git a/lib/rubocop/cop/rspec/subject_stub.rb b/lib/rubocop/cop/rspec/subject_stub.rb index 3b60cf1b4..9c8b3f392 100644 --- a/lib/rubocop/cop/rspec/subject_stub.rb +++ b/lib/rubocop/cop/rspec/subject_stub.rb @@ -77,7 +77,7 @@ class SubjectStub < Cop def on_block(node) return unless example_group?(node) - return unless (processed_example_groups & node.ancestors).empty? + return if (processed_example_groups & node.ancestors).any? processed_example_groups << node @explicit_subjects = find_all_explicit_subjects(node) @@ -94,7 +94,7 @@ def processed_example_groups end def find_all_explicit_subjects(node) - node.each_descendant(:block).each_with_object({}) do |child, h| + node.each_descendant(:block).with_object({}) do |child, h| name = subject(child) next unless name