Skip to content

Commit 0351650

Browse files
committed
Added JS.new translation. Made it so import works solely with the only option
1 parent 4a46d0a commit 0351650

8 files changed

Lines changed: 89 additions & 132 deletions

File tree

lib/elixir_script/lib/js.ex

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
defmodule ElixirScript.Lib.JS do
2+
alias ESTree.Tools.Builder
3+
alias ElixirScript.Translator
4+
alias ElixirScript.Translator.Utils
5+
6+
def translate_js_function(name, params, env) do
7+
do_translate({name, [], params}, env)
8+
end
9+
10+
defp do_translate({:new, _, [{:__aliases__, _, module_name}, params]}, env) do
11+
Builder.new_expression(
12+
Utils.make_module_expression_tree(module_name, false),
13+
Enum.map(params, &Translator.translate(&1, env))
14+
)
15+
end
16+
17+
end

lib/elixir_script/lib/kernel.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ defmodule ElixirScript.Lib.Kernel do
44
alias ElixirScript.Translator
55
alias ElixirScript.Translator.Function
66
alias ElixirScript.Translator.Expression
7-
alias ElixirScript.Translator.If
87
alias ElixirScript.Translator.Raise
98

109
def translate_kernel_function(name, params, env) do
@@ -149,8 +148,9 @@ defmodule ElixirScript.Lib.Kernel do
149148
)
150149
end
151150

152-
defp do_translate({:if, _, [test, blocks]}, _) do
153-
If.make_if(test, blocks)
151+
defp do_translate({:if, _, _} = ast, env) do
152+
Macro.expand(ast, env)
153+
|> Translator.translate(env)
154154
end
155155

156156
defp do_translate({:|>, _, [left, right]}, _) do

lib/elixir_script/translator.ex

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ defmodule ElixirScript.Translator do
2323
alias ElixirScript.Translator.Utils
2424
alias ElixirScript.Lib.Logger
2525
alias ElixirScript.Lib.Kernel, as: KernelLib
26+
alias ElixirScript.Lib.JS, as: JSLib
2627
alias ESTree.Tools.Builder, as: JS
2728

2829
@doc """
@@ -149,15 +150,18 @@ defmodule ElixirScript.Translator do
149150
end
150151

151152
defp do_translate({{:., _, [module_name, function_name]}, _, params } = ast, env) do
152-
if module_name == Kernel do
153-
KernelLib.translate_kernel_function(function_name, params, env)
154-
else
155-
expanded_ast = Macro.expand(ast, env)
156-
if expanded_ast == ast do
157-
Function.make_function_call(module_name, function_name, params, env)
158-
else
159-
translate(expanded_ast, env)
160-
end
153+
case module_name do
154+
Kernel ->
155+
KernelLib.translate_kernel_function(function_name, params, env)
156+
{:__aliases__, [alias: false], [:JS]} ->
157+
JSLib.translate_js_function(function_name, params, env)
158+
_ ->
159+
expanded_ast = Macro.expand(ast, env)
160+
if expanded_ast == ast do
161+
Function.make_function_call(module_name, function_name, params, env)
162+
else
163+
translate(expanded_ast, env)
164+
end
161165
end
162166
end
163167

@@ -195,15 +199,15 @@ defmodule ElixirScript.Translator do
195199
end
196200

197201
defp do_translate({:super, _, _expressions }, _) do
198-
raise ElixirScript.UnsupportedError, :super
202+
raise ElixirScript.UnsupportedError, "super"
199203
end
200204

201205
defp do_translate({:__CALLER__, _, _expressions }, _) do
202-
raise ElixirScript.UnsupportedError, :__CALLER__
206+
raise ElixirScript.UnsupportedError, "__CALLER__"
203207
end
204208

205209
defp do_translate({:__ENV__, _, _expressions }, _) do
206-
raise ElixirScript.UnsupportedError, :__ENV__
210+
raise ElixirScript.UnsupportedError, "__ENV__"
207211
end
208212

209213
defp do_translate({:quote, _, [[do: expr]]}, _) do
@@ -214,12 +218,12 @@ defmodule ElixirScript.Translator do
214218
Quote.make_quote(opts, expr)
215219
end
216220

217-
defp do_translate({:import, _, [{:__aliases__, _, module_name_list}]}, _) do
218-
Import.make_import(module_name_list, [])
221+
defp do_translate({:import, _, [{:__aliases__, _, module_name_list}, [only: functions] ]}, _) do
222+
Import.make_import(module_name_list, [only: functions])
219223
end
220224

221-
defp do_translate({:import, _, [{:__aliases__, _, module_name_list}, options ]}, _) do
222-
Import.make_import(module_name_list, options)
225+
defp do_translate({ :import, _, _ }, _) do
226+
raise ElixirScript.UnsupportedError, "import without `:only` option"
223227
end
224228

225229
defp do_translate({:alias, _, [alias_info, options]}, _) when is_tuple(alias_info) do

lib/elixir_script/translator/if.ex

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

test/lib/js_test.exs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule ElixirScript.Lib.JS.Test do
2+
use ShouldI
3+
import ElixirScript.TestHelper
4+
5+
should "translate new" do
6+
ex_ast = quote do
7+
JS.new A.B, [1, 2, 3]
8+
end
9+
10+
js_code = """
11+
new A.B(1, 2, 3)
12+
"""
13+
14+
assert_translation(ex_ast, js_code)
15+
16+
ex_ast = quote do
17+
JS.new A, [1, 2, 3]
18+
end
19+
20+
js_code = """
21+
new A(1, 2, 3)
22+
"""
23+
24+
assert_translation(ex_ast, js_code)
25+
end
26+
end

test/translator/function_test.exs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,15 @@ defmodule ElixirScript.Translator.Function.Test do
7171
end
7272

7373
js_code = """
74-
let test1 = fun(
75-
[
76-
[fun.parameter, fun.parameter],
77-
function(alpha, beta){
78-
return (function(){
79-
if(1 == 1){
80-
return 1;
81-
}else{
82-
return 2;
83-
}
84-
}.call(this));
85-
}
86-
]
87-
);
74+
let test1 = fun([[fun.parameter, fun.parameter], function(alpha,beta) {
75+
return fun([[fun.parameter], function(x) {
76+
return 2;
77+
}, function(x) {
78+
return Kernel.__in__(x,Erlang.list(false,null));
79+
}],[[fun.wildcard], function() {
80+
return 1;
81+
}]).call(this, 1 == 1);
82+
}]);
8883
"""
8984

9085
assert_translation(ex_ast, js_code)
@@ -104,21 +99,21 @@ defmodule ElixirScript.Translator.Function.Test do
10499
end
105100

106101
js_code = """
107-
let test1 = fun([[fun.parameter, fun.parameter], function(alpha,beta) {
108-
return (function() {
109-
if(1 == 1) {
110-
return (function() {
111-
if(2 == 2) {
112-
return 4;
113-
} else {
114-
let [a0] = fun.bind(fun.parameter,1);
115-
return a0;
116-
}
117-
}.call(this));
118-
} else {
102+
let test1 = fun([[fun.parameter, fun.parameter], function(alpha,beta) {
103+
return fun([[fun.parameter], function(x) {
119104
return 2;
120-
}
121-
}.call(this));
105+
}, function(x) {
106+
return Kernel.__in__(x,Erlang.list(false,null));
107+
}],[[fun.wildcard], function() {
108+
return fun([[fun.parameter], function(x) {
109+
let [a000] = fun.bind(fun.parameter,1);
110+
return a000;
111+
}, function(x) {
112+
return Kernel.__in__(x,Erlang.list(false,null));
113+
}],[[fun.wildcard], function() {
114+
return 4;
115+
}]).call(this,2 == 2);
116+
}]).call(this,1 == 1);
122117
}]);
123118
"""
124119

test/translator/if_test.exs

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

test/translator/import_test.exs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,6 @@ defmodule ElixirScript.Translator.Import.Test do
22
use ShouldI
33
import ElixirScript.TestHelper
44

5-
should "translate import without options" do
6-
ex_ast = quote do
7-
import Hello.World
8-
end
9-
10-
js_code = """
11-
import * as World from 'hello/world';
12-
"""
13-
14-
assert_translation(ex_ast, js_code)
15-
16-
end
17-
185
should "translate import with only" do
196
ex_ast = quote do
207
import US, only: [la: 1, al: 2]

0 commit comments

Comments
 (0)