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
- LINT_DEPARTMENT =
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'
- LINT_REDUNDANT_DIRECTIVE_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_DEPARTMENT}/RedundantCopDisableDirective"
- LINT_SYNTAX_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_DEPARTMENT}/Syntax"
- 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})"
- AVAILABLE_MODES =
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.
%w[disable enable todo].freeze
- DIRECTIVE_MARKER_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.
'# rubocop : '
- DIRECTIVE_MARKER_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(DIRECTIVE_MARKER_PATTERN.gsub(' ', '\s*'))
- DIRECTIVE_HEADER_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.
"#{DIRECTIVE_MARKER_PATTERN}((?:#{AVAILABLE_MODES.join('|')}))\\b"
- 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( "#{DIRECTIVE_HEADER_PATTERN} #{COPS_PATTERN}" .gsub(' ', '\s*') )
- TRAILING_COMMENT_MARKER =
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.
'--'
- MALFORMED_DIRECTIVE_WITHOUT_COP_NAME_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( "\\A#{DIRECTIVE_HEADER_PATTERN}\\s*\\z".gsub(' ', '\s*') )
Instance Attribute Summary collapse
-
#comment ⇒ Object
readonly
Returns the value of attribute comment.
-
#cop_registry ⇒ Object
readonly
Returns the value of attribute cop_registry.
-
#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.
-
#department_names ⇒ Object
Returns array of specified in this directive department names when all department disabled.
- #directive_count ⇒ Object
-
#disabled? ⇒ Boolean
Checks if this directive disables cops.
-
#disabled_all? ⇒ Boolean
Checks if this directive disables all cops.
-
#enabled? ⇒ Boolean
Checks if this directive enables cops.
-
#enabled_all? ⇒ Boolean
Checks if this directive enables all cops.
-
#in_directive_department?(cop) ⇒ Boolean
Checks if directive departments include cop.
-
#initialize(comment, cop_registry = Cop::Registry.global) ⇒ DirectiveComment
constructor
A new instance of DirectiveComment.
-
#line_number ⇒ Object
Returns line number for directive.
-
#malformed? ⇒ Boolean
Checks if the comment is malformed as a ‘# rubocop:` 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.
-
#missing_cop_name? ⇒ Boolean
Checks if the directive comment is missing a cop name.
-
#overridden_by_department?(cop) ⇒ Boolean
Checks if cop department has already used in directive comment.
- #range ⇒ Object
-
#raw_cop_names ⇒ Object
Returns an array of cops for this directive comment, without resolving departments.
-
#single_line? ⇒ Boolean
Checks if this directive relates to single line.
-
#start_with_marker? ⇒ Boolean
Checks if the comment starts with ‘# rubocop:` marker.
Constructor Details
#initialize(comment, cop_registry = Cop::Registry.global) ⇒ DirectiveComment
Returns a new instance of DirectiveComment.
46 47 48 49 50 51 |
# 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 |
Instance Attribute Details
#comment ⇒ Object (readonly)
Returns the value of attribute comment.
44 45 46 |
# File 'lib/rubocop/directive_comment.rb', line 44 def comment @comment end |
#cop_registry ⇒ Object (readonly)
Returns the value of attribute cop_registry.
44 45 46 |
# File 'lib/rubocop/directive_comment.rb', line 44 def cop_registry @cop_registry end |
#cops ⇒ Object (readonly)
Returns the value of attribute cops.
44 45 46 |
# File 'lib/rubocop/directive_comment.rb', line 44 def cops @cops end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
44 45 46 |
# File 'lib/rubocop/directive_comment.rb', line 44 def mode @mode end |
Class Method Details
.before_comment(line) ⇒ Object
40 41 42 |
# File 'lib/rubocop/directive_comment.rb', line 40 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
115 116 117 |
# File 'lib/rubocop/directive_comment.rb', line 115 def all_cops? cops == 'all' end |
#cop_names ⇒ Object
Returns array of specified in this directive cop names
120 121 122 |
# File 'lib/rubocop/directive_comment.rb', line 120 def cop_names @cop_names ||= all_cops? ? all_cop_names : parsed_cop_names end |
#department_names ⇒ Object
Returns array of specified in this directive department names when all department disabled
131 132 133 |
# File 'lib/rubocop/directive_comment.rb', line 131 def department_names raw_cop_names.select { |cop| department?(cop) } end |
#directive_count ⇒ Object
145 146 147 |
# File 'lib/rubocop/directive_comment.rb', line 145 def directive_count raw_cop_names.count end |
#disabled? ⇒ Boolean
Checks if this directive disables cops
95 96 97 |
# File 'lib/rubocop/directive_comment.rb', line 95 def disabled? %w[disable todo].include?(mode) end |
#disabled_all? ⇒ Boolean
Checks if this directive disables all cops
110 111 112 |
# File 'lib/rubocop/directive_comment.rb', line 110 def disabled_all? disabled? && all_cops? end |
#enabled? ⇒ Boolean
Checks if this directive enables cops
100 101 102 |
# File 'lib/rubocop/directive_comment.rb', line 100 def enabled? mode == 'enable' end |
#enabled_all? ⇒ Boolean
Checks if this directive enables all cops
105 106 107 |
# File 'lib/rubocop/directive_comment.rb', line 105 def enabled_all? !disabled? && all_cops? end |
#in_directive_department?(cop) ⇒ Boolean
Checks if directive departments include cop
136 137 138 |
# File 'lib/rubocop/directive_comment.rb', line 136 def in_directive_department?(cop) department_names.any? { |department| cop.start_with?(department) } end |
#line_number ⇒ Object
Returns line number for directive
150 151 152 |
# File 'lib/rubocop/directive_comment.rb', line 150 def line_number comment.source_range.line end |
#malformed? ⇒ Boolean
Checks if the comment is malformed as a ‘# rubocop:` directive
59 60 61 62 63 64 |
# 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 |
#match?(cop_names) ⇒ Boolean
Checks if this directive contains all the given cop names
77 78 79 |
# File 'lib/rubocop/directive_comment.rb', line 77 def match?(cop_names) parsed_cop_names.uniq.sort == cop_names.uniq.sort end |
#match_captures ⇒ Object
Returns match captures to directive comment pattern
90 91 92 |
# File 'lib/rubocop/directive_comment.rb', line 90 def match_captures @match_captures ||= @match_data&.captures end |
#missing_cop_name? ⇒ Boolean
Checks if the directive comment is missing a cop name
67 68 69 |
# File 'lib/rubocop/directive_comment.rb', line 67 def missing_cop_name? MALFORMED_DIRECTIVE_WITHOUT_COP_NAME_REGEXP.match?(comment.text) end |
#overridden_by_department?(cop) ⇒ Boolean
Checks if cop department has already used in directive comment
141 142 143 |
# File 'lib/rubocop/directive_comment.rb', line 141 def overridden_by_department?(cop) in_directive_department?(cop) && raw_cop_names.include?(cop) end |
#range ⇒ Object
81 82 83 84 85 86 87 |
# 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 ⇒ Object
Returns an array of cops for this directive comment, without resolving departments
125 126 127 |
# File 'lib/rubocop/directive_comment.rb', line 125 def raw_cop_names @raw_cop_names ||= (cops || '').split(/,\s*/) end |
#single_line? ⇒ Boolean
Checks if this directive relates to single line
72 73 74 |
# File 'lib/rubocop/directive_comment.rb', line 72 def single_line? !comment.text.start_with?(DIRECTIVE_COMMENT_REGEXP) end |
#start_with_marker? ⇒ Boolean
Checks if the comment starts with ‘# rubocop:` marker
54 55 56 |
# File 'lib/rubocop/directive_comment.rb', line 54 def start_with_marker? comment.text.start_with?(DIRECTIVE_MARKER_REGEXP) end |