Class: RuboCop::DirectiveComment

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comment) ⇒ DirectiveComment

Returns a new instance of DirectiveComment.



10
11
12
# File 'lib/rubocop/directive_comment.rb', line 10

def initialize(comment)
  @comment = comment
end

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



8
9
10
# File 'lib/rubocop/directive_comment.rb', line 8

def comment
  @comment
end

Instance Method Details

#copsObject

Return all the cops specified in the directive



15
16
17
18
19
20
21
# File 'lib/rubocop/directive_comment.rb', line 15

def cops
  match = comment.text.match(CommentConfig::COMMENT_DIRECTIVE_REGEXP)
  return unless match

  cops_string = match.captures[1]
  cops_string.split(/,\s*/).uniq.sort
end

#match?(cop_names) ⇒ Boolean

Checks if this directive contains all the given cop names

Returns:

  • (Boolean)


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

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

#rangeObject



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

def range
  comment.location.expression
end