Class: RuboCop::DirectiveComment
- Inherits:
-
Object
- Object
- RuboCop::DirectiveComment
- 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 collapse
- REDUNDANT_COP =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'Lint/RedundantCopDisableDirective'- COP_NAME_PATTERN =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
'([A-Z]\w+/)*(?:[A-Z]\w+)'- COP_NAMES_PATTERN =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"(?:#{COP_NAME_PATTERN} , )*#{COP_NAME_PATTERN}"- COPS_PATTERN =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"(all|#{COP_NAMES_PATTERN})"- DIRECTIVE_COMMENT_REGEXP =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Regexp.new( "# rubocop : ((?:disable|enable|todo))\\b #{COPS_PATTERN}" .gsub(' ', '\s*') )
Instance Attribute Summary collapse
-
#comment ⇒ Object
readonly
Returns the value of attribute comment.
-
#cops ⇒ Object
readonly
Returns the value of attribute cops.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Class Method Summary collapse
Instance Method Summary collapse
-
#all_cops? ⇒ Boolean
Checks if all cops specified in this directive.
-
#cop_names ⇒ Object
Returns array of specified in this directive cop names.
-
#disabled? ⇒ Boolean
Checks if this directive disables cops.
-
#enabled? ⇒ Boolean
Checks if this directive enables cops.
-
#enabled_all? ⇒ Boolean
Checks if this directive enables all cops.
-
#initialize(comment) ⇒ DirectiveComment
constructor
A new instance of DirectiveComment.
-
#line_number ⇒ Object
Returns line number for directive.
-
#match?(cop_names) ⇒ Boolean
Checks if this directive contains all the given cop names.
-
#match_captures ⇒ Object
Returns match captures to directive comment pattern.
- #range ⇒ Object
-
#single_line? ⇒ Boolean
Checks if this directive relates to single line.
Constructor Details
#initialize(comment) ⇒ DirectiveComment
Returns a new instance of DirectiveComment.
28 29 30 31 |
# File 'lib/rubocop/directive_comment.rb', line 28 def initialize(comment) @comment = comment @mode, @cops = match_captures end |
Instance Attribute Details
#comment ⇒ Object (readonly)
Returns the value of attribute comment.
26 27 28 |
# File 'lib/rubocop/directive_comment.rb', line 26 def comment @comment end |
#cops ⇒ Object (readonly)
Returns the value of attribute cops.
26 27 28 |
# File 'lib/rubocop/directive_comment.rb', line 26 def cops @cops end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
26 27 28 |
# File 'lib/rubocop/directive_comment.rb', line 26 def mode @mode end |
Class Method Details
.before_comment(line) ⇒ Object
22 23 24 |
# File 'lib/rubocop/directive_comment.rb', line 22 def self.before_comment(line) line.split(DIRECTIVE_COMMENT_REGEXP).first end |
Instance Method Details
#all_cops? ⇒ Boolean
Checks if all cops specified in this directive
68 69 70 |
# File 'lib/rubocop/directive_comment.rb', line 68 def all_cops? cops == 'all' end |
#cop_names ⇒ Object
Returns array of specified in this directive cop names
73 74 75 |
# File 'lib/rubocop/directive_comment.rb', line 73 def cop_names @cop_names ||= all_cops? ? all_cop_names : parsed_cop_names end |
#disabled? ⇒ Boolean
Checks if this directive disables cops
53 54 55 |
# File 'lib/rubocop/directive_comment.rb', line 53 def disabled? %w[disable todo].include?(mode) end |
#enabled? ⇒ Boolean
Checks if this directive enables cops
58 59 60 |
# File 'lib/rubocop/directive_comment.rb', line 58 def enabled? mode == 'enable' end |
#enabled_all? ⇒ Boolean
Checks if this directive enables all cops
63 64 65 |
# File 'lib/rubocop/directive_comment.rb', line 63 def enabled_all? !disabled? && all_cops? end |
#line_number ⇒ Object
Returns line number for directive
78 79 80 |
# File 'lib/rubocop/directive_comment.rb', line 78 def line_number comment.loc.expression.line end |
#match?(cop_names) ⇒ Boolean
Checks if this directive contains all the given cop names
39 40 41 |
# File 'lib/rubocop/directive_comment.rb', line 39 def match?(cop_names) parsed_cop_names.uniq.sort == cop_names.uniq.sort end |
#match_captures ⇒ Object
Returns match captures to directive comment pattern
48 49 50 |
# File 'lib/rubocop/directive_comment.rb', line 48 def match_captures @match_captures ||= comment.text.match(DIRECTIVE_COMMENT_REGEXP)&.captures end |
#range ⇒ Object
43 44 45 |
# File 'lib/rubocop/directive_comment.rb', line 43 def range comment.location.expression end |
#single_line? ⇒ Boolean
Checks if this directive relates to single line
34 35 36 |
# File 'lib/rubocop/directive_comment.rb', line 34 def single_line? !self.class.before_comment(comment.text).empty? end |