Class: Shirobai::Cop::Style::EmptyLiteral

Inherits:
RuboCop::Cop::Base
  • Object
show all
Extended by:
RuboCop::Cop::AutoCorrector
Includes:
RuboCop::Cop::RangeHelp, RuboCop::Cop::StringLiteralsHelp
Defined in:
lib/shirobai/cop/style/empty_literal.rb

Overview

Drop-in Rust-accelerated reimplementation of Style/EmptyLiteral.

Style/EmptyLiteral is almost entirely AST work (const-receiver Array.new / Hash.new / String.new / Array[] matchers plus the parenless-argument-wrapping autocorrect), and that part is stock's, run verbatim on the real parser AST — so detection and -A bytes match stock exactly, including the block / numblock exclusion asymmetry between Array and Hash. The ONE place it touches the parser-gem token stream (the "toucher" cost) is the String.new branch's frozen_strings?, which reaches FrozenStringLiteral#leading_comment_lines -> processed_source.tokens. This wrapper replaces ONLY that method with a token-free computation: Rust does the same leading-comment scan (shared with Lint/DuplicateMagicComment / Style/FrozenStringLiteralComment) from the prism parse, and the config half runs in Ruby. Everything else is stock, copied verbatim from vendor/rubocop/lib/rubocop/cop/style/empty_literal.rb.

Constant Summary collapse

ARR_MSG =
"Use array literal `[]` instead of `%<current>s`."
HASH_MSG =
"Use hash literal `{}` instead of `%<current>s`."
STR_MSG =
"Use string literal `%<prefer>s` instead of `String.new`."
RESTRICT_ON_SEND =
%i[new [] Array Hash].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.badgeObject



34
# File 'lib/shirobai/cop/style/empty_literal.rb', line 34

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

.bundle_args(_config) ⇒ Object

Not a bundled cop (it dispatches on_send normally); the only Rust call is the token-free frozen_strings? helper. Kept for the 4+1 convention.



38
39
40
# File 'lib/shirobai/cop/style/empty_literal.rb', line 38

def self.bundle_args(_config)
  []
end

.cop_nameObject



33
# File 'lib/shirobai/cop/style/empty_literal.rb', line 33

def self.cop_name = "Style/EmptyLiteral"

Instance Method Details

#array_node(node) ⇒ Object



43
# File 'lib/shirobai/cop/style/empty_literal.rb', line 43

def_node_matcher :array_node, "(send (const {nil? cbase} :Array) :new (array)?)"

#array_with_block(node) ⇒ Object



52
53
54
55
56
57
# File 'lib/shirobai/cop/style/empty_literal.rb', line 52

def_node_matcher :array_with_block, <<~PATTERN
  {
    (block (send (const {nil? cbase} :Array) :new) args _)
    ({numblock itblock} (send (const {nil? cbase} :Array) :new) ...)
  }
PATTERN

#array_with_index(node) ⇒ Object



68
69
70
71
72
73
# File 'lib/shirobai/cop/style/empty_literal.rb', line 68

def_node_matcher :array_with_index, <<~PATTERN
  {
    (send (const {nil? cbase} :Array) :[])
    (send nil? :Array (array))
  }
PATTERN

#hash_node(node) ⇒ Object



46
# File 'lib/shirobai/cop/style/empty_literal.rb', line 46

def_node_matcher :hash_node, "(send (const {nil? cbase} :Hash) :new)"

#hash_with_block(node) ⇒ Object



60
61
62
63
64
65
# File 'lib/shirobai/cop/style/empty_literal.rb', line 60

def_node_matcher :hash_with_block, <<~PATTERN
  {
    (block (send (const {nil? cbase} :Hash) :new) args _)
    ({numblock itblock} (send (const {nil? cbase} :Hash) :new) ...)
  }
PATTERN

#hash_with_index(node) ⇒ Object



76
77
78
79
80
81
# File 'lib/shirobai/cop/style/empty_literal.rb', line 76

def_node_matcher :hash_with_index, <<~PATTERN
  {
    (send (const {nil? cbase} :Hash) :[])
    (send nil? :Hash (array))
  }
PATTERN

#on_send(node) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/shirobai/cop/style/empty_literal.rb', line 83

def on_send(node)
  return unless (message = offense_message(node))

  add_offense(node, message: message) do |corrector|
    corrector.replace(replacement_range(node), correction(node))
  end
end

#str_node(node) ⇒ Object



49
# File 'lib/shirobai/cop/style/empty_literal.rb', line 49

def_node_matcher :str_node, "(send (const {nil? cbase} :String) :new)"