Class: ASTTransform::SourceMap

Inherits:
Object
  • Object
show all
Defined in:
lib/ast_transform/source_map.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_file_path, transformed_file_path, source_ranges_ast, transformed_ranges_ast) ⇒ SourceMap

Constructs a new SourceMap instance.

Note: source_ranges_ast and transformed_ranges_ast must be equivalent ASTs.

code.

Parameters:

  • source_file_path (String)

    The path to the source file.

  • transformed_file_path (String)

    The path to the transformed file.

  • source_ranges_ast (Parser::AST::Node)

    A transformed AST that contains the source code ranges.

  • transformed_ranges_ast (Parser::AST::Node)

    A transformed AST that contains the ranges for the executed



44
45
46
47
48
49
50
51
52
53
# File 'lib/ast_transform/source_map.rb', line 44

def initialize(source_file_path, transformed_file_path, source_ranges_ast, transformed_ranges_ast)
  @source_file_path = source_file_path
  @transformed_file_path = transformed_file_path
  @source_ranges_ast = source_ranges_ast
  @transformed_ranges_ast = transformed_ranges_ast

  @lines = Hash.new { |hash, key| hash[key] = [] }
  extract_source_map_data(@transformed_ranges_ast, [])
  @source_map = build_source_map.freeze
end

Instance Attribute Details

#source_file_pathObject (readonly)

Returns the value of attribute source_file_path.



55
56
57
# File 'lib/ast_transform/source_map.rb', line 55

def source_file_path
  @source_file_path
end

#source_mapObject (readonly)

Returns the value of attribute source_map.



55
56
57
# File 'lib/ast_transform/source_map.rb', line 55

def source_map
  @source_map
end

#transformed_file_pathObject (readonly)

Returns the value of attribute transformed_file_path.



55
56
57
# File 'lib/ast_transform/source_map.rb', line 55

def transformed_file_path
  @transformed_file_path
end

Class Method Details

.for_file_path(file_path) ⇒ SourceMap|nil

Retrieves the SourceMap for the given file_path.

Parameters:

  • file_path (String)

    The transformed file path.

Returns:

  • (SourceMap|nil)

    The associated source map.



24
25
26
# File 'lib/ast_transform/source_map.rb', line 24

def for_file_path(file_path)
  source_maps[file_path]
end

.register_source_map(source_map) ⇒ void

This method returns an undefined value.

Registers the given SourceMap.

Parameters:

  • source_map (SourceMap)

    The source map to be registered.



12
13
14
15
16
17
# File 'lib/ast_transform/source_map.rb', line 12

def register_source_map(source_map)
  source_maps[source_map.transformed_file_path] = source_map
  source_maps[source_map.source_file_path] = source_map

  nil
end

Instance Method Details

#line(line_number) ⇒ Integer|nil

Retrieves the mapped line number for the given line_number.

Parameters:

  • line_number (Integer)

    The line number in the executed code to be mapped to the source.

Returns:

  • (Integer|nil)

    The mapped line number, otherwise nil if not found.



62
63
64
# File 'lib/ast_transform/source_map.rb', line 62

def line(line_number)
  @source_map[line_number]
end

#line_countInteger

Retrieves the line count for the executed code.

Returns:

  • (Integer)

    The line count.



69
70
71
# File 'lib/ast_transform/source_map.rb', line 69

def line_count
  @transformed_ranges_ast&.loc&.expression&.last_line || 0
end