Class: SyntaxTree::ClassDeclaration

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

Class represents defining a class using the class keyword.

class Container
end

Classes can have path names as their class name in case it’s being nested under a namespace, as in:

class Namespace::Container
end

Classes can also be defined as a top-level path, in the case that it’s already in a namespace but you want to define it at the top-level instead, as in:

module OtherNamespace
  class ::Namespace::Container
  end
end

All of these declarations can also have an optional superclass reference, as in:

class Child < Parent
end

That superclass can actually be any Ruby expression, it doesn’t necessarily need to be a constant, as in:

class Child < method
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constant:, superclass:, bodystmt:, location:, comments: []) ⇒ ClassDeclaration

Returns a new instance of ClassDeclaration.



3066
3067
3068
3069
3070
3071
3072
# File 'lib/syntax_tree/node.rb', line 3066

def initialize(constant:, superclass:, bodystmt:, location:, comments: [])
  @constant = constant
  @superclass = superclass
  @bodystmt = bodystmt
  @location = location
  @comments = comments
end

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the expressions to execute within the context of the class



3058
3059
3060
# File 'lib/syntax_tree/node.rb', line 3058

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3064
3065
3066
# File 'lib/syntax_tree/node.rb', line 3064

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the class being

defined



3052
3053
3054
# File 'lib/syntax_tree/node.rb', line 3052

def constant
  @constant
end

#locationObject (readonly)

Location

the location of this node



3061
3062
3063
# File 'lib/syntax_tree/node.rb', line 3061

def location
  @location
end

#superclassObject (readonly)

nil | untyped

the optional superclass declaration



3055
3056
3057
# File 'lib/syntax_tree/node.rb', line 3055

def superclass
  @superclass
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



3074
3075
3076
# File 'lib/syntax_tree/node.rb', line 3074

def child_nodes
  [constant, superclass, bodystmt]
end

#deconstruct_keys(keys) ⇒ Object



3080
3081
3082
3083
3084
3085
3086
3087
3088
# File 'lib/syntax_tree/node.rb', line 3080

def deconstruct_keys(keys)
  {
    constant: constant,
    superclass: superclass,
    bodystmt: bodystmt,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
# File 'lib/syntax_tree/node.rb', line 3090

def format(q)
  declaration = -> do
    q.group do
      q.text("class ")
      q.format(constant)

      if superclass
        q.text(" < ")
        q.format(superclass)
      end
    end
  end

  if bodystmt.empty?
    q.group do
      declaration.call
      q.breakable(force: true)
      q.text("end")
    end
  else
    q.group do
      declaration.call

      q.indent do
        q.breakable(force: true)
        q.format(bodystmt)
      end

      q.breakable(force: true)
      q.text("end")
    end
  end
end

#pretty_print(q) ⇒ Object



3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
# File 'lib/syntax_tree/node.rb', line 3124

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("class")

    q.breakable
    q.pp(constant)

    if superclass
      q.breakable
      q.pp(superclass)
    end

    q.breakable
    q.pp(bodystmt)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
# File 'lib/syntax_tree/node.rb', line 3143

def to_json(*opts)
  {
    type: :class,
    constant: constant,
    superclass: superclass,
    bodystmt: bodystmt,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end