Class: SyntaxTree::Word

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

Overview

Word represents an element within a special array literal that accepts interpolation.

%W[a#{b}c xyz]

In the example above, there would be two Word nodes within a parent Words node.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parts:, location:, comments: []) ⇒ Word

Returns a new instance of Word.



11595
11596
11597
11598
11599
# File 'lib/syntax_tree/node.rb', line 11595

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11593
11594
11595
# File 'lib/syntax_tree/node.rb', line 11593

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



11590
11591
11592
# File 'lib/syntax_tree/node.rb', line 11590

def location
  @location
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

word



11587
11588
11589
# File 'lib/syntax_tree/node.rb', line 11587

def parts
  @parts
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



11605
11606
11607
# File 'lib/syntax_tree/node.rb', line 11605

def child_nodes
  parts
end

#deconstruct_keys(keys) ⇒ Object



11611
11612
11613
# File 'lib/syntax_tree/node.rb', line 11611

def deconstruct_keys(keys)
  { parts: parts, location: location, comments: comments }
end

#format(q) ⇒ Object



11615
11616
11617
# File 'lib/syntax_tree/node.rb', line 11615

def format(q)
  q.format_each(parts)
end

#match?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


11601
11602
11603
# File 'lib/syntax_tree/node.rb', line 11601

def match?(pattern)
  parts.any? { |part| part.is_a?(TStringContent) && part.match?(pattern) }
end

#pretty_print(q) ⇒ Object



11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
# File 'lib/syntax_tree/node.rb', line 11619

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

    q.breakable
    q.group(2, "(", ")") { q.seplist(parts) { |part| q.pp(part) } }

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

#to_json(*opts) ⇒ Object



11630
11631
11632
11633
11634
# File 'lib/syntax_tree/node.rb', line 11630

def to_json(*opts)
  { type: :word, parts: parts, loc: location, cmts: comments }.to_json(
    *opts
  )
end