Class: RuboCop::DirectiveComment
| Relationships & Source Files | |
| Inherits: | Object | 
| Defined in: | lib/rubocop/directive_comment.rb | 
Overview
This class wraps the Parser::Source::Comment object that represents a
special rubocop:disable and rubocop:enable comment and exposes what
cops it contains.
Constant Summary
- 
    AVAILABLE_MODES =
    Internal use only
    
 # File 'lib/rubocop/directive_comment.rb', line 21%w[disable enable todo].freeze 
- 
    COPS_PATTERN =
    Internal use only
    
 # File 'lib/rubocop/directive_comment.rb', line 19"(all|#{COP_NAMES_PATTERN})"
- 
    COP_NAMES_PATTERN =
    Internal use only
    
 # File 'lib/rubocop/directive_comment.rb', line 17"(?:#{COP_NAME_PATTERN} , )*#{COP_NAME_PATTERN}"
- 
    COP_NAME_PATTERN =
    Internal use only
    
 # File 'lib/rubocop/directive_comment.rb', line 15'([A-Za-z]\w+/)*(?:[A-Za-z]\w+)'
- 
    DIRECTIVE_COMMENT_REGEXP =
    Internal use only
    
 # File 'lib/rubocop/directive_comment.rb', line 29Regexp.new( "#{DIRECTIVE_HEADER_PATTERN} #{COPS_PATTERN}" .gsub(' ', '\s*') ) 
- 
    DIRECTIVE_HEADER_PATTERN =
    Internal use only
    
 # File 'lib/rubocop/directive_comment.rb', line 27"#{DIRECTIVE_MARKER_PATTERN}((?:#{AVAILABLE_MODES.join('|')}))\\b"
- 
    DIRECTIVE_MARKER_PATTERN =
    Internal use only
    
 # File 'lib/rubocop/directive_comment.rb', line 23'# rubocop : '
- 
    DIRECTIVE_MARKER_REGEXP =
    Internal use only
    
 # File 'lib/rubocop/directive_comment.rb', line 25Regexp.new(DIRECTIVE_MARKER_PATTERN.gsub(' ', '\s*')) 
- 
    LINT_DEPARTMENT =
    Internal use only
    
 # File 'lib/rubocop/directive_comment.rb', line 9'Lint'
- 
    LINT_REDUNDANT_DIRECTIVE_COP =
    Internal use only
    
 # File 'lib/rubocop/directive_comment.rb', line 11"#{LINT_DEPARTMENT}/RedundantCopDisableDirective"
- 
    LINT_SYNTAX_COP =
    Internal use only
    
 # File 'lib/rubocop/directive_comment.rb', line 13"#{LINT_DEPARTMENT}/Syntax"
- 
    MALFORMED_DIRECTIVE_WITHOUT_COP_NAME_REGEXP =
    Internal use only
    
 # File 'lib/rubocop/directive_comment.rb', line 36Regexp.new( "\\A#{DIRECTIVE_HEADER_PATTERN}\\s*\\z".gsub(' ', '\s*') ) 
- 
    TRAILING_COMMENT_MARKER =
    Internal use only
    
 # File 'lib/rubocop/directive_comment.rb', line 34'--'
Class Method Summary
Instance Attribute Summary
- 
    
      #all_cops?  ⇒ Boolean 
    
    readonly
    Checks if all cops specified in this directive. 
- #comment readonly
- #cop_registry readonly
- #cops readonly
- 
    
      #disabled?  ⇒ Boolean 
    
    readonly
    Checks if this directive disables cops. 
- 
    
      #disabled_all?  ⇒ Boolean 
    
    readonly
    Checks if this directive disables all cops. 
- 
    
      #enabled?  ⇒ Boolean 
    
    readonly
    Checks if this directive enables cops. 
- 
    
      #enabled_all?  ⇒ Boolean 
    
    readonly
    Checks if this directive enables all cops. 
- 
    
      #malformed?  ⇒ Boolean 
    
    readonly
    Checks if the comment is malformed as a # rubocop:directive.
- 
    
      #missing_cop_name?  ⇒ Boolean 
    
    readonly
    Checks if the directive comment is missing a cop name. 
- #mode readonly
- 
    
      #single_line?  ⇒ Boolean 
    
    readonly
    Checks if this directive relates to single line. 
- 
    
      #start_with_marker?  ⇒ Boolean 
    
    readonly
    Checks if the comment starts with # rubocop:marker.
Instance Method Summary
- 
    
      #cop_names  
    
    Returns array of specified in this directive cop names. 
- 
    
      #department_names  
    
    Returns array of specified in this directive department names when all department disabled. 
- #directive_count
- 
    
      #in_directive_department?(cop)  ⇒ Boolean 
    
    Checks if directive departments include cop. 
- 
    
      #line_number  
    
    Returns line number for directive. 
- 
    
      #match?(cop_names)  ⇒ Boolean 
    
    Checks if this directive contains all the given cop names. 
- 
    
      #match_captures  
    
    Returns match captures to directive comment pattern. 
- 
    
      #overridden_by_department?(cop)  ⇒ Boolean 
    
    Checks if cop department has already used in directive comment. 
- #range
- 
    
      #raw_cop_names  
    
    Returns an array of cops for this directive comment, without resolving departments. 
- #all_cop_names private
- #cop_names_for_department(department) private
- #department?(name) ⇒ Boolean private
- #exclude_lint_department_cops(cops) private
- #parsed_cop_names private
Constructor Details
    .new(comment, cop_registry = Cop::Registry.global)  ⇒ DirectiveComment 
  
# File 'lib/rubocop/directive_comment.rb', line 46
def initialize(comment, cop_registry = Cop::Registry.global) @comment = comment @cop_registry = cop_registry @match_data = comment.text.match(DIRECTIVE_COMMENT_REGEXP) @mode, @cops = match_captures end
Class Method Details
.before_comment(line)
[ GitHub ]# File 'lib/rubocop/directive_comment.rb', line 40
def self.before_comment(line) line.split(DIRECTIVE_COMMENT_REGEXP).first end
Instance Attribute Details
    #all_cops?  ⇒ Boolean  (readonly)
  
Checks if all cops specified in this directive
# File 'lib/rubocop/directive_comment.rb', line 115
def all_cops? cops == 'all' end
#comment (readonly)
[ GitHub ]# File 'lib/rubocop/directive_comment.rb', line 44
attr_reader :comment, :cop_registry, :mode, :cops
#cop_registry (readonly)
[ GitHub ]#cops (readonly)
[ GitHub ]# File 'lib/rubocop/directive_comment.rb', line 44
attr_reader :comment, :cop_registry, :mode, :cops
    #disabled?  ⇒ Boolean  (readonly)
  
Checks if this directive disables cops
# File 'lib/rubocop/directive_comment.rb', line 95
def disabled? %w[disable todo].include?(mode) end
    #disabled_all?  ⇒ Boolean  (readonly)
  
Checks if this directive disables all cops
    #enabled?  ⇒ Boolean  (readonly)
  
Checks if this directive enables cops
# File 'lib/rubocop/directive_comment.rb', line 100
def enabled? mode == 'enable' end
    #enabled_all?  ⇒ Boolean  (readonly)
  
Checks if this directive enables all cops
    #malformed?  ⇒ Boolean  (readonly)
  
Checks if the comment is malformed as a # rubocop: directive
# File 'lib/rubocop/directive_comment.rb', line 59
def malformed? return true if !start_with_marker? || @match_data.nil? tail = @match_data.post_match.lstrip !(tail.empty? || tail.start_with?(TRAILING_COMMENT_MARKER)) end
    #missing_cop_name?  ⇒ Boolean  (readonly)
  
Checks if the directive comment is missing a cop name
# File 'lib/rubocop/directive_comment.rb', line 67
def missing_cop_name? MALFORMED_DIRECTIVE_WITHOUT_COP_NAME_REGEXP.match?(comment.text) end
#mode (readonly)
[ GitHub ]# File 'lib/rubocop/directive_comment.rb', line 44
attr_reader :comment, :cop_registry, :mode, :cops
    #single_line?  ⇒ Boolean  (readonly)
  
Checks if this directive relates to single line
# File 'lib/rubocop/directive_comment.rb', line 72
def single_line? !comment.text.start_with?(DIRECTIVE_COMMENT_REGEXP) end
    #start_with_marker?  ⇒ Boolean  (readonly)
  
Checks if the comment starts with # rubocop: marker
# File 'lib/rubocop/directive_comment.rb', line 54
def start_with_marker? comment.text.start_with?(DIRECTIVE_MARKER_REGEXP) end
Instance Method Details
#all_cop_names (private)
[ GitHub ]# File 'lib/rubocop/directive_comment.rb', line 167
def all_cop_names exclude_lint_department_cops(cop_registry.names) end
#cop_names
Returns array of specified in this directive cop names
# File 'lib/rubocop/directive_comment.rb', line 120
def cop_names @cop_names ||= all_cops? ? all_cop_names : parsed_cop_names end
#cop_names_for_department(department) (private)
[ GitHub ]# File 'lib/rubocop/directive_comment.rb', line 171
def cop_names_for_department(department) names = cop_registry.names_for_department(department) department == LINT_DEPARTMENT ? exclude_lint_department_cops(names) : names end
    #department?(name)  ⇒ Boolean  (private)
  
# File 'lib/rubocop/directive_comment.rb', line 163
def department?(name) cop_registry.department?(name) end
#department_names
Returns array of specified in this directive department names when all department disabled
# File 'lib/rubocop/directive_comment.rb', line 131
def department_names raw_cop_names.select { |cop| department?(cop) } end
#directive_count
[ GitHub ]# File 'lib/rubocop/directive_comment.rb', line 145
def directive_count raw_cop_names.count end
#exclude_lint_department_cops(cops) (private)
[ GitHub ]# File 'lib/rubocop/directive_comment.rb', line 176
def exclude_lint_department_cops(cops) cops - [LINT_REDUNDANT_DIRECTIVE_COP, LINT_SYNTAX_COP] end
    #in_directive_department?(cop)  ⇒ Boolean 
  
Checks if directive departments include cop
# File 'lib/rubocop/directive_comment.rb', line 136
def in_directive_department?(cop) department_names.any? { |department| cop.start_with?(department) } end
#line_number
Returns line number for directive
# File 'lib/rubocop/directive_comment.rb', line 150
def line_number comment.source_range.line end
    #match?(cop_names)  ⇒ Boolean 
  
Checks if this directive contains all the given cop names
# File 'lib/rubocop/directive_comment.rb', line 77
def match?(cop_names) parsed_cop_names.uniq.sort == cop_names.uniq.sort end
#match_captures
Returns match captures to directive comment pattern
# File 'lib/rubocop/directive_comment.rb', line 90
def match_captures @match_captures ||= @match_data&.captures end
    #overridden_by_department?(cop)  ⇒ Boolean 
  
Checks if cop department has already used in directive comment
# File 'lib/rubocop/directive_comment.rb', line 141
def overridden_by_department?(cop) in_directive_department?(cop) && raw_cop_names.include?(cop) end
#parsed_cop_names (private)
[ GitHub ]# File 'lib/rubocop/directive_comment.rb', line 156
def parsed_cop_names cops = raw_cop_names.map do |name| department?(name) ? cop_names_for_department(name) : name end.flatten cops - [LINT_SYNTAX_COP] end
#range
[ GitHub ]# File 'lib/rubocop/directive_comment.rb', line 81
def range match = comment.text.match(DIRECTIVE_COMMENT_REGEXP) begin_pos = comment.source_range.begin_pos Parser::Source::Range.new( comment.source_range.source_buffer, begin_pos + match.begin(0), begin_pos + match.end(0) ) end
#raw_cop_names
Returns an array of cops for this directive comment, without resolving departments
# File 'lib/rubocop/directive_comment.rb', line 125
def raw_cop_names @raw_cop_names ||= (cops || '').split(/,\s*/) end