code
stringlengths
26
124k
docstring
stringlengths
23
125k
func_name
stringlengths
1
98
language
stringclasses
1 value
repo
stringlengths
5
53
path
stringlengths
7
151
url
stringlengths
50
211
license
stringclasses
7 values
def other_offense_in_same_range?(node) expr = node.source_range @offense_ranges ||= [] return true if @offense_ranges.any? { |r| within?(expr, r) } @offense_ranges << expr false end
Returns true if the given node is within another node that has already been marked for auto-correction by this cop.
other_offense_in_same_range?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/indentation_width.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/indentation_width.rb
Apache-2.0
def check(node) alignment_node = alignment_node(node) return if alignment_node.nil? alignment_loc = alignment_location(alignment_node) kw_loc = node.loc.keyword return if alignment_loc.column == kw_loc.column || alignment_loc.line == kw_loc.line add_offense( kw_loc, message: format_message(alignment_node, alignment_loc, kw_loc) ) do |corrector| autocorrect(corrector, node, alignment_loc) end end
Check alignment of node with rescue or ensure modifiers.
check
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/rescue_ensure_alignment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/rescue_ensure_alignment.rb
Apache-2.0
def alignment_node(node) ancestor_node = ancestor_node(node) return ancestor_node if ancestor_node.nil? || ancestor_node.kwbegin_type? assignment_node = assignment_node(ancestor_node) return assignment_node if same_line?(ancestor_node, assignment_node) access_modifier_node = access_modifier_node(ancestor_node) return access_modifier_node unless access_modifier_node.nil? ancestor_node end
We will use ancestor or wrapper with access modifier.
alignment_node
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/rescue_ensure_alignment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/rescue_ensure_alignment.rb
Apache-2.0
def check_body_lines(lines) lines.each do |line| case line.type when :or_asgn check_disjunctive_assignment(line) else # Once we encounter something other than a disjunctive # assignment, we cease our investigation, because we can't be # certain that any future disjunctive assignments are offensive. # You're off the case, detective! break end end end
@param [Array] lines the logical lines of the constructor
check_body_lines
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb
Apache-2.0
def check_disjunctive_assignment(node) lhs = node.child_nodes.first return unless lhs.ivasgn_type? add_offense(node.loc.operator) do |corrector| corrector.replace(node.loc.operator, '=') end end
Add an offense if the LHS of the given disjunctive assignment is an instance variable. For now, we only care about assignments to instance variables. @param [Node] node a disjunctive assignment
check_disjunctive_assignment
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb
Apache-2.0
def call_range_to_safely_reposition(node, heredoc) return nil if calls_on_multiple_lines?(node, heredoc) heredoc_end_pos = heredoc_end_pos(heredoc) call_end_pos = call_end_pos(node) call_range = range_between(heredoc_end_pos, call_end_pos) call_line_range = call_line_range(node) call_source = call_range.source.strip call_line_source = call_line_range.source.strip return call_range if call_source == call_line_source if trailing_comma?(call_source, call_line_source) # If there's some on the last line other than the call, e.g. # a trailing comma, then we leave the "\n" following the # heredoc_end in place. return range_between(heredoc_end_pos, call_end_pos + 1) end nil end
Returns nil if no range can be safely repositioned.
call_range_to_safely_reposition
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/heredoc_method_call_position.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/heredoc_method_call_position.rb
Apache-2.0
def prints_as_self?(node) node.basic_literal? || (COMPOSITE.include?(node.type) && node.children.all? { |child| prints_as_self?(child) }) end
Does node print its own source when converted to a string?
prints_as_self?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/literal_in_interpolation.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/literal_in_interpolation.rb
Apache-2.0
def last_arg_range(node) node.arguments.last.source_range.with( begin_pos: node.arguments[-2].source_range.end_pos ) end
Returns range of last argument including comma and whitespace. @return [Parser::Source::Range]
last_arg_range
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/non_deterministic_require_order.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/non_deterministic_require_order.rb
Apache-2.0
def find_redundant(comment, offenses, cop, line_range, next_line_range) if all_disabled?(comment) # If there's a disable all comment followed by a comment # specifically disabling `cop`, we don't report the `all` # comment. If the disable all comment is truly redundant, we will # detect that when examining the comments of another cop, and we # get the full line range for the disable all. if (next_line_range.nil? || line_range.last != next_line_range.first) && offenses.none? { |o| line_range.cover?(o.line) } 'all' end else cop_offenses = offenses.select { |o| o.cop_name == cop } cop if cop_offenses.none? { |o| line_range.cover?(o.line) } end end
rubocop:todo Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
find_redundant
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb
Apache-2.0
def assignment_without_argument_usage(argument) argument.assignments.reduce(true) do |location_known, assignment| assignment_node = assignment.meta_assignment_node || assignment.node # Shorthand assignments always use their arguments next false if assignment_node.shorthand_asgn? node_within_block_or_conditional = node_within_block_or_conditional?(assignment_node.parent, argument.scope.node) unless uses_var?(assignment_node, argument.name) # It's impossible to decide whether a branch or block is executed, # so the precise reassignment location is undecidable. next false if node_within_block_or_conditional yield(assignment.node, location_known) break end location_known end end
Find the first argument assignment, which doesn't reference the argument at the rhs. If the assignment occurs inside a branch or block, it is impossible to tell whether it's executed, so precise shadowing location is not known.
assignment_without_argument_usage
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/shadowed_argument.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/shadowed_argument.rb
Apache-2.0
def node_within_block_or_conditional?(node, stop_search_node) return false if node == stop_search_node node.conditional? || node.block_type? || node_within_block_or_conditional?(node.parent, stop_search_node) end
Check whether the given node is nested into block or conditional.
node_within_block_or_conditional?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/shadowed_argument.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/shadowed_argument.rb
Apache-2.0
def argument_references(argument) assignment_references = argument .assignments .flat_map(&:references) .map(&:source_range) argument.references.reject do |ref| next false unless ref.explicit? assignment_references.include?(ref.node.source_range) end end
Get argument references without assignments' references
argument_references
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/shadowed_argument.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/shadowed_argument.rb
Apache-2.0
def rescued_exceptions(rescue_group) klasses = *rescue_group klasses.map do |klass| next unless klass.const_type? klass.source end.compact end
@param [RuboCop::AST::Node] rescue_group is a node of array_type
rescued_exceptions
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/shadowed_exception.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/shadowed_exception.rb
Apache-2.0
def return_value_node_of_scope(scope) body_node = scope.body_node if body_node.begin_type? body_node.children.last else body_node end end
TODO: More precise handling (rescue, ensure, nested begin, etc.)
return_value_node_of_scope
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/useless_assignment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/lint/useless_assignment.rb
Apache-2.0
def irrelevant_line?(source_line) source_line.blank? || !count_comments? && comment_line?(source_line) end
Returns true for lines that shall not be included in the count.
irrelevant_line?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/metrics/utils/code_length_calculator.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/metrics/utils/code_length_calculator.rb
Apache-2.0
def block_method_name(node) case node.type when :block node.method_name when :block_pass node.parent.method_name end end
Returns the name of the method called with a block if node is a block node, or a block-pass node.
block_method_name
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/metrics/utils/iterating_block.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/metrics/utils/iterating_block.rb
Apache-2.0
def iterating_method?(name) KNOWN_ITERATING_METHODS.include? name end
Returns true iff name is a known iterating type (e.g. :each, :transform_values)
iterating_method?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/metrics/utils/iterating_block.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/metrics/utils/iterating_block.rb
Apache-2.0
def iterating_block?(node) name = block_method_name(node) name && iterating_method?(name) end
Returns nil if node is neither a block node or a block-pass node. Otherwise returns true/false if method call is a known iterating call
iterating_block?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/metrics/utils/iterating_block.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/metrics/utils/iterating_block.rb
Apache-2.0
def irrelevant_line(source_line) source_line.blank? || !count_comments? && comment_line?(source_line) end
Returns true for lines that shall not be included in the count.
irrelevant_line
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/code_length.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/code_length.rb
Apache-2.0
def preceding_comment?(node1, node2) node1 && node2 && precede?(node2, node1) && comment_line?(node2.loc.expression.source) end
The args node1 & node2 may represent a RuboCop::AST::Node or a Parser::Source::Comment. Both respond to #loc.
preceding_comment?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/documentation_comment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/documentation_comment.rb
Apache-2.0
def precede?(node1, node2) node2.loc.line - node1.loc.line == 1 end
The args node1 & node2 may represent a RuboCop::AST::Node or a Parser::Source::Comment. Both respond to #loc.
precede?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/documentation_comment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/documentation_comment.rb
Apache-2.0
def duplicates?(collection) collection.size > 1 && collection.size > collection.uniq.size end
Whether the `collection` contains any duplicates. @param [Array] collection an array to check for duplicates @return [Boolean] whether the array contains any duplicates
duplicates?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/duplication.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/duplication.rb
Apache-2.0
def consecutive_duplicates(collection) grouped_duplicates(collection).flat_map { |items| items[1..-1] } end
Returns the consecutive duplicates, leaving out the first instance of the duplicated elements. @param [Array] collection an array to return consecutive duplicates for @return [Array] the consecutive duplicates
consecutive_duplicates
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/duplication.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/duplication.rb
Apache-2.0
def grouped_duplicates(collection) collection.group_by { |item| item }.values.reject(&:one?) end
Returns a hash of grouped duplicates. The key will be the first instance of the element, and the value an `array` of the initial element and all duplicate instances. @param [Array] collection an array to group duplicates for @return [Array] the grouped duplicates
grouped_duplicates
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/duplication.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/duplication.rb
Apache-2.0
def each_match_range(range, regex) range.source.scan(regex) do yield match_range(range, Regexp.last_match) end end
Return a new `Range` covering the first matching group number for each match of `regex` inside `range`
each_match_range
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/match_range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/match_range.rb
Apache-2.0
def match_range(range, match) range_between(range.begin_pos + match.begin(1), range.begin_pos + match.end(1)) end
For a `match` inside `range`, return a new `Range` covering the match
match_range
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/match_range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/match_range.rb
Apache-2.0
def left_hand_side(lhs) lhs = lhs.parent while lhs.parent&.send_type? lhs end
In a chain of method calls, we regard the top send node as the base for indentation of all lines following the first. For example: a. b c { block }. <-- b is indented relative to a d <-- d is indented relative to a
left_hand_side
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/multiline_expression_indentation.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/multiline_expression_indentation.rb
Apache-2.0
def correct_indentation(node) kw_node = kw_node_with_special_indentation(node) if kw_node && !postfix_conditional?(kw_node) # This cop could have its own IndentationWidth configuration configured_indentation_width + @config.for_cop('Layout/IndentationWidth')['Width'] else configured_indentation_width end end
The correct indentation of `node` is usually `IndentationWidth`, with one exception: prefix keywords. ``` while foo && # Here, `while` is called a "prefix keyword" bar # This is called "special indentation" baz end ``` Note that *postfix conditionals* do *not* get "special indentation". ``` next if foo && bar # normal indentation, not special ```
correct_indentation
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/multiline_expression_indentation.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/multiline_expression_indentation.rb
Apache-2.0
def valid_method_rhs_candidate?(candidate, node) node.setter_method? && valid_rhs_candidate?(candidate, node.last_argument) end
The []= operator and setters (a.b = c) are parsed as :send nodes.
valid_method_rhs_candidate?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/multiline_expression_indentation.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/multiline_expression_indentation.rb
Apache-2.0
def postfix_conditional?(node) node.if_type? && node.modifier_form? end
Returns true if `node` is a conditional whose `body` and `condition` begin on the same line.
postfix_conditional?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/multiline_expression_indentation.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/multiline_expression_indentation.rb
Apache-2.0
def new_line_needed_before_closing_brace?(node) last_element_line = last_element_range_with_trailing_comma(node).last_line last_element_commented = processed_source.comment_at_line(last_element_line) last_element_commented && (node.chained? || node.argument?) end
Returns true for the case [a, b # comment ].some_method
new_line_needed_before_closing_brace?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb
Apache-2.0
def opening_brace_on_same_line?(node) node.loc.begin.line == children(node).first.first_line end
This method depends on the fact that we have guarded against implicit and empty literals.
opening_brace_on_same_line?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb
Apache-2.0
def closing_brace_on_same_line?(node) node.loc.end.line == children(node).last.last_line end
This method depends on the fact that we have guarded against implicit and empty literals.
closing_brace_on_same_line?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb
Apache-2.0
def last_line_heredoc?(node, parent = nil) parent ||= node if node.respond_to?(:loc) && node.loc.respond_to?(:heredoc_end) && node.loc.heredoc_end.last_line >= parent.last_line return true end return false unless node.respond_to?(:children) node.children.any? { |child| last_line_heredoc?(child, parent) } end
Starting with the parent node and recursively for the parent node's children, check if the node is a HEREDOC and if the HEREDOC ends below or on the last line of the parent node. Example: # node is `b: ...` parameter # last_line_heredoc?(node) => false foo(a, b: { a: 1, c: <<-EOM baz EOM } ) # node is `b: ...` parameter # last_line_heredoc?(node) => true foo(a, b: <<-EOM baz EOM )
last_line_heredoc?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb
Apache-2.0
def invalid_percent_array_context?(node) parent = node.parent parent&.send_type? && parent.arguments.include?(node) && !parent.parenthesized? && parent&.block_literal? end
Ruby does not allow percent arrays in an ambiguous block context. @example foo %i[bar baz] { qux }
invalid_percent_array_context?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/percent_array.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/percent_array.rb
Apache-2.0
def contents_range(node) range_between(node.loc.begin.end_pos, node.loc.end.begin_pos) end
A range containing only the contents of the percent literal (e.g. in %i{1 2 3} this will be the range covering '1 2 3' only)
contents_range
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/percent_literal.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/percent_literal.rb
Apache-2.0
def relevant_assignment_lines(line_range) result = [] original_line_indent = processed_source .line_indentation(line_range.first) relevant_line_indent_at_level = true line_range.each do |line_number| current_line_indent = processed_source.line_indentation(line_number) blank_line = processed_source.lines[line_number - 1].blank? if (current_line_indent < original_line_indent && !blank_line) || (relevant_line_indent_at_level && blank_line) break end result << line_number if assignment_lines.include?(line_number) && current_line_indent == original_line_indent unless blank_line relevant_line_indent_at_level = \ current_line_indent == original_line_indent end end result end
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity, Metrics/MethodLength
relevant_assignment_lines
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/preceding_following_alignment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/preceding_following_alignment.rb
Apache-2.0
def remove_optarg_equals(asgn_tokens, processed_source) optargs = processed_source.ast.each_node(:optarg) optarg_eql = optargs.map { |o| o.loc.operator.begin_pos }.to_set asgn_tokens.reject { |t| optarg_eql.include?(t.begin_pos) } end
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity, Metrics/MethodLength
remove_optarg_equals
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/preceding_following_alignment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/preceding_following_alignment.rb
Apache-2.0
def effective_column(range) if range.line == 1 && @processed_source.raw_source.codepoints.first == BYTE_ORDER_MARK range.column - 1 else range.column end end
# Helpers for above range methods. Do not use inside Cops. Returns the column attribute of the range, except if the range is on the first line and there's a byte order mark at the beginning of that line, in which case 1 is subtracted from the column value. This gives the column as it appears when viewing the file in an editor.
effective_column
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/range_help.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/range_help.rb
Apache-2.0
def multiline?(node) node.multiline? && !allowed_multiline_argument?(node) end
Returns true if the round/square/curly brackets of the given node are on different lines, each item within is on its own line, and the closing bracket is on its own line.
multiline?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/trailing_comma.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/trailing_comma.rb
Apache-2.0
def allowed_multiline_argument?(node) elements(node).one? && !Util.begins_its_line?(node.loc.end) end
A single argument with the closing bracket on the same line as the end of the argument is not considered multiline, even if the argument itself might span multiple lines.
allowed_multiline_argument?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/trailing_comma.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/trailing_comma.rb
Apache-2.0
def find_visibility_end(node) possible_visibilities = VISIBILITY_SCOPES - [node_visibility(node)] right = node.right_siblings right.find do |child_node| possible_visibilities.include?(node_visibility(child_node)) end || right.last end
Navigate to find the last protected method
find_visibility_end
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/visibility_help.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/mixin/visibility_help.rb
Apache-2.0
def scope_type(node) while (parent = node.parent) case parent.type when :class, :module return :lexical when :def, :defs return :dynamic when :block return :instance_eval if parent.method?(:instance_eval) return :dynamic end node = parent end :lexical end
In this expression, will `self` be the same as the innermost enclosing class or module block (:lexical)? Or will it be something else (:dynamic)? If we're in an instance_eval block, return that.
scope_type
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/alias.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/alias.rb
Apache-2.0
def correct_not(node, receiver, corrector) if node.prefix_bang? return unless receiver.send_type? correct_send(receiver, corrector) elsif node.prefix_not? correct_other(node, corrector) else raise 'unrecognized unary negation operator' end end
! is a special case: 'x and !obj.method arg' can be auto-corrected if we recurse down a level and add parens to 'obj.method arg' however, 'not x' also parses as (send x :!)
correct_not
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/and_or.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/and_or.rb
Apache-2.0
def regexp_with_working_captures?(node) case node.type when :match_with_lvasgn lhs, _rhs = *node node.loc.selector.source == '=~' && regexp_with_named_captures?(lhs) when :send lhs, method, rhs = *node method == :match && [lhs, rhs].any? { |n| regexp_with_named_captures?(n) } end end
Named captures work with `=~` (if regexp is on lhs) and with `match` (both sides)
regexp_with_working_captures?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/case_like_if.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/case_like_if.rb
Apache-2.0
def expand_elses(branch) elsif_branches = expand_elsif(branch) else_branch = elsif_branches.any? ? elsif_branches.pop : branch [elsif_branches, else_branch] end
`elsif` branches show up in the `node` as an `else`. We need to recursively iterate over all `else` branches and consider all but the last `node` an `elsif` branch and consider the last `node` the actual `else` branch.
expand_elses
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/conditional_assignment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/conditional_assignment.rb
Apache-2.0
def expand_when_branches(when_branches) when_branches.map { |branch| branch.children[1] } end
`when` nodes contain the entire branch including the condition. We only need the contents of the branch, not the condition.
expand_when_branches
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/conditional_assignment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/conditional_assignment.rb
Apache-2.0
def correction_exceeds_line_limit?(node, branches) return false unless line_length_cop_enabled? assignment = lhs(tail(branches[0])) longest_line_exceeds_line_limit?(node, assignment) end
If `Layout/LineLength` is enabled, we do not want to introduce an offense by auto-correcting this cop. Find the max configured line length. Find the longest line of condition. Remove the assignment from lines that contain the offending assignment because after correcting, this will not be on the line anymore. Check if the length of the longest line + the length of the corrected assignment is greater than the max configured line length
correction_exceeds_line_limit?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/conditional_assignment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/conditional_assignment.rb
Apache-2.0
def nodoc_comment?(node, require_all: false) return false unless node&.children&.first nodoc = nodoc(node) return true if same_line?(nodoc, node) && nodoc?(nodoc, require_all: require_all) nodoc_comment?(node.parent, require_all: true) end
First checks if the :nodoc: comment is associated with the class/module. Unless the element is tagged with :nodoc:, the search proceeds to check its ancestors for :nodoc: all. Note: How end-of-line comments are associated with code changed in parser-2.2.0.4.
nodoc_comment?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/documentation.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/documentation.rb
Apache-2.0
def accumulator_param_assigned_to?(body, args) first_arg, = *args accumulator_var, = *first_arg body.each_descendant.any? do |n| next unless n.assignment? lhs, _rhs = *n lhs.equal?(accumulator_var) end end
if the accumulator parameter is assigned to in the block, then we can't convert to each_with_object
accumulator_param_assigned_to?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/each_with_object.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/each_with_object.rb
Apache-2.0
def with_lineno?(node) if node.method?(:eval) node.arguments.size == 4 else node.arguments.size == 3 end end
FIXME: It's a Style/ConditionalAssignment's false positive. rubocop:disable Style/ConditionalAssignment
with_lineno?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/eval_with_location.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/eval_with_location.rb
Apache-2.0
def expand_elses(branch) if branch.nil? [nil] elsif branch.if_type? _condition, elsif_branch, else_branch = *branch expand_elses(else_branch).unshift(elsif_branch) else [branch] end end
`elsif` branches show up in the if node as nested `else` branches. We need to recursively iterate over all `else` branches.
expand_elses
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/identical_conditional_branches.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/identical_conditional_branches.rb
Apache-2.0
def possible_class_hierarchy_check?(lhs, rhs, method) CLASS_COMPARISON_METHODS.include?(method) && (camel_case_constant?(lhs) || (rhs.size == 1 && camel_case_constant?(rhs.first))) end
When comparing classes, `!(Integer < Numeric)` is not the same as `Integer > Numeric`.
possible_class_hierarchy_check?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/inverse_methods.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/inverse_methods.rb
Apache-2.0
def reindent(lines, node, corrector) range = node.source_range buffer = range.source_buffer target_indent = range.source_line =~ /\S/ delta = actual_indent(lines, buffer) - target_indent lines.each do |lineno| reindent_line(corrector, lineno, delta, buffer) end end
Adjust indentation of `lines` to match `node`
reindent
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/next.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/next.rb
Apache-2.0
def add_self_to_getters(right_elements) right_elements.map do |e| implicit_self_getter?(e) { |var| s(:send, s(:self), var) } || e end end
Converts (send nil :something) nodes to (send (:self) :something). This makes the sorting algorithm work for expressions such as `self.a, self.b = b, a`.
add_self_to_getters
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/parallel_assignment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/parallel_assignment.rb
Apache-2.0
def accesses?(rhs, lhs) if lhs.method?(:[]=) matching_calls(rhs, lhs.receiver, :[]).any? do |args| args == lhs.arguments end else access_method = lhs.method_name.to_s.chop.to_sym matching_calls(rhs, lhs.receiver, access_method).any? end end
`lhs` is an assignment method call like `obj.attr=` or `ary[idx]=`. Does `rhs` access the same value which is assigned by `lhs`?
accesses?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/parallel_assignment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/parallel_assignment.rb
Apache-2.0
def on_send(node) fix_exploded(node) || fix_compact(node) end
Switch `raise RuntimeError, 'message'` to `raise 'message'`, and `raise RuntimeError.new('message')` to `raise 'message'`.
on_send
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/redundant_exception.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/redundant_exception.rb
Apache-2.0
def accessor_start(node) if node.loc.dot node.loc.dot.begin_pos else node.loc.selector.begin_pos end end
This gets the start of the accessor whether it has a dot (e.g. `.first`) or doesn't (e.g. `[0]`)
accessor_start
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/redundant_sort.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/redundant_sort.rb
Apache-2.0
def format_list(items) items.join('` or `') end
For now, we assume that lists are 2 items or less. Easy grammar!
format_list
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/special_global_vars.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/special_global_vars.rb
Apache-2.0
def complex_condition?(condition) if condition.begin_type? condition.to_a.any? { |x| complex_condition?(x) } else non_complex_expression?(condition) ? false : true end end
If the condition is parenthesized we recurse and check for any complex expressions within it.
complex_condition?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/ternary_parentheses.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/ternary_parentheses.rb
Apache-2.0
def non_complex_expression?(condition) NON_COMPLEX_TYPES.include?(condition.type) || non_complex_send?(condition) end
Anything that is not a variable, constant, or method/.method call will be counted as a complex expression.
non_complex_expression?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/ternary_parentheses.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/ternary_parentheses.rb
Apache-2.0
def infinite_loop? (require_parentheses? || require_parentheses_when_complex?) && redundant_parentheses_enabled? end
When this cop is configured to enforce parentheses and the `RedundantParentheses` cop is enabled, it will cause an infinite loop as they compete to add and remove the parentheses respectively.
infinite_loop?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/ternary_parentheses.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/style/ternary_parentheses.rb
Apache-2.0
def arity @source.scan('*').count + 1 end
Number of arguments required for the format sequence
arity
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/utils/format_string.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/utils/format_string.rb
Apache-2.0
def explicit? ![ZERO_ARITY_SUPER_TYPE, SEND_TYPE].include?(@node.type) end
There's an implicit variable reference by the zero-arity `super`: def some_method(foo) super end Another case is `binding`: def some_method(foo) do_something(binding) end In these cases, the variable `foo` is not explicitly referenced, but it can be considered used implicitly by the `super` or `binding`.
explicit?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force/reference.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force/reference.rb
Apache-2.0
def reference!(node) reference = Reference.new(node, @scope) @references << reference consumed_branches = nil @assignments.reverse_each do |assignment| next if consumed_branches&.include?(assignment.branch) assignment.reference!(node) unless assignment.run_exclusively_with?(reference) # Modifier if/unless conditions are special. Assignments made in # them do not put the assigned variable in scope to the left of the # if/unless keyword. A preceding assignment is needed to put the # variable in scope. For this reason we skip to the next assignment # here. next if in_modifier_if?(assignment) break if !assignment.branch || assignment.branch == reference.branch unless assignment.branch.may_run_incompletely? (consumed_branches ||= Set.new) << assignment.branch end end end
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
reference!
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force/variable.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force/variable.rb
Apache-2.0
def in_modifier_if?(assignment) parent = assignment.node.parent parent = parent.parent if parent&.begin_type? parent&.if_type? && parent&.modifier_form? end
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
in_modifier_if?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force/variable.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force/variable.rb
Apache-2.0
def used? @captured_by_block || referenced? end
This is a convenient way to check whether the variable is used in its entire variable lifetime. For more precise usage check, refer Assignment#used?. Once the variable is captured by a block, we have no idea when, where, and how many times the block would be invoked. This means we cannot track the usage of the variable. So we consider it's used to suppress false positive offenses.
used?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force/variable.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force/variable.rb
Apache-2.0
def blank? empty? || lstrip.empty? end
Checks whether a string is blank. A string is considered blank if it is either empty or contains only whitespace characters. @return [Boolean] true is the string is blank, false otherwise @example ''.blank? #=> true @example ' '.blank? #=> true @example ' test'.blank? #=> false
blank?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/core_ext/string.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/core_ext/string.rb
Apache-2.0
def initialize(output, options = {}) @output = output @options = options end
@api public @param output [IO] `$stdout` or opened file
initialize
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/formatter/base_formatter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/formatter/base_formatter.rb
Apache-2.0
def hash_for_location(offense) { start_line: offense.line, start_column: offense.real_column, last_line: offense.last_line, last_column: offense.last_column, length: offense.location.length, # `line` and `column` exist for compatibility. # Use `start_line` and `start_column` instead. line: offense.line, column: offense.real_column } end
TODO: Consider better solution for Offense#real_column.
hash_for_location
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/formatter/json_formatter.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/formatter/json_formatter.rb
Apache-2.0
def expect_offense(source, file = nil, severity: nil, **replacements) source = format_offense(source, **replacements) RuboCop::Formatter::DisabledConfigFormatter .config_to_allow_offenses = {} RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {} cop.instance_variable_get(:@options)[:auto_correct] = true expected_annotations = AnnotatedSource.parse(source) if expected_annotations.plain_source == source raise 'Use `expect_no_offenses` to assert that no offenses are found' end @processed_source = parse_source(expected_annotations.plain_source, file) unless @processed_source.valid_syntax? raise 'Error parsing example code: ' \ "#{@processed_source.diagnostics.map(&:render).join("\n")}" end @offenses = _investigate(cop, @processed_source) actual_annotations = expected_annotations.with_offense_annotations(@offenses) expect(actual_annotations).to eq(expected_annotations), '' expect(@offenses.map(&:severity).uniq).to eq([severity]) if severity @offenses end
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
expect_offense
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/rspec/expect_offense.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/rspec/expect_offense.rb
Apache-2.0
def expect_no_corrections raise '`expect_no_corrections` must follow `expect_offense`' unless @processed_source return if @last_corrector.empty? # In order to print a nice diff, e.g. what source got corrected to, # we need to run the actual corrections new_source = @last_corrector.rewrite expect(new_source).to eq(@processed_source.buffer.source) end
rubocop:enable Metrics/AbcSize, Metrics/MethodLength
expect_no_corrections
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/rspec/expect_offense.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/rspec/expect_offense.rb
Apache-2.0
def initialize(lines, annotations) @lines = lines.freeze @annotations = annotations.sort.freeze end
@param lines [Array<String>] @param annotations [Array<(Integer, String)>] each entry is the annotated line number and the annotation text @note annotations are sorted so that reconstructing the annotation text via {#to_s} is deterministic
initialize
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/rspec/expect_offense.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/rspec/expect_offense.rb
Apache-2.0
def match_annotations?(other) annotations.zip(other.annotations) do |(_actual_line, actual_annotation), (_expected_line, expected_annotation)| if expected_annotation&.end_with?(ABBREV) && actual_annotation.start_with?(expected_annotation[0...-ABBREV.length]) expected_annotation.replace(actual_annotation) end end annotations == other.annotations end
Dirty hack: expectations with [...] are rewritten when they match This way the diff is clean.
match_annotations?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/rspec/expect_offense.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/rspec/expect_offense.rb
Apache-2.0
def to_s reconstructed = lines.dup annotations.reverse_each do |line_number, annotation| reconstructed.insert(line_number, annotation) end reconstructed.join end
Construct annotated source string (like what we parse) Reconstruct a deterministic annotated source string. This is useful for eliminating semantically irrelevant annotation ordering differences. @example standardization source1 = AnnotatedSource.parse(<<-RUBY) line1 ^ Annotation 1 ^^ Annotation 2 RUBY source2 = AnnotatedSource.parse(<<-RUBY) line1 ^^ Annotation 2 ^ Annotation 1 RUBY source1.to_s == source2.to_s # => true @return [String]
to_s
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/rspec/expect_offense.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/rspec/expect_offense.rb
Apache-2.0
def with_offense_annotations(offenses) offense_annotations = offenses.map do |offense| indent = ' ' * offense.column carets = '^' * offense.column_length carets = '^{}' if offense.column_length.zero? [offense.line, "#{indent}#{carets} #{offense.message}\n"] end self.class.new(lines, offense_annotations) end
Annotate the source code with the RuboCop offenses provided @param offenses [Array<RuboCop::Cop::Offense>] @return [self]
with_offense_annotations
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/rspec/expect_offense.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/rspec/expect_offense.rb
Apache-2.0
def initialize(type, children = EMPTY_CHILDREN, properties = EMPTY_PROPERTIES) @mutable_attributes = {} # ::AST::Node#initialize freezes itself. super # #parent= may be invoked multiple times for a node because there are # pending nodes while constructing AST and they are replaced later. # For example, `lvar` and `send` type nodes are initially created as an # `ident` type node and fixed to the appropriate type later. # So, the #parent attribute needs to be mutable. each_child_node do |child_node| child_node.parent = self unless child_node.complete? end end
@see https://www.rubydoc.info/gems/ast/AST/Node:initialize
initialize
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
Apache-2.0
def updated(type = nil, children = nil, properties = {}) properties[:location] ||= @location klass = RuboCop::AST::Builder::NODE_MAP[type || @type] || Node klass.new(type || @type, children || @children, properties) end
Override `AST::Node#updated` so that `AST::Processor` does not try to mutate our ASTs. Since we keep references from children to parents and not just the other way around, we cannot update an AST and share identical subtrees. Rather, the entire AST must be copied any time any part of it is changed.
updated
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
Apache-2.0
def sibling_index parent&.children&.index { |sibling| sibling.equal?(self) } end
Returns the index of the receiver node in its siblings. (Sibling index uses zero based numbering.) Use is discouraged, this is a potentially slow method. @return [Integer, nil] the index of the receiver node in its siblings
sibling_index
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
Apache-2.0
def right_sibling return unless parent parent.children[sibling_index + 1].freeze end
Use is discouraged, this is a potentially slow method and can lead to even slower algorithms @return [Node, nil] the right (aka next) sibling
right_sibling
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
Apache-2.0
def left_sibling i = sibling_index return if i.nil? || i.zero? parent.children[i - 1].freeze end
Use is discouraged, this is a potentially slow method and can lead to even slower algorithms @return [Node, nil] the left (aka previous) sibling
left_sibling
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
Apache-2.0
def left_siblings return [].freeze unless parent parent.children[0...sibling_index].freeze end
Use is discouraged, this is a potentially slow method and can lead to even slower algorithms @return [Array<Node>] the left (aka previous) siblings
left_siblings
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
Apache-2.0
def right_siblings return [].freeze unless parent parent.children[sibling_index + 1..].freeze end
Use is discouraged, this is a potentially slow method and can lead to even slower algorithms @return [Array<Node>] the right (aka next) siblings
right_siblings
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
Apache-2.0
def each_ancestor(*types, &block) return to_enum(__method__, *types) unless block visit_ancestors(types, &block) self end
Calls the given block for each ancestor node from parent to root. If no block is given, an `Enumerator` is returned. @overload each_ancestor Yield all nodes. @overload each_ancestor(type) Yield only nodes matching the type. @param [Symbol] type a node type @overload each_ancestor(type_a, type_b, ...) Yield only nodes matching any of the types. @param [Symbol] type_a a node type @param [Symbol] type_b a node type @yieldparam [Node] node each ancestor node @return [self] if a block is given @return [Enumerator] if no block is given
each_ancestor
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
Apache-2.0
def value_used? # Be conservative and return true if we're not sure. return false if parent.nil? case parent.type when :array, :defined?, :dstr, :dsym, :eflipflop, :erange, :float, :hash, :iflipflop, :irange, :not, :pair, :regexp, :str, :sym, :when, :xstr parent.value_used? when :begin, :kwbegin begin_value_used? when :for for_value_used? when :case, :if case_if_value_used? when :while, :until, :while_post, :until_post while_until_value_used? else true end end
Some expressions are evaluated for their value, some for their side effects, and some for both If we know that an expression is useful only for its side effects, that means we can transform it in ways which preserve the side effects, but change the return value So, does the return value of this node matter? If we changed it to `(...; nil)`, might that affect anything? rubocop:disable Metrics/MethodLength
value_used?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
Apache-2.0
def pure? # Be conservative and return false if we're not sure case type when :__FILE__, :__LINE__, :const, :cvar, :defined?, :false, :float, :gvar, :int, :ivar, :lvar, :nil, :str, :sym, :true, :regopt true when :and, :array, :begin, :case, :dstr, :dsym, :eflipflop, :ensure, :erange, :for, :hash, :if, :iflipflop, :irange, :kwbegin, :not, :or, :pair, :regexp, :until, :until_post, :when, :while, :while_post child_nodes.all?(&:pure?) else false end end
rubocop:enable Metrics/MethodLength Some expressions are evaluated for their value, some for their side effects, and some for both. If we know that expressions are useful only for their return values, and have no side effects, that means we can reorder them, change the number of times they are evaluated, or replace them with other expressions which are equivalent in value. So, is evaluation of this node free of side effects?
pure?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node.rb
Apache-2.0
def def_node_matcher(method_name, pattern_str, **keyword_defaults) NodePattern.new(pattern_str).def_node_matcher(self, method_name, **keyword_defaults) end
Define a method which applies a pattern to an AST node The new method will return nil if the node does not match. If the node matches, and a block is provided, the new method will yield to the block (passing any captures as block arguments). If the node matches, and no block is provided, the new method will return the captures, or `true` if there were none.
def_node_matcher
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node_pattern.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node_pattern.rb
Apache-2.0
def def_node_search(method_name, pattern_str, **keyword_defaults) NodePattern.new(pattern_str).def_node_search(self, method_name, **keyword_defaults) end
Define a method which recurses over the descendants of an AST node, checking whether any of them match the provided pattern If the method name ends with '?', the new method will return `true` as soon as it finds a descendant which matches. Otherwise, it will yield all descendants which match.
def_node_search
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node_pattern.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node_pattern.rb
Apache-2.0
def lines @lines ||= begin all_lines = @buffer.source_lines last_token_line = tokens.any? ? tokens.last.line : all_lines.size result = [] all_lines.each_with_index do |line, ix| break if ix >= last_token_line && line == '__END__' result << line end result end end
Returns the source lines, line break characters removed, excluding a possible __END__ and everything that comes after.
lines
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/processed_source.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/processed_source.rb
Apache-2.0
def each_comment_in_lines(line_range) return to_enum(:each_comment_in_lines, line_range) unless block_given? line_range.each do |line| if (comment = comment_index[line]) yield comment end end end
Enumerates on the comments contained with the given `line_range`
each_comment_in_lines
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/processed_source.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/processed_source.rb
Apache-2.0
def sorted_tokens # Use stable sort. @sorted_tokens ||= tokens.sort_by.with_index { |token, i| [token.begin_pos, i] } end
The tokens list is always sorted by token position, except for cases when heredoc is passed as a method argument. In this case tokens are interleaved by heredoc contents' tokens.
sorted_tokens
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/processed_source.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/processed_source.rb
Apache-2.0
def parser_class(ruby_version) case ruby_version when 1.9 require 'parser/ruby19' Parser::Ruby19 when 2.0 require 'parser/ruby20' Parser::Ruby20 when 2.1 require 'parser/ruby21' Parser::Ruby21 when 2.2 require 'parser/ruby22' Parser::Ruby22 when 2.3 require 'parser/ruby23' Parser::Ruby23 when 2.4 require 'parser/ruby24' Parser::Ruby24 when 2.5 require 'parser/ruby25' Parser::Ruby25 when 2.6 require 'parser/ruby26' Parser::Ruby26 when 2.7 require 'parser/ruby27' Parser::Ruby27 when 2.8, 3.0 require 'parser/ruby30' Parser::Ruby30 when 3.1 require 'parser/ruby31' Parser::Ruby31 when 3.2 require 'parser/ruby32' Parser::Ruby32 when 3.3 require 'parser/ruby33' Parser::Ruby33 else raise ArgumentError, "RuboCop found unknown Ruby version: #{ruby_version.inspect}" end end
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
parser_class
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/processed_source.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/processed_source.rb
Apache-2.0
def create_parser(ruby_version) builder = RuboCop::AST::Builder.new parser_class(ruby_version).new(builder).tap do |parser| # On JRuby there's a risk that we hang in tokenize() if we # don't set the all errors as fatal flag. The problem is caused by a bug # in Racc that is discussed in issue #93 of the whitequark/parser # project on GitHub. parser.diagnostics.all_errors_are_fatal = (RUBY_ENGINE != 'ruby') parser.diagnostics.ignore_warnings = false parser.diagnostics.consumer = lambda do |diagnostic| @diagnostics << diagnostic end end end
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
create_parser
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/processed_source.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/processed_source.rb
Apache-2.0
def s(type, *children) klass = Builder::NODE_MAP[type] || Node klass.new(type, children) end
Creates a {Node} with type `type` and children `children`.
s
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/sexp.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/sexp.rb
Apache-2.0
def space_after? pos.source_buffer.source.match(/\G\s/, end_pos) end
Checks if there is whitespace after token
space_after?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/token.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/token.rb
Apache-2.0
def space_before? position = begin_pos.zero? ? begin_pos : begin_pos - 1 pos.source_buffer.source.match(/\G\s/, position) end
Checks if there is whitespace before token
space_before?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/token.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/token.rb
Apache-2.0
def line_span(exclude_end: false) ::Range.new(first_line, last_line, exclude_end) end
@return [Range] the range of line numbers for the node If `exclude_end` is `true`, then the range will be exclusive. Assume that `node` corresponds to the following array literal: [ :foo, :bar ] node.loc.begin.line_span # => 1..1 node.source_range.line_span(exclude_end: true) # => 1...4
line_span
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/ext/range.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/ext/range.rb
Apache-2.0
def alternate_operator logical_operator? ? SEMANTIC_AND : LOGICAL_AND end
Returns the alternate operator of the `and` as a string. Returns `and` for `&&` and vice versa. @return [String] the alternate of the `and` operator
alternate_operator
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node/and_node.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node/and_node.rb
Apache-2.0
def inverse_operator logical_operator? ? LOGICAL_OR : SEMANTIC_OR end
Returns the inverse keyword of the `and` node as a string. Returns `||` for `&&` and `or` for `and`. @return [String] the inverse of the `and` operator
inverse_operator
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node/and_node.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node/and_node.rb
Apache-2.0
def default_value return unless default? node_parts[1] end
Returns the default value of the argument, if any. @return [Node, nil] the default value of the argument
default_value
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node/arg_node.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-ast-1.28.1/lib/rubocop/ast/node/arg_node.rb
Apache-2.0