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

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Word.



11142
11143
11144
11145
11146
# File 'lib/syntax_tree/node.rb', line 11142

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



11140
11141
11142
# File 'lib/syntax_tree/node.rb', line 11140

def comments
  @comments
end

#partsObject (readonly)

Array[ StringEmbExpr | StringDVar | TStringContent ]

the parts of the

word



11137
11138
11139
# File 'lib/syntax_tree/node.rb', line 11137

def parts
  @parts
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



11152
11153
11154
# File 'lib/syntax_tree/node.rb', line 11152

def child_nodes
  parts
end

#deconstruct_keys(keys) ⇒ Object



11158
11159
11160
# File 'lib/syntax_tree/node.rb', line 11158

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

#format(q) ⇒ Object



11162
11163
11164
# File 'lib/syntax_tree/node.rb', line 11162

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

#match?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


11148
11149
11150
# File 'lib/syntax_tree/node.rb', line 11148

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

#pretty_print(q) ⇒ Object



11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
# File 'lib/syntax_tree/node.rb', line 11166

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



11177
11178
11179
11180
11181
# File 'lib/syntax_tree/node.rb', line 11177

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