Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c0e1f50

Browse files
committed
refactoring
1 parent 334f324 commit c0e1f50

File tree

9 files changed

+44
-94
lines changed

9 files changed

+44
-94
lines changed

lib/elixir_script.ex

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ defmodule ElixirScript do
8383
|> ElixirScript.Passes.ConsolidateProtocols.execute(opts)
8484
|> ElixirScript.Passes.CreateJSModules.execute(opts)
8585
|> ElixirScript.Passes.JavaScriptCode.execute(opts)
86-
|> ElixirScript.Passes.JavaScriptName.execute(opts)
8786
|> ElixirScript.Passes.HandleOutput.execute(opts)
8887
end
8988

@@ -186,15 +185,11 @@ defmodule ElixirScript do
186185
|> ElixirScript.Passes.ConsolidateProtocols.execute(opts)
187186
|> ElixirScript.Passes.CreateJSModules.execute(opts)
188187
|> ElixirScript.Passes.JavaScriptCode.execute(opts)
189-
|> ElixirScript.Passes.JavaScriptName.execute(opts)
190188
|> ElixirScript.Passes.HandleOutput.execute(opts)
191189

192190
result
193191
end
194192

195-
@doc false
196-
def version(), do: @version
197-
198193
defp build_compiler_options(opts) do
199194
default_options = Map.new
200195
|> Map.put(:include_path, false)

lib/elixir_script/module_systems/common.ex

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ defmodule ElixirScript.ModuleSystems.Common do
1111
imports = js_imports
1212
|> Enum.map(fn
1313
{module, path} -> import_module(module, path)
14-
{module, path, true} -> import_module(module, path)
15-
{module, path, false} -> import_namespace_module(module, path)
14+
{module, path, _} -> import_module(module, path)
1615
end)
1716

1817
imports = Enum.uniq(imports ++ module_imports)
@@ -21,12 +20,7 @@ defmodule ElixirScript.ModuleSystems.Common do
2120
imports ++ body ++ export
2221
end
2322

24-
def import_namespace_module(module_name, from) do
25-
js_module_name = ElixirScript.Translator.Identifier.make_namespace_members(module_name)
26-
do_import_module(js_module_name, from)
27-
end
28-
29-
def import_module(module_name, from) do
23+
defp import_module(module_name, from) do
3024
js_module_name = ElixirScript.Translator.Identifier.make_namespace_members(module_name)
3125
do_import_module(js_module_name, from)
3226
end
@@ -45,7 +39,7 @@ defmodule ElixirScript.ModuleSystems.Common do
4539

4640
end
4741

48-
def export_module(exported_object) do
42+
defp export_module(exported_object) do
4943
JS.assignment_expression(
5044
:=,
5145
JS.member_expression(

lib/elixir_script/module_systems/es.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ defmodule ElixirScript.ModuleSystems.ES do
1111
imports = js_imports
1212
|> Enum.map(fn
1313
{module, path} -> import_module(module, path)
14-
{module, path, true} -> import_module(module, path)
15-
{module, path, false} -> import_namespace_module(module, path)
14+
{module, path, default: true} -> import_module(module, path)
15+
{module, path, default: false} -> import_namespace_module(module, path)
1616
end)
1717

1818
imports = Enum.uniq(imports ++ module_imports)
@@ -21,7 +21,7 @@ defmodule ElixirScript.ModuleSystems.ES do
2121
imports ++ body ++ export
2222
end
2323

24-
def import_namespace_module(module_name, from) do
24+
defp import_namespace_module(module_name, from) do
2525
js_module_name = ElixirScript.Translator.Identifier.make_namespace_members(module_name)
2626

2727
import_specifier = JS.import_namespace_specifier(
@@ -32,7 +32,7 @@ defmodule ElixirScript.ModuleSystems.ES do
3232
do_import_module([import_specifier], from)
3333
end
3434

35-
def import_module(import_name, from) do
35+
defp import_module(import_name, from) do
3636
js_module_name = ElixirScript.Translator.Identifier.make_namespace_members(import_name)
3737

3838
import_specifier = JS.import_default_specifier(
@@ -49,7 +49,7 @@ defmodule ElixirScript.ModuleSystems.ES do
4949
)
5050
end
5151

52-
def export_module(exported_object) do
52+
defp export_module(exported_object) do
5353
JS.export_default_declaration(exported_object)
5454
end
5555

lib/elixir_script/module_systems/namespace.ex

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ defmodule ElixirScript.ModuleSystems.Namespace do
44
alias ElixirScript.Translator
55
alias ElixirScript.Translator.State
66
alias ElixirScript.Translator.Utils
7+
alias ElixirScript.Translator.Identifier
78

89
def build(module_name, imports, body, exports, env) do
910
module_imports = imports
@@ -21,55 +22,40 @@ defmodule ElixirScript.ModuleSystems.Namespace do
2122
List.wrap(make_namespace_body(module_name, module_imports, body, exports))
2223
end
2324

25+
defp module_name_function_call(module_name, function) do
26+
members = ["Elixir"] ++ Module.split(module_name) ++ [function]
27+
Identifier.make_namespace_members(members)
28+
end
29+
2430
def import_module(module_name) do
2531
name = ["Elixir" | Module.split(module_name) ] |> Enum.join("$")
2632

2733
declarator = JS.variable_declarator(
2834
JS.identifier(name),
2935
JS.call_expression(
30-
JS.member_expression(
31-
JS.call_expression(
32-
JS.member_expression(
33-
JS.identifier("Bootstrap"),
34-
JS.member_expression(
35-
JS.identifier(:Core),
36-
JS.member_expression(
37-
JS.identifier(:Functions),
38-
JS.identifier(:build_namespace)
39-
)
40-
)
41-
),
42-
[JS.identifier("Elixir"), JS.literal(Utils.name_to_js_file_name(module_name))]
43-
),
44-
JS.identifier("__load")
45-
),
36+
module_name_function_call(module_name, "__load"),
4637
[JS.identifier("Elixir")]
4738
)
4839
)
4940

5041
JS.variable_declaration([declarator], :const)
5142
end
5243

44+
defp build_namespace() do
45+
JS.member_expression(
46+
JS.identifier("Bootstrap"),
47+
JS.member_expression(
48+
JS.identifier(:Core),
49+
JS.member_expression(
50+
JS.identifier(:Functions),
51+
JS.identifier(:build_namespace)
52+
)
53+
)
54+
)
55+
end
56+
5357
defp make_namespace_body(module_name, imports, body, exports) do
54-
_self =
55-
JS.call_expression(
56-
JS.member_expression(
57-
JS.identifier("Bootstrap"),
58-
JS.member_expression(
59-
JS.identifier(:Core),
60-
JS.member_expression(
61-
JS.identifier(:Functions),
62-
JS.identifier(:build_namespace)
63-
)
64-
)
65-
),
66-
[JS.identifier("Elixir"), JS.literal(Utils.name_to_js_file_name(module_name))]
67-
)
68-
69-
values = JS.member_expression(
70-
_self,
71-
JS.identifier("__exports")
72-
)
58+
values = module_name_function_call(module_name, "__exports")
7359

7460
_if = JS.if_statement(
7561
values,
@@ -99,19 +85,10 @@ defmodule ElixirScript.ModuleSystems.Namespace do
9985

10086
make = JS.member_expression(
10187
JS.call_expression(
102-
JS.member_expression(
103-
JS.identifier("Bootstrap"),
104-
JS.member_expression(
105-
JS.identifier(:Core),
106-
JS.member_expression(
107-
JS.identifier(:Functions),
108-
JS.identifier(:build_namespace)
109-
)
110-
)
111-
),
112-
[JS.identifier("Elixir"), JS.literal(Utils.name_to_js_file_name(module_name))]
113-
),
114-
JS.identifier("__load")
88+
build_namespace(),
89+
[JS.identifier("Elixir"), JS.literal(Utils.name_to_js_file_name(module_name))]
90+
),
91+
JS.identifier("__load")
11592
)
11693

11794
func_body = JS.block_statement([_if] ++ body ++ [declaration, assign] ++ imports ++ exports)

lib/elixir_script/module_systems/umd.ex

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ defmodule ElixirScript.ModuleSystems.UMD do
1111
imports = js_imports
1212
|> Enum.map(fn
1313
{module, path} -> import_module(module, path)
14-
{module, path, true} -> import_module(module, path)
15-
{module, path, false} -> import_namespace_module(module, path)
14+
{module, path, _} -> import_module(module, path)
1615
end)
1716

1817
imports = Enum.uniq(imports ++ module_imports)
@@ -21,26 +20,16 @@ defmodule ElixirScript.ModuleSystems.UMD do
2120
List.wrap(make_umd(imports, body, export))
2221
end
2322

24-
def import_namespace_module(module_name, from) do
25-
js_module_name = ElixirScript.Translator.Identifier.make_namespace_members(module_name)
26-
{js_module_name, JS.literal(from)}
27-
end
28-
29-
def import_module(module_name, from) do
23+
defp import_module(module_name, from) do
3024
js_module_name = ElixirScript.Translator.Identifier.make_namespace_members(module_name)
3125
{js_module_name, JS.literal(from)}
3226
end
3327

34-
def import_module(module_name, from) do
35-
js_module_name = ElixirScript.Translator.Identifier.make_namespace_members(module_name)
36-
{JS.identifier(module_name), JS.literal(from)}
37-
end
38-
39-
def export_module(exported_object) do
28+
defp export_module(exported_object) do
4029
exported_object
4130
end
4231

43-
def make_umd(imports, body, exports) do
32+
defp make_umd(imports, body, exports) do
4433
import_paths = Enum.map(imports, fn({_, path}) -> path end)
4534
import_identifiers = Enum.map(imports, fn({id, _}) -> id end)
4635
exports = if is_nil(exports), do: [], else: [JS.return_statement(exports)]

lib/elixir_script/passes/create_js_modules.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ defmodule ElixirScript.Passes.CreateJSModules do
9292

9393
ast = opts.module_formatter.build(
9494
[],
95-
[{"Bootstrap", "./Elixir.Bootstrap", true }] ++ opts.js_modules,
95+
[{"Bootstrap", "./Elixir.Bootstrap", default: true }] ++ opts.js_modules,
9696
[elixir, start] ++ body,
9797
JS.identifier("Elixir")
9898
)

lib/elixir_script/passes/handle_output.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule ElixirScript.Passes.HandleOutput do
22
@moduledoc false
33
alias ElixirScript.Translator.State
4+
@generated_name "Elixir.App.js"
45

56
def execute(compiler_data, opts) do
67
State.stop(compiler_data.state)
@@ -27,7 +28,7 @@ defmodule ElixirScript.Passes.HandleOutput do
2728
ElixirScript.copy_bootstrap_to_destination(compiler_opts.format, output_path)
2829
end
2930

30-
file_name = Path.join([output_path, compiler_output.generated_name])
31+
file_name = Path.join([output_path, @generated_name])
3132

3233
if !File.exists?(Path.dirname(file_name)) do
3334
File.mkdir_p!(Path.dirname(file_name))
@@ -39,7 +40,7 @@ defmodule ElixirScript.Passes.HandleOutput do
3940
defp process_include_path(compiler_output, compiler_opts) do
4041
case compiler_opts.include_path do
4142
true ->
42-
{compiler_output.generated, compiler_output.generated_name}
43+
{compiler_output.generated, @generated_name}
4344
false ->
4445
compiler_output.generated
4546
end

lib/elixir_script/passes/java_script_name.ex

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

lib/elixir_script/translator/kernel/special_forms/identifier.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ defmodule ElixirScript.Translator.Identifier do
7171

7272
def make_namespace_members(module_name) do
7373
case module_name do
74+
m when is_list(m) ->
75+
m
7476
m when is_binary(m) ->
7577
String.split(m, ".")
7678
m when is_atom(m) ->
7779
Module.split(m)
7880
end
7981
|> Enum.reverse
8082
|> make_alias
81-
end
83+
end
8284

8385
end

0 commit comments

Comments
 (0)