Class: ASTTransform::Transformer
- Inherits:
-
Object
- Object
- ASTTransform::Transformer
- Defined in:
- lib/ast_transform/transformer.rb
Instance Method Summary collapse
-
#build_ast(source, file_path: 'tmp') ⇒ Parser::AST::Node
Builds the AST for the given
source. -
#build_ast_from_file(file_path) ⇒ Parser::AST::Node
Builds the AST for the given
file_path. -
#initialize(*transformations, builder: Parser::Builders::Default.new) ⇒ Transformer
constructor
Constructs a new Transformer instance.
-
#transform(source) ⇒ String
Transforms the given
source. -
#transform_ast(ast) ⇒ Parser::AST::Node
Transforms the given
ast. -
#transform_file(file_path, transformed_file_path) ⇒ String
Transforms the give
file_path. -
#transform_file_source(source, file_path, transformed_file_path) ⇒ String
Transforms the given
sourceinfile_path.
Constructor Details
#initialize(*transformations, builder: Parser::Builders::Default.new) ⇒ Transformer
Constructs a new Transformer instance.
12 13 14 15 |
# File 'lib/ast_transform/transformer.rb', line 12 def initialize(*transformations, builder: Parser::Builders::Default.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.
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.
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.
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.
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.
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.
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 |