Skip to content

Commit a34c875

Browse files
committed
Make sure that remote ast works correctly with variables
1 parent fa2455b commit a34c875

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

lib/elixir_script/passes/translate/form.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ defmodule ElixirScript.Translate.Form do
323323
{ast, state}
324324
end
325325

326-
def compile({{:., _, [{_, _, nil} = var, func_or_prop]}, _, []}, state) do
326+
def compile({{:., _, [{_, _, atom} = var, func_or_prop]}, _, []}, state) when is_atom(atom) do
327327
ast = Helpers.call(
328328
ElixirScript.Translate.Forms.JS.call_property(),
329329
[compile!(var, state), J.literal(to_string(func_or_prop))]

src/javascript/lib/core/functions.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ function map_to_object(map, options = []) {
9898
if (opt_keys === Symbol.for('string') && typeof key === 'number') {
9999
key = key.toString();
100100
} else if (
101-
(opt_keys === Symbol.for('string') || opt_symbols !== Symbol.for('undefined'))
102-
&& typeof key === 'symbol'
101+
(opt_keys === Symbol.for('string') || opt_symbols !== Symbol.for('undefined')) &&
102+
typeof key === 'symbol'
103103
) {
104104
key = erlang.atom_to_binary(key);
105105
}
@@ -121,8 +121,8 @@ function object_to_map(object, options = []) {
121121
const opt_recurse_array = proplists.get_value(Symbol.for('recurse_array'), options) === true;
122122

123123
if (object.constructor === Object) {
124-
let map = new Map();
125-
Reflect.ownKeys(object).forEach(key => {
124+
const map = new Map();
125+
Reflect.ownKeys(object).forEach((key) => {
126126
let key2 = key;
127127
let value = object[key];
128128
if (opt_atom_keys && typeof key === 'string') {
@@ -135,18 +135,15 @@ function object_to_map(object, options = []) {
135135
map.set(key2, value);
136136
});
137137
return map;
138-
139138
} else if (object instanceof Array && opt_recurse_array) {
140-
return object.map(function(ele) {
139+
return object.map((ele) => {
141140
if (ele.constructor === Object || ele instanceof Array) {
142141
return object_to_map(ele, options);
143142
}
144143
return ele;
145144
});
146-
147-
} else {
148-
throw new Error(`Object ${object} is not an native object or array`);
149145
}
146+
throw new Error(`Object ${object} is not an native object or array`);
150147
}
151148

152149
class Recurse {

test/integration/integration_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ defmodule ElixirScript.Integration.Test do
1111
val = call_compiled_function Integration, :test_string_interpolation, []
1212
assert val == "5"
1313
end
14+
15+
test "shorthand failure" do
16+
val = call_compiled_function Integration, :shorthand_failure, []
17+
assert val == [
18+
[:option, %{value: "[email protected]"}, "[email protected]"],
19+
[:option, %{value: "[email protected]"}, "[email protected]"]
20+
]
21+
end
1422
end

test/support/integration.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@ defmodule Integration do
44
def test_string_interpolation do
55
"#{5}"
66
end
7+
8+
def shorthand_failure do
9+
orders = [%{email: "[email protected]"},%{email: "[email protected]"}]
10+
options = Enum.reduce(orders, [],
11+
&(&2 ++ [ [:option, %{value: &1.email}, &1.email] ]))
12+
end
713
end

test/support/term_converter.ex

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ defmodule ElixirScript.TermConverter do
3838
String.to_atom(term)
3939
end
4040

41-
def decode("null") do
42-
nil
43-
end
44-
45-
def decode(term) when is_binary(term) or is_boolean(term) or is_number(term) do
41+
def decode(term) when is_binary(term) or is_boolean(term) or is_number(term) or is_nil(term) do
4642
term
4743
end
4844

@@ -54,8 +50,8 @@ defmodule ElixirScript.TermConverter do
5450
List.to_tuple(terms)
5551
end
5652

57-
def decode(%{"__type__" => "map", "terms" => terms}) do
58-
Enum.map(terms, fn [key, term] -> {decode(key), decode(term)} end)
53+
def decode(%{"__type__" => "map", "values" => values}) do
54+
Enum.map(values, fn [key, term] -> {decode(key), decode(term)} end)
5955
|> Enum.into(%{})
6056
end
6157
end

0 commit comments

Comments
 (0)