Class: SimpleCov::Formatter::AIFormatter::ASTResolver::SemanticNode

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/simplecov-ai/ast_resolver/semantic_node.rb

Overview

An immutable value object housing the bounds and identity of one structural entity (root scope, module, class or method) derived from traversing the AST.

Constant Summary collapse

ROOT_NAME =

The name Ruby itself gives the top-level execution context of a script.

T.let('main', String)
ROOT_TYPE =

Type label of the synthetic node that spans a whole file.

T.let('Root Script Scope', String)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, type:, start_line:, end_line:, method_name: nil) ⇒ SemanticNode

Returns a new instance of SemanticNode.



33
34
35
36
37
38
39
# File 'lib/simplecov-ai/ast_resolver/semantic_node.rb', line 33

def initialize(name:, type:, start_line:, end_line:, method_name: nil)
  @name = name
  @type = type
  @start_line = start_line
  @end_line = end_line
  @method_name = method_name
end

Instance Attribute Details

#end_lineObject (readonly)

Returns the value of attribute end_line.



22
23
24
# File 'lib/simplecov-ai/ast_resolver/semantic_node.rb', line 22

def end_line
  @end_line
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



27
28
29
# File 'lib/simplecov-ai/ast_resolver/semantic_node.rb', line 27

def method_name
  @method_name
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/simplecov-ai/ast_resolver/semantic_node.rb', line 19

def name
  @name
end

#start_lineObject (readonly)

Returns the value of attribute start_line.



22
23
24
# File 'lib/simplecov-ai/ast_resolver/semantic_node.rb', line 22

def start_line
  @start_line
end

#typeObject (readonly)

Returns the value of attribute type.



19
20
21
# File 'lib/simplecov-ai/ast_resolver/semantic_node.rb', line 19

def type
  @type
end

Class Method Details

.root(line_count) ⇒ Object



48
49
50
# File 'lib/simplecov-ai/ast_resolver/semantic_node.rb', line 48

def self.root(line_count)
  new(name: ROOT_NAME, type: ROOT_TYPE, start_line: 1, end_line: [line_count, 1].max)
end

.spanning(ast_node, name:, type:, method_name: nil) ⇒ Object



63
64
65
66
67
# File 'lib/simplecov-ai/ast_resolver/semantic_node.rb', line 63

def self.spanning(ast_node, name:, type:, method_name: nil)
  location = T.cast(ast_node.loc, Parser::Source::Map)
  new(name: name, type: type, start_line: T.cast(location.line, Integer),
      end_line: T.cast(location.last_line, Integer), method_name: method_name)
end

Instance Method Details

#line_rangeObject



71
72
73
# File 'lib/simplecov-ai/ast_resolver/semantic_node.rb', line 71

def line_range
  start_line..end_line
end