Class: RuboCop::Cop::Metrics::Utils::CodeLengthCalculator

Inherits:
Object
  • Object
show all
Extended by:
Macros
Includes:
Util
Defined in:
lib/rubocop/cop/metrics/utils/code_length_calculator.rb

Overview

Helps to calculate code length for the provided node.

Constant Summary

Constants included from Util

Util::LITERAL_REGEX

Constants included from PathUtil

PathUtil::HIDDEN_FILE_PATTERN

Instance Method Summary collapse

Methods included from Util

add_parentheses, any_descendant?, args_begin, args_end, begins_its_line?, comment_line?, comment_lines?, double_quotes_required?, escape_string, first_part_of_call_chain, indent, interpret_string_escapes, line, line_range, needs_escaping?, on_node, parentheses?, same_line?, to_string_literal, to_supported_styles, trim_string_interpolation_escape_character

Methods included from PathUtil

absolute?, glob?, hidden_dir?, hidden_file?, hidden_file_in_not_hidden_dir?, match_path?, maybe_hidden_file?, relative_path, smart_path

Constructor Details

#initialize(node, processed_source, count_comments: false, foldable_types: []) ⇒ CodeLengthCalculator

Returns a new instance of CodeLengthCalculator.



16
17
18
19
20
21
22
# File 'lib/rubocop/cop/metrics/utils/code_length_calculator.rb', line 16

def initialize(node, processed_source, count_comments: false, foldable_types: [])
  @node = node
  @processed_source = processed_source
  @count_comments = count_comments
  @foldable_checks = build_foldable_checks(foldable_types)
  @foldable_types = normalize_foldable_types(foldable_types)
end

Instance Method Details

#calculateObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/cop/metrics/utils/code_length_calculator.rb', line 24

def calculate
  length = code_length(@node)
  return length if @foldable_types.empty?

  each_top_level_descendant(@node, @foldable_types) do |descendant|
    next unless foldable_node?(descendant)

    descendant_length = code_length(descendant)
    length = length - descendant_length + 1
    # Subtract length of opening and closing brace if method argument omits hash braces.
    length -= omit_length(descendant) if descendant.hash_type? && !descendant.braces?
  end

  length
end