Skip to content

Commit bab9a91

Browse files
committed
Add more test. Fix variable scoping bug
1 parent 4864fe4 commit bab9a91

File tree

6 files changed

+48
-9
lines changed

6 files changed

+48
-9
lines changed

lib/elixir_script/translator.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,13 +645,20 @@ defmodule ElixirScript.Translator do
645645
is_from_js_module(name, params, env) ->
646646
do_translate({{:., [], [{:__aliases__, [], [:JS]}, name]}, [], params }, env)
647647
ElixirScript.Translator.LexicalScope.has_var?(env, name) ->
648+
name = case env.vars[name] do
649+
0 ->
650+
name
651+
num ->
652+
String.to_atom("#{name}#{num}")
653+
end
654+
648655
{ Identifier.make_identifier(name), env }
649656
has_function?(env.module, {name, 0}, env) ->
650657
Call.make_function_call(name, [], env)
651658
ElixirScript.Translator.LexicalScope.find_module(env, {name, 0}) ->
652659
imported_module_name = ElixirScript.Translator.LexicalScope.find_module(env, {name, 0})
653660
Call.make_module_function_call(imported_module_name, name, params, env)
654-
true ->
661+
true ->
655662
{ Identifier.make_identifier(name), env }
656663
end
657664
end

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,8 @@ defmodule ElixirScript.Translator.Function do
173173
Enum.map_reduce(list, env, fn(x, env) ->
174174
Translator.translate(x, env)
175175
end)
176-
_ ->
177-
178-
Enum.map_reduce(List.wrap(body), env, fn(x, env) ->
176+
_ ->
177+
Enum.map_reduce(List.wrap(body), env, fn(x, env) ->
179178
Translator.translate(x, env)
180179
end)
181180
end

lib/elixir_script/translator/lexical_scope.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ defmodule ElixirScript.Translator.LexicalScope do
124124
end
125125

126126
def function_scope(env, { _, _ } = func) do
127-
%{ env | function: func, caller: env, vars: [] }
127+
%{ env | function: func, caller: env}
128128
end
129129

130130
def function_scope(env, nil) do

priv/std_lib/enum.ex

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ defmodule ElixirScript.Enum do
99
end
1010
end)
1111

12-
IO.inspect acc
13-
1412
acc
1513
end
1614

@@ -131,9 +129,16 @@ defmodule ElixirScript.Enum do
131129
count == 0
132130
end
133131

134-
def fetch(enumerable, index) do
132+
def fetch(enumerable, something_unique) do
133+
something_unique = if something_unique < 0 do
134+
{_, count} = Enumerable.count(enumerable)
135+
count + something_unique
136+
else
137+
something_unique
138+
end
139+
135140
result = Enumerable.reduce(enumerable, {:cont, 0}, fn(item, acc) ->
136-
if index == acc do
141+
if something_unique == acc do
137142
{:halt, {:ok, item}}
138143
else
139144
{:cont, acc + 1}

test/app/spec/enum.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const expect = require('chai').expect;
22
const Elixir = require('../build/Elixir.App');
3+
const Tuple = require('../../../src/javascript/lib/core').default.Tuple;
34

45
const Enum = Elixir.load(Elixir.ElixirScript.Enum);
56

@@ -109,4 +110,17 @@ describe('Enum', () => {
109110
expect(Enum.empty__qmark__([])).to.eql(true);
110111
expect(Enum.empty__qmark__([1, 2, 3])).to.eql(false);
111112
});
113+
114+
it('fetch/2', () => {
115+
expect(Enum.fetch([66], 0)).to.eql(new Tuple(Symbol.for('ok'), 66));
116+
expect(Enum.fetch([66], -1)).to.eql(new Tuple(Symbol.for('ok'), 66));
117+
expect(Enum.fetch([66], 1)).to.eql(Symbol.for('error'));
118+
expect(Enum.fetch([66], -2)).to.eql(Symbol.for('error'));
119+
});
120+
121+
it('fetch!/2', () => {
122+
expect(Enum.fetch__emark__([2, 4, 6], 0)).to.eql(2);
123+
expect(Enum.fetch__emark__([2, 4, 6], 2)).to.eql(6);
124+
expect(Enum.fetch__emark__([2, 4, 6], -2)).to.eql(4);
125+
});
112126
});

test/translator/bug_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,18 @@ defmodule ElixirScript.Translator.Bug.Test do
159159
assert_translation(ex_ast, js_code)
160160
end
161161

162+
163+
test "variable" do
164+
ex_ast = quote do
165+
Enum.fetch([2, 4, 6], 0)
166+
end
167+
168+
js_code = """
169+
Object.assign(document.getElementById('main'), Object.freeze({
170+
innerHTML: html
171+
}))
172+
"""
173+
174+
assert_translation(ex_ast, js_code)
175+
end
162176
end

0 commit comments

Comments
 (0)