Sha256: 0a74e846213273a6181edcffaca5a6396fe54eada20f48d95ee27740f2c9ffdf

Contents?: true

Size: 1.15 KB

Versions: 34

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module InternalAffairs
      # Checks that node destructuring is using the node extensions.
      #
      # @example Using splat expansion
      #
      #   # bad
      #   _receiver, method_name, _arguments = send_node.children
      #
      #   # bad
      #   _receiver, method_name, _arguments = *send_node
      #
      #   # good
      #   method_name = send_node.method_name
      class NodeDestructuring < Base
        MSG = 'Use the methods provided with the node extensions instead ' \
              'of manually destructuring nodes.'

        def_node_matcher :node_variable?, <<~PATTERN
          {(lvar [#node_suffix? _]) (send nil? [#node_suffix? _])}
        PATTERN

        def_node_matcher :node_destructuring?, <<~PATTERN
          {(masgn (mlhs ...) {(send #node_variable? :children) (array (splat #node_variable?))})}
        PATTERN

        def on_masgn(node)
          node_destructuring?(node) do
            add_offense(node)
          end
        end

        private

        def node_suffix?(method_name)
          method_name.to_s.end_with?('node')
        end
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/node_destructuring.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/node_destructuring.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/node_destructuring.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/node_destructuring.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/node_destructuring.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/internal_affairs/node_destructuring.rb
rubocop-1.10.0 lib/rubocop/cop/internal_affairs/node_destructuring.rb
rubocop-1.9.1 lib/rubocop/cop/internal_affairs/node_destructuring.rb
rubocop-1.9.0 lib/rubocop/cop/internal_affairs/node_destructuring.rb
rubocop-1.8.1 lib/rubocop/cop/internal_affairs/node_destructuring.rb
rubocop-1.8.0 lib/rubocop/cop/internal_affairs/node_destructuring.rb
rubocop-1.7.0 lib/rubocop/cop/internal_affairs/node_destructuring.rb
rubocop-1.6.1 lib/rubocop/cop/internal_affairs/node_destructuring.rb
rubocop-1.6.0 lib/rubocop/cop/internal_affairs/node_destructuring.rb
rubocop-1.5.2 lib/rubocop/cop/internal_affairs/node_destructuring.rb
rubocop-1.5.1 lib/rubocop/cop/internal_affairs/node_destructuring.rb
rubocop-1.5.0 lib/rubocop/cop/internal_affairs/node_destructuring.rb
rubocop-1.4.2 lib/rubocop/cop/internal_affairs/node_destructuring.rb
rubocop-1.4.1 lib/rubocop/cop/internal_affairs/node_destructuring.rb
rubocop-1.4.0 lib/rubocop/cop/internal_affairs/node_destructuring.rb