Class: RuboCop::CommentConfig

Inherits:
Object
  • Object
show all
Extended by:
SimpleForwardable
Includes:
DisableNext
Defined in:
lib/rubocop/comment_config.rb,
lib/rubocop/comment_config/disable_next.rb,
lib/rubocop/comment_config/directive_range.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

Modules: DisableNext Classes: ConfigDisabledCopDirectiveComment, CopAnalysis, DirectiveRange

Constant Summary collapse

CONFIG_DISABLED_LINE_RANGE_MIN =
-Float::INFINITY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DisableNext

#detached_next_directives, #statement_scope_after

Constructor Details

#initialize(processed_source) ⇒ CommentConfig

Returns a new instance of CommentConfig.



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

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

Instance Attribute Details

#processed_sourceObject (readonly)

Returns the value of attribute processed_source.



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

def processed_source
  @processed_source
end

Instance Method Details

#comment_only_line?(line_number) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/rubocop/comment_config.rb', line 74

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

#cop_disabled_line_rangesObject



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

def cop_disabled_line_ranges
  @cop_disabled_line_ranges ||= analyze
end

#cop_enabled_at_line?(cop, line_number) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rubocop/comment_config.rb', line 40

def cop_enabled_at_line?(cop, line_number)
  cop_enabled_at_lines?(cop, line_number, line_number)
end

#cop_enabled_at_lines?(cop, first_line, last_line) ⇒ Boolean

Whether the cop is enabled for all of the given line span. The cop counts as disabled for the span when any of its disabled ranges overlaps it, so that a directive on any line of a multi-line offense suppresses the offense.

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
# File 'lib/rubocop/comment_config.rb', line 48

def cop_enabled_at_lines?(cop, first_line, last_line)
  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? do |range|
    range.end >= first_line && range.begin <= last_line
  end
end

#cop_opted_in?(cop) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rubocop/comment_config.rb', line 58

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

#extra_enabled_commentsObject



66
67
68
69
70
71
72
# File 'lib/rubocop/comment_config.rb', line 66

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:



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rubocop/comment_config.rb', line 83

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