class RuboCop::Cop::Lint::DuplicateMagicComment

Checks for duplicated magic comments.

@example

# bad

# encoding: ascii
# encoding: ascii

# good

# encoding: ascii

# bad

# frozen_string_literal: true
# frozen_string_literal: true

# good

# frozen_string_literal: true

Constants

MSG

Public Instance Methods

on_new_investigation() click to toggle source
# File lib/rubocop/cop/lint/duplicate_magic_comment.rb, line 35
def on_new_investigation
  return if processed_source.buffer.source.empty?

  magic_comment_lines.each_value do |comment_lines|
    next if comment_lines.count <= 1

    comment_lines[1..].each do |comment_line|
      range = processed_source.buffer.line_range(comment_line + 1)

      register_offense(range)
    end
  end
end

Private Instance Methods

magic_comment_lines() click to toggle source
# File lib/rubocop/cop/lint/duplicate_magic_comment.rb, line 51
def magic_comment_lines
  comment_lines = { encoding_magic_comments: [], frozen_string_literal_magic_comments: [] }

  leading_magic_comments.each.with_index do |magic_comment, index|
    if magic_comment.encoding_specified?
      comment_lines[:encoding_magic_comments] << index
    elsif magic_comment.frozen_string_literal_specified?
      comment_lines[:frozen_string_literal_magic_comments] << index
    end
  end

  comment_lines
end
register_offense(range) click to toggle source
# File lib/rubocop/cop/lint/duplicate_magic_comment.rb, line 65
def register_offense(range)
  add_offense(range) do |corrector|
    corrector.remove(range_by_whole_lines(range, include_final_newline: true))
  end
end