Skip to content
Permalink
Browse files
Rename module name in type specs
Fixes #206.
  • Loading branch information
eproxus committed Feb 17, 2021
1 parent cb8f2f6 commit ff07804ad6f444d37bb8a3d3fe8c9eb1779d2ceb
Showing 4 changed files with 24 additions and 12 deletions.
@@ -7,6 +7,10 @@ The format is based on [Keep a Changelog], and this project adheres to

## [Unreleased]

### Fixed

- Module references in typespecs are not included when renaming modules [\#206](https://github.com/eproxus/meck/issues/206)

## [0.9.0] - 2020-06-25

### Added
@@ -29,7 +29,7 @@
-export([compile_and_load_forms/2]).
-export([compile_options/1]).
-export([enable_on_load/2]).
-export([rename_module/2]).
-export([rename_module/3]).

%% Types
-type erlang_form() :: term().
@@ -98,16 +98,9 @@ enable_on_load(Forms, false) ->
enable_on_load(Forms, _) ->
Forms.

-spec rename_module(erlang_form(), module()) -> erlang_form().
rename_module([{attribute, Line, module, OldAttribute}|T], NewName) ->
case OldAttribute of
{_OldName, Variables} ->
[{attribute, Line, module, {NewName, Variables}}|T];
_OldName ->
[{attribute, Line, module, NewName}|T]
end;
rename_module([H|T], NewName) ->
[H|rename_module(T, NewName)].
-spec rename_module(erlang_form(), module(), module()) -> erlang_form().
rename_module(Forms, Old, New) ->
lists:map(fun(F) -> rename_module_in_form(F, Old, New) end, Forms).

%%=============================================================================
%% Internal functions
@@ -139,3 +132,17 @@ filter_options (Options) ->
(_) -> true
end, Options)
end.

rename_module_in_form({attribute, Line, AttrName, AttrData}, Old, New) ->
{attribute, Line, AttrName,
rename_module_in_attribute(AttrName, AttrData, Old, New)
};
rename_module_in_form(Form, _Old, _New) ->
Form.

rename_module_in_attribute(module, Old, Old, New) ->
New;
rename_module_in_attribute(spec, {{Old, Fun, Arity}, Spec}, Old, New) ->
{{New, Fun, Arity}, Spec};
rename_module_in_attribute(_AttrName, AttrData, _Old, _New) ->
AttrData.
@@ -389,7 +389,7 @@ backup_original(Mod, Passthrough, NoPassCover, EnableOnLoad) ->
Forms = meck_code:enable_on_load(Forms0, EnableOnLoad),
NewName = meck_util:original_name(Mod),
CompileOpts = [debug_info | meck_code:compile_options(meck_code:beam_file(Mod))],
Renamed = meck_code:rename_module(Forms, NewName),
Renamed = meck_code:rename_module(Forms, Mod, NewName),
Binary = meck_code:compile_and_load_forms(Renamed, CompileOpts),

%% At this point we care about `Binary' if and only if we want
@@ -3,6 +3,7 @@
-deprecated([a/0]).
-export([a/0, b/0, c/2]).

-spec ?MODULE:a() -> a | b.
a() -> a.
b() -> b.

0 comments on commit ff07804

Please sign in to comment.