class RuboCop::DirectiveComment

This class wraps the `Parser::Source::Comment` object that represents a special `rubocop:disable` and `rubocop:enable` comment and exposes what cops it contains.

Constants

COPS_PATTERN

@api private

COP_NAMES_PATTERN

@api private

COP_NAME_PATTERN

@api private

DIRECTIVE_COMMENT_REGEXP

@api private

REDUNDANT_DIRECTIVE_COP

@api private

REDUNDANT_DIRECTIVE_COP_DEPARTMENT

@api private

Attributes

comment[R]
cop_registry[R]
cops[R]
mode[R]

Public Class Methods

before_comment(line) click to toggle source
# File lib/rubocop/directive_comment.rb, line 24
def self.before_comment(line)
  line.split(DIRECTIVE_COMMENT_REGEXP).first
end
new(comment, cop_registry = Cop::Registry.global) click to toggle source
# File lib/rubocop/directive_comment.rb, line 30
def initialize(comment, cop_registry = Cop::Registry.global)
  @comment = comment
  @cop_registry = cop_registry
  @mode, @cops = match_captures
end

Public Instance Methods

all_cops?() click to toggle source

Checks if all cops specified in this directive

# File lib/rubocop/directive_comment.rb, line 80
def all_cops?
  cops == 'all'
end
cop_names() click to toggle source

Returns array of specified in this directive cop names

# File lib/rubocop/directive_comment.rb, line 85
def cop_names
  @cop_names ||= all_cops? ? all_cop_names : parsed_cop_names
end
department_names() click to toggle source

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

# File lib/rubocop/directive_comment.rb, line 91
def department_names
  splitted_cops_string.select { |cop| department?(cop) }
end
directive_count() click to toggle source
# File lib/rubocop/directive_comment.rb, line 105
def directive_count
  splitted_cops_string.count
end
disabled?() click to toggle source

Checks if this directive disables cops

# File lib/rubocop/directive_comment.rb, line 60
def disabled?
  %w[disable todo].include?(mode)
end
disabled_all?() click to toggle source

Checks if this directive disables all cops

# File lib/rubocop/directive_comment.rb, line 75
def disabled_all?
  disabled? && all_cops?
end
enabled?() click to toggle source

Checks if this directive enables cops

# File lib/rubocop/directive_comment.rb, line 65
def enabled?
  mode == 'enable'
end
enabled_all?() click to toggle source

Checks if this directive enables all cops

# File lib/rubocop/directive_comment.rb, line 70
def enabled_all?
  !disabled? && all_cops?
end
in_directive_department?(cop) click to toggle source

Checks if directive departments include cop

# File lib/rubocop/directive_comment.rb, line 96
def in_directive_department?(cop)
  department_names.any? { |department| cop.start_with?(department) }
end
line_number() click to toggle source

Returns line number for directive

# File lib/rubocop/directive_comment.rb, line 110
def line_number
  comment.loc.expression.line
end
match?(cop_names) click to toggle source

Checks if this directive contains all the given cop names

# File lib/rubocop/directive_comment.rb, line 42
def match?(cop_names)
  parsed_cop_names.uniq.sort == cop_names.uniq.sort
end
match_captures() click to toggle source

Returns match captures to directive comment pattern

# File lib/rubocop/directive_comment.rb, line 55
def match_captures
  @match_captures ||= comment.text.match(DIRECTIVE_COMMENT_REGEXP)&.captures
end
overridden_by_department?(cop) click to toggle source

Checks if cop department has already used in directive comment

# File lib/rubocop/directive_comment.rb, line 101
def overridden_by_department?(cop)
  in_directive_department?(cop) && splitted_cops_string.include?(cop)
end
range() click to toggle source
# File lib/rubocop/directive_comment.rb, line 46
def range
  match = comment.text.match(DIRECTIVE_COMMENT_REGEXP)
  begin_pos = comment.loc.expression.begin_pos
  Parser::Source::Range.new(
    comment.loc.expression.source_buffer, begin_pos + match.begin(0), begin_pos + match.end(0)
  )
end
single_line?() click to toggle source

Checks if this directive relates to single line

# File lib/rubocop/directive_comment.rb, line 37
def single_line?
  !self.class.before_comment(comment.text).empty?
end

Private Instance Methods

all_cop_names() click to toggle source
# File lib/rubocop/directive_comment.rb, line 130
def all_cop_names
  exclude_redundant_directive_cop(cop_registry.names)
end
cop_names_for_department(department) click to toggle source
# File lib/rubocop/directive_comment.rb, line 134
def cop_names_for_department(department)
  names = cop_registry.names_for_department(department)
  has_redundant_directive_cop = department == REDUNDANT_DIRECTIVE_COP_DEPARTMENT
  has_redundant_directive_cop ? exclude_redundant_directive_cop(names) : names
end
department?(name) click to toggle source
# File lib/rubocop/directive_comment.rb, line 126
def department?(name)
  cop_registry.department?(name)
end
exclude_redundant_directive_cop(cops) click to toggle source
# File lib/rubocop/directive_comment.rb, line 140
def exclude_redundant_directive_cop(cops)
  cops - [REDUNDANT_DIRECTIVE_COP]
end
parsed_cop_names() click to toggle source
# File lib/rubocop/directive_comment.rb, line 120
def parsed_cop_names
  splitted_cops_string.map do |name|
    department?(name) ? cop_names_for_department(name) : name
  end.flatten
end
splitted_cops_string() click to toggle source
# File lib/rubocop/directive_comment.rb, line 116
def splitted_cops_string
  (cops || '').split(/,\s*/)
end