Sha256: cdfa6f0a51af64d3e48aa2a152c1d98b492f58b42e7d18d4dc0af756a38d2d0b

Contents?: true

Size: 1.6 KB

Versions: 151

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

module RuboCop
  module AST
    # A node extension for `const` nodes.
    class ConstNode < Node
      # @return [Node, nil] the node associated with the scope (e.g. cbase, const, ...)
      def namespace
        children[0]
      end

      # @return [Symbol] the demodulized name of the constant: "::Foo::Bar" => :Bar
      def short_name
        children[1]
      end

      # @return [Boolean] if the constant is a Module / Class, according to the standard convention.
      #                   Note: some classes might have uppercase in which case this method
      #                         returns false
      def module_name?
        short_name.match?(/[[:lower:]]/)
      end
      alias class_name? module_name?

      # @return [Boolean] if the constant starts with `::` (aka s(:cbase))
      def absolute?
        return false unless namespace

        each_path.first.cbase_type?
      end

      # @return [Boolean] if the constant does not start with `::` (aka s(:cbase))
      def relative?
        !absolute?
      end

      # Yield nodes for the namespace
      #
      #   For `::Foo::Bar::BAZ` => yields:
      #      s(:cbase), then
      #      s(:const, :Foo), then
      #      s(:const, s(:const, :Foo), :Bar)
      def each_path(&block)
        return to_enum(__method__) unless block

        descendants = []
        last = self
        loop do
          last = last.children.first
          break if last.nil?

          descendants << last
          break unless last.const_type?
        end
        descendants.reverse_each(&block)

        self
      end
    end
  end
end

Version data entries

151 entries across 145 versions & 19 rubygems

Version Path
harbr-0.1.94 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.93 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.91 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.90 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.89 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.88 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.87 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.86 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.85 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.84 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.83 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.82 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.81 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.80 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.79 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.78 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.77 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.76 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.75 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb
harbr-0.1.74 vendor/bundle/ruby/3.2.0/gems/rubocop-ast-1.30.0/lib/rubocop/ast/node/const_node.rb