Class: Shirobai::Cop::Style::StabbyLambdaParentheses

Inherits:
RuboCop::Cop::Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector
Includes:
RuboCop::Cop::ConfigurableEnforcedStyle
Defined in:
lib/shirobai/cop/style/stabby_lambda_parentheses.rb

Overview

Drop-in Rust reimplementation of ‘Style/StabbyLambdaParentheses`.

Rust walks the AST once and emits one record per stabby-lambda ‘LambdaNode` whose `parameters` is a `BlockParametersNode` with a non-nil inner `parameters` AND whose `()` presence disagrees with the configured `EnforcedStyle`. This mirrors stock’s ‘on_send` guard (`lambda_literal? && block_node.arguments?`) plus the `redundant_parentheses?` / `missing_parentheses?` style comparison.

For each emitted offense the wrapper reproduces stock’s ‘add_offense` on the `arguments` source range (`args.loc.expression`) and, depending on the style, wraps with `(` `)` (require_parentheses, missing) or replaces `(` with `”` + removes `)` (require_no_parentheses, redundant). The behaviour is purely config-driven, so this cop is always bundle-eligible.

Constant Summary collapse

STYLE_TO_U8 =
{
  require_parentheses: 0,
  require_no_parentheses: 1
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



32
# File 'lib/shirobai/cop/style/stabby_lambda_parentheses.rb', line 32

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

.bundle_args(config) ⇒ Object

Packed args for the bundled run: ‘[style]`.



35
36
37
38
39
40
41
# File 'lib/shirobai/cop/style/stabby_lambda_parentheses.rb', line 35

def self.bundle_args(config)
  own = config.for_badge(badge)
  style = STYLE_TO_U8.fetch(
    (own["EnforcedStyle"] || "require_parentheses").to_sym, 0
  )
  [style]
end

.cop_nameObject



31
# File 'lib/shirobai/cop/style/stabby_lambda_parentheses.rb', line 31

def self.cop_name = "Style/StabbyLambdaParentheses"

Instance Method Details

#on_new_investigationObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/shirobai/cop/style/stabby_lambda_parentheses.rb', line 43

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

  records_for_source.each do |start, fin, paren_open_start, paren_open_end,
                               paren_close_start, paren_close_end, message|
    args_range = Parser::Source::Range.new(buffer, off[start], off[fin])
    add_offense(args_range, message: message) do |corrector|
      if style == :require_parentheses
        corrector.wrap(args_range, "(", ")")
      else
        open_range = Parser::Source::Range.new(
          buffer, off[paren_open_start], off[paren_open_end]
        )
        close_range = Parser::Source::Range.new(
          buffer, off[paren_close_start], off[paren_close_end]
        )
        corrector.replace(open_range, "")
        corrector.remove(close_range)
      end
    end
  end
end