Class: SyntaxTree::ClassDeclaration

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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.



3272
3273
3274
3275
3276
3277
3278
# File 'lib/syntax_tree.rb', line 3272

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



3264
3265
3266
# File 'lib/syntax_tree.rb', line 3264

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3270
3271
3272
# File 'lib/syntax_tree.rb', line 3270

def comments
  @comments
end

#constantObject (readonly)

ConstPathRef | ConstRef | TopConstRef

the name of the class being

defined



3258
3259
3260
# File 'lib/syntax_tree.rb', line 3258

def constant
  @constant
end

#locationObject (readonly)

Location

the location of this node



3267
3268
3269
# File 'lib/syntax_tree.rb', line 3267

def location
  @location
end

#superclassObject (readonly)

nil | untyped

the optional superclass declaration



3261
3262
3263
# File 'lib/syntax_tree.rb', line 3261

def superclass
  @superclass
end

Instance Method Details

#child_nodesObject



3280
3281
3282
# File 'lib/syntax_tree.rb', line 3280

def child_nodes
  [constant, superclass, bodystmt]
end

#format(q) ⇒ Object



3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
# File 'lib/syntax_tree.rb', line 3284

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



3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
# File 'lib/syntax_tree.rb', line 3318

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



3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
# File 'lib/syntax_tree.rb', line 3337

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