class RuboCop::Cop::Lint::RedundantCopEnableDirective
baz
# rubocop:enable all
foo = “1”
# rubocop:disable Style/StringLiterals
# good
# rubocop:enable all
baz
# rubocop:enable Style/StringLiterals
foo = “1”
# rubocop:disable Style/StringLiterals
# bad
@example
foo = 1
# good
# rubocop:enable Layout/LineLength
foo = 1
# bad
@example
that cop checks whether any cop was actually enabled.
When comment enables all cops at once ‘rubocop:enable all`
removed.
Detects instances of rubocop:enable comments that can be
def all_or_name(name)
def all_or_name(name) name == 'all' ? 'all cops' : name end
def comment_start(comment)
def comment_start(comment) comment.source_range.begin_pos end
def cop_name_indention(comment, name)
def cop_name_indention(comment, name) comment.text.index(name) end
def department?(directive, name)
def department?(directive, name) directive.in_directive_department?(name) && !directive.overridden_by_department?(name) end
def on_new_investigation
def on_new_investigation return if processed_source.blank? || !processed_source.raw_source.include?('enable') offenses = processed_source.comment_config.extra_enabled_comments offenses.each { |comment, cop_names| register_offense(comment, cop_names) } end
def range_of_offense(comment, name)
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
def range_to_remove(begin_pos, end_pos, comment)
def range_to_remove(begin_pos, end_pos, comment) start = comment_start(comment) source = comment.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.source_range.end_pos) end end
def range_with_comma(comment, name)
def range_with_comma(comment, name) source = comment.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
def range_with_comma_after(comment, start, begin_pos, end_pos)
If the list of cops is comma-separated, but without a empty space after the comma,
def range_with_comma_after(comment, start, begin_pos, end_pos) begin_pos += 1 if comment.source[end_pos + 1] != ' ' range_between(start + begin_pos, start + end_pos + 1) end
def range_with_comma_before(start, begin_pos, end_pos)
def range_with_comma_before(start, begin_pos, end_pos) range_between(start + begin_pos - 1, start + end_pos) end
def register_offense(comment, cop_names)
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