Class: ASTTransform::Transformer

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

Instance Method Summary collapse

Constructor Details

#initialize(*transformations, builder: Prism::Translation::Parser::Builder.new) ⇒ Transformer

Constructs a new Transformer instance.

Parameters:

  • transformations (Array<ASTTransform::AbstractTransformation>)

    The transformations to be run.

  • builder (Prism::Translation::Parser::Builder) (defaults to: Prism::Translation::Parser::Builder.new)

    The AST Node builder.



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

def initialize(*transformations, builder: Prism::Translation::Parser::Builder.new)
  @transformations = transformations
  @builder = builder
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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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