Skip to content

Commit e89139b

Browse files
author
José Valim
committed
Only rearrange not in if explicitly opted-in
1 parent ecad849 commit e89139b

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

lib/elixir/lib/code/formatter.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,15 @@ defmodule Code.Formatter do
506506

507507
# not(left in right)
508508
# left not in right
509-
defp quoted_to_algebra({:not, meta, [{:in, _, [left, right]}]}, context, state) do
510-
binary_op_to_algebra(:in, "not in", meta, left, right, context, state)
509+
defp quoted_to_algebra({:not, meta, [{:in, _, [left, right]} = arg]}, context, state) do
510+
%{rename_deprecated_at: since} = state
511+
512+
# TODO: Remove since check on Elixir v2.0 and the OP arrengement is removed.
513+
if meta[:operator] == :"not in" || (since && Version.match?(since, "~> 1.5")) do
514+
binary_op_to_algebra(:in, "not in", meta, left, right, context, state)
515+
else
516+
unary_op_to_algebra(:not, meta, arg, context, state)
517+
end
511518
end
512519

513520
defp quoted_to_algebra({:fn, meta, [_ | _] = clauses}, _context, state) do

lib/elixir/src/elixir_parser.yrl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,13 @@ build_op({_Kind, Location, 'in'}, {UOp, _, [Left]}, Right) when ?rearrange_uop(U
688688
{UOp, meta_from_location(Location), [{'in', meta_from_location(Location), [Left, Right]}]};
689689

690690
build_op({_Kind, Location, 'not in'}, Left, Right) ->
691-
{'not', meta_from_location(Location), [{'in', meta_from_location(Location), [Left, Right]}]};
691+
InMeta = meta_from_location(Location),
692+
NotMeta =
693+
case ?formatter_metadata() of
694+
true -> [{operator, 'not in'} | InMeta];
695+
false -> InMeta
696+
end,
697+
{'not', NotMeta, [{'in', InMeta, [Left, Right]}]};
692698
build_op({_Kind, Location, Op}, Left, Right) ->
693699
{Op, eol_op(Location) ++ meta_from_location(Location), [Left, Right]}.
694700

lib/elixir/test/elixir/code_formatter/operators_test.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,13 @@ defmodule Code.Formatter.OperatorsTest do
121121
end
122122

123123
test "not in" do
124-
assert_format "not(foo in bar)", "foo not in bar"
124+
assert_format "not foo in bar", "not (foo in bar)"
125+
assert_format "not foo in bar", "foo not in bar", rename_deprecated_at: "1.5.0"
126+
127+
assert_format "not(foo in bar)", "not (foo in bar)"
128+
assert_format "not(foo in bar)", "foo not in bar", rename_deprecated_at: "1.5.0"
129+
130+
assert_same "foo not in bar"
125131
assert_same "(not foo) in bar"
126132
assert_same "(!foo) in bar"
127133
end

0 commit comments

Comments
 (0)