Class: Shirobai::Cop::Layout::SpaceInsideBlockBraces

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

Overview

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

Rust walks the AST once, replicating stock’s ‘on_block` (plus the `numblock` / `itblock` aliases): it skips `do`..`end` blocks and multi-line empty braces, then judges the inside-brace spacing per `EnforcedStyle`, the empty-brace spacing per `EnforcedStyleForEmptyBraces` and the `spacing per `SpaceBeforeBlockParameters`, using the same character rules and range arithmetic as stock (`range_with_surrounding_space`, the multi-line / `]`-ending right-brace cases, `aligned_braces?`).

Each offense comes back as ‘[start, end, message_code]`. The message and the corrector action both follow stock’s ‘offense` method, which derives the action from the live `range.source`: a whitespace range is removed, `{` becomes `{ }`, `{|` becomes `{ |`, otherwise a space is inserted before the range. Because the action is recomputed from the fresh source on every autocorrect pass (the bundle is recomputed per pass), the cop keeps no cross-pass state.

Constant Summary collapse

MESSAGES =
[
  "Space missing inside {.",
  "Space inside { detected.",
  "Space missing inside }.",
  "Space inside } detected.",
  "Space inside empty braces detected.",
  "Space missing inside empty braces.",
  "Space between { and | missing.",
  "Space between { and | detected."
].freeze
STYLES =
{ "space" => 0, "no_space" => 1 }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.autocorrect_incompatible_withObject

Stock declares the same incompatibility: both cops can rewrite the same brace, so RuboCop’s correction loop defers a conflicting pass.



30
31
32
# File 'lib/shirobai/cop/layout/space_inside_block_braces.rb', line 30

def self.autocorrect_incompatible_with
  [RuboCop::Cop::Style::BlockDelimiters]
end

.badgeObject



48
# File 'lib/shirobai/cop/layout/space_inside_block_braces.rb', line 48

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

.bundle_args(config) ⇒ Object

Packed args for the bundled run: ‘[nums, lists]` with `nums = [EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters]` and no lists.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/shirobai/cop/layout/space_inside_block_braces.rb', line 53

def self.bundle_args(config)
  cop_config = config.for_badge(badge)
  [
    [
      STYLES.fetch(cop_config["EnforcedStyle"] || "space"),
      STYLES.fetch(cop_config["EnforcedStyleForEmptyBraces"] || "no_space"),
      cop_config["SpaceBeforeBlockParameters"] == false ? 0 : 1
    ],
    []
  ]
end

.cop_nameObject



47
# File 'lib/shirobai/cop/layout/space_inside_block_braces.rb', line 47

def self.cop_name = "Layout/SpaceInsideBlockBraces"

Instance Method Details

#on_new_investigationObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/shirobai/cop/layout/space_inside_block_braces.rb', line 65

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

  offenses_for_source.each do |start, fin, code|
    range = Parser::Source::Range.new(buffer, off[start], off[fin])
    add_offense(range, message: MESSAGES.fetch(code)) do |corrector|
      # Stock's `offense` corrector, keyed on the live range source.
      case range.source
      when /\s/ then corrector.remove(range)
      when "{}" then corrector.replace(range, "{ }")
      when "{|" then corrector.replace(range, "{ |")
      else           corrector.insert_before(range, " ")
      end
    end
  end
end