Skip to content

Commit 2908051

Browse files
authored
Merge pull request #361 from elixirscript/refactor_js_ast_calls
Refactor js ast calls to use Helpers module
2 parents 9d87e70 + d4b3d75 commit 2908051

File tree

22 files changed

+336
-497
lines changed

22 files changed

+336
-497
lines changed

lib/elixir_script/module_systems/namespace.ex

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule ElixirScript.ModuleSystems.Namespace do
22
@moduledoc false
33
alias ESTree.Tools.Builder, as: JS
4+
alias ElixirScript.Translate.Helpers
45
alias ElixirScript.Translate.Identifier
56

67
def build(module_name, body, exports) do
@@ -39,23 +40,14 @@ defmodule ElixirScript.ModuleSystems.Namespace do
3940
exports
4041
end
4142

42-
declarator = JS.variable_declarator(
43-
JS.identifier("__exports"),
44-
exports
45-
)
46-
47-
declaration = JS.variable_declaration([declarator], :const)
43+
declaration = Helpers.declare("__exports", exports)
4844

49-
assign = JS.assignment_expression(
50-
:=,
51-
values,
52-
JS.identifier("__exports")
53-
)
45+
assign = Helpers.assign(values, JS.identifier("__exports"))
5446

5547
exports = [JS.return_statement(JS.identifier("__exports"))]
5648

5749
make = JS.member_expression(
58-
JS.call_expression(
50+
Helpers.call(
5951
build_namespace(),
6052
[JS.identifier("Elixir"), JS.literal(Enum.join(["Elixir"] ++ Module.split(module_name), "."))]
6153
),
@@ -64,9 +56,8 @@ defmodule ElixirScript.ModuleSystems.Namespace do
6456

6557
func_body = JS.block_statement([js_if] ++ body ++ [declaration, assign] ++ exports)
6658

67-
func = JS.function_expression([JS.identifier("Elixir")], [], func_body)
68-
JS.assignment_expression(
69-
:=,
59+
func = Helpers.function([JS.identifier("Elixir")], func_body)
60+
Helpers.assign(
7061
make,
7162
func
7263
)

lib/elixir_script/passes/output/js_module.ex

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ defmodule ElixirScript.Output.JSModule do
22
@moduledoc false
33

44
alias ESTree.Tools.Builder, as: J
5+
alias ElixirScript.Translate.Helpers
56

67
def compile(body, opts, js_modules) do
7-
declarator = J.variable_declarator(
8-
J.identifier("Elixir"),
9-
J.object_expression([])
10-
)
11-
12-
elixir = J.variable_declaration([declarator], :const)
8+
elixir = Helpers.declare("Elixir", J.object_expression([]))
139

1410
ast = opts.module_formatter.build(
1511
js_modules,
@@ -21,27 +17,19 @@ defmodule ElixirScript.Output.JSModule do
2117
end
2218

2319
def start do
24-
normal = J.call_expression(
25-
J.member_expression(
26-
J.identifier("Symbol"),
27-
J.identifier("for")
28-
),
29-
[J.literal("normal")]
30-
)
20+
normal = Helpers.symbol("normal")
3121

32-
J.assignment_expression(
33-
:=,
22+
Helpers.assign(
3423
J.member_expression(
3524
J.identifier("Elixir"),
3625
J.identifier("start")
3726
),
38-
J.function_expression(
27+
Helpers.function(
3928
[J.identifier(:app), J.identifier(:args)],
40-
[],
4129
J.block_statement([
42-
J.call_expression(
30+
Helpers.call(
4331
J.member_expression(
44-
J.call_expression(
32+
Helpers.call(
4533
J.member_expression(
4634
J.identifier(:app),
4735
J.identifier("__load")
@@ -58,18 +46,16 @@ defmodule ElixirScript.Output.JSModule do
5846
end
5947

6048
def load do
61-
J.assignment_expression(
62-
:=,
49+
Helpers.assign(
6350
J.member_expression(
6451
J.identifier("Elixir"),
6552
J.identifier("load")
6653
),
67-
J.function_expression(
54+
Helpers.function(
6855
[J.identifier(:module)],
69-
[],
7056
J.block_statement([
7157
J.return_statement(
72-
J.call_expression(
58+
Helpers.call(
7359
J.member_expression(
7460
J.identifier(:module),
7561
J.identifier("__load")
@@ -83,8 +69,7 @@ defmodule ElixirScript.Output.JSModule do
8369
end
8470

8571
defp create_atom_table() do
86-
J.assignment_expression(
87-
:=,
72+
Helpers.assign(
8873
J.member_expression(
8974
J.identifier("Elixir"),
9075
J.identifier("__table__")

lib/elixir_script/passes/translate/clause.ex

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,11 @@ defmodule ElixirScript.Translate.Clause do
44
# Handles translation of all of the clause ASTs
55

66
alias ESTree.Tools.Builder, as: J
7+
alias ElixirScript.Translate.Helpers
78
alias ElixirScript.Translate.Form
89
alias ElixirScript.Translate.Forms.Pattern
910
alias ElixirScript.Translate.Function
1011

11-
@patterns J.member_expression(
12-
J.member_expression(
13-
J.identifier("ElixirScript"),
14-
J.identifier("Core")
15-
),
16-
J.identifier("Patterns")
17-
)
18-
1912
def compile({ _, args, guards, body}, state) do
2013
{patterns, params, state} = Pattern.compile(args, state)
2114
guard = compile_guard(params, guards, state)
@@ -25,16 +18,15 @@ defmodule ElixirScript.Translate.Clause do
2518
body = body
2619
|> return_last_statement
2720

28-
ast = J.call_expression(
21+
ast = Helpers.call(
2922
J.member_expression(
30-
@patterns,
23+
Helpers.patterns(),
3124
J.identifier("clause")
3225
),
3326
[
3427
J.array_expression(patterns),
35-
J.arrow_function_expression(
28+
Helpers.arrow_function(
3629
params,
37-
[],
3830
J.block_statement(body)
3931
),
4032
guard
@@ -108,9 +100,8 @@ defmodule ElixirScript.Translate.Clause do
108100
|> process_guards
109101
|> Form.compile!(state)
110102

111-
J.arrow_function_expression(
103+
Helpers.arrow_function(
112104
params,
113-
[],
114105
J.block_statement([
115106
J.return_statement(guards)
116107
])

lib/elixir_script/passes/translate/form.ex

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ defmodule ElixirScript.Translate.Form do
44
# Handles translation of all forms that are not functions or clauses
55

66
alias ESTree.Tools.Builder, as: J
7+
alias ElixirScript.Translate.Helpers
78
alias ElixirScript.Translate.Forms.{Bitstring, Match, Try, For, Receive, Remote, Pattern, With}
89
alias ElixirScript.Translate.Clause
910
require Logger
@@ -23,7 +24,7 @@ defmodule ElixirScript.Translate.Form do
2324
end
2425

2526
def compile([{:|, _, [head, tail]}], state) do
26-
ast = J.call_expression(
27+
ast = Helpers.call(
2728
J.member_expression(
2829
J.array_expression([compile!(head, state)]),
2930
J.identifier("concat")
@@ -35,7 +36,7 @@ defmodule ElixirScript.Translate.Form do
3536
end
3637

3738
def compile({:|, _, [head, tail]}, state) do
38-
ast = J.call_expression(
39+
ast = Helpers.call(
3940
J.member_expression(
4041
J.array_expression([compile!(head, state)]),
4142
J.identifier("concat")
@@ -58,13 +59,7 @@ defmodule ElixirScript.Translate.Form do
5859
ast = if ElixirScript.Translate.Module.is_elixir_module(form) do
5960
Remote.process_module_name(form, state)
6061
else
61-
J.call_expression(
62-
J.member_expression(
63-
J.identifier("Symbol"),
64-
J.identifier("for")
65-
),
66-
[J.literal(form)]
67-
)
62+
Helpers.symbol(form)
6863
end
6964

7065
{ ast, state }
@@ -75,14 +70,8 @@ defmodule ElixirScript.Translate.Form do
7570
end
7671

7772
def compile({:{}, _, elements}, state) do
78-
ast = J.new_expression(
79-
J.member_expression(
80-
J.member_expression(
81-
J.identifier("ElixirScript"),
82-
J.identifier("Core")
83-
),
84-
J.identifier("Tuple")
85-
),
73+
ast = Helpers.new(
74+
Helpers.tuple(),
8675
Enum.map(elements, &compile!(&1, state)) |> List.flatten
8776
)
8877

@@ -130,7 +119,7 @@ defmodule ElixirScript.Translate.Form do
130119
end
131120

132121
def compile({:%, _, [module, params]}, state) do
133-
ast = J.call_expression(
122+
ast = Helpers.call(
134123
J.member_expression(
135124
Remote.process_module_name(module, state),
136125
J.identifier("__struct__")
@@ -155,15 +144,15 @@ defmodule ElixirScript.Translate.Form do
155144
end
156145

157146
def compile({:case, _, [condition, [do: clauses]]}, state) do
158-
func = J.call_expression(
147+
func = Helpers.call(
159148
J.member_expression(
160-
ElixirScript.Translate.Function.patterns_ast(),
149+
Helpers.patterns(),
161150
J.identifier("defmatch")
162151
),
163152
Enum.map(clauses, fn x -> Clause.compile(x, state) |> elem(0) end) |> List.flatten
164153
)
165154

166-
ast = J.call_expression(
155+
ast = Helpers.call(
167156
J.member_expression( func, J.identifier("call")),
168157
[J.identifier(:this), compile!(condition, state)]
169158
)
@@ -178,7 +167,7 @@ defmodule ElixirScript.Translate.Form do
178167
translated_body = translated_body
179168
|> Clause.return_last_statement
180169

181-
translated_body = J.arrow_function_expression([], [], J.block_statement(translated_body))
170+
translated_body = Helpers.arrow_function([], J.block_statement(translated_body))
182171

183172
{ translated_clause, _ } = compile(hd(clause), state)
184173

@@ -188,17 +177,11 @@ defmodule ElixirScript.Translate.Form do
188177

189178

190179
cond_function = J.member_expression(
191-
J.member_expression(
192-
J.identifier("ElixirScript"),
193-
J.member_expression(
194-
J.identifier("Core"),
195-
J.identifier("SpecialForms")
196-
)
197-
),
180+
Helpers.special_forms(),
198181
J.identifier("cond")
199182
)
200183

201-
ast = J.call_expression(
184+
ast = Helpers.call(
202185
cond_function,
203186
processed_clauses
204187
)
@@ -308,7 +291,7 @@ defmodule ElixirScript.Translate.Form do
308291
end
309292

310293
def compile({{:., _, [{_, _, nil} = var, func_or_prop]}, _, []}, state) do
311-
ast = J.call_expression(
294+
ast = Helpers.call(
312295
ElixirScript.Translate.Forms.JS.call_property(),
313296
[compile!(var, state), J.literal(to_string(func_or_prop))]
314297
)
@@ -328,7 +311,7 @@ defmodule ElixirScript.Translate.Form do
328311
{function_name, _} = Map.get(state, :function)
329312
{var_decs, params} = compile_params(params, state)
330313

331-
ast = J.call_expression(
314+
ast = Helpers.call(
332315
ElixirScript.Translate.Identifier.make_function_name(function_name),
333316
params
334317
)
@@ -348,7 +331,7 @@ defmodule ElixirScript.Translate.Form do
348331
def compile({var, _, params}, state) when is_list(params) and is_atom(var) do
349332
{var_decs, params} = compile_params(params, state)
350333

351-
ast = J.call_expression(
334+
ast = Helpers.call(
352335
ElixirScript.Translate.Identifier.make_function_name(var),
353336
params
354337
)
@@ -362,7 +345,7 @@ defmodule ElixirScript.Translate.Form do
362345
end
363346

364347
def compile({function, _, []}, state) do
365-
ast = J.call_expression(
348+
ast = Helpers.call(
366349
ElixirScript.Translate.Forms.JS.call_property(),
367350
[compile!(function, state)]
368351
)
@@ -373,7 +356,7 @@ defmodule ElixirScript.Translate.Form do
373356
def compile({function, _, params}, state) when is_list(params) do
374357
{var_decs, params} = compile_params(params, state)
375358

376-
ast = J.call_expression(
359+
ast = Helpers.call(
377360
compile!(function, state),
378361
params
379362
)

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

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
defmodule ElixirScript.Translate.Forms.Bitstring do
22
@moduledoc false
33
alias ESTree.Tools.Builder, as: JS
4+
alias ElixirScript.Translate.Helpers
45
alias ElixirScript.Translate.Form
56

67

78
def compile({:<<>>, _, elements}, state) do
8-
js_ast = JS.new_expression(
9-
JS.member_expression(
10-
JS.member_expression(
11-
JS.identifier("ElixirScript"),
12-
JS.identifier("Core")
13-
),
14-
JS.identifier("BitString")
15-
),
9+
js_ast = Helpers.new(
10+
Helpers.bitstring(),
1611
Enum.map(elements, &compile_element(&1, state))
1712
)
1813

@@ -85,20 +80,10 @@ defmodule ElixirScript.Translate.Forms.Bitstring do
8580
end)
8681
end
8782

88-
defp bitstring_class() do
89-
JS.member_expression(
90-
JS.member_expression(
91-
JS.identifier("ElixirScript"),
92-
JS.identifier("Core")
93-
),
94-
JS.identifier("BitString")
95-
)
96-
end
97-
9883
defp do_compile_element({type, ast}) do
99-
JS.call_expression(
84+
Helpers.call(
10085
JS.member_expression(
101-
bitstring_class(),
86+
Helpers.bitstring(),
10287
JS.identifier(type)
10388
),
10489
[
@@ -108,9 +93,9 @@ defmodule ElixirScript.Translate.Forms.Bitstring do
10893
end
10994

11095
defp do_compile_element({type, ast, params}) when is_list(params) do
111-
JS.call_expression(
96+
Helpers.call(
11297
JS.member_expression(
113-
bitstring_class(),
98+
Helpers.bitstring(),
11499
JS.identifier(type)
115100
),
116101
[

0 commit comments

Comments
 (0)