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
38
# File 'lib/rubocop/comment_config.rb', line 34

def initialize(processed_source)
  @processed_source = processed_source
  @no_directives = !processed_source.raw_source.include?('rubocop')
  @stack = []
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)


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

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

#cop_disabled_line_rangesObject



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

def cop_disabled_line_ranges
  @cop_disabled_line_ranges ||= analyze
end

#cop_enabled_at_line?(cop, line_number) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


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

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

#extra_enabled_commentsObject



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

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

#opt_in_copsSet<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The names of the cops that are opted in by an enable comment directive, used to mobilize cops disabled in the config on demand.

Returns:



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rubocop/comment_config.rb', line 73

def opt_in_cops
  @opt_in_cops ||= begin
    cops = Set.new
    each_directive do |directive|
      next unless directive.enabled?
      next if directive.all_cops?

      cops.merge(directive.raw_cop_names)
    end
    cops
  end
end