Skip to content

Commit eb20acf

Browse files
committed
Add some tests for the Form module
1 parent 672cc5c commit eb20acf

File tree

6 files changed

+165
-38
lines changed

6 files changed

+165
-38
lines changed
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
defmodule Example do
2-
def start(v, _) do
3-
case v do
4-
:normal ->
5-
"yolo"
6-
_ ->
7-
"yolo too"
8-
end
2+
def start(_, _) do
3+
-1
94
end
105
end

lib/elixir_script/next/passes/translate/form.ex

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule ElixirScript.Translate.Form do
22
alias ESTree.Tools.Builder, as: J
3-
alias ElixirScript.Translate.Forms.{Bitstring, Match, Try, For, Struct, Receive, Remote, Pattern}
3+
alias ElixirScript.Translate.Forms.{Bitstring, Match, Try, For, Receive, Remote, Pattern}
44
alias ElixirScript.Translate.Functions.{Erlang, Lists, Maps}
55
alias ElixirScript.Translator.Identifier
66
alias ElixirScript.Translate.Clause
@@ -34,8 +34,7 @@ defmodule ElixirScript.Translate.Form do
3434

3535
def compile(form, state) when is_atom(form) do
3636
ast = if ElixirScript.Translate.Module.is_elixir_module(form) do
37-
members = if form == Elixir, do: ["Elixir"], else: ["Elixir"] ++ Module.split(form)
38-
J.identifier(Enum.join(members, "_"))
37+
Remote.process_module_name(form, state)
3938
else
4039
J.call_expression(
4140
J.member_expression(
@@ -80,8 +79,16 @@ defmodule ElixirScript.Translate.Form do
8079
Match.compile(match, state)
8180
end
8281

83-
def compile({:%, _, [_, _]} = ast, state) do
84-
Struct.compile(ast, state)
82+
def compile({:%, _, [module, params]}, state) do
83+
ast = J.call_expression(
84+
J.member_expression(
85+
Remote.process_module_name(module),
86+
J.identifier("__struct__")
87+
),
88+
[Form.compile!(params, state)]
89+
)
90+
91+
{ ast, state }
8592
end
8693

8794
def compile({:for, _, _} = ast, state) do

lib/elixir_script/next/passes/translate/forms/remote.ex

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
defmodule ElixirScript.Translate.Forms.Remote do
2+
@moduledoc false
3+
24
alias ESTree.Tools.Builder, as: J
35
alias ElixirScript.Translate.Form
46
alias ElixirScript.Translator.Identifier
@@ -16,6 +18,13 @@ defmodule ElixirScript.Translate.Forms.Remote do
1618
:file
1719
]
1820

21+
@doc """
22+
Compiles functions into JavaScript AST.
23+
These are not actual function calls, but
24+
the function identifiers themselves. Also
25+
includes function heads for converting some
26+
erlang functions into JavaScript functions.
27+
"""
1928
def compile({:., _, [:erlang, :+]}, state) do
2029
ast = erlang_compat_function("erlang", "plus")
2130
{ ast, state }
@@ -131,20 +140,24 @@ defmodule ElixirScript.Translate.Forms.Remote do
131140
{ast, state}
132141
end
133142

134-
defp process_module_name(module, state) when is_atom(module) do
143+
def process_module_name(module, state) when is_atom(module) do
135144
cond do
136145
ElixirScript.Translate.Module.is_js_module(module, state) ->
137146
members = tl(Module.split(module))
138147
Identifier.make_namespace_members(members)
139148
ElixirScript.Translate.Module.is_elixir_module(module) ->
140-
members = ["Elixir"] ++ Module.split(module)
141-
J.identifier(Enum.join(members, "_"))
149+
members = ["Elixir"] ++ Module.split(module) ++ ["__load"]
150+
151+
J.call_expression(
152+
Identifier.make_namespace_members(members),
153+
[J.identifier("Elixir")]
154+
)
142155
true ->
143156
ElixirScript.Translator.Identifier.make_identifier(module)
144157
end
145158
end
146159

147-
defp process_module_name(module, state) do
160+
def process_module_name(module, state) do
148161
Form.compile!(module, state)
149162
end
150163

lib/elixir_script/next/passes/translate/forms/struct.ex

Lines changed: 0 additions & 22 deletions
This file was deleted.

lib/elixir_script/next/passes/translate/module.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ defmodule ElixirScript.Translate.Module do
1414
end
1515

1616
def compile(module, info, pid) do
17+
if Atom.to_string(module) == "Elixir.Example" do
18+
IO.inspect info
19+
end
1720
%{
1821
attributes: attrs,
1922
compile_opts: _compile_opts,
@@ -86,6 +89,10 @@ defmodule ElixirScript.Translate.Module do
8689
J.object_expression(exports)
8790
end
8891

92+
@doc """
93+
Determins if the given atom
94+
is an Elixir function
95+
"""
8996
def is_elixir_module(Elixir) do
9097
true
9198
end
@@ -105,6 +112,12 @@ defmodule ElixirScript.Translate.Module do
105112
false
106113
end
107114

115+
@doc """
116+
Determines is given function is a JS module.
117+
A JS module is either one that begins with "JS"
118+
or is a module defined from the js_modules compiler
119+
opt
120+
"""
108121
def is_js_module(module, state) do
109122
cond do
110123
module in ModuleState.get_javascript_modules(state.pid) ->
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
defmodule ElixirScript.Translate.Forms.Test do
2+
use ExUnit.Case
3+
alias ElixirScript.Translate.Form
4+
alias ElixirScript.Translator.Identifier
5+
alias ESTree.Tools.Builder, as: J
6+
7+
8+
setup_all do
9+
{:ok, pid} = ElixirScript.State.start_link(%{})
10+
11+
state = %{
12+
pid: pid
13+
}
14+
15+
[state: state]
16+
end
17+
18+
test "integer" do
19+
ast = 1
20+
state = %{}
21+
22+
{js_ast, _} = Form.compile(ast, state)
23+
assert js_ast == J.literal(1)
24+
end
25+
26+
test "boolean" do
27+
ast = true
28+
state = %{}
29+
30+
{js_ast, _} = Form.compile(ast, state)
31+
assert js_ast == J.literal(true)
32+
end
33+
34+
test "float" do
35+
ast = 1.0
36+
state = %{}
37+
38+
{js_ast, _} = Form.compile(ast, state)
39+
assert js_ast == J.literal(1.0)
40+
end
41+
42+
test "binary" do
43+
ast = "hello"
44+
state = %{}
45+
46+
{js_ast, _} = Form.compile(ast, state)
47+
assert js_ast == J.literal("hello")
48+
end
49+
50+
test "atom" do
51+
ast = :tiger
52+
state = %{}
53+
54+
{js_ast, _} = Form.compile(ast, state)
55+
assert js_ast == J.call_expression(
56+
J.member_expression(
57+
J.identifier("Symbol"),
58+
J.identifier("for")
59+
),
60+
[J.literal(:tiger)]
61+
)
62+
end
63+
64+
test "module", %{state: state} do
65+
ast = IO
66+
67+
{js_ast, _} = Form.compile(ast, state)
68+
assert js_ast == J.call_expression(
69+
Identifier.make_namespace_members(["Elixir", "IO", "__load"]),
70+
[J.identifier("Elixir")]
71+
)
72+
end
73+
74+
test "tuple" do
75+
ast = {1, 2}
76+
state = %{}
77+
78+
{js_ast, _} = Form.compile(ast, state)
79+
assert js_ast == J.call_expression(
80+
J.member_expression(
81+
J.member_expression(
82+
J.identifier("Bootstrap"),
83+
J.identifier("Core")
84+
),
85+
J.identifier("Tuple")
86+
),
87+
[J.literal(1), J.literal(2)]
88+
)
89+
end
90+
91+
test "list" do
92+
ast = [1, 2]
93+
state = %{}
94+
95+
{js_ast, _} = Form.compile(ast, state)
96+
assert js_ast == J.array_expression([J.literal(1), J.literal(2)])
97+
end
98+
99+
test "super" do
100+
ast = {:super, [], [1]}
101+
state = %{function: {:my_function, nil}}
102+
103+
{js_ast, _} = Form.compile(ast, state)
104+
assert js_ast == J.call_expression(
105+
J.identifier("my_function"),
106+
[J.literal(1)]
107+
)
108+
end
109+
110+
test "local function" do
111+
ast = {:my_function, [], [1]}
112+
state = %{}
113+
114+
{js_ast, _} = Form.compile(ast, state)
115+
assert js_ast == J.call_expression(
116+
J.identifier("my_function"),
117+
[J.literal(1)]
118+
)
119+
end
120+
121+
end

0 commit comments

Comments
 (0)