Skip to content

Commit 128218e

Browse files
committed
Add string interpolation
1 parent 20f893d commit 128218e

File tree

5 files changed

+62
-8
lines changed

5 files changed

+62
-8
lines changed

lib/elixir_script/passes/find_used_functions.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ defmodule ElixirScript.FindUsedFunctions do
9898
walk(tail, state)
9999
end
100100

101-
defp walk({:::, _, _}, _) do
102-
nil
101+
defp walk({:::, _, [target, _type]}, state) do
102+
walk(target, state)
103103
end
104104

105105
defp walk(form, state) when is_list(form) do

lib/elixir_script/passes/find_used_modules.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ defmodule ElixirScript.FindUsedModules do
9595
walk(tail, state)
9696
end
9797

98-
defp walk({:::, _, _}, _) do
99-
nil
98+
defp walk({:::, _, [target, _type]}, state) do
99+
walk(target, state)
100100
end
101101

102102
defp walk(form, state) when is_list(form) do

lib/elixir_script/passes/translate/form.ex

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,23 @@ defmodule ElixirScript.Translate.Form do
9090
ElixirScript.Translate.Forms.Map.compile(map, state)
9191
end
9292

93-
def compile({:<<>>, _, _} = bitstring, state) do
94-
Bitstring.compile(bitstring, state)
93+
def compile({:<<>>, _, elements} = bitstring, state) do
94+
is_interpolated_string = Enum.all?(elements, fn(x) ->
95+
case x do
96+
b when is_binary(b) ->
97+
true
98+
{:::, _, [_target, {:binary, _, _}]} ->
99+
true
100+
_ ->
101+
false
102+
end
103+
end)
104+
105+
if is_interpolated_string do
106+
Bitstring.make_interpolated_string(elements, state)
107+
else
108+
Bitstring.compile(bitstring, state)
109+
end
95110
end
96111

97112
def compile({:=, _, [_, _]} = match, state) do

lib/elixir_script/passes/translate/forms/bitstring.ex

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,19 @@ defmodule ElixirScript.Translate.Forms.Bitstring do
129129
end
130130
end)
131131

132-
{ do_make_interpolated_string(tl(translated_elements), hd(translated_elements)) }
132+
result = case translated_elements do
133+
[] ->
134+
JS.literal('')
135+
[element] ->
136+
do_make_interpolated_string([], hd(translated_elements))
137+
elements ->
138+
do_make_interpolated_string(tl(elements), hd(elements))
139+
end
140+
141+
{result, state}
133142
end
134143

135-
defp do_make_interpolated_string([], ast, _) do
144+
defp do_make_interpolated_string([], ast) do
136145
ast
137146
end
138147

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
defmodule ElixirScript.Translate.Forms.Bitstring.Test do
2+
use ExUnit.Case
3+
alias ElixirScript.Translate.Form
4+
alias ESTree.Tools.Builder, as: J
5+
6+
setup_all do
7+
{:ok, pid} = ElixirScript.State.start_link(%{})
8+
9+
state = %{
10+
pid: pid,
11+
vars: %{}
12+
}
13+
14+
[state: state]
15+
end
16+
17+
test "string interpolation", %{state: state} do
18+
ast = {:<<>>, [line: 5],
19+
[{:::, [], ["Hello, ", {:binary, [], []}]},
20+
{:::, [line: 5],
21+
[{{:., [line: 5], [String.Chars, :to_string]}, [line: 5],
22+
[{:name, [line: 5], nil}]}, {:binary, [], []}]}]}
23+
24+
{js_ast, _} = Form.compile(ast, state)
25+
assert js_ast.type == "BinaryExpression"
26+
assert js_ast.left == J.literal("Hello, ")
27+
assert js_ast.right.type == "CallExpression"
28+
end
29+
30+
end

0 commit comments

Comments
 (0)