Class: TreeHaver::Backends::Prism::Tree Private

Inherits:
TreeHaver::Base::Tree show all
Defined in:
lib/tree_haver/backends/prism.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Prism tree wrapper

Wraps a Prism::ParseResult to provide tree-sitter-compatible API.

Instance Attribute Summary collapse

Attributes inherited from TreeHaver::Base::Tree

#inner_tree, #lines, #source

Instance Method Summary collapse

Methods inherited from TreeHaver::Base::Tree

#edit, #has_error?, #inspect

Constructor Details

#initialize(parse_result, source) ⇒ Tree

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Tree.



235
236
237
238
# File 'lib/tree_haver/backends/prism.rb', line 235

def initialize(parse_result, source)
  super(parse_result, source: source)
  @parse_result = parse_result
end

Instance Attribute Details

#parse_result::Prism::ParseResult (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the underlying Prism parse result.

Returns:

  • (::Prism::ParseResult)

    the underlying Prism parse result



233
234
235
# File 'lib/tree_haver/backends/prism.rb', line 233

def parse_result
  @parse_result
end

Instance Method Details

#commentsArray<Comment>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get comments from the parse

Returns:



271
272
273
274
275
276
277
278
# File 'lib/tree_haver/backends/prism.rb', line 271

def comments
  @comments ||= begin
    hint_map = comment_hint_map
    @parse_result.comments.map do |comment|
      Comment.new(comment, source: source, attachment_hint: hint_map[comment.object_id])
    end
  end
end

#data_loc::Prism::Location?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get data locations (END section)

Returns:

  • (::Prism::Location, nil)


290
291
292
# File 'lib/tree_haver/backends/prism.rb', line 290

def data_loc
  @parse_result.data_loc
end

#errorsArray<::Prism::ParseError>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get parse errors

Returns:

  • (Array<::Prism::ParseError>)


257
258
259
# File 'lib/tree_haver/backends/prism.rb', line 257

def errors
  @parse_result.errors
end

#has_errors?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if the parse had errors

Returns:

  • (Boolean)


250
251
252
# File 'lib/tree_haver/backends/prism.rb', line 250

def has_errors?
  @parse_result.failure?
end

#magic_commentsArray<::Prism::MagicComment>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get magic comments (e.g., frozen_string_literal)

Returns:

  • (Array<::Prism::MagicComment>)


283
284
285
# File 'lib/tree_haver/backends/prism.rb', line 283

def magic_comments
  @parse_result.magic_comments
end

#root_nodeNode

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the root node of the parse tree

Returns:

  • (Node)

    wrapped root node



243
244
245
# File 'lib/tree_haver/backends/prism.rb', line 243

def root_node
  Node.new(@parse_result.value, source)
end

#warningsArray<::Prism::ParseWarning>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get parse warnings

Returns:

  • (Array<::Prism::ParseWarning>)


264
265
266
# File 'lib/tree_haver/backends/prism.rb', line 264

def warnings
  @parse_result.warnings
end