Skip to content

Commit 680612a

Browse files
committed
Make sure to filter vars before trying to find name in vars field in state
1 parent 2ef0e03 commit 680612a

File tree

5 files changed

+28
-44
lines changed

5 files changed

+28
-44
lines changed

lib/elixir_script/passes/translate/form.ex

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,6 @@ defmodule ElixirScript.Translate.Form do
99
alias ElixirScript.Translate.Clause
1010
require Logger
1111

12-
@js_reserved_words [
13-
:break,
14-
:case,
15-
:class,
16-
:const,
17-
:continue,
18-
:debugger,
19-
:default,
20-
:delete,
21-
:do,
22-
:else,
23-
:export,
24-
:extends,
25-
:finally,
26-
:function,
27-
:if,
28-
:import,
29-
:in,
30-
:instanceof,
31-
:new,
32-
:return,
33-
:super,
34-
:switch,
35-
:throw,
36-
:try,
37-
:typeof,
38-
:var,
39-
:void,
40-
:while,
41-
:with,
42-
:yield
43-
]
44-
4512
def compile!(ast, state) do
4613
{js_ast, _} = compile(ast, state)
4714

@@ -406,18 +373,11 @@ defmodule ElixirScript.Translate.Form do
406373
end
407374
end
408375

409-
def compile({var, meta, _}, state) when var in @js_reserved_words do
410-
counter = Pattern.get_counter(meta)
411-
412-
var = String.to_atom("__#{var}__")
413-
var = Pattern.get_variable_name(to_string(var) <> counter, state)
414-
{ ElixirScript.Translate.Identifier.make_identifier(var), state }
415-
end
416-
417376
def compile({var, meta, _}, state) do
418377
counter = Pattern.get_counter(meta)
419378

420-
var = Pattern.get_variable_name(to_string(var) <> counter, state)
379+
var = ElixirScript.Translate.Identifier.filter_name(var)
380+
var = Pattern.get_variable_name(var <> counter, state)
421381
{ ElixirScript.Translate.Identifier.make_identifier(var), state }
422382
end
423383

lib/elixir_script/passes/translate/identifier.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ defmodule ElixirScript.Translate.Identifier do
4141
|> J.identifier
4242
end
4343

44-
defp filter_name(reserved_word) when reserved_word in @js_reserved_words do
44+
def filter_name(reserved_word) when reserved_word in @js_reserved_words do
4545
"__#{Atom.to_string(reserved_word)}__"
4646
end
4747

48-
defp filter_name(name) do
48+
def filter_name(name) do
4949
name = to_string(name)
5050

5151
if String.contains?(name, ["?", "!"]) do

test/integration/integration_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ defmodule ElixirScript.Integration.Test do
2929
val = call_compiled_function Integration, :multi_field_call, []
3030
assert val == "5,000,000"
3131
end
32+
33+
test "filter names in guards" do
34+
val = call_compiled_function Integration, :filter_names_in_guards, []
35+
assert val == true
36+
end
3237
end

test/passes/translate/form_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,14 @@ defmodule ElixirScript.Translate.Forms.Test do
168168
]
169169
)
170170
end
171+
172+
test "make sure counter used in guard", %{state: state} do
173+
state = Map.merge(state, %{anonymous_fn: false, function: {:filter_names_in_guards, nil}, in_guard: true,
174+
module: Integration, vars: %{"has__qmark__" => 0}})
175+
176+
ast = {{:., [], [:erlang, :==]}, [line: 29], [{:has?, [line: 29], nil}, 5]}
177+
178+
{js_ast, _} = Form.compile(ast, state)
179+
assert hd(js_ast.arguments).name === "has__qmark__0"
180+
end
171181
end

test/support/integration.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,13 @@ defmodule Integration do
2121
map = %{token_count: 5000000}
2222
map.token_count.toLocaleString()
2323
end
24+
25+
def filter_names_in_guards do
26+
has? = 5
27+
28+
case 5 do
29+
_ when has? == 5 ->
30+
true
31+
end
32+
end
2433
end

0 commit comments

Comments
 (0)