Class: RuboCop::CommentConfig

Inherits:
Object
  • Object
show all
Extended by:
SimpleForwardable
Defined in:
lib/rubocop/comment_config.rb

Overview

This class parses the special ‘rubocop:disable` comments in a source and provides a way to check if each cop is enabled at arbitrary line.

Defined Under Namespace

Classes: ConfigDisabledCopDirectiveComment, CopAnalysis

Constant Summary collapse

CONFIG_DISABLED_LINE_RANGE_MIN =
-Float::INFINITY

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(processed_source) ⇒ CommentConfig

Returns a new instance of CommentConfig.



34
35
36
37
# File 'lib/rubocop/comment_config.rb', line 34

def initialize(processed_source)
  @processed_source = processed_source
  @no_directives = !processed_source.raw_source.include?('rubocop')
end

Instance Attribute Details

#processed_sourceObject (readonly)

Returns the value of attribute processed_source.



30
31
32
# File 'lib/rubocop/comment_config.rb', line 30

def processed_source
  @processed_source
end

Instance Method Details

#comment_only_line?(line_number) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/rubocop/comment_config.rb', line 63

def comment_only_line?(line_number)
  non_comment_token_line_numbers.none?(line_number)
end

#cop_disabled_line_rangesObject



51
52
53
# File 'lib/rubocop/comment_config.rb', line 51

def cop_disabled_line_ranges
  @cop_disabled_line_ranges ||= analyze
end

#cop_enabled_at_line?(cop, line_number) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
# File 'lib/rubocop/comment_config.rb', line 39

def cop_enabled_at_line?(cop, line_number)
  cop = cop.cop_name if cop.respond_to?(:cop_name)
  disabled_line_ranges = cop_disabled_line_ranges[cop]
  return true unless disabled_line_ranges

  disabled_line_ranges.none? { |range| range.include?(line_number) }
end

#cop_opted_in?(cop) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/rubocop/comment_config.rb', line 47

def cop_opted_in?(cop)
  opt_in_cops.include?(cop.cop_name)
end

#extra_enabled_commentsObject



55
56
57
58
59
60
61
# File 'lib/rubocop/comment_config.rb', line 55

def extra_enabled_comments
  disable_count = Hash.new(0)
  registry.disabled(config).each do |cop|
    disable_count[cop.cop_name] += 1
  end
  extra_enabled_comments_with_names(extras: Hash.new { |h, k| h[k] = [] }, names: disable_count)
end