Skip to content

Commit 1ea3fe2

Browse files
committed
Fix merl:compile_and_load/1 with comments
Use erl_syntax:revert_forms/1 instead of manually reverting each form to properly handle syntax trees containing comments. The manual approach failed when comment nodes were passed to the compiler. Add test case to verify compile_and_load works with various comment placements in quoted code.
1 parent 8fb9b41 commit 1ea3fe2

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

lib/syntax_tools/src/merl.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ compile(Code, Options) when not is_list(Code)->
429429
_ -> compile([Code], Options)
430430
end;
431431
compile(Code, Options0) when is_list(Options0) ->
432-
Forms = [erl_syntax:revert(F) || F <- Code],
432+
Forms = erl_syntax:revert_forms(Code),
433433
Options = [verbose, report_errors, report_warnings, binary | Options0],
434434
compile:noenv_forms(Forms, Options).
435435

lib/syntax_tools/test/merl_SUITE.erl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,21 @@
2525
-include_lib("eunit/include/eunit.hrl").
2626

2727
%% Test server specific exports
28-
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1,
28+
-export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1,
2929
init_per_group/2,end_per_group/2]).
3030

3131
%% Test cases
3232
-export([merl_smoke_test/1,
33-
transform_parse_error_test/1, otp_15291/1]).
33+
transform_parse_error_test/1, otp_15291/1,
34+
compile_and_load_with_comments/1]).
3435

3536
suite() -> [{ct_hooks,[ts_install_cth]}].
3637

3738
all() ->
3839
[merl_smoke_test,
3940
transform_parse_error_test,
40-
otp_15291].
41+
otp_15291,
42+
compile_and_load_with_comments].
4143

4244
groups() ->
4345
[].
@@ -112,6 +114,18 @@ otp_15291(_Config) ->
112114
{clause,A1,[{var,A1,'_'}],[],[{atom,A1,ok}]} = C1,
113115
ok.
114116

117+
compile_and_load_with_comments(_Config) ->
118+
?assertMatch({ok, _},
119+
merl:compile_and_load(merl:quote(
120+
~"-module(merl_test_module_1)."))),
121+
?assertMatch({ok, _},
122+
merl:compile_and_load(merl:quote(
123+
~"-module(merl_test_module_2). % comment"))),
124+
?assertMatch({ok, _},
125+
merl:compile_and_load(merl:quote(
126+
~"\n-module(merl_test_module_3).\n% comment"))),
127+
ok.
128+
115129
%% utilities
116130

117131
f(Ts) when is_list(Ts) ->

0 commit comments

Comments
 (0)