Sha256: 431f20e71e4a0be4d631cc1b258aa8bf5b69bdfcef6ce00a1a86ce9308c768ef

Contents?: true

Size: 1.96 KB

Versions: 11

Compression:

Stored size: 1.96 KB

Contents

# frozen_string_literal: true

module RuboCop
  module AST
    # A node extension for `masgn` nodes.
    # This will be used in place of a plain node when the builder constructs
    # the AST, making its methods available to all assignment nodes within RuboCop.
    class MasgnNode < Node
      # @return [MlhsNode] the `mlhs` node
      def lhs
        # The first child is a `mlhs` node
        node_parts[0]
      end

      # @return [Array<Node>] the assignment nodes of the multiple assignment
      def assignments
        lhs.assignments
      end

      # @return [Array<Symbol>] names of all the variables being assigned
      def names
        assignments.map do |assignment|
          if assignment.send_type? || assignment.indexasgn_type?
            assignment.method_name
          else
            assignment.name
          end
        end
      end

      # The RHS (right hand side) of the multiple assignment. This returns
      # the nodes as parsed: either a single node if the RHS has a single value,
      # or an `array` node containing multiple nodes.
      #
      # NOTE: Due to how parsing works, `expression` will return the same for
      # `a, b = x, y` and `a, b = [x, y]`.
      #
      # @return [Node] the right hand side of a multiple assignment.
      def expression
        node_parts[1]
      end
      alias rhs expression

      # In contrast to `expression`, `values` always returns a Ruby array
      # containing all the nodes being assigned on the RHS.
      #
      # Literal arrays are considered a singular value; but unlike `expression`,
      # implied `array` nodes from assigning multiple values on the RHS are treated
      # as separate.
      #
      # @return [Array<Node>] individual values being assigned on the RHS of the multiple assignment
      def values
        multiple_rhs? ? expression.children : [expression]
      end

      private

      def multiple_rhs?
        expression.array_type? && !expression.bracketed?
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-ast-1.38.0/lib/rubocop/ast/node/masgn_node.rb
rubocop-ast-1.38.0 lib/rubocop/ast/node/masgn_node.rb
rubocop-ast-1.37.0 lib/rubocop/ast/node/masgn_node.rb
rubocop-ast-1.36.2 lib/rubocop/ast/node/masgn_node.rb
rubocop-ast-1.36.1 lib/rubocop/ast/node/masgn_node.rb
rubocop-ast-1.36.0 lib/rubocop/ast/node/masgn_node.rb
rubocop-ast-1.35.0 lib/rubocop/ast/node/masgn_node.rb
rubocop-ast-1.34.1 lib/rubocop/ast/node/masgn_node.rb
rubocop-ast-1.34.0 lib/rubocop/ast/node/masgn_node.rb
rubocop-ast-1.33.1 lib/rubocop/ast/node/masgn_node.rb
rubocop-ast-1.33.0 lib/rubocop/ast/node/masgn_node.rb