Class: SyntaxTree::StringEmbExpr

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

Overview

StringEmbExpr represents interpolated content. It can be contained within a couple of different parent nodes, including regular expressions, strings, and dynamic symbols.

"string #{expression}"

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(statements:, location:, comments: []) ⇒ StringEmbExpr

Returns a new instance of StringEmbExpr.



8376
8377
8378
8379
8380
# File 'lib/syntax_tree/node.rb', line 8376

def initialize(statements:, location:, comments: [])
  @statements = statements
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



8374
8375
8376
# File 'lib/syntax_tree/node.rb', line 8374

def comments
  @comments
end

#statementsObject (readonly)

Statements

the expressions to be interpolated



8371
8372
8373
# File 'lib/syntax_tree/node.rb', line 8371

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



8382
8383
8384
# File 'lib/syntax_tree/node.rb', line 8382

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

#child_nodesObject Also known as: deconstruct



8386
8387
8388
# File 'lib/syntax_tree/node.rb', line 8386

def child_nodes
  [statements]
end

#deconstruct_keys(_keys) ⇒ Object



8392
8393
8394
# File 'lib/syntax_tree/node.rb', line 8392

def deconstruct_keys(_keys)
  { statements: statements, location: location, comments: comments }
end

#format(q) ⇒ Object



8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
# File 'lib/syntax_tree/node.rb', line 8396

def format(q)
  if location.start_line == location.end_line
    # If the contents of this embedded expression were originally on the
    # same line in the source, then we're going to leave them in place and
    # assume that's the way the developer wanted this expression
    # represented.
    q.remove_breaks(q.group(0, '#{', "}") { q.format(statements) })
  else
    q.group do
      q.text('#{')
      q.indent do
        q.breakable("")
        q.format(statements)
      end
      q.breakable("")
      q.text("}")
    end
  end
end