Repository: 100phlecs/tailwind_formatter (elixir)

Issue:
Formatter inserts incorrect `}`
The first time I tried the formatter in one of our projects it worked. The second time I got an FunctionClauseError `no function clause matching in Phoenix.LiveView.HTMLFormatter.to_tree/4`.

It turns out that the first run of the tailwind formatter formats the following

```elixir
    <div class={"h-6 " <> if @active, do: "bg-white", else: "bg-red"}></div>
```

to

```elixir
    <div class={"h-6"} <> if @active, do: "bg-white", else: "bg-red"}></div>
```

And this is invalid code because of the inserted `}` :-) 


Pull request description:
handle nested interpolated strings
Related to #18 

Patch:
diff --git a/lib/tailwind_formatter/defaults.ex b/lib/tailwind_formatter/defaults.ex
index 9cb68b4..6cb88fd 100644
--- a/lib/tailwind_formatter/defaults.ex
+++ b/lib/tailwind_formatter/defaults.ex
@@ -1,7 +1,7 @@
 defmodule TailwindFormatter.Defaults do
   @moduledoc false
-  @class_pattern ~r/(?:class\s*(?:=|:)\s*)(({)|('|"))(.|\s)*?(?(2)}|("|'))/i
-  @inline_func_pattern ~r/#\{([^}]+)\}*/i
+  @class_pattern ~r/(?:class\s*(?:=|:)\s*)((\{)|('|"))(.|\s)*?(?(2)\}|("|'))/i
+  @inline_func_pattern ~r/#\{((?:(?:(?!(#\{|\})).)|(?R))*)\}/i
   @dynamic_classes ~r/[[:alnum:]-]*#\{([^}]+)\}*/i
   @invalid_input ~r/[^_a-zA-Z0-9\$\s\-:\[\]\/\.\#]+/
 


Test patch:
diff --git a/test/tailwind_formatter_test.exs b/test/tailwind_formatter_test.exs
index 47c4519..f8edc63 100644
--- a/test/tailwind_formatter_test.exs
+++ b/test/tailwind_formatter_test.exs
@@ -300,6 +300,18 @@ defmodule TailwindFormatterTest do
     assert_formatter_output(input, expected)
   end
 
+  test "can handle multiple interpolated strings" do
+    input = ~S"""
+    <.img src={~p"/images/#{"card-#{@card.brand}.svg"}"} />
+    """
+
+    expected = ~S"""
+    <.img src={~p"/images/#{"card-#{@card.brand}.svg"}"} />
+    """
+
+    assert_formatter_output(input, expected)
+  end
+
   describe "aborts on bad input" do
     test "missing final quote" do
       input = ~S"""
@@ -340,6 +352,22 @@ defmodule TailwindFormatterTest do
         href="#"></a>
       """
 
+      output = ~S"""
+      <a class={"#{if false, do: "bg-white" text-sm potato sm:lowercase uppercase"
+        id="testing
+        href="#"></a>
+      """
+
+      assert_formatter_output(input, output)
+    end
+
+    test "invalid elixir fn" do
+      input = ~S"""
+      <a class={"#{if false, do: } text-sm potato sm:lowercase uppercase"
+        id="testing
+        href="#"></a>
+      """
+
       assert_formatter_raise(input)
     end