diff --git a/doc/parse_trans.md b/doc/parse_trans.md index 8f3e7b8..296b9bb 100644 --- a/doc/parse_trans.md +++ b/doc/parse_trans.md @@ -124,7 +124,7 @@ Performs a transform of Forms using the fun Fun(Form). Reads debug_info from the beam file Beam and returns a string containing the pretty-printed corresponding erlang source code.pp_beam/2 Reads debug_info from the beam file Beam and pretty-prints it as -Erlang source code, storing it in the file Out.pp_src/2Pretty-prints the erlang source code corresponding to Forms into Out.replace_function/4return/2Checks the transformed result for errors and warnings.revert/1Reverts back from Syntax Tools format to Erlang forms.revert_form/1Reverts a single form back from Syntax Tools format to Erlang forms.top/3transform/4 +Erlang source code, storing it in the file Out.pp_src/2Pretty-prints the erlang source code corresponding to Forms into Out.replace_function/4replace_function/5return/2Checks the transformed result for errors and warnings.revert/1Reverts back from Syntax Tools format to Erlang forms.revert_form/1Reverts a single form back from Syntax Tools format to Erlang forms.top/3transform/4 Makes one pass. @@ -486,6 +486,13 @@ Pretty-prints the erlang source code corresponding to Forms into Out `replace_function(F, Arity, NewForm, Forms) -> any()` + + +### replace_function/5 ### + +`replace_function(F, Arity, NewForm, Forms, Opts) -> any()` + + ### return/2 ### diff --git a/src/parse_trans.erl b/src/parse_trans.erl index f8ff97d..f305e80 100644 --- a/src/parse_trans.erl +++ b/src/parse_trans.erl @@ -60,6 +60,7 @@ -export([do_insert_forms/4, replace_function/4, + replace_function/5, export_function/3]). -export([ @@ -351,19 +352,39 @@ top(F, Forms, Options) -> end. replace_function(F, Arity, NewForm, Forms) -> + replace_function(F, Arity, NewForm, Forms, []). + +replace_function(F, Arity, NewForm, Forms, Opts) -> {NewForms, _} = do_transform( fun(function, Form, _Ctxt, Acc) -> case erl_syntax:revert(Form) of - {function, _, F, Arity, _} -> - {NewForm, false, Acc}; + {function, _, F, Arity, _} = RevForm -> + {[], NewForm, with_original_f(RevForm, Opts), + false, Acc}; _ -> {Form, false, Acc} end; (_, Form, _Ctxt, Acc) -> {Form, false, Acc} end, false, Forms, initial_context(Forms, [])), - revert(NewForms). + revert(maybe_export_renamed(NewForms, Arity, Opts)). + +with_original_f({function,_,_,_,_} = Form, Opts) -> + case lists:keyfind(rename_original, 1, Opts) of + {_, NewName} when is_atom(NewName) -> + [setelement(3, Form, NewName)]; + _ -> + [] + end. + +maybe_export_renamed(Forms, Arity, Opts) -> + case lists:keyfind(rename_original, 1, Opts) of + {_, NewName} when is_atom(NewName) -> + export_function(NewName, Arity, Forms); + _ -> + Forms + end. export_function(F, Arity, Forms) -> do_insert_forms(above, [{attribute, 1, export, [{F, Arity}]}], Forms,