Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This moves several files to a new `mixins/` folder. I was going to add some new macros, per [this comment][1], but thought it might clutter things up a bit to put them in the root folder. I thought of adding a `macros/` folder, but decided to emulate the [rubocop source][2] instead. [1]: #934 (comment) [2]: https://github.com/rubocop-hq/rubocop/tree/master/lib/rubocop/cop/mixin
- Loading branch information
Showing
26 changed files
with
226 additions
and
158 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
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
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
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,51 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Helps determine the offending location if there is not an empty line | ||
# following the node. Allows comments to follow directly after. | ||
module EmptyLineSeparation | ||
include FinalEndLocation | ||
include RangeHelp | ||
|
||
def missing_separating_line_offense(node) | ||
return if last_child?(node) | ||
|
||
missing_separating_line(node) do |location| | ||
msg = yield(node.method_name) | ||
add_offense(location, message: msg) do |corrector| | ||
corrector.insert_after(location.end, "\n") | ||
end | ||
end | ||
end | ||
|
||
def missing_separating_line(node) | ||
line = final_end_location(node).line | ||
|
||
line += 1 while comment_line?(processed_source[line]) | ||
|
||
return if processed_source[line].blank? | ||
|
||
yield offending_loc(line) | ||
end | ||
|
||
def offending_loc(last_line) | ||
offending_line = processed_source[last_line - 1] | ||
|
||
content_length = offending_line.lstrip.length | ||
start = offending_line.length - content_length | ||
|
||
source_range(processed_source.buffer, | ||
last_line, start, content_length) | ||
end | ||
|
||
def last_child?(node) | ||
return true unless node.parent&.begin_type? | ||
|
||
node.equal?(node.parent.children.last) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Helps find the true end location of nodes which might contain heredocs. | ||
module FinalEndLocation | ||
def final_end_location(start_node) | ||
heredoc_endings = | ||
start_node.each_node(:str, :dstr, :xstr) | ||
.select(&:heredoc?) | ||
.map { |node| node.loc.heredoc_end } | ||
|
||
[start_node.loc.end, *heredoc_endings].max_by(&:line) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Helper methods for top level describe cops | ||
module TopLevelDescribe | ||
extend NodePattern::Macros | ||
|
||
def on_send(node) | ||
return unless respond_to?(:on_top_level_describe) | ||
return unless top_level_describe?(node) | ||
|
||
on_top_level_describe(node, node.arguments) | ||
end | ||
|
||
private | ||
|
||
def top_level_describe?(node) | ||
return false unless node.method?(:describe) | ||
|
||
top_level_nodes.include?(node) | ||
end | ||
|
||
def top_level_nodes | ||
nodes = describe_statement_children(root_node) | ||
# If we have no top level describe statements, we need to check any | ||
# blocks on the top level (e.g. after a require). | ||
if nodes.empty? | ||
nodes = root_node.each_child_node(:block).flat_map do |child| | ||
describe_statement_children(child) | ||
end | ||
end | ||
|
||
nodes | ||
end | ||
|
||
def root_node | ||
processed_source.ast | ||
end | ||
|
||
def single_top_level_describe? | ||
top_level_nodes.one? | ||
end | ||
|
||
def describe_statement_children(node) | ||
node.each_child_node(:send).select do |element| | ||
element.method?(:describe) | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
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_new_investigation | ||
super | ||
|
||
return unless root_node | ||
|
||
top_level_groups.each do |node| | ||
on_top_level_example_group(node) if example_group?(node) | ||
on_top_level_group(node) | ||
end | ||
end | ||
|
||
def top_level_groups | ||
@top_level_groups ||= | ||
top_level_nodes(root_node).select do |node| | ||
example_or_shared_group?(node) | ||
end | ||
end | ||
|
||
private | ||
|
||
# Dummy methods to be overridden in the consumer | ||
def on_top_level_example_group(_node); end | ||
|
||
def on_top_level_group(_node); end | ||
|
||
def top_level_group?(node) | ||
top_level_groups.include?(node) | ||
end | ||
|
||
def top_level_nodes(node) | ||
if node.nil? | ||
[] | ||
elsif node.begin_type? | ||
node.children | ||
elsif node.module_type? || node.class_type? | ||
top_level_nodes(node.body) | ||
else | ||
[node] | ||
end | ||
end | ||
|
||
def root_node | ||
processed_source.ast | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Helps check offenses with variable definitions | ||
module Variable | ||
include RuboCop::RSpec::Language | ||
extend RuboCop::NodePattern::Macros | ||
|
||
def_node_matcher :variable_definition?, <<~PATTERN | ||
(send nil? #{(Helpers::ALL + Subject::ALL).node_pattern_union} | ||
$({sym str dsym dstr} ...) ...) | ||
PATTERN | ||
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
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
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
Oops, something went wrong.