From 014733b534332f654b7dadf4ce1f61a22ed88045 Mon Sep 17 00:00:00 2001 From: uohzxela Date: Wed, 2 Aug 2017 19:39:14 +0800 Subject: [PATCH 1/4] Enforce keyword syntax for tuples of size 2 inside list --- lib/ex_format.ex | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/ex_format.ex b/lib/ex_format.ex index 9d74bbf..fcbee4c 100644 --- a/lib/ex_format.ex +++ b/lib/ex_format.ex @@ -428,7 +428,15 @@ defmodule ExFormat do # Tuple containers def to_string({:{}, _, args} = ast, fun, state) do - tuple = "{" <> tuple_to_string(args, fun, state) <> "}" + inside_list? = Map.get(state, :inside_list?) + tuple = + # Enforce keyword syntax for tuples of size 2 inside list + if inside_list? == true and length(args) == 2 do + [k, v] = args + kw_list_to_string([{k, v}], fun, state) + else + "{" <> tuple_to_string(args, fun, state) <> "}" + end fun.(ast, tuple) end @@ -972,6 +980,7 @@ defmodule ExFormat do end defp list_to_string(list, fun, state) do + state = Map.put(state, :inside_list?, true) list_string = Enum.map_join(list, ", ", &to_string(&1, fun, state)) if not fits?(" " <> list_string <> " ") or line_breaks?(list) do list_to_multiline_string(list, fun, state) @@ -988,12 +997,22 @@ defmodule ExFormat do "\n " <> list_string <> ",\n" end - defp kw_list_to_string(list, fun, state) do - list_string = Enum.map_join(list, ", ", fn {key, value} -> - atom_name = case Inspect.Atom.inspect(key) do + defp kw_list_to_string([{key, value}], fun, state) when not is_atom(key) do + atom_name = + case to_string(key, fun, state) do ":" <> rest -> rest other -> other end + atom_name <> ": " <> to_string(value, fn(_ast, string) -> string end, state) + end + + defp kw_list_to_string(list, fun, state) do + list_string = Enum.map_join(list, ", ", fn {key, value} -> + atom_name = + case Inspect.Atom.inspect(key) do + ":" <> rest -> rest + other -> other + end atom_name <> ": " <> to_string(value, fn(_ast, string) -> string end, state) end) if not fits?(" " <> list_string <> " ") or line_breaks?(list) do From bdb8b0bffa0398305c1cb80160c14c15912835bd Mon Sep 17 00:00:00 2001 From: uohzxela Date: Thu, 3 Aug 2017 07:37:21 +0800 Subject: [PATCH 2/4] Add tests for keyword sugar in lists --- test/unit/indentation_test.exs | 7 +++---- test/unit/kw_list_syntax_test.exs | 16 ++++++++++++++++ test/unit/kw_list_test.exs | 2 +- test/unit/list_test.exs | 8 ++++---- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/test/unit/indentation_test.exs b/test/unit/indentation_test.exs index 912c38f..a8a023d 100644 --- a/test/unit/indentation_test.exs +++ b/test/unit/indentation_test.exs @@ -67,10 +67,9 @@ defmodule ExFormat.Unit.IndentationTest do [{:prefix_newline, get_prefix_newline(curr_lineno-1, prev_lineno)}] ++ curr_ctx """ good = """ - [{:prev, prev_lineno}] ++ - [{:prefix_comments, get_prefix_comments(curr_lineno - 1, prev_lineno)}] ++ - [{:prefix_newline, get_prefix_newline(curr_lineno - 1, prev_lineno)}] ++ - curr_ctx + [prev: prev_lineno] ++ + [prefix_comments: get_prefix_comments(curr_lineno - 1, prev_lineno)] ++ + [prefix_newline: get_prefix_newline(curr_lineno - 1, prev_lineno)] ++ curr_ctx """ assert_format_string(bad, good) end diff --git a/test/unit/kw_list_syntax_test.exs b/test/unit/kw_list_syntax_test.exs index e7edf45..0878410 100644 --- a/test/unit/kw_list_syntax_test.exs +++ b/test/unit/kw_list_syntax_test.exs @@ -44,4 +44,20 @@ defmodule ExFormat.Unit.KwListSyntaxTest do """ end end + + describe "keyword syntax for tuples inside list" do + test "maintain keyword sugar for tuples of size 2" do + assert_format_string("[:foo, bar: :baz]\n") + assert_format_string("[bar: :baz]\n") + end + + test "enforce keyword sugar for tuples of size 2" do + assert_format_string("[{:bar, :baz}]", "[bar: :baz]\n") + assert_format_string("[:foo, {:bar, :baz}]", "[:foo, bar: :baz]\n") + end + + test "do not enforce keyword sugar for tuples of size more than 2" do + assert_format_string("[{:foo, :bar, :baz}]\n") + end + end end diff --git a/test/unit/kw_list_test.exs b/test/unit/kw_list_test.exs index 47e54a7..d80cb2f 100644 --- a/test/unit/kw_list_test.exs +++ b/test/unit/kw_list_test.exs @@ -147,7 +147,7 @@ defmodule ExFormat.Unit.KeywordListTest do package: package(), aliases: aliases(), test_coverage: [tool: ExCoveralls], - preferred_cli_env: [{:"test.local", :test}, {:coveralls, :test}, {:"coveralls.travis", :test}], + preferred_cli_env: ["test.local": :test, coveralls: :test, "coveralls.travis": :test], name: "Mssqlex", source_url: "https://github.com/findmypast-oss/mssqlex", docs: [main: "readme", extras: ["README.md"]], diff --git a/test/unit/list_test.exs b/test/unit/list_test.exs index c855c50..e58bfe4 100644 --- a/test/unit/list_test.exs +++ b/test/unit/list_test.exs @@ -92,13 +92,13 @@ defmodule ExFormat.Unit.ListTest do defp deps() do [ # Web server - {:cowboy, "~> 1.0"}, + cowboy: "~> 1.0", # Web framework - {:phoenix, "~> 1.3.0-rc"}, + phoenix: "~> 1.3.0-rc", # XML parser helper - {:sweet_xml, "~> 0.6"}, + sweet_xml: "~> 0.6", # Statsd metrics sink client - {:statix, "~> 1.0"}, + statix: "~> 1.0", ] end """ From 55b6410e4a05bd525df61a785a7a6c16ed06f9d8 Mon Sep 17 00:00:00 2001 From: uohzxela Date: Thu, 3 Aug 2017 23:08:01 +0800 Subject: [PATCH 3/4] Add inside_list? as default value, use cleaner syntax --- lib/ex_format.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ex_format.ex b/lib/ex_format.ex index fcbee4c..3c83c40 100644 --- a/lib/ex_format.ex +++ b/lib/ex_format.ex @@ -75,6 +75,7 @@ defmodule ExFormat do state = %{ parenless_calls: MapSet.new(@parenless_calls), parenless_zero_arity?: false, + inside_list?: false, in_spec: nil, } for {line, i} <- Enum.with_index(lines) do @@ -428,10 +429,9 @@ defmodule ExFormat do # Tuple containers def to_string({:{}, _, args} = ast, fun, state) do - inside_list? = Map.get(state, :inside_list?) tuple = # Enforce keyword syntax for tuples of size 2 inside list - if inside_list? == true and length(args) == 2 do + if state.inside_list? and length(args) == 2 do [k, v] = args kw_list_to_string([{k, v}], fun, state) else @@ -980,7 +980,7 @@ defmodule ExFormat do end defp list_to_string(list, fun, state) do - state = Map.put(state, :inside_list?, true) + state = %{state | inside_list?: true} list_string = Enum.map_join(list, ", ", &to_string(&1, fun, state)) if not fits?(" " <> list_string <> " ") or line_breaks?(list) do list_to_multiline_string(list, fun, state) From c95735bf7ccdfdeb3bc8c3699a74a6f7d3baf58a Mon Sep 17 00:00:00 2001 From: uohzxela Date: Fri, 4 Aug 2017 06:31:08 +0800 Subject: [PATCH 4/4] Rename from inside_list? to in_list? for consistency --- lib/ex_format.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ex_format.ex b/lib/ex_format.ex index 3c83c40..5bcc811 100644 --- a/lib/ex_format.ex +++ b/lib/ex_format.ex @@ -75,7 +75,7 @@ defmodule ExFormat do state = %{ parenless_calls: MapSet.new(@parenless_calls), parenless_zero_arity?: false, - inside_list?: false, + in_list?: false, in_spec: nil, } for {line, i} <- Enum.with_index(lines) do @@ -431,7 +431,7 @@ defmodule ExFormat do def to_string({:{}, _, args} = ast, fun, state) do tuple = # Enforce keyword syntax for tuples of size 2 inside list - if state.inside_list? and length(args) == 2 do + if state.in_list? and length(args) == 2 do [k, v] = args kw_list_to_string([{k, v}], fun, state) else @@ -980,7 +980,7 @@ defmodule ExFormat do end defp list_to_string(list, fun, state) do - state = %{state | inside_list?: true} + state = %{state | in_list?: true} list_string = Enum.map_join(list, ", ", &to_string(&1, fun, state)) if not fits?(" " <> list_string <> " ") or line_breaks?(list) do list_to_multiline_string(list, fun, state)