Class: ASTTransform::Transformer

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

Instance Method Summary collapse

Constructor Details

#initialize(*transformations) ⇒ Transformer

Constructs a new Transformer instance.

Parameters:



13
14
15
# File 'lib/ast_transform/transformer.rb', line 13

def initialize(*transformations)
  @transformations = transformations
end

Instance Method Details

#build_ast(source, file_path: 'tmp') ⇒ Parser::AST::Node

Builds the AST for the given source.

Parameters:

  • source (String)

    The input source code.

  • file_path (String) (defaults to: 'tmp')

    The file_path. This is important for source mapping in backtraces.

Returns:

  • (Parser::AST::Node)

    The AST.



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

def build_ast(source, file_path: 'tmp')
  buffer = create_buffer(source, file_path)
  parser.parse(buffer)
end

#build_ast_from_file(file_path) ⇒ Parser::AST::Node

Builds the AST for the given file_path.

Parameters:

  • file_path (String)

    The input file path.

Returns:

  • (Parser::AST::Node)

    The AST.



33
34
35
36
# File 'lib/ast_transform/transformer.rb', line 33

def build_ast_from_file(file_path)
  source = File.read(file_path)
  build_ast(source, file_path: file_path)
end

#transform(source) ⇒ String

Transforms the given source.

Parameters:

  • source (String)

    The input source code to be transformed.

Returns:

  • (String)

    The transformed code.



43
44
45
46
47
# File 'lib/ast_transform/transformer.rb', line 43

def transform(source)
  ast = build_ast(source)
  transformed_ast = transform_ast(ast)
  Unparser.unparse(transformed_ast)
end

#transform_ast(ast) ⇒ Parser::AST::Node

Transforms the given ast.

Parameters:

  • ast (Parser::AST::Node)

    The input AST to be transformed.

Returns:

  • (Parser::AST::Node)

    The transformed AST.



85
86
87
88
89
# File 'lib/ast_transform/transformer.rb', line 85

def transform_ast(ast)
  @transformations.inject(ast) do |ast, transformation|
    transformation.run(ast)
  end
end

#transform_file(file_path, transformed_file_path) ⇒ String

Transforms the give file_path.

Parameters:

  • file_path (String)

    The input file to be transformed. This is required for source mapping in backtraces.

  • transformed_file_path (String)

    The file path to the transformed file.

Returns:

  • (String)

    The transformed code.



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

def transform_file(file_path, transformed_file_path)
  source = File.read(file_path)
  transform_file_source(source, file_path, transformed_file_path)
end

#transform_file_source(source, file_path, transformed_file_path) ⇒ String

Transforms the given source in file_path.

SourceMap.

Parameters:

  • source (String)

    The input source code to be transformed.

  • file_path (String)

    The file path for the input source. This is required for source mapping in backtraces.

  • transformed_file_path (String)

    The file path to the transformed filed. This is required to register the

Returns:

  • (String)

    The transformed code.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ast_transform/transformer.rb', line 68

def transform_file_source(source, file_path, transformed_file_path)
  source_ast = build_ast(source, file_path: file_path)
  # At this point, the transformed_ast contains line number mappings for the original +source+.
  transformed_ast = transform_ast(source_ast)

  transformed_source = Unparser.unparse(transformed_ast)

  register_source_map(file_path, transformed_file_path, transformed_ast, transformed_source)

  transformed_source
end