Skip to content

Commit a57cd1c

Browse files
committed
Fix crash with sigils inside ignore
For some reason the sigil_suffix token might be missing text property. Fixes #363
1 parent 7483a28 commit a57cd1c

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

src/erlfmt_scan.erl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,17 @@ drop_initial_white_space(Rest) ->
235235

236236
-spec stringify_tokens([erl_scan:token()]) -> string().
237237
stringify_tokens(Tokens) ->
238-
lists:flatmap(fun erl_scan:text/1, Tokens).
238+
lists:flatmap(fun token_text/1, Tokens).
239+
240+
-spec token_text(erl_scan:token()) -> string().
241+
token_text(Token) ->
242+
case erl_scan:text(Token) of
243+
%% sigil_suffix might miss the text field
244+
undefined when is_record(Token, sigil_suffix, 3) ->
245+
"";
246+
Text ->
247+
Text
248+
end.
239249

240250
-spec split_tokens([erl_scan:token()], [erl_scan:token()]) ->
241251
{[token()], [erl_scan:token()], [comment()], [token()]}.

test/erlfmt_SUITE.erl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
snapshot_range_reinjected/1,
101101
snapshot_tripple_string/1,
102102
snapshot_tripple_crash/1,
103+
snapshot_sigil_crash/1,
103104
contains_pragma/1,
104105
insert_pragma/1,
105106
overlong_warning/1,
@@ -184,7 +185,8 @@ groups() ->
184185
]},
185186
{otp_27_snapshot_tests, [parallel], [
186187
snapshot_tripple_string,
187-
snapshot_tripple_crash
188+
snapshot_tripple_crash,
189+
snapshot_sigil_crash
188190
]},
189191
{error_tests, [parallel], [
190192
error_ignore_begin_ignore,
@@ -1115,6 +1117,8 @@ snapshot_tripple_string(Config) -> snapshot_formatted("tripple_string.erl", Conf
11151117

11161118
snapshot_tripple_crash(Config) -> snapshot_same("tripple_crash.erl", Config).
11171119

1120+
snapshot_sigil_crash(Config) -> snapshot_same("sigil_crash.erl", Config).
1121+
11181122
snapshot_same(Module, Config) ->
11191123
Pragma = proplists:get_value(pragma, Config, ignore),
11201124
snapshot_match(Module, Module, Config, [{pragma, Pragma}]).
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-module(sigil).
2+
-export([f/0]).
3+
% erlfmt:ignore
4+
f() -> ~"foo", ~"foo"a.
5+
6+
% erlfmt:ignore
7+
f() -> ~B"foo", ~B"foo"a.
8+
9+
%erlfmt:ignore
10+
f() -> ~|foo|, ~|foo|a.

0 commit comments

Comments
 (0)