Skip to content

Commit

Permalink
dont run charlist transforms on higher versions
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Nov 27, 2023
1 parent bf47ef9 commit 1968bf1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 10 additions & 8 deletions lib/style/single_node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ defmodule Styler.Style.SingleNode do

def run({node, meta}, ctx), do: {:cont, {style(node), meta}, ctx}

# Our use of the `literal_encoder` option of `Code.string_to_quoted_with_comments!/2` creates
# invalid charlists literal AST nodes from `'foo'`. this rewrites them to use the `~c` sigil
# 'foo' => ~c"foo".
defp style({:__block__, meta, [chars]} = node) when is_list(chars) do
if meta[:delimiter] == "'" do
{:sigil_c, Keyword.put(meta, :delimiter, "\""), [{:<<>>, [line: meta[:line]], [List.to_string(chars)]}, []]}
else
node
if Version.match?(System.version(), "< 1.15.0-dev") do
# Our use of the `literal_encoder` option of `Code.string_to_quoted_with_comments!/2` creates
# invalid charlists literal AST nodes from `'foo'`. this rewrites them to use the `~c` sigil
# 'foo' => ~c"foo".
defp style({:__block__, meta, [chars]} = node) when is_list(chars) do
if meta[:delimiter] == "'" do
{:sigil_c, Keyword.put(meta, :delimiter, "\""), [{:<<>>, [line: meta[:line]], [List.to_string(chars)]}, []]}
else
node
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/style/single_node_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
defmodule Styler.Style.SingleNodeTest do
use Styler.StyleCase, async: true

# elixir's formatter >= 1.15 does this for us.
test "charlist literals: rewrites single quote charlists to ~c" do
assert_style("'foo'", ~s|~c"foo"|)
assert_style(~s|'"'|, ~s|~c"\\""|)
# elixir's formatter >= 1.15 does this for us.
assert_style("''", ~s|~c""|)
end

Expand Down

0 comments on commit 1968bf1

Please sign in to comment.