Class: GLRubocop::GLCops::ViewComponentInitializeKeywordArgs

Inherits:
RuboCop::Cop::Cop
  • Object
show all
Defined in:
lib/gl_rubocop/gl_cops/view_component_initialize_keyword_args.rb

Overview

This cop ensures that ViewComponent initialize methods use keyword arguments only.

Good:

def initialize(name:, age:)
def initialize(name:, age: 18)
def initialize(**options)

Bad:

def initialize(name, age)
def initialize(name, age:)

Constant Summary collapse

MSG =
'ViewComponent initialize methods must use keyword arguments only.'.freeze

Instance Method Summary collapse

Instance Method Details

#on_def(node) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/gl_rubocop/gl_cops/view_component_initialize_keyword_args.rb', line 16

def on_def(node)
  return unless node.method_name == :initialize
  return if node.arguments.empty?

  has_positional_args = node.arguments.any? do |arg|
    arg.arg_type? || arg.optarg_type? || arg.restarg_type?
  end

  add_offense(node, message: MSG) if has_positional_args
end