Class: RuboCop::CommentConfig

Inherits:
Object
  • Object
show all
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: CopAnalysis

Constant Summary collapse

UNNEEDED_DISABLE =
'Lint/UnneededCopDisableDirective'
COP_NAME_PATTERN =
'([A-Z]\w+/)?(?:[A-Z]\w+)'
COP_NAMES_PATTERN =
"(?:#{COP_NAME_PATTERN} , )*#{COP_NAME_PATTERN}"
COPS_PATTERN =
"(all|#{COP_NAMES_PATTERN})"
COMMENT_DIRECTIVE_REGEXP =
Regexp.new(
  ('# rubocop : ((?:disable|enable|todo))\b ' + COPS_PATTERN)
    .gsub(' ', '\s*')
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(processed_source) ⇒ CommentConfig

Returns a new instance of CommentConfig.



22
23
24
# File 'lib/rubocop/comment_config.rb', line 22

def initialize(processed_source)
  @processed_source = processed_source
end

Instance Attribute Details

#processed_sourceObject (readonly)

Returns the value of attribute processed_source.



20
21
22
# File 'lib/rubocop/comment_config.rb', line 20

def processed_source
  @processed_source
end

Instance Method Details

#cop_disabled_line_rangesObject



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

def cop_disabled_line_ranges
  @cop_disabled_line_ranges ||= analyze
end

#cop_enabled_at_line?(cop, line_number) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/rubocop/comment_config.rb', line 26

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

#extra_enabled_commentsObject



38
39
40
# File 'lib/rubocop/comment_config.rb', line 38

def extra_enabled_comments
  extra_enabled_comments_with_names([], {})
end