class RuboCop::Cop::Lint::RedundantCopEnableDirective
Detects instances of rubocop:enable comments that can be removed.
When comment enables all cops at once ‘rubocop:enable all` that cop checks whether any cop was actually enabled. @example
# bad foo = 1 # rubocop:enable Layout/LineLength # good foo = 1
@example
# bad # rubocop:disable Style/StringLiterals foo = "1" # rubocop:enable Style/StringLiterals baz # rubocop:enable all # good # rubocop:disable Style/StringLiterals foo = "1" # rubocop:enable all baz
Constants
- MSG
Public Instance Methods
on_new_investigation()
click to toggle source
# File lib/rubocop/cop/lint/redundant_cop_enable_directive.rb, line 44 def on_new_investigation return if processed_source.blank? offenses = processed_source.comment_config.extra_enabled_comments offenses.each { |comment, cop_names| register_offense(comment, cop_names) } end
Private Instance Methods
all_or_name(name)
click to toggle source
# File lib/rubocop/cop/lint/redundant_cop_enable_directive.rb, line 120 def all_or_name(name) name == 'all' ? 'all cops' : name end
comment_start(comment)
click to toggle source
# File lib/rubocop/cop/lint/redundant_cop_enable_directive.rb, line 76 def comment_start(comment) comment.loc.expression.begin_pos end
cop_name_indention(comment, name)
click to toggle source
# File lib/rubocop/cop/lint/redundant_cop_enable_directive.rb, line 80 def cop_name_indention(comment, name) comment.text.index(name) end
department?(directive, name)
click to toggle source
# File lib/rubocop/cop/lint/redundant_cop_enable_directive.rb, line 124 def department?(directive, name) directive.in_directive_department?(name) && !directive.overridden_by_department?(name) end
range_of_offense(comment, name)
click to toggle source
# File lib/rubocop/cop/lint/redundant_cop_enable_directive.rb, line 71 def range_of_offense(comment, name) start_pos = comment_start(comment) + cop_name_indention(comment, name) range_between(start_pos, start_pos + name.size) end
range_to_remove(begin_pos, end_pos, comment)
click to toggle source
# File lib/rubocop/cop/lint/redundant_cop_enable_directive.rb, line 95 def range_to_remove(begin_pos, end_pos, comment) start = comment_start(comment) source = comment.loc.expression.source if source[begin_pos - 1] == ',' range_with_comma_before(start, begin_pos, end_pos) elsif source[end_pos] == ',' range_with_comma_after(comment, start, begin_pos, end_pos) else range_between(start, comment.loc.expression.end_pos) end end
range_with_comma(comment, name)
click to toggle source
# File lib/rubocop/cop/lint/redundant_cop_enable_directive.rb, line 84 def range_with_comma(comment, name) source = comment.loc.expression.source begin_pos = cop_name_indention(comment, name) end_pos = begin_pos + name.size begin_pos = reposition(source, begin_pos, -1) end_pos = reposition(source, end_pos, 1) range_to_remove(begin_pos, end_pos, comment) end
range_with_comma_after(comment, start, begin_pos, end_pos)
click to toggle source
If the list of cops is comma-separated, but without a empty space after the comma, we should not remove the prepending empty space, thus begin_pos += 1
# File lib/rubocop/cop/lint/redundant_cop_enable_directive.rb, line 114 def range_with_comma_after(comment, start, begin_pos, end_pos) begin_pos += 1 if comment.loc.expression.source[end_pos + 1] != ' ' range_between(start + begin_pos, start + end_pos + 1) end
range_with_comma_before(start, begin_pos, end_pos)
click to toggle source
# File lib/rubocop/cop/lint/redundant_cop_enable_directive.rb, line 108 def range_with_comma_before(start, begin_pos, end_pos) range_between(start + begin_pos - 1, start + end_pos) end
register_offense(comment, cop_names)
click to toggle source
# File lib/rubocop/cop/lint/redundant_cop_enable_directive.rb, line 53 def register_offense(comment, cop_names) directive = DirectiveComment.new(comment) cop_names.each do |name| name = name.split('/').first if department?(directive, name) add_offense( range_of_offense(comment, name), message: format(MSG, cop: all_or_name(name)) ) do |corrector| if directive.match?(cop_names) corrector.remove(range_with_surrounding_space(directive.range, side: :right)) else corrector.remove(range_with_comma(comment, name)) end end end end