-
-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #932 from andrykonchin/add-top-level-group-callback
RSpec/SubjectStub. Add top level group callback
- Loading branch information
Showing
4 changed files
with
90 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module RSpec | ||
# Helper methods for top level example group cops | ||
module TopLevelGroup | ||
extend RuboCop::NodePattern::Macros | ||
include RuboCop::RSpec::Language | ||
|
||
def_node_matcher :example_or_shared_group?, | ||
(ExampleGroups::ALL + SharedGroups::ALL).block_pattern | ||
|
||
def on_block(node) | ||
return unless respond_to?(:on_top_level_group) | ||
return unless top_level_group?(node) | ||
|
||
on_top_level_group(node) | ||
end | ||
|
||
private | ||
|
||
def top_level_group?(node) | ||
top_level_groups.include?(node) | ||
end | ||
|
||
def top_level_groups | ||
@top_level_groups ||= | ||
top_level_nodes.select { |n| example_or_shared_group?(n) } | ||
end | ||
|
||
def top_level_nodes | ||
if root_node.begin_type? | ||
root_node.children | ||
else | ||
[root_node] | ||
end | ||
end | ||
|
||
def root_node | ||
processed_source.ast | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters