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

Skip to content

Commit 362b04b

Browse files
committed
Update struct translation to call modules directly
1 parent d2fdc7a commit 362b04b

File tree

6 files changed

+17
-130
lines changed

6 files changed

+17
-130
lines changed

lib/elixir_script/passes/consolidate_protocols.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ defmodule ElixirScript.Passes.ConsolidateProtocols do
7070

7171
body = Enum.map(implementations, fn({_, impl_data}) ->
7272
x = Utils.quoted_to_name(impl_data.for)
73-
members = ["Elixir"] ++ Module.split(name) ++ ["DefImpl"] ++ Module.split(x) ++ ["__load"]
73+
members = ["Elixir"] ++ Module.split(name) ++ ["DefImpl", "Elixir"] ++ Module.split(x) ++ ["__load"]
7474
ast = JS.call_expression(
7575
Identifier.make_namespace_members(members),
7676
[JS.identifier("Elixir")]

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

Lines changed: 0 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -110,25 +110,6 @@ defmodule ElixirScript.Translator.Call do
110110
{ js_ast, env }
111111
end
112112

113-
114-
def get_module_name_for_function(module_name, env) do
115-
case module_name do
116-
{:__aliases__, _, name} ->
117-
module_name = Utils.quoted_to_name(name)
118-
get_js_name(module_name, env)
119-
{name, _, _} when is_atom(name) ->
120-
get_js_name(name, env)
121-
{{:., _, [_, _]}, _, _ } = ast ->
122-
ast
123-
{{:., _, [{:__aliases__, _, _}]}, _, _} = ast ->
124-
ast
125-
ast when is_list(ast) ->
126-
ast
127-
name ->
128-
get_js_name(name, env)
129-
end
130-
end
131-
132113
def make_function_call(module_name, function_name, [], env) when is_atom(module_name) and is_atom(function_name) do
133114
js_ast = JS.call_expression(
134115
JS.member_expression(
@@ -227,106 +208,4 @@ defmodule ElixirScript.Translator.Call do
227208

228209
{ call, env }
229210
end
230-
231-
def get_js_name([Elixir | _] = list, _) do
232-
list
233-
end
234-
235-
def get_js_name({:__aliases__, _, _} = name, env) do
236-
Utils.quoted_to_name(name)
237-
|> get_js_name(env)
238-
end
239-
240-
def get_js_name(module_name, env) when is_list(module_name) do
241-
Utils.quoted_to_name({:__aliases__, [], module_name})
242-
|> get_js_name(env)
243-
end
244-
245-
def get_js_name(module_name, env) do
246-
247-
cond do
248-
module_name in env.requires ->
249-
Utils.name_to_js_name(module_name)
250-
251-
module_name in ElixirScript.Translator.State.list_module_names(env.state) ->
252-
ElixirScript.Translator.State.add_module_reference(env.state, env.module, module_name)
253-
Utils.name_to_js_name(module_name)
254-
255-
true ->
256-
case Atom.to_string(module_name) do
257-
"Elixir." <> _ ->
258-
{:__aliases__, _, name } = Utils.name_to_quoted(module_name)
259-
name
260-
_ ->
261-
module_name
262-
end
263-
end
264-
end
265-
266-
defp make_member_expression(module_name, function_name, env, computed \\ false) do
267-
case module_name do
268-
modules when is_list(modules) and length(modules) > 1 ->
269-
ast = make_module_expression_tree(modules, computed, env)
270-
JS.member_expression(
271-
ast,
272-
Identifier.make_identifier(function_name),
273-
computed
274-
)
275-
modules when is_list(modules) and length(modules) == 1 ->
276-
JS.member_expression(
277-
Identifier.make_identifier(hd(modules)),
278-
Identifier.make_identifier(function_name),
279-
computed
280-
)
281-
{{:., _, [_module_name, _function_name]}, _, _params } = ast ->
282-
JS.member_expression(
283-
Translator.translate!(ast, env),
284-
Identifier.make_identifier(function_name),
285-
computed
286-
)
287-
{{:., _, [{:__aliases__, _, _}]}, _, _} = ast ->
288-
JS.member_expression(
289-
Translator.translate!(ast, env),
290-
Identifier.make_identifier(function_name),
291-
computed
292-
)
293-
{:., _, _} = ast ->
294-
JS.member_expression(
295-
Translator.translate!(ast, env),
296-
Identifier.make_identifier(function_name),
297-
computed
298-
)
299-
_ ->
300-
JS.member_expression(
301-
Identifier.make_identifier(module_name),
302-
Identifier.make_identifier(function_name),
303-
computed
304-
)
305-
end
306-
end
307-
308-
defp make_module_expression_tree([module], computed, env) do
309-
make_module_expression_tree(module, computed, env)
310-
end
311-
312-
defp make_module_expression_tree(modules, computed, _) when is_list(modules) do
313-
Enum.reduce(modules, nil, fn(x, ast) ->
314-
case ast do
315-
nil ->
316-
JS.member_expression(Identifier.make_identifier(x), nil, computed)
317-
%ESTree.MemberExpression{ property: nil } ->
318-
%{ ast | property: Identifier.make_identifier(x) }
319-
_ ->
320-
JS.member_expression(ast, Identifier.make_identifier(x), computed)
321-
end
322-
end)
323-
end
324-
325-
defp make_module_expression_tree(module, _, _) when is_binary(module) or is_atom(module) do
326-
Identifier.make_identifier(module)
327-
end
328-
329-
defp make_module_expression_tree(module, _, env) do
330-
Translator.translate!(module, env)
331-
end
332211
end

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@ defmodule ElixirScript.Translator.Struct do
1313
name = ElixirScript.Translator.LexicalScope.get_module_name(env, candiate_module_name)
1414
ident = JS.identifier(Utils.name_to_js_name(name))
1515
ElixirScript.Translator.State.add_module_reference(env.state, env.module, name)
16-
JS.member_expression(ident, ident)
16+
17+
members = ["Elixir"] ++ Module.split(name) ++ ["__load"]
18+
19+
JS.member_expression(
20+
JS.call_expression(
21+
Identifier.make_namespace_members(members),
22+
[JS.identifier("Elixir")]
23+
),
24+
ident
25+
)
1726

1827
else
1928
Identifier.make_identifier(module_name)
2029
end
21-
2230
end
2331

2432
def new_struct(module_name, data, env) do

test/prelude/kernel_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule ElixirScript.Lib.Elixir.Kernel.Test do
99

1010
js_code = """
1111
12-
Elixir$ElixirScript$Range.Elixir$ElixirScript$Range.create(Object.freeze({
12+
Elixir.ElixirScript.Range.__load(Elixir).Elixir$ElixirScript$Range.create(Object.freeze({
1313
[Symbol.for('first')]: 1,
1414
[Symbol.for('last')]: 4
1515
}))

test/translator/function_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ defmodule ElixirScript.Translator.Function.Test do
451451

452452

453453
js_code = """
454-
const something = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.type(Elixir$AStruct.Elixir$AStruct, {})], function() {
454+
const something = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.type(Elixir.AStruct.__load(Elixir).Elixir$AStruct, {})], function() {
455455
return null;
456456
}));
457457
"""
@@ -471,7 +471,7 @@ defmodule ElixirScript.Translator.Function.Test do
471471
end
472472

473473
js_code = """
474-
const something = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.capture(Bootstrap.Core.Patterns.type(Elixir$AStruct.Elixir$AStruct, {}))], function(a) {
474+
const something = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.capture(Bootstrap.Core.Patterns.type(Elixir.AStruct.__load(Elixir).Elixir$AStruct, {}))], function(a) {
475475
return null;
476476
}));
477477
"""
@@ -507,7 +507,7 @@ defmodule ElixirScript.Translator.Function.Test do
507507

508508

509509
js_code = """
510-
const something = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.type(Elixir$AStruct.Elixir$AStruct, {
510+
const something = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.type(Elixir.AStruct.__load(Elixir).Elixir$AStruct, {
511511
[Symbol.for('key')]: Bootstrap.Core.Patterns.variable(), [Symbol.for('key1')]: 2
512512
})], function(value) {
513513
return null;
@@ -527,7 +527,7 @@ defmodule ElixirScript.Translator.Function.Test do
527527

528528

529529
js_code = """
530-
const something = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.type(Elixir$AStruct.Elixir$AStruct, {
530+
const something = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.type(Elixir.AStruct.__load(Elixir).Elixir$AStruct, {
531531
[Symbol.for('key')]: Bootstrap.Core.Patterns.variable(), [Symbol.for('key1')]: 2
532532
})], function(value) {
533533
return null;

test/translator/struct_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ defmodule ElixirScript.Translator.Struct.Test do
156156
end
157157

158158
js_code = """
159-
throw Elixir$MyAppError.Elixir$MyAppError.create(Object.freeze({
159+
throw Elixir.MyAppError.__load(Elixir).Elixir$MyAppError.create(Object.freeze({
160160
[Symbol.for('message')]: 'did not get what was expected'
161161
}));
162162
"""

0 commit comments

Comments
 (0)