Skip to content

Commit 54743fa

Browse files
committed
Abstraction of JavaScript module system
1 parent ae49a1a commit 54743fa

File tree

6 files changed

+53
-38
lines changed

6 files changed

+53
-38
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
defmodule ElixirScript.ModuleSystems do
2+
3+
defp module_system() do
4+
ElixirScript.ModuleSystems.ES6
5+
end
6+
7+
def import_module(module_names, from, env) when is_list(module_names) do
8+
module_system.import_module(module_names, from, env)
9+
end
10+
11+
def import_module(module_name, from, env) do
12+
module_system.import_module(module_name, from, env)
13+
end
14+
15+
def import_module(module_name, %ElixirScript.Macro.Env{} = env) do
16+
module_system.import_module(module_name, env)
17+
end
18+
19+
def import_module(import_name, from) do
20+
module_system.import_module(import_name, from)
21+
end
22+
23+
def export_module(exported_object) do
24+
module_system.export_module(exported_object)
25+
end
26+
27+
end

lib/elixir_script/module_systems/common.ex

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,39 @@ defmodule ElixirScript.ModuleSystems.Common do
33
alias ElixirScript.Translator
44
alias ElixirScript.Translator.Utils
55

6-
def import_module(module_name, from, env) do
7-
ref_declarator = JS.variable_declarator(
8-
Translator.translate!(module_name, env),
9-
JS.call_expression(
10-
JS.identifier("require"),
11-
[JS.literal(from)]
12-
)
13-
)
6+
def import_module(module_names, from, env) when is_list(module_names) do
7+
assignment_properties = Enum.map(module_names, fn(x) ->
8+
JS.assignment_property(Translator.translate!(x, env))
9+
end)
1410

15-
JS.variable_declaration([ref_declarator], :const)
11+
do_import_module(JS.object_pattern(assignment_properties), from)
12+
end
13+
14+
def import_module(module_name, from, env) do
15+
do_import_module(Translator.translate!(module_name, env), from)
1616
end
1717

1818
def import_module(module_name, %ElixirScript.Macro.Env{} = env) do
1919
{from, _ } = Code.eval_quoted(module_name)
20-
21-
ref_declarator = JS.variable_declarator(
22-
Translator.translate!(module_name, env),
23-
JS.call_expression(
24-
JS.identifier("require"),
25-
[JS.literal(Macro.underscore(from))]
26-
)
27-
)
28-
29-
JS.variable_declaration([ref_declarator], :const)
30-
20+
do_import_module(Translator.translate!(module_name, env), Macro.underscore(from))
3121
end
3222

3323
def import_module(import_name, from) do
24+
do_import_module(JS.identifier(import_name), from)
25+
end
26+
27+
defp do_import_module(ref, file_path) do
3428

3529
ref_declarator = JS.variable_declarator(
36-
JS.identifier(import_name),
30+
ref,
3731
JS.call_expression(
3832
JS.identifier("require"),
39-
[JS.literal(from)]
33+
[JS.literal(file_path)]
4034
)
4135
)
4236

4337
JS.variable_declaration([ref_declarator], :const)
44-
end
4538

46-
defp do_import_module(import_specifiers, file_path) do
47-
JS.import_declaration(
48-
import_specifiers,
49-
JS.literal(file_path)
50-
)
5139
end
5240

5341
def export_module(exported_object) do

lib/elixir_script/translator/js.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule ElixirScript.Translator.JS do
33

44
alias ESTree.Tools.Builder
55
alias ElixirScript.Translator
6-
alias ElixirScript.ModuleSystems.ES6
6+
alias ElixirScript.ModuleSystems
77

88
@doc false
99
def translate_js_function(name, params, env) do
@@ -56,15 +56,15 @@ defmodule ElixirScript.Translator.JS do
5656
end
5757

5858
defp do_translate({:import, _, [module_names, from]}, env) when is_list(module_names) do
59-
ES6.import_module(module_names, from, env)
59+
ModuleSystems.import_module(module_names, from, env)
6060
end
6161

6262
defp do_translate({:import, _, [module_name, from]}, env) do
63-
ES6.import_module(module_name, from, env)
63+
ModuleSystems.import_module(module_name, from, env)
6464
end
6565

6666
defp do_translate({:import, _, [module_name]}, env) do
67-
ES6.import_module(module_name, env)
67+
ModuleSystems.import_module(module_name, env)
6868
end
6969

7070
end

lib/elixir_script/translator/module.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule ElixirScript.Translator.Module do
44
alias ElixirScript.Translator
55
alias ElixirScript.Translator.Utils
66
alias ElixirScript.Translator.Function
7-
alias ElixirScript.ModuleSystems.{ES6,Common}
7+
alias ElixirScript.ModuleSystems
88

99
def make_module(ElixirScript.Temp, body, env) do
1010
{ body, _ } = translate_body(body, env)
@@ -51,7 +51,7 @@ defmodule ElixirScript.Translator.Module do
5151
exported_functions = Enum.map(exported_functions, fn({_key, value}) -> value end)
5252
private_functions = Enum.map(private_functions, fn({_key, value}) -> value end)
5353

54-
default = Common.export_module(exported_object)
54+
default = ModuleSystems.export_module(exported_object)
5555

5656
result = %{
5757
name: Utils.quoted_to_name({:__aliases__, [], module }),
@@ -176,7 +176,7 @@ defmodule ElixirScript.Translator.Module do
176176
compiler_opts = ElixirScript.Translator.State.get().compiler_opts
177177
case compiler_opts.import_standard_libs do
178178
true ->
179-
[Common.import_module(:Elixir, Utils.make_local_file_path(compiler_opts.core_path))]
179+
[ModuleSystems.import_module(:Elixir, Utils.make_local_file_path(compiler_opts.core_path))]
180180
false ->
181181
[]
182182
end
@@ -209,7 +209,7 @@ defmodule ElixirScript.Translator.Module do
209209
end
210210

211211
def make_imports(enum) do
212-
Enum.map(enum, fn(x) -> Common.import_module(Utils.name_to_js_name(x), Utils.make_local_file_path(Utils.name_to_js_file_name(x))) end)
212+
Enum.map(enum, fn(x) -> ModuleSystems.import_module(Utils.name_to_js_name(x), Utils.make_local_file_path(Utils.name_to_js_file_name(x))) end)
213213
end
214214

215215
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ defmodule ElixirScript.Mixfile do
2828

2929
defp deps do
3030
[
31-
{:estree, "~> 2.2"},
31+
{:estree, "~> 2.3" },
3232
{:earmark, "~> 0.2", only: :dev },
3333
{:ex_doc, "~> 0.11", only: :dev },
3434
{:excoveralls, "~> 0.4", only: :test},

mix.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"credo": {:hex, :credo, "0.2.6"},
55
"dogma": {:hex, :dogma, "0.0.7"},
66
"earmark": {:hex, :earmark, "0.2.1"},
7-
"estree": {:hex, :estree, "2.2.0"},
7+
"estree": {:hex, :estree, "2.3.0"},
88
"ex_doc": {:hex, :ex_doc, "0.11.4"},
99
"excoveralls": {:hex, :excoveralls, "0.5.1"},
1010
"exjsx": {:hex, :exjsx, "3.2.0"},

0 commit comments

Comments
 (0)