Skip to content

Commit d9392d2

Browse files
authored
Merge pull request elixirscript#441 from elixirscript/fix-error-with-imports
Test file line numbers and compiler fixes
2 parents f14598f + 313605c commit d9392d2

File tree

25 files changed

+609
-68
lines changed

25 files changed

+609
-68
lines changed

.tool-versions

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
erlang 20.0
2-
elixir 1.5.0-otp-20
3-
nodejs 8.3.0
1+
erlang 20.1
2+
elixir 1.5.2-otp-20
3+
nodejs 8.9.1

lib/elixir_script/compiler.ex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ defmodule ElixirScript.Compiler do
4141

4242
def compile(path, opts) when is_binary(path) do
4343
opts = build_compiler_options(opts)
44-
{:ok, pid} = State.start_link()
44+
{:ok, pid} = State.start_link(opts)
4545

4646
path = if String.ends_with?(path, [".ex", ".exs"]) do
4747
path
@@ -64,7 +64,7 @@ defmodule ElixirScript.Compiler do
6464

6565
def compile(entry_modules, opts) do
6666
opts = build_compiler_options(opts)
67-
{:ok, pid} = State.start_link()
67+
{:ok, pid} = State.start_link(opts)
6868

6969
entry_modules = List.wrap(entry_modules)
7070

@@ -90,11 +90,13 @@ defmodule ElixirScript.Compiler do
9090
end
9191

9292
defp build_compiler_options(opts) do
93+
remove_used_functions? = Keyword.get(opts, :remove_unused_functions, true)
94+
9395
default_options = Map.new
9496
|> Map.put(:output, Keyword.get(opts, :output))
9597
|> Map.put(:format, :es)
9698
|> Map.put(:root, Keyword.get(opts, :root, "."))
97-
|> Map.put(:remove_unused_functions, Keyword.get(opts, :remove_unused_functions, Mix.env == :prod))
99+
|> Map.put(:remove_unused_functions, remove_used_functions?)
98100

99101
options = default_options
100102
Map.put(options, :module_formatter, ES)

lib/elixir_script/module_systems/es.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ defmodule ElixirScript.ModuleSystems.ES do
44

55
def build(js_imports, body, exports) do
66
imports = js_imports
7+
|> Enum.filter(fn
8+
{_module, _name, nil, _import_path} -> false
9+
_ -> true
10+
end)
711
|> Enum.map(fn
812
{_module, name, _path, import_path} -> import_module(name, import_path)
913
end)

lib/elixir_script/passes/output.ex

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ defmodule ElixirScript.Output do
99
"""
1010
@spec execute([atom], pid, map) :: [{atom, binary}]
1111
def execute(modules, pid, opts) do
12-
prepared_modules = modules
13-
|> Enum.filter(fn {_, info} -> Map.has_key?(info, :js_ast) end)
14-
|> Enum.map(fn {module, info} ->
15-
{module, info.js_ast, info.used_modules}
16-
end
17-
)
18-
1912
js_modules = ModuleState.js_modules(pid)
2013
|> Enum.filter(fn
2114
{_module, _name, nil} -> false
@@ -27,9 +20,23 @@ defmodule ElixirScript.Output do
2720
{module, name, path, import_path}
2821
end)
2922

23+
prepared_modules = modules
24+
|> Enum.filter(fn {_, info} -> Map.has_key?(info, :js_ast) end)
25+
|> Enum.map(fn {module, info} ->
26+
{module, info.js_ast, filter_used_modules(info.used_modules, pid)}
27+
end
28+
)
29+
3030
create_modules(prepared_modules, opts, js_modules)
3131
end
3232

33+
defp filter_used_modules(used_modules, pid) do
34+
used_modules
35+
|> Enum.filter(fn module ->
36+
ModuleState.is_global_module(pid, module) == false
37+
end)
38+
end
39+
3340
defp concat(code) do
3441
"""
3542
'use strict';

lib/elixir_script/passes/translate/form.ex

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,13 +392,26 @@ defmodule ElixirScript.Translate.Form do
392392

393393
defp compile_params(params, state) do
394394
{params, var_decs} = Enum.map_reduce(params, [], fn
395+
({:=, _, [{left_var, _, atom} = left, right]} = ast, acc) when is_atom(atom) ->
396+
case Atom.to_string(left_var) do
397+
"_" <> _ ->
398+
{compile!(right, state), acc}
399+
_ ->
400+
{ast, state} = compile(ast, state)
401+
left = compile!(left, state)
402+
403+
{left, acc ++ List.wrap(ast)}
404+
end
405+
395406
({:=, _, [left, _]} = ast, acc) ->
396407
{ast, state} = compile(ast, state)
397408
left = compile!(left, state)
398409

399410
{left, acc ++ List.wrap(ast)}
400411
(x, acc) ->
401-
{compile!(x, state), acc}
412+
compiled = compile!(x, state)
413+
414+
{compiled, acc}
402415
end)
403416

404417
{var_decs, params}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ defmodule ElixirScript.Translate.Forms.For do
7878
([into: expression], state) ->
7979
%{state | into: Form.compile!(expression, module_state)}
8080

81+
([do: expression2, into: expression], state) ->
82+
fun = create_function_expression(expression2, state, module_state)
83+
84+
%{state | into: Form.compile!(expression, module_state), fun: fun}
85+
8186
([into: expression, do: expression2], state) ->
8287
fun = create_function_expression(expression2, state, module_state)
8388

@@ -96,12 +101,9 @@ defmodule ElixirScript.Translate.Forms.For do
96101

97102

98103
defp create_function_expression(ast, state, module_state) do
99-
{ ast, _ } = Enum.map_reduce(List.wrap(ast), module_state, fn x, acc_state ->
100-
Form.compile(x, acc_state)
101-
end)
102-
103104
ast = ast
104-
|> List.flatten
105+
|> ElixirScript.Translate.Function.compile_block(module_state)
106+
|> elem(0)
105107
|> Clause.return_last_statement
106108

107109
Helpers.arrow_function(

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,15 @@ defmodule ElixirScript.Translate.Forms.Match do
5656
defp compile_match(%{patterns: [left], expression: right}, state) do
5757
{ right_ast, state } = Form.compile(right, state)
5858

59-
{var_dec, right_ast} = case right_ast do
60-
[variable_declaration, x] ->
61-
{variable_declaration, x}
59+
{var_decs, right_ast} = case right_ast do
60+
x when is_list(x) ->
61+
l = Enum.reverse(x)
62+
[head | tail] = l
63+
l = Enum.reverse(tail)
64+
65+
{l, head}
6266
x ->
63-
{nil, x}
67+
{[], x}
6468
end
6569

6670
{ patterns, params, state } = Pattern.compile([left], state)
@@ -84,14 +88,7 @@ defmodule ElixirScript.Translate.Forms.Match do
8488
List.wrap(array_pattern)
8589
end
8690

87-
js_ast = case var_dec do
88-
nil ->
89-
js_ast
90-
x ->
91-
[x] ++ js_ast
92-
end
93-
94-
{ js_ast, state }
91+
{ var_decs ++ js_ast, state }
9592
end
9693

9794
defp compile_match(%{patterns: lefts, expression: right}, state) do

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ defmodule ElixirScript.Translate.Forms.Remote do
5454
{ ast, state }
5555
end
5656

57+
def compile({:., _, [:erlang, :"=<"]}, state) do
58+
ast = erlang_compat_function("erlang", "lessThanEqualTo")
59+
{ ast, state }
60+
end
61+
62+
def compile({:., _, [:erlang, :+]}, state) do
63+
ast = erlang_compat_function("erlang", "add")
64+
{ ast, state }
65+
end
66+
5767
def compile({:., _, [module, function]}, state) when module in @erlang_modules do
5868
ast = J.member_expression(
5969
Helpers.core_module(module),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ defmodule ElixirScript.Translate.Forms.Try do
6262
names = Enum.map(names, &make_exception_ast(&1))
6363

6464
param = {:_e, context, atom}
65-
reason_call = {{:., [], [{:_e0, context, atom}, :__reason]}, [], []}
65+
reason_call = {{:., [], [param, :__reason]}, [], []}
6666
reason_call = {{:., [], [reason_call, :__struct__]}, [], []}
6767
reason_call = {{:., [], [reason_call, :__MODULE__]}, [], []}
6868

lib/elixir_script/passes/translate/function.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ defmodule ElixirScript.Translate.Function do
173173
{ast, state}
174174
end
175175

176-
@spec update_last_call([ESTree.Node.t], map) :: ESTree.Node.t
176+
@spec update_last_call([ESTree.Node.t], map) :: list
177177
def update_last_call(clause_body, %{function: {name, _}, anonymous_fn: anonymous?}) do
178178
last_item = List.last(clause_body)
179179
function_name = ElixirScript.Translate.Identifier.make_function_name(name)

0 commit comments

Comments
 (0)