Class: RuboCop::Cop::Grape::PreferNamespace

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/grape/prefer_namespace.rb

Overview

Prevent usage of ‘namespace` aliases

# bad:

group :my_group do ... end
resource :my_resource do ... end
resources :my_resources do ... end
segment :my_segment do ... end

# good:

namespace :my_namespace do ... end

Constant Summary collapse

MSG =
'Prefer using `namespace` over its aliases.'
NAMESPACE_ALIASES =
%i[resource resources group segment].freeze
RESTRICT_ON_SEND =
NAMESPACE_ALIASES

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/grape/prefer_namespace.rb', line 35

def on_send(node)
  return unless using_alias_on_api? node
  # Check if use block
  return unless node.block_node&.children&.last

  add_offense(node) do |corrector|
    corrector.replace(node, node.source.gsub(/^#{namespace_alias(node)}/, 'namespace'))
  end
end