Sha256: 3c066475c7a7fe9078e2de1ffedfdb7e1a04a1fc7968cf9390ac097939a264fd

Contents?: true

Size: 1.92 KB

Versions: 54

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module RuboCop
  module AST
    # A node extension for `pair` nodes. This will be used in place of a plain
    # node when the builder constructs the AST, making its methods available
    # to all `pair` nodes within RuboCop.
    class PairNode < Node
      include HashElementNode

      HASH_ROCKET = '=>'
      SPACED_HASH_ROCKET = ' => '
      COLON = ':'
      SPACED_COLON = ': '

      # Checks whether the `pair` uses a hash rocket delimiter.
      #
      # @return [Boolean] whether this `pair` uses a hash rocket delimiter
      def hash_rocket?
        loc.operator.is?(HASH_ROCKET)
      end

      # Checks whether the `pair` uses a colon delimiter.
      #
      # @return [Boolean] whether this `pair` uses a colon delimiter
      def colon?
        loc.operator.is?(COLON)
      end

      # Returns the delimiter of the `pair` as a string. Returns `=>` for a
      # colon delimited `pair` and `:` for a hash rocket delimited `pair`.
      #
      # @param [Boolean] with_spacing whether to include spacing
      # @return [String] the delimiter of the `pair`
      def delimiter(with_spacing = false)
        if with_spacing
          hash_rocket? ? SPACED_HASH_ROCKET : SPACED_COLON
        else
          hash_rocket? ? HASH_ROCKET : COLON
        end
      end

      # Returns the inverse delimiter of the `pair` as a string.
      #
      # @param [Boolean] with_spacing whether to include spacing
      # @return [String] the inverse delimiter of the `pair`
      def inverse_delimiter(with_spacing = false)
        if with_spacing
          hash_rocket? ? SPACED_COLON : SPACED_HASH_ROCKET
        else
          hash_rocket? ? COLON : HASH_ROCKET
        end
      end

      # Checks whether the value starts on its own line.
      #
      # @return [Boolean] whether the value in the `pair` starts its own line
      def value_on_new_line?
        key.loc.line != value.loc.line
      end
    end
  end
end

Version data entries

54 entries across 35 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/ast/node/pair_node.rb
rubocop-ast-0.2.0 lib/rubocop/ast/node/pair_node.rb
rubocop-ast-0.1.0 lib/rubocop/ast/node/pair_node.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-ast-0.0.3/lib/rubocop/ast/node/pair_node.rb
rubocop-ast-0.0.3 lib/rubocop/ast/node/pair_node.rb
rubocop-ast-0.0.2 lib/rubocop/ast/node/pair_node.rb
rubocop-ast-0.0.1 lib/rubocop/ast/node/pair_node.rb
rubocop-0.83.0 lib/rubocop/ast/node/pair_node.rb
rubocop-0.82.0 lib/rubocop/ast/node/pair_node.rb
rubocop-0.81.0 lib/rubocop/ast/node/pair_node.rb
rubocop-0.80.1 lib/rubocop/ast/node/pair_node.rb
rubocop-0.80.0 lib/rubocop/ast/node/pair_node.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/ast/node/pair_node.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/ast/node/pair_node.rb
rubocop-0.79.0 lib/rubocop/ast/node/pair_node.rb
rubocop-0.78.0 lib/rubocop/ast/node/pair_node.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/ast/node/pair_node.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/ast/node/pair_node.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/ast/node/pair_node.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/ast/node/pair_node.rb