Class: Shirobai::Cop::Lint::OrderedMagicComments

Inherits:
RuboCop::Cop::Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector
Includes:
BundleEligible
Defined in:
lib/shirobai/cop/lint/ordered_magic_comments.rb

Overview

Drop-in Rust reimplementation of Lint/OrderedMagicComments.

Rust reproduces stock's magic_comment_lines scan over the same leading comment lines as Lint/DuplicateMagicComment (leading_comment_lines from the FrozenStringLiteral mixin) and the MagicComment.parse encoding_specified? / elsif valid? bucketing. It returns at most one offense as the two 1-based line numbers (encoding_line, other_line) when the encoding magic comment does NOT precede the other magic comment.

The wrapper rebuilds the offense range and the line swap with stock's own buffer.line_range, so detection and autocorrect are byte-identical by construction. Line numbers are byte/char agnostic, so no SourceOffsets conversion is needed; line_range is only built on offense files (stock asks the first token for its line — materializing Buffer#line_begins — on every file).

Constant Summary collapse

MSG =
"The encoding magic comment should precede all other magic comments."

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



28
# File 'lib/shirobai/cop/lint/ordered_magic_comments.rb', line 28

def self.badge = RuboCop::Cop::Badge.parse(cop_name)

.bundle_args(_config) ⇒ Object

Config-less: contributes nothing to nums / lists. Kept for the 4+1 single-source-of-config convention.



32
33
34
# File 'lib/shirobai/cop/lint/ordered_magic_comments.rb', line 32

def self.bundle_args(_config)
  []
end

.cop_nameObject



27
# File 'lib/shirobai/cop/lint/ordered_magic_comments.rb', line 27

def self.cop_name = "Lint/OrderedMagicComments"

Instance Method Details

#on_new_investigationObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/shirobai/cop/lint/ordered_magic_comments.rb', line 36

def on_new_investigation
  buffer = processed_source.buffer
  return if buffer.source.empty?

  resolved_offense.each do |encoding_line, other_line|
    range = buffer.line_range(encoding_line)
    add_offense(range) do |corrector|
      range2 = buffer.line_range(other_line)
      corrector.replace(range, range2.source)
      corrector.replace(range2, range.source)
    end
  end
end