Class: Shirobai::Cop::Layout::SpaceInsideReferenceBrackets

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

Overview

Drop-in Rust reimplementation of Layout/SpaceInsideReferenceBrackets.

Rust checks the bracket pair of every index reference (CallNode [] / []= plus prism's Index*Write / IndexTarget assignment forms, which legacy parser folds into send :[] / send :[]=), reproducing stock's empty-brackets handling (before the multiline guard), the legacy node extent for multiline?, and the [ \t] space tests.

The result is [offenses, node_ops] (same shape as SpaceInsideArrayLiteralBrackets):

  • each offense is [start, end, message_code, node, suppress]; suppress mirrors stock's autocorrect_with_disable_uncorrectable? && !start_ok early return on the right-bracket offense;
  • node_ops[node] is the node's corrector program (SpaceCorrector reduced to remove / insert-after / insert-before calls), applied on the node's first offense like stock's ignore_node grouping.

Constant Summary collapse

MESSAGES =
[
  "Use space inside reference brackets.",
  "Do not use space inside reference brackets.",
  "Use one space inside empty reference brackets.",
  "Do not use space inside empty reference brackets."
].freeze
STYLES =
{ "no_space" => 0, "space" => 1 }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



40
# File 'lib/shirobai/cop/layout/space_inside_reference_brackets.rb', line 40

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

.bundle_args(config) ⇒ Object

Packed args for the bundled run: [nums, lists] with nums = [EnforcedStyle, EnforcedStyleForEmptyBrackets == 'space'].



44
45
46
47
48
49
50
51
52
53
# File 'lib/shirobai/cop/layout/space_inside_reference_brackets.rb', line 44

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

.cop_nameObject



39
# File 'lib/shirobai/cop/layout/space_inside_reference_brackets.rb', line 39

def self.cop_name = "Layout/SpaceInsideReferenceBrackets"

Instance Method Details

#on_new_investigationObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/shirobai/cop/layout/space_inside_reference_brackets.rb', line 55

def on_new_investigation
  buffer = processed_source.buffer
  source = bundle_eligible? ? processed_source.raw_source : buffer.source
  off = SourceOffsets.for(source)
  offenses, node_ops = result_for_source
  drop_suppressed = autocorrect_with_disable_uncorrectable?
  corrected = {}

  offenses.each do |start, fin, code, node, suppress|
    next if suppress && drop_suppressed

    range = Parser::Source::Range.new(buffer, off[start], off[fin])
    add_offense(range, message: MESSAGES.fetch(code)) do |corrector|
      unless corrected[node]
        apply_ops(corrector, buffer, off, node_ops.fetch(node))
      end
      corrected[node] = true
    end
  end
end