Class: Shirobai::Cop::Layout::SpaceBeforeComment

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

Overview

Drop-in Rust reimplementation of Layout/SpaceBeforeComment.

Stock pairs sorted_tokens and flags a comment token that starts exactly where the previous same-line token ends. Byte-side that is "the byte before the comment exists and is not whitespace": tokens are separated by whitespace only, a comment never shares its line with a later token, and =begin docs sit at column 0. Comments come from the shared prism parse (one comment per =begin/=end block, matching parser-gem's single tCOMMENT).

Each offense is the [start, end) range of the comment; the corrector inserts a space before it.

Constant Summary collapse

MSG =
"Put a space before an end-of-line comment."

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



25
# File 'lib/shirobai/cop/layout/space_before_comment.rb', line 25

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

.bundle_args(_config) ⇒ Object

Config-less: contributes nothing to nums / lists.



28
29
30
# File 'lib/shirobai/cop/layout/space_before_comment.rb', line 28

def self.bundle_args(_config)
  []
end

.cop_nameObject



24
# File 'lib/shirobai/cop/layout/space_before_comment.rb', line 24

def self.cop_name = "Layout/SpaceBeforeComment"

Instance Method Details

#on_new_investigationObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/shirobai/cop/layout/space_before_comment.rb', line 32

def on_new_investigation
  buffer = processed_source.buffer
  source = bundle_eligible? ? processed_source.raw_source : buffer.source
  off = SourceOffsets.for(source)
  resolved_offenses.each do |start, fin|
    range = Parser::Source::Range.new(buffer, off[start], off[fin])
    add_offense(range, message: MSG) do |corrector|
      corrector.insert_before(range, " ")
    end
  end
end