123456789_123456789_123456789_123456789_123456789_

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

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(comment, cop_registry = Cop::Registry.global) ⇒ DirectiveComment

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 32

def initialize(comment, cop_registry = Cop::Registry.global)
  @comment = comment
  @cop_registry = cop_registry
  @mode, @cops = match_captures
end

Class Method Details

.before_comment(line)

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 26

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

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 82

def all_cops?
  cops == 'all'
end

#comment (readonly)

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 30

attr_reader :comment, :cop_registry, :mode, :cops

#cop_registry (readonly)

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 30

attr_reader :comment, :cop_registry, :mode, :cops

#cops (readonly)

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 30

attr_reader :comment, :cop_registry, :mode, :cops

#disabled?Boolean (readonly)

Checks if this directive disables cops

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 62

def disabled?
  %w[disable todo].include?(mode)
end

#disabled_all?Boolean (readonly)

Checks if this directive disables all cops

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 77

def disabled_all?
  disabled? && all_cops?
end

#enabled?Boolean (readonly)

Checks if this directive enables cops

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 67

def enabled?
  mode == 'enable'
end

#enabled_all?Boolean (readonly)

Checks if this directive enables all cops

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 72

def enabled_all?
  !disabled? && all_cops?
end

#mode (readonly)

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 30

attr_reader :comment, :cop_registry, :mode, :cops

#single_line?Boolean (readonly)

Checks if this directive relates to single line

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 39

def single_line?
  !comment.text.start_with?(DIRECTIVE_COMMENT_REGEXP)
end

Instance Method Details

#all_cop_names (private)

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 133

def all_cop_names
  exclude_lint_department_cops(cop_registry.names)
end

#cop_names

Returns array of specified in this directive cop names

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 87

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 137

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)

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 129

def department?(name)
  cop_registry.department?(name)
end

#department_names

Returns array of specified in this directive department names when all department disabled

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 93

def department_names
  splitted_cops_string.select { |cop| department?(cop) }
end

#directive_count

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 107

def directive_count
  splitted_cops_string.count
end

#exclude_lint_department_cops(cops) (private)

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 142

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

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 98

def in_directive_department?(cop)
  department_names.any? { |department| cop.start_with?(department) }
end

#line_number

Returns line number for directive

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 112

def line_number
  comment.source_range.line
end

#match?(cop_names) ⇒ Boolean

Checks if this directive contains all the given cop names

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 44

def match?(cop_names)
  parsed_cop_names.uniq.sort == cop_names.uniq.sort
end

#match_captures

Returns match captures to directive comment pattern

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 57

def match_captures
  @match_captures ||= comment.text.match(DIRECTIVE_COMMENT_REGEXP)&.captures
end

#overridden_by_department?(cop) ⇒ Boolean

Checks if cop department has already used in directive comment

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 103

def overridden_by_department?(cop)
  in_directive_department?(cop) && splitted_cops_string.include?(cop)
end

#parsed_cop_names (private)

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 122

def parsed_cop_names
  cops = splitted_cops_string.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 48

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

#splitted_cops_string (private)

[ GitHub ]

  
# File 'lib/rubocop/directive_comment.rb', line 118

def splitted_cops_string
  (cops || '').split(/,\s*/)
end