Class: Prism::ClassNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
ext/prism/api_node.c

Overview

Represents a class declaration involving the ‘class` keyword.

class Foo end
^^^^^^^^^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locals, class_keyword_loc, constant_path, inheritance_operator_loc, superclass, body, end_keyword_loc, name, location) ⇒ ClassNode

def initialize: (locals: Array, class_keyword_loc: Location, constant_path: Node, inheritance_operator_loc: Location?, superclass: Node?, body: Node?, end_keyword_loc: Location, name: Symbol, location: Location) -> void



3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
# File 'lib/prism/node.rb', line 3227

def initialize(locals, class_keyword_loc, constant_path, inheritance_operator_loc, superclass, body, end_keyword_loc, name, location)
  @locals = locals
  @class_keyword_loc = class_keyword_loc
  @constant_path = constant_path
  @inheritance_operator_loc = inheritance_operator_loc
  @superclass = superclass
  @body = body
  @end_keyword_loc = end_keyword_loc
  @name = name
  @location = location
end

Instance Attribute Details

#bodyObject (readonly)

attr_reader body: Node?



3218
3219
3220
# File 'lib/prism/node.rb', line 3218

def body
  @body
end

#class_keyword_locObject (readonly)

attr_reader class_keyword_loc: Location



3206
3207
3208
# File 'lib/prism/node.rb', line 3206

def class_keyword_loc
  @class_keyword_loc
end

#constant_pathObject (readonly)

attr_reader constant_path: Node



3209
3210
3211
# File 'lib/prism/node.rb', line 3209

def constant_path
  @constant_path
end

#end_keyword_locObject (readonly)

attr_reader end_keyword_loc: Location



3221
3222
3223
# File 'lib/prism/node.rb', line 3221

def end_keyword_loc
  @end_keyword_loc
end

#inheritance_operator_locObject (readonly)

attr_reader inheritance_operator_loc: Location?



3212
3213
3214
# File 'lib/prism/node.rb', line 3212

def inheritance_operator_loc
  @inheritance_operator_loc
end

#localsObject (readonly)

attr_reader locals: Array



3203
3204
3205
# File 'lib/prism/node.rb', line 3203

def locals
  @locals
end

#nameObject (readonly)

attr_reader name: Symbol



3224
3225
3226
# File 'lib/prism/node.rb', line 3224

def name
  @name
end

#superclassObject (readonly)

attr_reader superclass: Node?



3215
3216
3217
# File 'lib/prism/node.rb', line 3215

def superclass
  @superclass
end

Class Method Details

.typeObject

Similar to #type, this method returns a symbol that you can use for splitting on the type of the node without having to do a long === chain. Note that like #type, it will still be slower than using == for a single class, but should be faster in a case statement or an array comparison.

def self.type: () -> Symbol



3350
3351
3352
# File 'lib/prism/node.rb', line 3350

def self.type
  :class_node
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



3240
3241
3242
# File 'lib/prism/node.rb', line 3240

def accept(visitor)
  visitor.visit_class_node(self)
end

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



3245
3246
3247
# File 'lib/prism/node.rb', line 3245

def child_nodes
  [constant_path, superclass, body]
end

#class_keywordObject

def class_keyword: () -> String



3287
3288
3289
# File 'lib/prism/node.rb', line 3287

def class_keyword
  class_keyword_loc.slice
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



3259
3260
3261
# File 'lib/prism/node.rb', line 3259

def comment_targets
  [class_keyword_loc, constant_path, *inheritance_operator_loc, *superclass, *body, end_keyword_loc]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



3250
3251
3252
3253
3254
3255
3256
# File 'lib/prism/node.rb', line 3250

def compact_child_nodes
  compact = []
  compact << constant_path
  compact << superclass if superclass
  compact << body if body
  compact
end

#copy(**params) ⇒ Object

def copy: (**params) -> ClassNode



3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
# File 'lib/prism/node.rb', line 3264

def copy(**params)
  ClassNode.new(
    params.fetch(:locals) { locals },
    params.fetch(:class_keyword_loc) { class_keyword_loc },
    params.fetch(:constant_path) { constant_path },
    params.fetch(:inheritance_operator_loc) { inheritance_operator_loc },
    params.fetch(:superclass) { superclass },
    params.fetch(:body) { body },
    params.fetch(:end_keyword_loc) { end_keyword_loc },
    params.fetch(:name) { name },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



3282
3283
3284
# File 'lib/prism/node.rb', line 3282

def deconstruct_keys(keys)
  { locals: locals, class_keyword_loc: class_keyword_loc, constant_path: constant_path, inheritance_operator_loc: inheritance_operator_loc, superclass: superclass, body: body, end_keyword_loc: end_keyword_loc, name: name, location: location }
end

#end_keywordObject

def end_keyword: () -> String



3297
3298
3299
# File 'lib/prism/node.rb', line 3297

def end_keyword
  end_keyword_loc.slice
end

#inheritance_operatorObject

def inheritance_operator: () -> String?



3292
3293
3294
# File 'lib/prism/node.rb', line 3292

def inheritance_operator
  inheritance_operator_loc&.slice
end

#inspect(inspector = NodeInspector.new) ⇒ Object

def inspect(inspector: NodeInspector) -> String



3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
# File 'lib/prism/node.rb', line 3302

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── locals: #{locals.inspect}\n"
  inspector << "├── class_keyword_loc: #{inspector.location(class_keyword_loc)}\n"
  inspector << "├── constant_path:\n"
  inspector << inspector.child_node(constant_path, "")
  inspector << "├── inheritance_operator_loc: #{inspector.location(inheritance_operator_loc)}\n"
  if (superclass = self.superclass).nil?
    inspector << "├── superclass: ∅\n"
  else
    inspector << "├── superclass:\n"
    inspector << superclass.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  if (body = self.body).nil?
    inspector << "├── body: ∅\n"
  else
    inspector << "├── body:\n"
    inspector << body.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── end_keyword_loc: #{inspector.location(end_keyword_loc)}\n"
  inspector << "└── name: #{name.inspect}\n"
  inspector.to_str
end

#typeObject

Sometimes you want to check an instance of a node against a list of classes to see what kind of behavior to perform. Usually this is done by calling ‘[cls1, cls2].include?(node.class)` or putting the node into a case statement and doing `case node; when cls1; when cls2; end`. Both of these approaches are relatively slow because of the constant lookups, method calls, and/or array allocations.

Instead, you can call #type, which will return to you a symbol that you can use for comparison. This is faster than the other approaches because it uses a single integer comparison, but also because if you’re on CRuby you can take advantage of the fact that case statements with all symbol keys will use a jump table.

def type: () -> Symbol



3340
3341
3342
# File 'lib/prism/node.rb', line 3340

def type
  :class_node
end