Class: Shirobai::Cop::Layout::SpaceAroundKeyword

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

Overview

Drop-in Rust reimplementation of ‘Layout/SpaceAroundKeyword`.

Rust walks the AST and reproduces every stock ‘on_xxx` callback (if / unless / while / until / case / for / block / begin / super / yield / return / break / next / defined? / and / or / not / rescue / ensure / when / in / BEGIN / END), checking the space before and/or after each keyword range with the same character rules as stock (the `space_before_missing?` / `space_after_missing?` accept sets, the ACCEPT_LEFT_PAREN / ACCEPT_LEFT_SQUARE_BRACKET / namespace / safe- navigation exceptions, and the `preceded_by_operator?` ancestor walk).

It returns, per offense, the keyword range plus a ‘before` flag: `true` is a missing space before the keyword (autocorrect inserts a space before the range, `MSG_BEFORE`), `false` a missing space after it (inserts a space after, `MSG_AFTER`).

The cop has no configuration, so it is always bundle eligible; the offenses come from the per-file bundled run (‘Shirobai::Dispatch`). The autocorrect re-passes re-investigate a fresh `ProcessedSource`, which recomputes the bundle from scratch, so this cop keeps no cross-pass state.

Constant Summary collapse

MSG_BEFORE =
"Space before keyword `%<range>s` is missing."
MSG_AFTER =
"Space after keyword `%<range>s` is missing."

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



35
# File 'lib/shirobai/cop/layout/space_around_keyword.rb', line 35

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

.bundle_args(_config) ⇒ Object

No packed configuration: the cop contributes nothing to the bundle’s ‘(nums, lists)` wire format. Kept for symmetry with the other wrappers.



39
40
41
# File 'lib/shirobai/cop/layout/space_around_keyword.rb', line 39

def self.bundle_args(_config)
  []
end

.cop_nameObject



34
# File 'lib/shirobai/cop/layout/space_around_keyword.rb', line 34

def self.cop_name = "Layout/SpaceAroundKeyword"

Instance Method Details

#on_new_investigationObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/shirobai/cop/layout/space_around_keyword.rb', line 43

def on_new_investigation
  buffer = processed_source.buffer
  off = SourceOffsets.for(processed_source.raw_source)

  offenses_for_source.each do |start, fin, before|
    range = Parser::Source::Range.new(buffer, off[start], off[fin])
    if before
      add_offense(range, message: format(MSG_BEFORE, range: range.source)) do |corrector|
        corrector.insert_before(range, " ")
      end
    else
      add_offense(range, message: format(MSG_AFTER, range: range.source)) do |corrector|
        corrector.insert_after(range, " ")
      end
    end
  end
end